来自字符串的可替换函数和函数调用

时间:2015-12-04 14:36:40

标签: modelica dymola openmodelica jmodelica

以下三个问题捆绑在一起,请原谅帖子的长度。

使用Dymola 2016。

在模型中使用可替换的函数调用为用户提供了下拉选项的机会。示例如下:

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

在函数内执行相同的可替换函数调用似乎不允许调用函数的相同下拉功能(即在包浏览器中右键单击调用函数。我认为这是故意的,因为函数通常在其他函数中调用功能/模型。例如:

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

问题#1。是否可以像在模型中一样使用函数内的可替换函数调用?如果是这样,那么适当的语法是什么?替代方法?

或者,另一种选择是在模型中执行可替换函数调用,然后将结果传递给另一个函数,然后进行适当的调用。示例如下所示:

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

将参数x和函数句柄a传递给:

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

问题#2。在Modelica中这是允许的吗?如果是这样,怎么样?替代方法?

问题#3。是否可以定义一个字符串并将其转换为函数的名称。示例如下:

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

提前谢谢!我继续探索Modelica的使用,感谢您的反馈。

1 个答案:

答案 0 :(得分:1)

这应该可以正常工作:

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;