Variables in Modelica

In Modelica, variables are explicitly declared and used by the compiler to check type-correctness of equations and algorithms.

model Variables
  String a;
  Integer b;
  Integer c;
  Boolean d;
  Integer e;
  String f;
equation
  a = "initial";
  b = 1;
  c = 2;
  d = true;
  f = "apple";
algorithm
  Modelica.Utilities.Streams.print(a);
  Modelica.Utilities.Streams.print(String(b) + " " + String(c));
  Modelica.Utilities.Streams.print(String(d));
  Modelica.Utilities.Streams.print(String(e));
  Modelica.Utilities.Streams.print(f);
end Variables;

In Modelica, variables are typically declared at the beginning of a model or function.

You can declare multiple variables of the same type in one line.

Modelica will infer the type of initialized variables, but it’s generally recommended to explicitly specify types.

Variables declared without a corresponding initialization are given default values. For example, the default value for an Integer is 0.

In Modelica, there’s no direct equivalent to the := syntax for declaring and initializing variables. Variables are typically declared at the top of the model and then assigned values in the equation or algorithm sections.

To run this Modelica model, you would typically use a Modelica simulation environment like OpenModelica or Dymola. The output would be similar to:

initial
1 2
true
0
apple

Note that Modelica is primarily used for modeling physical systems and simulations, so this example, which is more procedural in nature, isn’t typical of how Modelica is usually used. In a more typical Modelica model, you would define equations that describe the behavior of a system over time, rather than just printing values.