Question

The variables of Modelica classes are instantiated per object.

Can anyone clarify this statement?

Was it helpful?

Solution

First of all Modelica has only one type of object, the class, and the other objects (model, record, package) are only special types of class with restrictions. That means that every object must follow the same rules, therefore I'll refer to the object class, but what I'll write will apply to every Modelica object. The variables are instantiated per object means that if you have a Modelica class like the following one:

partial class MySimpleClass

  Real variable1;

equation

  variable1 = time;

end MySimpleClass;

When you declare a member variable such as MySimpleClass in instanceOfTypeMySimpleClass:

class mySecondClass

  MySimpleClass instanceOfTypeMySimpleClass;
  MySimpleClass instanceTwoOfTypeMySimpleClass;

  Real variable1;

equation

  variable1 = instanceOfTypeMySimpleClass.variable1;
  instanceTwoOfTypeMySimpleClass.variable1 = 3;

end mySecondClass;

you declare an instance variable. Every time you create an instance of a class, the Modelica compiler creates one copy of each the class's instance variables for the instance and this is done by objects. You can access an object's instance variables from an object using the syntax <instanceName>.<VariableName>.

The Modelica compiler allocates class variables once per instance. The The Modelica compiler allocates memory for class variables every time it encounters an instance of that class. Therefore all instances DID NOT share the same copy of the class's class variables as, for example, for static classes in Java. In other words in this example instanceOfTypeMySimpleClass.variable1 and instanceTwoOfTypeMySimpleClass.variable1 are two different variables.

"The variables of Modelica classes are instantiated per object." meant to describe this difference.

I hope this helps, Marco

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top