Archived OpenModelica forums. Posting is disabled.

Alternative forums include GitHub discussions or StackOverflow (make sure to read the Stack Overflow rules; you need to have well-formed questions)


Forgot password? | Forgot username? | Register
  • Index
  • » Users
  • » mburisch
  • » Profile

Posts

Posts

Hello,

it appears that the recent change in parameter evaluation at runtime (revision 11850) does break external object constructors which use depending parameters. Since the change their initial value is 0 or the empty string. However, the constructor of external objects are called before the parameters are assigned their values, therefore leading to wrong initializations.

Regards,

Michael

Hello,

I am having a problem with variables being set to large parameter values in an initial equation block. I have added a simple model demonstrating this problem.

Code:


model SubModel
  parameter Real fillFactor_start = 0.5;
  parameter Real Qtotal;
 
  Real fillFactor;
  Real Qcurrent;
initial equation
  Qcurrent = fillFactor_start * Qtotal; 
equation 
  fillFactor = Qcurrent / Qtotal;
  der(Qcurrent) = 100000;
end SubModel;

model Test
  SubModel m1(Qtotal=60000000000.0);
  SubModel m2(Qtotal=30000000000.0);
end Test;

Depending on the value to be set init the initial equation block I get different results at time=0:

m1.fillFactor = -0.124999999975
m2.fillFactor = 0.5

Looking at the generated code it seems as OpenModelica is calculating the initial residuals for Qcurrent, which I do not think is necessary in this case.

Note also that the values result from wrong calculations for the initial residual using the "nelder_mead_ex" method. Switching to "simplex" method gives the correct results.

I am currently using OpenModelica 1.8.1 (r11436).


Regards,

Michael

I was able to create a small model which reproduces the error. In my case the generated function "eqFunction_6" declares a non-linear system with with an array of size 12. In "residualFunc6", however, this array is accessed at index 15. Although for me this does not give a run-time error, it does corrupts the stack.

As uploading does not work for me, I post the model here:

Code:


package Medium
  function h_pt
    input Real p;
    input Real t;
    output Real h;
  algorithm
    h := t;
  end h_pt;
 
  function d_ph
    input Real p;
    input Real h;
    output Real d;
  algorithm
    d := h;
  end d_ph; 
end Medium;

connector Port
  flow Real mf;
  Real p;
  stream Real h;
end Port;


model Pipe
  Port port_a, port_b;
  Real loopPressure;
   
protected
  Real density;
  Real outputPressure;
 
  function func
    input Real inputPressure;
    input Real density;
    output Real outputPressure;
    output Real loopPressure;
  algorithm
    loopPressure := inputPressure;
    outputPressure := inputPressure;
  end func;

equation
  density = Medium.d_ph(port_a.p, port_a.h);
  (outputPressure, loopPressure) = func(port_a.p, density);

  port_a.mf + port_b.mf = 0;
  port_b.p = outputPressure;
  port_a.h = inStream(port_b.h);
  port_b.h = inStream(port_a.h);
end Pipe;

model Split
  Port port_a, port_b, port_c;
equation
  connect(port_a, port_b);
  connect(port_a, port_c); 
end Split;

model Join
  Port port_a, port_b, port_c;
  final parameter Real epsFlow = 1e-15;
equation
  port_a.mf + port_b.mf + port_c.mf = 0;
  port_b.p = min(port_a.p, port_c.p);
  port_a.h = (max(port_b.mf, epsFlow) * inStream(port_b.h) + max(port_c.mf, epsFlow) * inStream(port_c.h)) / (max(port_b.mf, epsFlow) + max(port_c.mf, epsFlow));
  port_b.h = (max(port_a.mf, epsFlow) * inStream(port_a.h) + max(port_c.mf, epsFlow) * inStream(port_c.h)) / (max(port_a.mf, epsFlow) + max(port_c.mf, epsFlow));
  port_c.h = (max(port_a.mf, epsFlow) * inStream(port_a.h) + max(port_b.mf, epsFlow) * inStream(port_b.h)) / (max(port_a.mf, epsFlow) + max(port_b.mf, epsFlow));
