4

I get an error on Modelica saying:

All branches in if equation with non-parameter tests must have the same number of equations

The source of the error is following section of the code:

equation
  if der(Posit2.s)<=0 then
    pressure=4e5+((500e5-4e5)/0.0111)*(0.0111-Posit2.s);
  end if;

Do you know how to deal with this error?

marco
  • 5,944
  • 10
  • 22
alimuradpasa
  • 135
  • 1
  • 10

1 Answers1

6

You need an else, so the obvious idea is to say that pressure doesn't change:

 equation
    if der(Posit2.s)<=0 then
           pressure=4e5+((500e5-4e5)/0.0111)*(0.0111-Posit2.s);
    else
           der(pressure)=0;
    end if;

However, this will likely not compile due to the index-problem. One possibility is to do manual index reduction and write something like:

 initial equation
    if der(Posit2.s)<=0 then
           pressure=4e5+((500e5-4e5)/0.0111)*(0.0111-Posit2.s);
    else
           pressure=4e5;
    end if;
 equation
    if der(Posit2.s)<=0 then
           der(pressure)=((500e5-4e5)/0.0111)*(-der(Posit2.s));
    else
           der(pressure)=0;
    end if;

Note that this equation has der(pressure)=...*der(Posit2.s); - due to the manual index reduction.

Hans Olsson
  • 11,123
  • 15
  • 38
  • I implemented this code suggested by you. It throws no error when it compiles but gives an error when it calculates. Error is : Error encountered in the Initialite function of the user model. – alimuradpasa Nov 16 '20 at 08:46
  • You could try to replace the initial equation by something simpler like: initial equation pressure=4e5+((500e5-4e5)/0.0111)*(0.0111-Posit2.s); (without any if), or even initial equation pressure=4e5; – Hans Olsson Nov 16 '20 at 09:09