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

Variable/equation mismatch with operator records

Variable/equation mismatch with operator records

Hi,

I have the following operator record,

Code:


operator record MultivariateGaussian
  replaceable Real u[:];
  replaceable Real s[size(u,1),size(u,1)];

  encapsulated operator 'constructor'
    import MultivariateGaussian;
    function fromReal     
      input Real[:] u;
      input Real[size(u, 1), size(u, 1)] s;
      output MultivariateGaussian result(u=u, s=s)
        annotation(Inline=true);
    end fromReal;
  end 'constructor';

  encapsulated operator function '+'
    import MultivariateGaussian;
    input MultivariateGaussian left;
    input MultivariateGaussian right;
    output MultivariateGaussian result;
  protected
    Real u[:], s[size(u, 1), size(u, 1)];
  algorithm
    u := left.u + right.u;
    s := left.s + right.s;
    result := MultivariateGaussian(u=u, s=s)
      annotation(Inline=true);
  end '+';

end MultivariateGaussian;

and the following model,

Code:


model Add
  MultivariateGaussian state;
  MultivariateGaussian process(
    redeclare parameter Real[2] u={1.0, 2.0},
    redeclare parameter Real[2,2] s={{0.1, 0.2}, {0.3, 0.4}}
  );
algorithm
  when initial() then
    state := MultivariateGaussian(
      u={1.5, 1.5},
      s={{1.1e-2, 1.2e-2}, {1.3e-2, 1.4e-2}}
    );
  end when;
  when sample(1.0, 1.0) then
    state := state + process;
  end when;
end Add;

Running the model with following script,

Code:


loadFiles({
  "OpRec.mo",
  "AddMG.mo"
});
getErrorString();

checkModel(Add);
getErrorString();

complains about a variable equation mismatch,

Code:


$ omc addmg.mos
true
""
"Check of Add completed successfully.
Class Add has 0 equation(s) and 2 variable(s).
0 of these are trivial equation(s)."
""

The flattened model looks like this:

Code:


function MultivariateGaussian "Automatically generated record constructor for MultivariateGaussian"
  input Real[:] u;
  input Real[size(u, 1), size(u, 1)] s;
  output MultivariateGaussian res;
end MultivariateGaussian;

function MultivariateGaussian.'+'
  input MultivariateGaussian left;
  input MultivariateGaussian right;
  output MultivariateGaussian result;
  protected Real[:] u;
  protected Real[size(u, 1), size(u, 1)] s;
algorithm
  u := left.u + right.u;
  s := left.s + right.s;
  result := MultivariateGaussian(u, s);
end MultivariateGaussian.'+';

class Add
  Real state.u[1];
  Real state.s[1,1];
  parameter Real process.u[1] = 1.0;
  parameter Real process.u[2] = 2.0;
  parameter Real process.s[1,1] = 0.1;
  parameter Real process.s[1,2] = 0.2;
  parameter Real process.s[2,1] = 0.3;
  parameter Real process.s[2,2] = 0.4;
algorithm
  when initial() then
    state := MultivariateGaussian({1.5, 1.5}, {{0.011, 0.012}, {0.013, 0.014}});
  end when;
  when sample(1.0, 1.0) then
    state := MultivariateGaussian.'+'(state, process);
  end when;
end Add;

If a MultivariateGaussian is instantiated as a parameter it seems as if everything works correctly. However, if it is treated as a variable then it does not look like it's flattened correctly. Can anyone shed some light on what is going on here?

Many thanks.
AddMG.mo

OpRec.mo

addmg.mos

Re: Variable/equation mismatch with operator records

Hi again,

A quick follow up. If I explicitly variables with a certain size, i.e.

Code:


model Add
  MultivariateGaussian state(
    redeclare Real u[2],
    redeclare Real s[2,2]
  );
...

then the variable/equation mismatch disappears. Trying to simulate the model then leads to this problem:

Code:


function BackendEquation.solveEquation failed w.r.t state.s[1,2]
ALGORITHM: 0. algorithm
  state := MultivariateGaussian({1.5, 1.5}, {{0.011, 0.012}, {0.013, 0.014}});
  $whenCondition1 := false;
(dynamic)
===================
function BackendEquation.solveEquation failed w.r.t state.s[2,1]
ALGORITHM: 0. algorithm
  state := MultivariateGaussian({1.5, 1.5}, {{0.011, 0.012}, {0.013, 0.014}});
  $whenCondition1 := false;
