3
votes

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.

1
PS: this is related to / follow-up to this question: stackoverflow.com/questions/60546082/…Priyanka
Does this help? Seems related. stackoverflow.com/questions/58977884/…Markus A.
Could you give a minimal example? Why is there nothing connected? Can't you connect a constant-source block instead in that case?marco
I can connect a constant source block, that is how I do it now (updated the question also). But if I have 20 components, and 19 use constant 0, then it is a bit annoying to add and connect 19 constant source blocks with value 0.Priyanka

1 Answers

4
votes

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.