Modelica 可替换函数和来自字符串的函数调用
以下三个问题联系在一起,因此请原谅文章的长度 使用Dymola 2016 在模型中使用可替换函数调用为用户提供了拥有下拉选项的机会。示例如下:Modelica 可替换函数和来自字符串的函数调用,modelica,dymola,openmodelica,jmodelica,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 i
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;
model Test4
parameter String 'functionname';
parameter Real x = 1;
Real y;
equation
y = functionname(x);
end Test4;
在一个函数中执行相同的可替换函数调用似乎不允许在调用该函数时使用相同的下拉功能(即在程序包浏览器中单击鼠标右键调用函数)。我假设这是有意的,因为函数通常在其他函数/模型中调用。示例如下:
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;
model Test4
parameter String 'functionname';
parameter Real x = 1;
Real y;
equation
y = functionname(x);
end Test4;
问题#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 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;
model Test4
parameter String 'functionname';
parameter Real x = 1;
Real y;
equation
y = functionname(x);
end Test4;
提前感谢您!在我继续探索Modelica的使用时,我感谢您的反馈。这应该很好:
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;
您可以在Modelica中使用函数指针,例如,请参见Modelica规范3.3中函数的12.4.2函数输入参数:该部分正好在“点”上。双关语:)非常感谢。我还为modelica规范文档添加了书签,以备将来参考。关于问题#3(字符串到函数句柄名称)modelica支持什么?问题#3只有在使用if表达式或if方程时才起作用:y:=if func#u string=“func1”,然后f1(x)或者f2(x);知道了。我再次感谢你的洞察力。