I have a Modelica model that has a RealInput connector. Usually, a constant source block with value 0 is connected to that input, but sometimes (not often) different values or time varying signals are used.
Is there a possibility/solution to not connect a constant source block and change the model to use a default value if there is no signal coming (i.e. the RealInput is not connected from outside)? Currently, I get a warning that the model is not balanced if the RealInput is not connected from outside.
I am looking for a similar solution like Modelica functions, where a default value can be defined for inputs, or parameters, that can have a default value if nothing else is specified.
Make the input conditional and use an internal constant block if its not active.
Below is a minimal example (without graphical annotations, to make the code sleeker):
block ConditionalInput
import Modelica.Blocks;
parameter Boolean useInput = false "true: use input connector for source signal. false: use 0";
Blocks.Interfaces.RealInput u if useInput "Variable input value";
// Output only needed for exemplary equation
Blocks.Interfaces.RealOutput y "Output value";
protected
Blocks.Interfaces.RealOutput val "Helper to access the actual value";
Blocks.Sources.Constant const(k=0) if not useInput;
equation
connect(const.y, y);
connect(u, y);
// Exemplary equation
y = val * 3;
end ConditionalInput;
You can simply instantiate this block and it will use 0 for val. In the cases, where you need an input, activate it by setting useInput=true.
Note: This example uses conditional components. The Modelica standard permits their usage only in connect statements. Accessing u in regular equations is not allowed, so the protected RealOutput val is included.
In other words: It is not allowed to write
y = if useInput then u else 0;
so the protected Constant source block, the RealOutput and the connect statements are needed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With