3

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.

Priyanka
  • 559
  • 3
  • 13
  • PS: this is related to / follow-up to this question: https://stackoverflow.com/questions/60546082/modelica-nested-combined-connectors – Priyanka Apr 08 '20 at 08:19
  • 2
    Does this help? Seems related. https://stackoverflow.com/questions/58977884/replacement-of-deprecated-function-cardinalityc-in-modelica – Markus A. Apr 08 '20 at 08:44
  • Could you give a minimal example? Why is there nothing connected? Can't you connect a constant-source block instead in that case? – marco Apr 08 '20 at 09:45
  • 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 Apr 08 '20 at 11:01

1 Answers1

5

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.

marco
  • 5,944
  • 10
  • 22