end Join; 

model Boundary
  Port port;
  Real t;
  Real mf;
  Real p;
equation
  port.mf = mf;
  port.p = p;
  port.h = Medium.h_pt(p, t);
end Boundary;

model Test
  Split[numberOfSplits - 1] split;
  Join[numberOfSplits - 1] join;                                                                 
  Pipe[numberOfSplits] pipe;
   
  Boundary source;
  Boundary sink;
   
  parameter Integer numberOfSplits = 2;   
  parameter Integer numberOfLoops = 10;
   
equation 
  source.mf = -10;
  source.p = 100000;
  source.t = 300;
  sink.t = 300;
 
  // Grid:
  //  split
  // --+--+---
  //   |  |  | pipe
  // --+--+---
  //  join
 
  connect(source.port, split[1].port_a);
 
  for i in 1:numberOfSplits - 1 loop
    connect(split[i].port_c, pipe[i].port_a);
    connect(pipe[i].port_b, join[i].port_c);

    split[i].port_c.mf = -source.port.mf / numberOfSplits;   
  end for;
 
 
  for i in 1:numberOfSplits - 2 loop
    connect(split[i].port_b, split[i + 1].port_a);
    connect(join[i + 1].port_b, join[i].port_a);
  end for; 
   
  connect(split[numberOfSplits - 1].port_b, pipe[numberOfSplits].port_a);
  connect(pipe[numberOfSplits].port_b, join[numberOfSplits - 1].port_a);
 
  connect(join[1].port_b, sink.port);
end Test;

I am using OpenModelica for Windows (r11098)

Regards,
Michael

Note: I have compiled the model using the gcc option "-fstack-protector-all" to check for buffer overflows and it reported an error (stack smashing) during execution.

This only happends in one of our larger model under certain conditions. I will try to make a small model showing this behaviour.

Regards,
Michael

Hi,

I am not sure wheter I am correct, but it appears as OpenModelica generates inccorect code for the solving of non-linear systems.

In the generated C code a function is called from "functionAlgebraics" which calls a macro (start_nonlinear_system(68)) leading to the definition of the variable: double nls_fvec[68].
Later on a solver macro (solve_nonlinear_system) is called with a function pointer to some function which apperas to calculate some residual values. In this functions the results are written to a pointer pointing to nls_fvec. Here seems to be the problem, as some values are written beyond the allocated space (in my case up to index 74). I am not sure wheter I am correct here, as in some models the simulation runs correctly. However, I have a model where this seems to cause a segmentation fault crashing the simulation program.

Im am using OpenModelica (r11077).

Regards,

Michael

Hello,

I have a question regarding the difference between fill(-a,N) and -fill(a, N).

If I have the following model

Code:


model Test
  parameter Integer N = 1;
  Real a[N];
  Real b[N];
equation   
  a = fill(-1, N);
  b = -fill(1, N);
end Test;

the flattend code is the following:

Code:


class Test
  parameter Integer N = 1;
  Real a[1];
  Real b[1];
equation
  a[1] = -1.0;
  b[1] = DAE.CAST(/tp:T_REAL[1]/, -{1})[1];
end Test;

This appears more or less equal to me, however, I have a more complex model where using fill(-1,N) simulates correctly and -fill(1,N) leads to a simulation error.

Code:


warning | Error solving nonlinear system SES_ALGORITHM 0 at time 0

In my opinion the two solutions should be equal.

Best regards,

Michael

Hello,

I get an error using OpenModelica (r10977) when I want to simulate the following model.

Code:


model ArrayModel
  parameter Integer N=1;
  Real y[N];
end ArrayModel;

model TestModel
  parameter Integer N[:]={1, 2};
  ArrayModel a[2](N=N);
equation
  a[1].y[1] = 1;
 
  a[2].y[1] = 1;
  a[2].y[2] = 1;
end TestModel;

The error message is

Code:


Error: Illegal subscript [2] for dimensions 1 in component a[2]

