OpenModelica / OpenModelica

OpenModelica is an open-source Modelica-based modeling and simulation environment intended for industrial and academic usage.
https://openmodelica.org
Other
850 stars 306 forks source link

Inconsistent handling of $cse expressions in models involving interpolate() and state variable change #11841

Open casella opened 10 months ago

casella commented 10 months ago

We have a fairy involved model that generates invalid C code due to a missing definition of a $cse variable which is then used in an expression.

We were not able to exactly reproduce the issue, but we managed to reproduce one which seems to be closely related and should be fixed anyway. This is the MWE:

model TestJac
  Real P(stateSelect = StateSelect.prefer), T(stateSelect = StateSelect.prefer);
  Real M(stateSelect = StateSelect.avoid), E(stateSelect = StateSelect.avoid);
  Real rho;
  Real V = 2 + sin(time);
  Real X[2];
  Real MMmix;
  Real c = Modelica.Math.Vectors.interpolate({1, 2, 3}, {1, 3, 8}, T, 1);
  constant Real MM[2] = {1, 2};
  parameter Real R = 1;
equation
  der(M) = 0;
  der(E) = 0;
  der(M*X) = {0, 0};
  M = rho*V;
  E = M*c*T;
  P/rho = R/MMmix*T;
  MMmix = 1/(X[1]/MM[1] + X[2]/MM[2]); 
end TestJac;

If we compile this model and then analyze it with the declarative debugger, we see some weird things happening:

What we observe is that cse3 involves differentiating a built-in function sin(time), which is not a big deal, while cse1 and cse2 involve the interpolate function, which is a true algorithm that require extra steps for differentiation. Maybe that holds the key for the solution of this issue.

casella commented 10 months ago

Keeping @AndreaBartolini in the loop

casella commented 7 months ago

@phannebohm, @kabdelhak, I tried this with the NB and I got this:

[1] 17:46:54 Translation Error
Internal error NBSlice.fillDependencyArray failed because number of flattened indices 1 for dependency $DER.M could not be divided by the body size 2 without rest.

[2] 17:46:55 Translation Error
Internal error NBAdjacency.Matrix.createPseudo failed for:
[ARRY] (2) $DER.M * X .+ M * $DER.X = {0.0, 0.0} ($RES_SIM_4)

[3] 17:46:55 Translation Error
Internal error NBAdjacency.Matrix.create failed to create adjacency matrix for system:
System Variables (10/11)
**************************
(1)       [ALGB] (1) Real T (StateSelect = prefer)
(2)       [ALGB] (1) Real rho
(3)       [ALGB] (1) Real MMmix
(4)       [DER-] (1) Real $DER.M
(5)       [ALGB] (1) Real P (StateSelect = prefer)
(6)       [DER-] (2) Real[2] $DER.X
(7)       [DISC] (1) Integer $FUN_2
(8)       [DER-] (1) Real $DER.E
(9)       [ALGB] (1) Real V = 2.0 + sin(time)
(10)      [ALGB] (1) Real c = Modelica.Math.Vectors.interpolate({1.0, 2.0, 3.0}, {1.0, 3.0, 8.0}, T, 1)[1] 
System Equations (9/11)
*************************
(1)       [SCAL] (1) MMmix = 1/(X[1] + 0.5 * X[2]) ($RES_SIM_0)
(2)       [SCAL] (1) P / rho = (R / MMmix) * T ($RES_SIM_1)
(3)       [SCAL] (1) E = c * M * T ($RES_SIM_2)
(4)       [TUPL] (2) (c, $FUN_2) = Modelica.Math.Vectors.interpolate({1.0, 2.0, 3.0}, {1.0, 3.0, 8.0}, T, 1) ($RES_$AUX_10)
(5)       [SCAL] (1) M = rho * V ($RES_SIM_3)
(6)       [ARRY] (2) $DER.M * X .+ M * $DER.X = {0.0, 0.0} ($RES_SIM_4)
(7)       [SCAL] (1) $DER.E = 0.0 ($RES_SIM_5)
(8)       [SCAL] (1) -(2.0 - V) = sin(time) ($RES_$AUX_9)
(9)       [SCAL] (1) $DER.M = 0.0 ($RES_SIM_6) 

so the NB is not there, either. I would be grateful if you could have a look at this issue with the OB, this is a blocker for us on an important project. Looks like some weird bug in WrapFunctionCalls and/or causalization.

Thanks!