Search code examples
annotationsconditional-statementsmodelica

Modelica annotation inverse conditional


I have a function that does or does not have a derivative and inverse, depending on values of some parameters/coefficients. Can I make the annotations derivative and inverse conditional somehow? Something like

function y_from_x
input Real x;
input Boolean hasInverse;
output Real y;
...
equation
...
annotation(inverse(x=x_from_y(y=y) if hasInverse));
end y_from_x;

Solution

  • There is no direct support for that in the Modelica specification. What you could do is something like:

    function y_from_x
      input Real x;
      input Boolean hasInverse;
      output Real y;
    ...
    algorithm
      y:=if hasInverse then y_from_x1(x) else y_from_x2(x);
      annotation(Inline=true);
    end y_from_x;
    
    function y_from_x1
      input Real x;
      output Real y;
      ...
    algorithm
       y:=y_from_x2(x);
      annotation(inverse(x=x_from_y(y=y)));
    end y_from_x1;
    
    function y_from_x2
      input Real x;
      output Real y;
      ...
    algorithm
      ...
    end y_from_x2;