which is related to acessing a[2].y[2]. Acessing only a[2].y[1] does not cause any errors and for this the flattened model has all the declared variables, i.e. a[2].y[2] is defined.

Is this incorrect usage of the Modelica language from me or a bug?

Best regards,

Michael

I have a simple model for which OpenModelica fails to generate compileable code. Flatting and model check, however, succeed without errors.

Code:


package MyPackage
  record State
    Real a;
  end State;
 
  model Properties
    State state;
    Real b;
  equation
    b = func_b(state.a);
  end Properties;
 
  function func
    input State s;
    output Real b;
  algorithm
    b := func_b(s.a);
  end func;
 
  function func_b
    input Real a;
    output Real b;
  algorithm
    b := 2 * a;
  end func_b;
end MyPackage;


model MyModel
  replaceable package Medium = MyPackage;
  parameter Integer numberOfNodes = 1;
 
  input Medium.State[numberOfNodes] states;
  Real[numberOfNodes] b;

equation
  b = Medium.func(states);
end MyModel;


model TestModel
  package Medium = MyPackage;
  MyModel m(states = props.state);
  Medium.Properties[1] props;
equation
  props.state.a = {1};
end TestModel;

Best regards,

Michael

Dec-22-11 18:57:57
Uncompilable models

Ok, thank you for your reply and sorry for the missing record. For completness I added it to the initial post.

Dec-22-11 12:53:07
Uncompilable models

Hello,

I noticed a strange behaviour in a simple model, which appears like a bug.

Code:


record Value
  Real a;
end Value;

function Func
  input Value v;
  output Real a;
algorithm
  a := v.a;
end Func;

model Model
  Value[1] values;
  Value v;
  Real a;
equation 
  v = values[1];
  a = Func(values[1]);  // This leads to a compilation error
  //a = Func(v);        // This does work
end Model;

model Test
  Model m(values = values);
  Value[1] values;
equation
  values[1].a = 1;
end Test;

Instantiating and checking the model proceeds without problems, however, building the model fails due to errors in the generated C code.
I am currently using OpenModelica trunk (r10809).

Regards,
Michael

Edit: Added missing record definition

Dec-05-11 20:51:35
Preprocesser define problems

I am having a problem in compiling the latest OpenModelica SVN version (10649) for windows.

