Skip to content

How to: Conditional input connectors

marcusfuchs edited this page Jun 16, 2017 · 1 revision

This page describes how to model conditional input connectors following the practice of the Modelica IBPSA Library.

Use Case

You want to model a component that needs an input from outside and you want the users to decide on whether

  • to provide the input value as a parameter or
  • to provide the input via an optional connector (in this case the input can be a fixed value or a time series).

Example

The Modelica IBPSA Library provides some models that implement the above use case to AixLib. One of these models is AixLib.Fluid.Sources.Boundary_pT. The model uses e.g. the Boolean parameter use_p_in to specify the input for the source's pressure. From the model's documentation:

If use_p_in is false (default option), the p parameter is used as boundary pressure, and the p_in input connector is disabled; if use_p_in is true, then the p parameter is ignored, and the value provided by the input connector is used instead. The same applies to the temperature, composition and trace substances.

How to use this approach in your model

Let's say we want to model a conditional input connector for the prescribed heat flow rate that our model should extract from or add to a fluid stream. Therefore, we use the parameter use_Q_in to decide between inputs from the parameter precribedQ and the conditional input connector Q_in.

To begin, we add the Boolean decision parameter to our model:

parameter Boolean use_Q_in = false
  "Get the prescribed heat flow rate from the input connector"
  annotation(Evaluate=true, HideResult=true);

Next, we define the parameter prescribedQ and annotate it to only be enabled if use_Q_in is false:

parameter Modelica.SIunits.HeatFlowRate prescribedQ
  "Fixed value of prescribed heat flow rate"
  annotation (Dialog(enable = not use_Q_in));

In addition, we define the input connector Q_in. The if use_Q_in makes the use of this connector conditional on the value of use_Q_in:

Modelica.Blocks.Interfaces.RealInput Q_in(final unit="W") if use_Q_in
  "Prescribed heat flow rate connector"
  annotation (Placement(transformation(extent={{-140,60},{-100,100}})));

After defining the inputs, we also have to define an internal connector Q_in_internal. As this is only needed internally, we will make that protected:

protected 
  Modelica.Blocks.Interfaces.RealInput Q_in_internal(final unit="W")
    "Needed to connect to conditional connector";

Now we can put all these options together in the equation section. First, we'll connect Q_in to Q_in_internal

equation
  connect(Q_in, Q_in_internal);

Now, if the users do not want to use the conditional connector, we use an if-clause to set Q_in_internal to the value of prescribedQ for this case:

  if not use_Q_in then
    Q_in_internal = prescribedQ;
  end if;

From here on, we can use the variable Q_in_internal in all our further equations to work with the conditional input.

Acknowledgment

This approach is taken from the Modelica IBPSA Library, thanks to the developers for coming up with this solution

Call to action

If

  • anything on this page is not clear to you,
  • you found an error on this page,
  • you have a suggestion for improvement,
  • you experience a problem with this approach in your branch of AixLib,
  • you have a better solution for modeling conditional input connectors,
  • ...

Make sure to raise the issue on our Issue Tracker!

Clone this wiki locally