Search code examples
modelicaopenmodelicadymola

Add Explicit Derivative Function to Modelica.Math.Vectors.interpolate


I have a steady-state algebraic model (no der() expressions) with one non-linear equation. When translating in Dymola, the set of equations results in a numerical Jacobian. I would like to remove the numerical Jacobian (if possible) to improve performance of the model.

After setting the parameter Hidden.PrintFailureToDifferentiate=true (thanks to this tip from Claytex), I see that Dymola issues the warning:

Cannot find derivative of function:
InitializationParameterLoop.InitFunctions.interpolate(xVector, yVector, x, 1)

Note that the InitializationParameterLoop.InitFunctions.interpolate function is a copy of the Modelica.Math.Vectors.interpolate function, copied so that I can make modifications if necessary within my own package.

I attempted to add an explicit definition for the derivative dy/dx by making the following changes to the interpolate function:

  • Add the derivative annotation to the function definition:

    function interpolate "Interpolate in a vector" extends Modelica.Icons.Function annotation(derivative = dydx);

  • Add a protected Real variable dydx:

    Real dydx;

  • Calculate dydx in the algorithm section:

    if abs(x2-x1)>0 then dydx :=(y2 - y1)/(x2 - x1); else dydx := sign(y2 - y1)*Modelica.Constants.inf; end if;

I was hoping that this simple method could be used to explicitly define dydx and remove the numerical Jacobian calculation, but it does not seem to make any change and Dymola still issues a warning about the

Am I misunderstanding the use of the derivative annotation? If so, can someone help me to understand how to define the derivative for the interpolate function?

Thank you!


Solution

  • This is the same issue as https://github.com/modelica/Modelica/issues/2078 and will be fixed in the next backward-compatible maintenance release of the Modelica Standard Library.