有没有办法将参数传递给可替换/已声明的组件?

时间:2019-01-28 09:49:24

标签: modelica

这个问题与this previous question有关。

我有一些子模型可以互换,并且我使用可替换/重新声明机制将它们包括在模型中(例如,冷却回路模型中不同类型的热交换器的子模型)。

我想将主模型的某些参数(例如管道的长度和直径)“链接”到子模块的相应参数。通常在定义模型实例时(即在replaceable行中),但是重新声明componenet时也如何应用此链接?特别是如果使用choicesAllMatching

这是“我的”模型(感谢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;

我可以通过编写例如choice(redeclare Test.Call2 OBJ(pp=pp0))而不是使用choiceAllMatching来在注释中添加所有可能的重新声明,但是当许多子模块可互换时,这可能会变得乏味且容易出错(这将更容易,更安全。只需写一次“链接”)。 我尝试通过在“主要模型参数”部分中添加通用OBJ.pp = pp0,但是不接受。正确的做法是什么?

1 个答案:

答案 0 :(得分:3)

您只需要将修饰符写入约束类:

>>> myclass = MyException
>>> isinstance(myclass, Exception)
False