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

Equations and Variables

Equations and Variables

Hello everyone,
I have a problem to level up my equations and variables or better a problem to identify what is counted by open modelica as a variable and what as an equation.
This is a small Model I created:

model ConstanteTest
Modelica.Blocks.Interfaces.RealInput u = 2;
Durchreiche durch;
EffluentSinkModif Sink;
FlowSourceModif Source;
equation
durch.u = u;
connect(Source.Out,durch.In);
connect(Sink.In,durch.Out)
  annotation(Icon(coordinateSystem(extent = {{-100,-100},{100,100}}, preserveAspectRatio = true, initialScale = 0.1, grid = {2,2})), Diagram(coordinateSystem(extent = {{-100,-100},{100,100}}, preserveAspectRatio = true, initialScale = 0.1, grid = {2,2})));
end ConstanteTest; 

When I check the Model OMedit tells me the model has 11 equations and 11 variables. So far so good. When I Initiate the model I get the following message.

class ConstanteTest
  input Real u = 2.0;
  Real durch.In.Q(quantity = "VolumeFlowRate", unit = "m3/s");
  Real durch.In.So(quantity = "MassConcentration", unit = "kg/m3");
  Real durch.Out.Q(quantity = "VolumeFlowRate", unit = "m3/s");
  Real durch.Out.So(quantity = "MassConcentration", unit = "kg/m3");
  input Real durch.u;
  Real Sink.In.Q(quantity = "VolumeFlowRate", unit = "m3/s");
  Real Sink.In.So(quantity = "MassConcentration", unit = "kg/m3");
  Real Source.Out.Q(quantity = "VolumeFlowRate", unit = "m3/s");
  Real Source.Out.So(quantity = "MassConcentration", unit = "kg/m3");
  input Real Source.u;
  input Real Source.w = 4.0;
equation
  durch.Out.Q = durch.In.Q * durch.u;
  durch.Out.So = durch.In.So;
  durch.Out.Q + durch.In.Q = 0.0;
  Source.Out.Q = Source.u;
  Source.Out.So = Source.w;
  durch.u = u;
  durch.In.Q + Source.Out.Q = 0.0;
  durch.Out.Q + Sink.In.Q = 0.0;
  Source.Out.So = durch.In.So;
  Sink.In.So = durch.Out.So;
end ConstanteTest;



As far as I see it there are 12 variables and 10 equations. So my question is how does OMedit count the variables and equations? Is there a formula for it or an algorithm to know maybe why some variables/equations are not counted?


Regards,
Pliskin

Re: Equations and Variables

You have a top-level input variable "u", which as far as I know is special and might not count (is ignored for counting). And source.w has a binding equation (which is your 11th equation).

Re: Equations and Variables

Thanks for the fast reply!

Maybe you can help me with the next one. It's the same Problem. Only that there are counted 16 variables but I only see 15 variables

model SimpleWWInOut
  import WWSourceModif;
  WWSourceModif WWSo;
  Modelica.Blocks.Sources.CombiTimeTable CombiTimeTable1(tableOnFile = true, tableName = "drysimnord", fileName = "drysimnord.txt", columns = {2,3});
  SimplePump SimplePump1;
  EffluentSinkModif WWSi;
Modelica.Blocks.Sources.Constant const1;
equation
connect(const1.y,SimplePump1.u);
  connect(CombiTimeTable1.y,WWSo.data);
  connect(WWSo.Out,SimplePump1.In);
  connect(SimplePump1.Out,WWSi.In);
  annotation(Icon(coordinateSystem(extent = {{-100,-100},{100,100}}, preserveAspectRatio = true, initialScale = 0.1, grid = {2,2})), Diagram(coordinateSystem(extent = {{-100,-100},{100,100}}, preserveAspectRatio = true, initialScale = 0.1, grid = {2,2})));
end SimpleWWInOut;

check gives 17 equations, 16 variables.
variables i am counting(15):

Real WWSo.Out.Q
Real WWSo.Out.So
input Real WWSo.data[1]
input Real WWSo.data[2]
output Real CombiTimeTable1.y[1]
output Real CombiTimeTable1.y[2]
Real SimplePump1.H(start = 1.0)
Real SimplePump1.In.Q
  Real SimplePump1.In.So
  Real SimplePump1.Out.Q
  Real SimplePump1.Out.So
  input Real SimplePump1.u;
  Real WWSi.In.Q
  Real WWSi.In.So
  output Real const1.y



Instantiating brings:

