I build a model that includes a derivative of dy/dx=5
, as shown in the following segment, but the model fails during initialization in Dymola, in another question, it has been answered. The reason is that at time=0, the values of der(x)
and der(y)
are both 0. So there would be an error of division by 0.
model HowToExpressDerivative "dy/dx=5, how to describe this equation in Modelica?"
extends Modelica.Icons.Example;
Real x, y;
equation
x = time ^ 2;
der(y) / der(x) = 5;
end HowToExpressDerivative;
But when I try the same model in the Wolfram System Modeler and OpenModelica, the model works fine.
In the Wolfram System Modeler, it uses the equation of der(y)/der(x)=5
directly.
My questions are:
Dymola does currently not recognize that trivial simplification in that case (it is done in another case), not because it would be too complicated, but because Dymola uses a simple rule:
Division in the Modelica code are safe, other divisions are not considered safe.
Thus der(x)/der(y)=5
indicates that division by der(y) is safe (even though it clearly isn't), whereas solving that equation technically involves dividing by 1/der(y)
which isn't deemed safe (as it didn't occur in the Modelica code).
Obviously the logic can be improved, but the simplest solution is to use the following rule: don't divide in your code unless it is safe to do so.
A benefit of following that rule is that it will be possible to check all the original equations and see that they are satisfied (up to numerical precision) - without triggering division by zero.