Question

Let's say I maybe want to import a component based on some condition, let's say a boolean variable. I've tried this, but it gives me an error message. For instance, consider the following code:

model myNewModel
    parameter Boolean use_Something;
    myLibrary.myComponent component[if use_Something then 1 else 0];
    // In other words (pseudo):
    // if use_Something then 'Import The Component' else 'Do Nothing At All';
end myNewModel;

This is, intuitively, a safe statement, and as long as the boolean variable is true, it'll work as intended. For some units, for instance the fluidports of the Modelica Standard Library, it also works with the [0] size. But as soon as I turn the variable to false, I encounter errors regarding the fact that many components are not compatible with "zero size". I've had this problem, for instance, with the MassFlowSources in the Modelica Standard Library. Is there a smooth/elegant way to work around this? Thanks in advance!

Was it helpful?

Solution

You can use conditional components in Modelica.

model myNewModel
    parameter Boolean use_Something;
    myLibrary.myComponent component if use_Something;
end myNewModel;

This component may then only be used in connect-statements. If the condition is false, these connections are ignored by the tool.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top