class SimpleWWInOut
Real WWSo.Out.Q(quantity = "VolumeFlowRate", unit = "m3/s");
  Real WWSo.Out.So(quantity = "MassConcentration", unit = "kg/m3");
  input Real WWSo.data[1];
  input Real WWSo.data[2];
  parameter Boolean CombiTimeTable1.tableOnFile = true "= true, if table is defined on file or in function usertab";
  parameter String CombiTimeTable1.tableName = "drysimnord" "Table name on file or in function usertab (see docu)";
  parameter String CombiTimeTable1.fileName = "drysimnord.txt" "File where matrix is stored";
  parameter Boolean CombiTimeTable1.verboseRead = true "= true, if info message that file is loading is to be printed";
  parameter enumeration(LinearSegments, ContinuousDerivative, ConstantSegments) CombiTimeTable1.smoothness = Modelica.Blocks.Types.Smoothness.LinearSegments "Smoothness of table interpolation";
  parameter enumeration(HoldLastPoint, LastTwoPoints, Periodic, NoExtrapolation) CombiTimeTable1.extrapolation = Modelica.Blocks.Types.Extrapolation.LastTwoPoints "Extrapolation of data outside the definition range";
  parameter Real CombiTimeTable1.offset[1] = 0.0 "Offsets of output signals";
  parameter Real CombiTimeTable1.startTime(quantity = "Time", unit = "s") = 0.0 "Output = offset for time < startTime";
  final parameter Real CombiTimeTable1.t_min(quantity = "Time", unit = "s", fixed = false) "Minimum abscissa value defined in table";
  final parameter Real CombiTimeTable1.t_max(quantity = "Time", unit = "s", fixed = false) "Maximum abscissa value defined in table";
  protected discrete Real CombiTimeTable1.nextTimeEvent(quantity = "Time", unit = "s", start = 0.0, fixed = true) "Next time event instant";
  protected parameter Real CombiTimeTable1.tableOnFileRead(fixed = false) "= 1, if table was successfully read from file";
  parameter Integer CombiTimeTable1.columns[1] = 2 "Columns of table to be interpolated";
  parameter Integer CombiTimeTable1.columns[2] = 3 "Columns of table to be interpolated";
  parameter Integer CombiTimeTable1.nout(min = 1) = 2 "Number of outputs";
  protected Modelica.Blocks.Types.ExternalCombiTimeTable ExternalObject CombiTimeTable1.tableID = Modelica.Blocks.Types.ExternalCombiTimeTable.constructor("drysimnord", "drysimnord.txt", {{}}, 0.0, {2, 3}, Modelica.Blocks.Types.Smoothness.LinearSegments, Modelica.Blocks.Types.Extrapolation.LastTwoPoints) "External table object";
output Real CombiTimeTable1.y[1] "Connector of Real output signals";
  output Real CombiTimeTable1.y[2] "Connector of Real output signals"
  protected final parameter Real CombiTimeTable1.p_offset[1] = CombiTimeTable1.offset[1] "Offsets of output signals";
  protected final parameter Real CombiTimeTable1.p_offset[2] = CombiTimeTable1.offset[1] "Offsets of output signals";
  parameter Real SimplePump1.Q_min(quantity = "VolumeFlowRate", unit = "m3/s") = 1.0 "minimum pump capacity";
  parameter Real SimplePump1.Q_max(quantity = "VolumeFlowRate", unit = "m3/s") = 10.0 "maximum pump capacity";
Real SimplePump1.H(start = 1.0);
  Real SimplePump1.In.Q(quantity = "VolumeFlowRate", unit = "m3/s");
  Real SimplePump1.In.So(quantity = "MassConcentration", unit = "kg/m3");
  Real SimplePump1.Out.Q(quantity = "VolumeFlowRate", unit = "m3/s");
  Real SimplePump1.Out.So(quantity = "MassConcentration", unit = "kg/m3");
  input Real SimplePump1.u;
  Real WWSi.In.Q(quantity = "VolumeFlowRate", unit = "m3/s");
  Real WWSi.In.So(quantity = "MassConcentration", unit = "kg/m3");
  output Real const1.y "Connector of Real output signal"
  parameter Real const1.k(start = 1.0) "Constant output value";
initial algorithm
  if CombiTimeTable1.tableOnFile then
    CombiTimeTable1.tableOnFileRead := Modelica.Blocks.Sources.CombiTimeTable.readTableData(CombiTimeTable1.tableID, false, CombiTimeTable1.verboseRead);
  else
    CombiTimeTable1.tableOnFileRead := 1.0;
  end if;
  CombiTimeTable1.t_min := Modelica.Blocks.Sources.CombiTimeTable.getTableTimeTmin(CombiTimeTable1.tableID, CombiTimeTable1.tableOnFileRead);
  CombiTimeTable1.t_max := Modelica.Blocks.Sources.CombiTimeTable.getTableTimeTmax(CombiTimeTable1.tableID, CombiTimeTable1.tableOnFileRead);
