1

I'm pretty new to openmodelica and wondering why OME creates new equations for every loop it makes and how to solve this.

For this "doubletank system" it creates 6 equations and 4 variables while I loop twice through q as a function of u.

**model DoubleTankSystem
 
  Real q[u];
  constant Integer u=2;
  
  Real h1(start=1);
  Real h2(start=1);
  
  
  parameter Real h0=3.2;
  parameter Real Area=33;
  parameter Real area=0.16;
  parameter Real g=982;
 
 equation
  for u in 1:u loop // u går från 1 till 10
  q[u]=21.96*u^(0.3853) + 0.3477;
 
 
  0 = -der(h1)+ q[u]/Area - (area/Area)* sqrt(2*g*(h1+h0));
  
  
  0 = -der(h2)+ (area/Area)* sqrt(2*g*(h1+h0)) - (area/Area)* sqrt(2*g*(h2+h0));
  
  
  end for;
  
 
end DoubleTankSystem;**

1 Answers1

0
Below code worked for me:

model DoubleTankSystem

  Real q[u];
  constant Integer u=2;

  Real h1(start=1);
  Real h2(start=1);


  parameter Real h0=3.2;
  parameter Real Area=33;
  parameter Real area=0.16;
  parameter Real g=982;

algorithm 
  for u in 1:u loop
  q[u]:=21.96*u^(0.3853) + 0.3477;


  der(h1) :=  q[u]/Area - (area/Area)* sqrt(2*g*(h1+h0));


  der(h2):=  (area/Area)* sqrt(2*g*(h1+h0)) - (area/Area)* sqrt(2*g*(h2+h0));


  end for;


end DoubleTankSystem;

plots of h1 and h2 as simulated in Dymola

Akhil Nandan
  • 315
  • 2
  • 9
  • As you have rightly pointed out Modelica creates a new equation for every equation inside a 'for' loop. Here you wish to overwrite at least one variable as it loops like a imperative code, so it is better to write the statements inside **algorithm** instead of **equation** section. algorithm section treats the statements as assignments from one variable to another similar to a imperative code and allows overriding, which means they won't create additional equations as you have experienced. See https://stackoverflow.com/questions/20078276/difference-between-equation-and-algorithm-section. – Akhil Nandan Oct 14 '22 at 05:13