One of the advantages of Modelica is that you can define your model non-causally. For example you can write h=10 - g/2 * t^2
to define the relation between the height of a falling object (starting at 10m above the ground) and time, but you can also let modelica solve time as function of height, without having to rewrite the equation to a sqrt. This works as long as the system has only one possible solution.
The reverse function in this case however has 2 solutions, -t gives the same height as +t. When the object was thrown up in the air and reached its highest point (=10m) at t=0, the negative solution is when the object was thrown up.
The subject is mentioned on modelica university, but no solution is given there.
In general it applies to any situation where multiple solutions are possible, e.g. solve alpha in y=sin(alpha)
and define -pi/2<alpha<pi/2
or pi/2<alpha<3pi/2
. This is relevant because the slopes in both intervals are opposite.
The problem is demonstrated in the model below
model test_range
Real x,x_rev(start=2),y;
equation
der(x)=1000;
when x>=3*Modelica.Constants.pi/2 then
reinit(x,Modelica.Constants.pi/2);
end when;
y=sin(x);
y=sin(x_rev);
annotation (experiment(
__Dymola_NumberOfIntervals=4000,
Tolerance=1e-06,
__Dymola_Algorithm="Dassl"));
end test_range;
Dymola simulation with solution interval jump: it starts in the interval around pi, but jumps to the interval around 2pi.
Simulation of same model in OpenModelica: the solution for x_rev jumps back and forth between the -pi/2--pi/2 interval and the pi/2--3pi/2 interval. (Note that the solution depends on the number of intervals, =100 in this plot)
Is it possible to set a solution domain for variables when there are multiple possible solutions?