Parameterized Generic Classes

1 Class Parameters using Replaceable and Redeclare

Modelica classes and packages can be parameterized in a general way, corresponding to generic classes or packages. Both classes and variables which are members of some class can be used as a kind of formal class parameter by attaching the prefix replaceable in front of their declarations.


1.1 ResistorCircuit

ResistorCircuit, which is a class consisting of three Resistors connected at one node.


2 Class Parameters Being Components

The class ResistorCircuit has been converted into a parameterized generic class GenericResistorCircuit in the example below. In GenericResistorCircuit class parameters are components (instances or objects).




A more specialized class
TemperatureDependentResistorCircuit is created by changing the types of R1 and R2 to TemperatureResistor, which is a subtype of Resistor.




Now we add a temperature variable
Temp.




TemperatureDependentResistorCircuit is equivalent to the class below. The prefix replaceable has been lost for R2 and R3, since we did not use redeclare replaceable.



3 Generalizing Constraining Types of Class Parameters

Sometimes it can be useful to allow a more general constraining type of a class parameter, e.g. TwoPin instead of Resistor for the formal class parameters and components comp1 and comp2 below. This allows replacement by another kind of electrical component than a resistor in the circuit.


3.1 Connectors





3.2 GeneralSimpleCircuit



The generalized parameterization properties of
GeneralSimpleCircuit are used below when replacing the circuit components comp1 and comp2 with components of the types Capacitor and Inductor that are subtypes of TwoPin.



4 Class Parameters Being Types

A formal class parameter, can also be a type, which is useful for changing the type of many objects. For example, by providing a type parameter ResistorModel in the class below it is easy to change the resistor type of all objects of type ResistorModel, e.g. from the default type Resistor to the temperature dependent type TempResistor.




A more specialized temperature dependent circuit
RefinedSimpleCircuit2 can be created where the resistor type TempResistor is supplied as an actual argument to the formal class parameter ResistorModel:




RefinedSimpleCircuit2 is equivalent to the "expanded" class RefinedSimpleCircuit2expanded