modelicadymolaopenmodelicajmodelica

Replaceable function and function calls from strings


The following three questions are tied together so please forgive the length of the post.

Using Dymola 2016.

Using a replaceable function call within a model provides the opportunity for the user to have the drop down options. Example below:

model Test1
  parameter Real x = 1;
  Real y;
  replaceable function a=b constrainedby d annotation(choicesAllMatching=true);
equation 
  y = a(x);
end Test1;

Doing the same replaceable function call within a function seems to not permit the same drop down functionality with the the function is called (i.e. right click call function in package browser. I assume this is intentional as a function is typically called within other functions/models. Example below:

function Test2
  input Real x;
  output Real y;
  replaceable function a=b constrainedby d annotation(choicesAllMatching=true);
algorithm 
  y :=a(x);
end Test2;

Question #1. Is it possible to use a replaceable function call within a function in the same way you do a model? If so, what is the appropriate syntax? Alternative approach?

Alternatively, a different option would be to perform the replaceable function call in the model and then pass the result to another function that then makes the appropriate call. Example shown below:

model Test3mod
  parameter Real x = 1;
  Real y;
  replaceable function a=b constrainedby d annotation(choicesAllMatching=true);
equation 
  y = Test3func(x,a);
end Test3mod;

Which passes parameter x and function handle a to:

function Test3func
  input Real x;
  input ???? a;
  output Real y;
algorithm 
  y :=a(x);
end Test3func;

Question #2. Is this allowable in Modelica and if so, how? Alternative approach?

Question #3. Is it possible to define a string and turn that into a the name of a function. Example below:

model Test4
  parameter String 'functionname';
  parameter Real x = 1;
  Real y;
equation
  y = functionname(x);
end Test4;

Thank you in advance! I appreciate your feedback as I continue to explore the use of Modelica.


Solution

  • This should work fine:

    model Test3mod
      parameter Real x = 1;
      Real y;
      replaceable function a=b constrainedby d annotation(choicesAllMatching=true);
    equation 
      y = Test3Func(x, function a);
    end blah;
    
    function Test3func
      input Real x;
      input d f;
      output Real y;
    algorithm 
      y := f(x);
    end Test3func;