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

IF statement errors - help needed

IF statement errors - help needed

Hi,

I am having a problem using an if-statement in a model I am developing  to simulate bacteria growth in microfluidic systems. 

While I have used if-else statements in Modelica before (actually in other sub-models for this system), I am encountering simulation failures for a current model that I am attempting to develop. 

The model code is as follows:

model GrowthVelocity
  BiofilmNode i,o,p;
  parameter Real dz = 1;
  parameter Real biofilm_density = 1;
  Real Number;
  Real biomass_production;
  Real inactivebiomass_prod;
equation
  if Number > 1 then
    i.uz = (( (2*dz) * (biomass_production + inactivebiomass_prod)) / biofilm_density) + p.uz;
  else
    o.uz = 0;
    i.uz = (( (2*dz) * (biomass_production + inactivebiomass_prod)) / biofilm_density) + p.uz;
  end if ;

end GrowthVelocity;

When I attempt to run the simulation, I get the following error:

Error: The language feature if-equations is not supported. Suggested workaround: rewrite equations using if-expressions: equation
  if thebiofilm.velocity_char[1].Number > 1.0 then
  thebiofilm.velocity_char[1].i.uz = 2.0 * (thebiofilm.velocity_char[1].dz * (thebiofilm.velocity_char[1].biomass_production + thebiofilm.velocity_char[1].inactivebiomass_prod) / thebiofilm.velocity_char[1].biofilm_density) + thebiofilm.velocity_char[1].p.uz;
  else
  thebiofilm.velocity_char[1].o.uz = 0.0;
  thebiofilm.velocity_char[1].i.uz = 2.0 * (thebiofilm.velocity_char[1].dz * (thebiofilm.velocity_char[1].biomass_production + thebiofilm.velocity_char[1].inactivebiomass_prod) / thebiofilm.velocity_char[1].biofilm_density) + thebiofilm.velocity_char[1].p.uz;
  end if;


I am confused because it is essentially telling me that I am using the wrong if-statement syntax, and then suggests the exact same syntax as a way to fix the error.  Again, I have used this exact same construct before in a similar model such as the following with no problems:

model ActiveBiomass "a characteristic :: similar to biomass model"
  BiofilmNode i,o,p;
  parameter Real dz = 1;
  parameter Real Voxygen = 1;
  parameter Real Yboxygen = 1;
  parameter Real Beta = 1;
  parameter Real Koxygen = 1;
  parameter Real B = 1;
  Real Number;   
  Real biomass_production;
equation
  biomass_production = B*o.rho_biomass*(o.rho_oxygen / (Koxygen+o.rho_oxygen));
  if Number > 1 then
    der(o.rho_biomass) = biomass_production - (o.uz * ((i.rho_biomass - p.rho_biomass) / (2*dz))) - (o.rho_biomass * ((i.uz - p.uz) / (2*dz)));   
  else
    o.rho_biomass = 0;
  end if;


end ActiveBiomass;

I do not see why one model would work with this syntax and another would not, even though they are identical in structure.  Has anyone else encountered a similar problem or have any insight as to why this would not be working?

Any help would be greatly appreciated.

Matt

Re: IF statement errors - help needed

No, it's suggesting using if-expressions instead of if-statements (and then showing the old if-statement). It failed when trying to rewrite the statement to if-expressions; our backend cannot actually handle statements.

Anyway, I believe the problem is that you have a mismatch in the number of equations in the if-equations. This is only allowed if the condition expression is parameter (?) or constant because then the statement can be optimized to be only one branch.

Re: IF statement errors - help needed

Thanks for the response @sjoelund !

Technically, what is the difference between an if-statement and an if-equation? And if the issue is that there is 1 equation in the first if statement and 2 equations in the else statement, how would you suggest working around this problem? 

Again, thanks for the help!

Re: IF statement errors - help needed

Well, since Modelica is balanced you must not change the number of equations in a system, or it cannot be simulated.
The main difference between if-statement and if-expression is that you can use things like assertions in statements.

In OpenModelica, a statement like

Code:

if cond then

  a = b;
else
  d = e;
end if;

is converted into

Code:

0 = if cond then a-b else d-e;

If you really need to have a different number of equations, make the condition a parameter. That way you specify that the number of equations cannot change during the simulation.

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