2

I have classes:

class Parent 
end parent;

class Child 
  extends Parent; 
end Child;

In another class I need to declare a variable of type Parent and initialize it by an instance of type Child; something similar to the Java statement:

Parent p = new Child();

How can I do it in Modelica?

Thanks

rudyment
  • 31
  • 1

2 Answers2

1

The class concept of Modelica is not comparable with that of Java. For example, you don't create new objects, so there is nothing like

Parent p = new Child();

Instead, you just instantiate a class by writing

Parent p;

Therefore, you can not assign a child object to a parent base class. Instead, the inheritance feature of Modelica can be used for redeclaration. That means, that you can instantiate a replaceable base class somewhere which can later be changed using the redeclare statement.

See a simple example below. Example2 redeclares cls to the child class, so we get cls.c=2 when we simulate it.

package Demo

  class Parent
    constant Real c=1;
  end Parent;

  class Child
    extends Parent(c=2);
  end Child;

  model Example1
    replaceable Parent cls;
  end Example1;

  model Example2
     extends Example1(redeclare Child cls);
  end Example2;

end Demo;

As a summary, one could say that the object-orientation of Modelica reduces to

  • inheritance, using extends
  • redeclaration, using redeclare
marco
  • 5,944
  • 10
  • 22
1

I am not sure about what you want to achieve. I tried to figure it out considering this other question, but it could help to have more details of what features/behavior your parent, child, and Parent p = new Child() should do.

I've put together a quick overview of a couple common ways for dealing with inheritance. But you might be looking for a more specific case.

package Inheritance

import SI = Modelica.SIunits;

  connector PositivePin "Positive pin of an electrical component"
    SI.ElectricPotential v "Potential at the pin";
    flow SI.Current i "Current flowing into the pin";
  end PositivePin;

  partial model OnePort
    "Component with two electrical pins p and n and current i from p to n"
    SI.Voltage v "Voltage drop of the two pins (= p.v - n.v)";
    SI.Current i "Current flowing from pin p to pin n";
    Inheritance.PositivePin p "Positive electrical pin";
    Modelica.Electrical.Analog.Interfaces.NegativePin n "Negative electrical pin";
  equation 
    v = p.v - n.v;
    0 = p.i + n.i;
    i = p.i;
  end OnePort;

  model Resistor "Ideal linear electrical resistor"
    parameter SI.Resistance R = 1
      "Resistance at temperature T_ref";
    extends Inheritance.OnePort;
  equation 
    v = R*i;
  end Resistor;

  model Circuit
    Resistor r( R=10);
    Modelica.Electrical.Analog.Basic.Ground ground;
    Modelica.Electrical.Analog.Sources.ConstantVoltage constantVoltage(V=1);
  equation 
    connect(constantVoltage.n, ground.p);
    connect(constantVoltage.p, r.n);
    connect(r.p, ground.p);
  end Circuit;

end Inheritance;

So that the instance p of the connector class PositivePin

Inheritance.PositivePin p;

brings the variables i, v in the partial model OnePort (see language spec for potential and flow variables).

In the model class Resistor

extends Inheritance.OnePort;

instantiates the variables

  • i, v
  • p.i, p.v throught the instance p of the class PositivePin
  • n.i, n.v through the instance n of the class NegativePin

as well as the equations

  • v = p.v - n.v;
  • 0 = p.i + n.i;
  • i = p.i;

Finally, in the model class Circuit

Resistor r(R=10);

instantiates the Resistor class with a so-called modifier, so that the instance r has resistance value R=10 instead of the default class value R=1