(dynamic)
===================
function BackendEquation.solveEquation failed w.r.t state.s[2,2]
ALGORITHM: 0. algorithm
  state := MultivariateGaussian({1.5, 1.5}, {{0.011, 0.012}, {0.013, 0.014}});
  $whenCondition1 := false;
(dynamic)
===================
function BackendEquation.solveEquation failed w.r.t state.u[1]
ALGORITHM: 0. algorithm
  state := MultivariateGaussian({1.5, 1.5}, {{0.011, 0.012}, {0.013, 0.014}});
  $whenCondition1 := false;
(dynamic)
===================
function BackendEquation.solveEquation failed w.r.t state.u[2]
ALGORITHM: 0. algorithm
  state := MultivariateGaussian({1.5, 1.5}, {{0.011, 0.012}, {0.013, 0.014}});
  $whenCondition1 := false;
(dynamic)
===================
record SimulationResult
    resultFile = "",
    simulationOptions = "startTime = 0.0, stopTime = 10.0, numberOfIntervals = 500, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'Add', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = ''",
    messages = "Failed to build model: Add",
    timeFrontend = 0.0,
    timeBackend = 0.0,
    timeSimCode = 0.0,
    timeTemplates = 0.0,
    timeCompile = 0.0,
    timeSimulation = 0.0,
    timeTotal = 0.0
end SimulationResult;
"Notification: It was not possible to analyze the given system symbolically, because the relevant equations are part of an algebraic loop. This is not supported yet.
[/home/build/tmp/build/openmodelica-1.9.4~dev-24-g7633fca/OMCompiler/Compiler/BackEnd/BackendEquation.mo:2609:7-2609:76:writable] Error: Internal error function solveEquation failed
Notification: The following equation is consistent and got removed from the initialization problem: $PRE.state.s[1,2] = state.s[1,2]
Notification: It was not possible to analyze the given system symbolically, because the relevant equations are part of an algebraic loop. This is not supported yet.
[/home/build/tmp/build/openmodelica-1.9.4~dev-24-g7633fca/OMCompiler/Compiler/BackEnd/BackendEquation.mo:2609:7-2609:76:writable] Error: Internal error function solveEquation failed
Notification: The following equation is consistent and got removed from the initialization problem: $PRE.state.s[2,1] = state.s[2,1]
Notification: It was not possible to analyze the given system symbolically, because the relevant equations are part of an algebraic loop. This is not supported yet.
[/home/build/tmp/build/openmodelica-1.9.4~dev-24-g7633fca/OMCompiler/Compiler/BackEnd/BackendEquation.mo:2609:7-2609:76:writable] Error: Internal error function solveEquation failed
Notification: The following equation is consistent and got removed from the initialization problem: $PRE.state.s[2,2] = state.s[2,2]
Notification: It was not possible to analyze the given system symbolically, because the relevant equations are part of an algebraic loop. This is not supported yet.
[/home/build/tmp/build/openmodelica-1.9.4~dev-24-g7633fca/OMCompiler/Compiler/BackEnd/BackendEquation.mo:2609:7-2609:76:writable] Error: Internal error function solveEquation failed
Notification: The following equation is consistent and got removed from the initialization problem: $PRE.state.u[1] = state.u[1]
Notification: It was not possible to analyze the given system symbolically, because the relevant equations are part of an algebraic loop. This is not supported yet.
[/home/build/tmp/build/openmodelica-1.9.4~dev-24-g7633fca/OMCompiler/Compiler/BackEnd/BackendEquation.mo:2609:7-2609:76:writable] Error: Internal error function solveEquation failed
Notification: The following equation is consistent and got removed from the initialization problem: $PRE.state.u[2] = state.u[2]
Warning: The initial conditions are over specified. Use +d=initialization for more information.
[CodegenCFunctions.tpl:3719:39-3719:39:writable] Error: Template error: Unknown dimensions may not be part of generated code. This is most likely an error on the part of OpenModelica. Please submit a detailed bug-report.
[CodegenCFunctions.tpl:3727:14-3727:14:writable] Error: Template error: dimensionExp: INVALID_DIMENSION size(u, 1)
[CodegenCFunctions.tpl:3719:39-3719:39:writable] Error: Template error: Unknown dimensions may not be part of generated code. This is most likely an error on the part of OpenModelica. Please submit a detailed bug-report.
[CodegenCFunctions.tpl:3727:14-3727:14:writable] Error: Template error: dimensionExp: INVALID_DIMENSION size(u, 1)
"

There are 0 guests and 0 other users also viewing this topic
You are here: