1

I am trying to prepare a modelica code to understand array and for loop. When I am compiling the code, I get following error.

The initialization problem is inconsistent due to the following equation: 0 != 1 = 1.0 - I[0] Error in initialization. Storing results and exiting.
Use -lv=LOG_INIT -w for more information. Simulation process failed. Exited with code -1.

I have tried to adjust array numbers to see if this solves. Could not trace the reason why the code is failing.

class flu
    parameter Real beta = 10.0/(40*8*24);
    parameter Real gamma= 3.0/(15*24);
    parameter Real dt = 0.1;
    parameter Real D  = 30;
    parameter Integer N_t = integer(D*24/dt);
    parameter Integer array = integer(N_t*dt);
    //parameter Real time[array];
    Real S[array] ;
    Real I[array] ;
    Real R[array] ;

initial equation
    S[0] = 50;
    I[0] = 1;
    R[0] = 0;

equation
    for n in 0:(array-1) loop
    S[n+1] = S[n] - dt*beta*S[n]*I[n];
    I[n+1] = I[n] + dt*beta*S[n]*I[n] - dt*gamma*I[n];
    R[n+1] = R[n] + dt*gamma*I[n];
    end for;

annotation(
    __OpenModelica_simulationFlags(lv = "LOG_STATS", outputFormat = "mat", s = "dassl"));end flu;

I am expecting to get three curves as a result .

damu_d
  • 53
  • 1
  • 8

2 Answers2

3

There seems to be a few things wrong here. First of all, it seems OpenModelica does not output the index of the equation so the debugger can be opened.

But more importantly, you are setting

I[0] = 1

I[1] is the first index in Modelica... I don't know why this wouldn't give any warning in OpenModelica. Probably some edge case in an if equation that never triggers and thus no warning/error in this case.

class flu
    parameter Real beta = 10.0/(40*8*24);
    parameter Real gamma= 3.0/(15*24);
    parameter Real dt = 0.1;
    parameter Real D  = 30;
    parameter Integer N_t = integer(D*24/dt);
    parameter Integer array = integer(N_t*dt);
    //parameter Real time[array];
    Real S[array] ;
    Real I[array] ;
    Real R[array] ;

equation
    S[1] = 50;
    I[1] = 1;
    R[1] = 0;

equation
    for n in 1:(array-1) loop
    S[n+1] = S[n] - dt*beta*S[n]*I[n];
    I[n+1] = I[n] + dt*beta*S[n]*I[n] - dt*gamma*I[n];
    R[n+1] = R[n] + dt*gamma*I[n];
    end for;
end flu;
sjoelund.se
  • 3,468
  • 12
  • 15
  • - Since Open Modelica does not output the index of the equation, do you have any suggestion how to solve this equations? – damu_d Aug 17 '19 at 19:30
  • I have tried to find & run the diff equation without arrays and it seems to solve the error. – damu_d Aug 17 '19 at 19:59
  • Well, the code I posted is a working version of the code; I found the equation by looking in the generated C-files (it is also shown in the error-message you got that `1.0 - I[0]` gave unexpected results). Or you solve it like damu_d suggested. – sjoelund.se Aug 18 '19 at 05:42
0
class flu
    parameter Real beta = 10.0/(40*8*24);
    parameter Real gamma= 3.0/(15*24);


    Real S(start = 50) ;
    Real I(start = 1) ;
    Real R(start = 0) ;


equation
    der(S)= -beta*S*I;
    der(I)=(beta*S*I) - (gamma*I);
    der(R)=gamma*I;

annotation(
    __OpenModelica_simulationFlags(lv = "LOG_STATS", outputFormat = "mat", s = "dassl"));end flu;
damu_d
  • 53
  • 1
  • 8