Constants in Modelica

Our program will demonstrate the use of constants. Modelica supports constants of various types including strings, numbers, and booleans.

model ConstantsExample
  // Declare a string constant
  constant String s = "constant";

  // Declare a numeric constant
  constant Integer n = 500000000;

  // A constant expression with arbitrary precision
  constant Real d = 3e20 / n;

algorithm 
  // Print the constants
  Modelica.Utilities.Streams.print(s);
  Modelica.Utilities.Streams.print(String(d));
  Modelica.Utilities.Streams.print(String(Integer(d)));
  Modelica.Utilities.Streams.print(String(Modelica.Math.sin(n)));
end ConstantsExample;

To run this program, save the code in a file named ConstantsExample.mo and use a Modelica environment like OpenModelica to compile and execute it.

$ omc ConstantsExample.mo
$ ./ConstantsExample
constant
6e+11
600000000000
-0.28470407323754404

Now that we have demonstrated the basics of using constants in Modelica, let’s move on to more advanced features.