The file "c_runtime/meta/readString.c" does not compile as the inclueded file "dtoa.c" defines Long as int (#define Long int).
This definition interfers with a structure variable of name Long in winnt.h leading to the definition of "DWORD Long" => "DWORD int".

Best regards,
Michael

Hello,

I have a simple model (see below) which conditionally assigns a value to a parameter. If this parameter is final the its value is always 0.

Furthermore if the right hand side is a final parameter value, the result also is 0, wheter or not the left hand side is a final parameter.

Code:


type CondEnum = enumeration(a, b);

model Test
  final parameter CondEnum cond = CondEnum.b;
        parameter Real val = 1.0;
  final parameter Real final_value = 1.0;

        parameter Real value1 = if cond == CondEnum.a then val         else -val;
  final parameter Real value2 = if cond == CondEnum.a then val         else -val;
        parameter Real value3 = if cond == CondEnum.a then final_value else -final_value;                                 

  Real result1; 
  Real result2;
  Real result3;

equation
  result1 = value1; // ok    (==-val)
  result2 = value2; // error (==0)
  result3 = value3; // error (==0)
end Test;

Model checking and simulation proceeds without errors.

Best regards,

Michael

Hi,

I just got the latest version from SVN (r10585) and it gives the same error as before.

Code:


package Lib
  package TypePackage
    type Type = Real;
  end TypePackage;

  model PackageModel
    replaceable package Pack = TypePackage;
    Pack.Type v;       
  equation
    v = 1;
  end PackageModel;
end Lib;

model Test
  package Pack = Lib.TypePackage;
 
  Lib.PackageModel mod1(redeclare package Pack = Pack);              // Gives error
  Lib.PackageModel mod2(redeclare package Pack = Lib.TypePackage);   // Works
equation
end Test;

Calling checkModel(Test) results in the following error:


[test.mo:19:35-19:54:writable] Error: Class .Test.Pack not found in scope Lib.PackageModel.Pack.
[test.mo:9:3-9:14:writable] Error: Class Pack.Type not found in scope Lib.PackageModel while instantiating mod1.v.
Error: Error occurred while flattening model Test

It seems that the redeclaration in PackageModel is converted to:

Code:


model PackageModel
PackageModel.Pack = .Test.Pack;
...
end PackageModel;

instead of what I expected to be:

Code:


model PackageModel
PackageModel.Pack = Lib.TypePackage;
...
end PackageModel;

Is this the correct behaviour and how can I achieve what I was intenting?

Best regards,

Michael

Nov-22-11 15:00:31
Category: Developer

In OMPlot/OMPlotGUI/PlotWindow.cpp is a memory leak when opening "mat" files.

For opening the file a "omc_new_matlab4_reader" is created in line 359. However, this object is never deleted. This does not only lead to a memory leak but also keeps the file handle openend. Therefore, the file which was opened cannot be overwritten as long as the application using the PlotWindow object is running.

It seems like adding "omc_free_matlab4_reader" somewhere arround line 411 would solve this problem.

Best regards,

Michael

I am encountering some strange behavior regarding a redeclarable package and its use in a model. When I want to declare a parameter and set its value to the result of one of the functions, this does not work if the input parameter is a parameter itself.

Take a look at the following simple model, declaring a partial package, a concrete package and a model using this package.

Code:


package BasePackage
  replaceable partial function func
    input Real value;
    output Real result;
  end func;
end BasePackage;

package ConcretePackage
  extends BasePackage;
  redeclare function extends func
  algorithm
    result := value;
  end func;
end ConcretePackage;

model TestModel
  replaceable package Function = BasePackage;        // Does not work
  //replaceable package Function = ConcretePackage;  // This way it works
 
  final parameter Real value = 1;
  parameter Real result1 = Function.func(value);
  parameter Real result2 = Function.func(1);
  Real x;
       
equation
  x = Function.func(time + 1);
end TestModel;

model Test
  TestModel mod(redeclare package Function = ConcretePackage);
equation
end Test;

If I simulate this, result1 will be 0, while result2 1 and x will have the correct values. However, if I instead directly refer to the ConcretePackage (commented line), the code does work and result1 also has value 1.

Is this an error or intendended behavior?

Best regards,

Michael

Thank you for your help, however, your suggested set of parameter gives me another error:

Error: Internal error - BackendDAETransform.reduceIndexDummyDer failed!
[../test.mo:24:3-24:13:writable] Error: Model is structurally singular, error found sorting equations sIn = time;
for variables

This also only effects the TestArray model, the other (model Test) still simulates without errors.

Regards,

Michael

Hello,

I have a simple model (see below) which fails to simulate with the current (9772) nightly windows build. I get an error about an unreferenced variable.


class Loop
  input Real sIn;
  output Real sOut;
equation
  sOut = sIn;
  //connect(sIn, sOut);
end Loop;

// This model works
model Test
  Real sIn;
  Real sOut;
  Loop l;
equation
  sIn = time;
  connect(sIn, l.sIn);
  connect(l.sOut, sOut);
end Test;

// This model does not work
model TestArray
  Real sIn;
  Real sOut;
  Loop l[1];
equation
  sIn = time;
  connect(sIn, l[1].sIn);
  connect(l[1].sOut, sOut);
end TestArray;

The error message is:
Error: Variable l[1].sIn is not referenced in any equation (possibly after symbolic manipulations)

The model TestArray references a block with an array variable of length one. If I do not use the array (model Test) it works.

Note also, that the model worked in older versions of OpenModelica, namely the last version (9366) of the nightly builds. Also, if I use the connect statement within the Loop block it works as well.

Am I missing something here or is this an error of OpenModelica?


Greetings,

Michael

  • Index
  • » Users
  • » mburisch
  • » Profile
You are here: