Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

default value for not connected RealInput

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.

like image 970
Priyanka Avatar asked Apr 23 '26 09:04

Priyanka


1 Answers

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.

like image 102
marco Avatar answered Apr 27 '26 21:04

marco