equation
  WWSo.Out.Q = -WWSo.data[1];
  WWSo.Out.So = WWSo.data[2];
  assert(CombiTimeTable1.tableName <> "NoName","tableOnFile = true and no table name given");
  when {time >= pre(CombiTimeTable1.nextTimeEvent), initial()} then
  CombiTimeTable1.nextTimeEvent = Modelica.Blocks.Sources.CombiTimeTable.getNextTimeEvent(CombiTimeTable1.tableID, time, CombiTimeTable1.tableOnFileRead);
  end when;
  CombiTimeTable1.y[1] = CombiTimeTable1.p_offset[1] + Modelica.Blocks.Sources.CombiTimeTable.getTableValue(CombiTimeTable1.tableID, 1, time, CombiTimeTable1.nextTimeEvent, pre(CombiTimeTable1.nextTimeEvent), CombiTimeTable1.tableOnFileRead);
  CombiTimeTable1.y[2] = CombiTimeTable1.p_offset[2] + Modelica.Blocks.Sources.CombiTimeTable.getTableValue(CombiTimeTable1.tableID, 2, time, CombiTimeTable1.nextTimeEvent, pre(CombiTimeTable1.nextTimeEvent), CombiTimeTable1.tableOnFileRead);
  SimplePump1.H = 0.5 * (SimplePump1.Q_max - SimplePump1.Q_min) * (1.0 + SimplePump1.u) + SimplePump1.Q_min;
  SimplePump1.Out.Q = -(if SimplePump1.H > SimplePump1.Q_max then SimplePump1.Q_max else if SimplePump1.H < SimplePump1.Q_min then SimplePump1.Q_min else SimplePump1.H);
  SimplePump1.Out.So = SimplePump1.In.So;
  SimplePump1.Out.Q + SimplePump1.In.Q = 0.0;
  const1.y = const1.k;
  WWSo.Out.Q + SimplePump1.In.Q = 0.0;
  SimplePump1.Out.Q + WWSi.In.Q = 0.0;
  SimplePump1.u = const1.y;
  CombiTimeTable1.y[1] = WWSo.data[1];
  CombiTimeTable1.y[2] = WWSo.data[2];
  SimplePump1.In.So = WWSo.Out.So;
  SimplePump1.Out.So = WWSi.In.So;
end SimpleWWInOut;

Regards,
Pliskin

Edited by: Pliskin - Jun-19-14 14:53:47

Re: Equations and Variables

Does buildModel work? Maybe it is not counting the external object. The checkModel count is different from the one used when actually trying to compile a model.

Re: Equations and Variables

No i can not build it. It tells me to many equations and counts 4 equations and three variables.

Re: Equations and Variables

It would be really nice if there was a description on how variables and equations are counted and when variables are left out. But i guess there is no such thing right?!

Re: Equations and Variables

Pliskin wrote:

It would be really nice if there was a description on how variables and equations are counted and when variables are left out. But i guess there is no such thing right?!

Of course there is, it's part of the Modelica specification. See section 4.7 Balanced Models in the Modelica specification.

Re: Equations and Variables

Thanks I will read it. But maybe you can answer me this. It would really help me big time! Why is the following Model structurally singular? Which equation is to much?

connector ConnOutModif
  flow Modelica.SIunits.VolumeFlowRate Q;
  Modelica.SIunits.MassConcentration So;
  annotation(Icon(coordinateSystem(extent = {{-100,-100},{100,100}}, preserveAspectRatio = true, initialScale = 0.1, grid = {2,2})), Diagram(coordinateSystem(extent = {{-100,-100},{100,100}}, preserveAspectRatio = true, initialScale = 0.1, grid = {2,2})));
end ConnOutModif;

model SimplePump "modified pump"
  import ConnInModif;
  import ConnOutModif;
  parameter Modelica.SIunits.VolumeFlowRate Q_min = 1.0 "minimum pump capacity";
  parameter Modelica.SIunits.VolumeFlowRate Q_max = 10.0 "maximum pump capacity";
  Real H(start = 1.0);
  ConnInModif In;
  ConnOutModif Out;
  Modelica.Blocks.Interfaces.RealInput u;
