Search code examples
modelicaopenmodelica

Switch type based on boolean parameter (without inheriting)?


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.


Solution

  • 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