This question is related to this previous question.
I have some submodels that are interchangeable and I use the replaceable/redeclare mechanism to include them in a model (e.g. submodels of different type of heat-exchangers in a cooling loop model).
I would like to "link" some parameters of the main model (let's say the pipes' length and diameter) to the corresponding parameters of the submodule. This is usually done when defining a model instance (i.e. in the replaceable
line), but how can this link be applied also when the componenet is redeclared? Expecially if choicesAllMatching
is used?
Here is "my" model (thanks to helpers in the previous question):
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;
I could add all possible redeclarations in the annotation by writing for example choice(redeclare Test.Call2 OBJ(pp=pp0))
instead of using choiceAllMatching
but that may become tedius and error prone when many submodules are interchangeables (it would be easier and safer to write the "link" just once).
I tried by adding a generic OBJ.pp = pp0
in Main model parameter section, but this is not accepted. What is the proper way of doing that?
You simply have to write the modifiers to the constraining class:
replaceable Test.Call1 OBJ constrainedby Test.Call_Interface(pp=pp0)
annotation (choicesAllMatching);