equation
  H = 0.5 * ((-Q_min) + Q_max) + u * 0.5 * ((-Q_min) + Q_max) + Q_min;
  Out.Q = -(if H > Q_max then Q_max else if H < Q_min then Q_min else H);
  Out.So = In.So;
  Out.Q + In.Q = 0;
end SimplePump;

Re: Equations and Variables

Hard to know without ConnInModif

Re: Equations and Variables

Sorry. It is the same as the other connector.

Re: Equations and Variables

The problem here is that if you have two unconnected connectors, they will each generate one flow equation. You added:
  Out.So = In.So;
  Out.Q + In.Q = 0;
Which would be automatically generated by connecting the two connectors. You could change the connector to a model and the flow to nothing. Or better:

Code:

connector ConnOutModif

  flow Modelica.SIunits.VolumeFlowRate Q;
  Modelica.SIunits.MassConcentration So;
end ConnOutModif;

model SimplePump "modified pump"
  import ConnInModif = ConnOutModif;
  import ConnOutModif;
  parameter Modelica.SIunits.VolumeFlowRate Q_min = 1.0 "minimum pump capacity";
  parameter Modelica.SIunits.VolumeFlowRate Q_max = 10.0 "maximum pump capacity";
  Real H(start = 1.0);
  ConnInModif In;
  ConnOutModif Out;
  Modelica.Blocks.Interfaces.RealInput u;
equation
  H = 0.5 * ((-Q_min) + Q_max) + u * 0.5 * ((-Q_min) + Q_max) + Q_min;
  Out.Q = -(if H > Q_max then Q_max else if H < Q_min then Q_min else H);
  connect(In,Out);
end SimplePump;

This is still unbalanced because you forgot an equation:
OpenModelica says: Error: Internal error Found Equation without time dependent variables In.Q = 0.0

Re: Equations and Variables

Or actually (because you should not connect two connectors that are both in the same model):

Code:

connector ConnOutModif

  flow Modelica.SIunits.VolumeFlowRate Q;
  Modelica.SIunits.MassConcentration So;
end ConnOutModif;

model M
  ConnOutModif c;
end M;

model SimplePump "modified pump"
  parameter Modelica.SIunits.VolumeFlowRate Q_min = 1.0 "minimum pump capacity";
  parameter Modelica.SIunits.VolumeFlowRate Q_max = 10.0 "maximum pump capacity";
  Real H(start = 1.0);
  M In,Out;
  Modelica.Blocks.Interfaces.RealInput u;
equation
  H = 0.5 * ((-Q_min) + Q_max) + u * 0.5 * ((-Q_min) + Q_max) + Q_min;
  Out.c.Q = -(if H > Q_max then Q_max else if H < Q_min then Q_min else H);
  connect(In.c,Out.c);
end SimplePump;

And you realise you miss an equation for .So.

Re: Equations and Variables

Hey there,
I was actually not missing the equation for .So. it was in the Model I send to you. I thin the equation   Out.Q + In.Q = 0; might have been a problem. I deleted it from the SimplePump Model and now it seems to add up.
I did not really understand what you meant by your last post.

sjoelund.se wrote:

Or actually (because you should not connect two connectors that are both in the same model):

SimplePump is a Model I want to use in a bigger model. therefor it needs two connectors.

This is what I did now:
ConnInModif and ConnOutModif are the same as before

model SimplePump "modified pump"
  import ConnInModif;
  import ConnOutModif;
  parameter Modelica.SIunits.VolumeFlowRate Q_min = 1.0 "minimum pump capacity";
  parameter Modelica.SIunits.VolumeFlowRate Q_max = 10.0 "maximum pump capacity";
  Real H(start = 1.0);
  ConnInModif In;
  ConnOutModif Out;
  Modelica.Blocks.Interfaces.RealInput u = 1;
equation
  H = 0.5 * ((-Q_min) + Q_max) + u * 0.5 * ((-Q_min) + Q_max) + Q_min;
  Out.Q = -(if H > Q_max then Q_max else if H < Q_min then Q_min else H);
  Out.So = In.So;
end SimplePump;

model Test
SimplePump Pumpe;
EffluentSinkModif Sink;
FlowSourceModif Source;
equation
connect(Pumpe.In,Source.Out);   
connect(Pumpe.Out,Sink.In);
end Test; 

model FlowSourceModif
  import ConnOutModif;
  ConnOutModif Out;
Modelica.Blocks.Interfaces.RealInput u = 3;
Modelica.Blocks.Interfaces.RealInput w = 4;
equation
Out.Q = u; 
Out.So = w;
end FlowSourceModif;

model EffluentSinkModif
  import ConnInModif;
  ConnInModif In;
end EffluentSinkModif;

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