4

I have a model representing some chemical process, and I want the reaction model to be switchable between an absorption and a desorption class (which define the respective model), based on a boolean parameter. I tried to do it like this:

model Sorption
  boolean parameter absorbing;
  AbsorptionModel if absorbing else Desorptionmodel reaction;
equation
  reaction.T = T; //dummy usage
  ...

Use it like:

Sorption TestAbsorption(absorbing=true); // uses the absorption model
Sorption TestDesorption(absorbing=false); // uses the desorption model

Of course, this way does not work. absorbing is known at compile time, so I have a feeling it should be ok to achieve this somehow.

I tried to use replaceable, but I don't want to (unnecessarily) make two separate subclasses of Sorption just to switch the type of reaction model. It seems replaceable/redeclare is only useable when inheriting, but I may be wrong? Is there a way to do what I want?

AbsorptionModel and DesorptionModel both inherit from the same base class, and have identical interfaces, if that is relevant.

Christoph
  • 5,480
  • 6
  • 36
  • 61

1 Answers1

4

No if is needed and you cannot use if with component declaration, except for conditional components (but that will only remove the component declaration and its connection equations).

model Sorption
  boolean parameter absorbing;
  replaceable model RModel = AbsorptionModel;
  RModel reaction;
equation
  reaction.T = T; //dummy usage
  ...

Use it like:

Sorption TestAbsorption(redeclare model RModel = AbsorptionModel); // uses the absorption model
Sorption TestDesorption(redeclare model RModel = Desorptionmodel); // uses the desorption model
Adrian Pop
  • 4,034
  • 13
  • 16
  • OK, that's what I suspected. The real application has not one, but several models for absorption and desorption, respectively. That gets unwieldy quickly, and is why I want to hide the implementation details, instead only presenting the boolean to the user. Maybe I'll try with the conditional components approach. Thank you! – Christoph Jul 29 '16 at 11:46
  • So, the way I finally solved this was to pull the boolean switch into the reaction model class itself, and switch in the equations section between ab- and desorption. A bit worse w.r.t. compartmentalization of models, but it feels less hackish than the alternatives. – Christoph Aug 01 '16 at 12:49
  • I wanted to suggest it, but I don't like it that much because it breaks the encapsulation and the model becomes large and unmanageable if you have several cases. Some tools have support for redeclared choices, see annotation “choices” in Modelica Specification. – Adrian Pop Aug 01 '16 at 16:44