有没有办法将参数传递给 replaceable/redeclared 组件?
Is there a way to pass parameter to a replaceable/redeclared component?
此问题与有关。
我有一些可以互换的子模型,我使用 replaceable/redeclare 机制将它们包含在模型中(例如,冷却回路模型中不同类型热交换器的子模型)。
我想 "link" 将主模型的一些参数(比方说管道的长度和直径)设置为子模块的相应参数。这通常是在定义模型实例时完成的(即在 replaceable
行中),但是如何在重新声明组件时也应用此 link 呢?特别是如果使用 choicesAllMatching
?
这是"my"模型(感谢中的帮助):
package Test
// Original definition of Component 1 and 2 in the external library
// COMP1 (COMP2) has a parameter p1 (p2) defined with a default value
package ReadOnlyLibrary
model COMP1
parameter Real p1=1 "";
Real v "";
equation
v=p1*time;
end COMP1;
model COMP2
parameter Real p2=1 "";
Real v "";
equation
v=p2*time;
end COMP2;
end ReadOnlyLibrary;
// Interface and variants with modified default values
partial model Call_Interface
parameter Real pp = 10; // New parameter definition to have the same name for all variants
Real v "";
end Call_Interface;
// Both Call1 and Call2 parameters (p1 and p2) are linked to pp
model Call1 "Default"
extends Call_Interface;
extends ReadOnlyLibrary.COMP1(p1=pp);
end Call1;
model Call2 "Variant"
extends Call_Interface;
extends ReadOnlyLibrary.COMP2(p2=pp);
end Call2;
// Main module (system)
model Main
parameter Real pm=100 "";
parameter Real pp0=1 ""; //Actual parameter value to be used by submodules for this application -> pp
Real vm "";
replaceable Test.Call1 OBJ(pp=pp0) constrainedby Test.Call_Interface annotation (choicesAllMatching); //For default definition, pp, and finally p1, are linked to pp0. But when OBJ is redeclarated, the link is lost and p1/p2 gets its default value.
equation
vm = OBJ.v+pm;
end Main;
// Application model, using the main model
model App
Main main;
end App;
end Test;
我可以通过编写例如 choice(redeclare Test.Call2 OBJ(pp=pp0))
而不是使用 choiceAllMatching
在注释中添加所有可能的重新声明,但是当许多子模块可以互换时,这可能会变得乏味且容易出错(这会更容易和只写一次 "link" 更安全)。
我尝试在主模型参数部分添加一个通用的 OBJ.pp = pp0
,但这不被接受。这样做的正确方法是什么?
您只需将修饰符写入约束 class:
replaceable Test.Call1 OBJ constrainedby Test.Call_Interface(pp=pp0)
annotation (choicesAllMatching);
此问题与
我有一些可以互换的子模型,我使用 replaceable/redeclare 机制将它们包含在模型中(例如,冷却回路模型中不同类型热交换器的子模型)。
我想 "link" 将主模型的一些参数(比方说管道的长度和直径)设置为子模块的相应参数。这通常是在定义模型实例时完成的(即在 replaceable
行中),但是如何在重新声明组件时也应用此 link 呢?特别是如果使用 choicesAllMatching
?
这是"my"模型(感谢
package Test
// Original definition of Component 1 and 2 in the external library
// COMP1 (COMP2) has a parameter p1 (p2) defined with a default value
package ReadOnlyLibrary
model COMP1
parameter Real p1=1 "";
Real v "";
equation
v=p1*time;
end COMP1;
model COMP2
parameter Real p2=1 "";
Real v "";
equation
v=p2*time;
end COMP2;
end ReadOnlyLibrary;
// Interface and variants with modified default values
partial model Call_Interface
parameter Real pp = 10; // New parameter definition to have the same name for all variants
Real v "";
end Call_Interface;
// Both Call1 and Call2 parameters (p1 and p2) are linked to pp
model Call1 "Default"
extends Call_Interface;
extends ReadOnlyLibrary.COMP1(p1=pp);
end Call1;
model Call2 "Variant"
extends Call_Interface;
extends ReadOnlyLibrary.COMP2(p2=pp);
end Call2;
// Main module (system)
model Main
parameter Real pm=100 "";
parameter Real pp0=1 ""; //Actual parameter value to be used by submodules for this application -> pp
Real vm "";
replaceable Test.Call1 OBJ(pp=pp0) constrainedby Test.Call_Interface annotation (choicesAllMatching); //For default definition, pp, and finally p1, are linked to pp0. But when OBJ is redeclarated, the link is lost and p1/p2 gets its default value.
equation
vm = OBJ.v+pm;
end Main;
// Application model, using the main model
model App
Main main;
end App;
end Test;
我可以通过编写例如 choice(redeclare Test.Call2 OBJ(pp=pp0))
而不是使用 choiceAllMatching
在注释中添加所有可能的重新声明,但是当许多子模块可以互换时,这可能会变得乏味且容易出错(这会更容易和只写一次 "link" 更安全)。
我尝试在主模型参数部分添加一个通用的 OBJ.pp = pp0
,但这不被接受。这样做的正确方法是什么?
您只需将修饰符写入约束 class:
replaceable Test.Call1 OBJ constrainedby Test.Call_Interface(pp=pp0)
annotation (choicesAllMatching);