2
votes

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.

1
You can use function pointers in Modelica, see for example 12.4.2 Functional Input Arguments to Functions in Modelica Specification 3.3: modelica.org/documents/ModelicaSpec33Revision1.pdf - Adrian Pop
That section was exactly on 'point'. pun intended :) Thank you very much. I've also bookmarked the modelica spec doc for future reference. - Scott G
In regards to Question #3 (string to function handle name) is that something Modelica supports? - Scott G
Question #3 will only work if you use if expressions or if equations: y := if func_string = "func1" then f1(x) else f2(x); - Adrian Pop
Got it. Once again I appreciate your insight. - Scott G

1 Answers

1
votes

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,a);
end blah;

function Test3func
  input Real x;
  input d f;
  output Real y;
algorithm 
  y := f(x);
end Test3func;