September 27th, 2011 by Alessandro Picarelli
There is a fundamental difference between duplicating a class and extending a class (Blocks, Models, Functions, Types, Enumerations are all types of Class).
When we duplicate a class we effectively create a separate instance or copy of that class. Any changes to the original class or duplicate class are local changes.
When we extend a class we are effectively adding a layer to the class we extend from. This means that all parameter and variable declarations as well as equations and algorithms are inherited (they filter through and are accessible to the upper layers). Modification of the lower layers or levels is prohibited from this class. These lower levels will need to be modified locally. We can however add detail in the extended class and edit the parameter values of the lower levels.
One advantage of extending is that we maintain single instances of the building blocks of our extended class. These building blocks can be reused to create classes with different functionality and detail. If we make any changes or improvements to the classes we have extended from, these will automatically filter through to (or be inherited by) the extended classes. Another advantage of creating a class from layers is that we can keep the complexity of each layer low when building complex classes. This, in turn, facilitates debugging.
For example: if we were to create a rotational damper, we might extend from a building block (known as base class or partial class) which includes a right and a left rotational flange. We could re-use the two flange partial model and extend from it to create the model of a rotational spring damper which also requires the same two rotational flange connectors. Below we can see that both the damper and spring damper are extended from PartialCompliantWithRelativeStates. This partial model will include the two rotational flanges which Damper and SpringDamper are built upon. Any improvements to the PartialCompliantWithRelativeStates will automatically filter through to Damper and SpringDamper.
Damper model text:
model Damper "Linear 1D rotational damper" extends Interfaces.PartialCompliantWithRelativeStates; parameter SI.RotationalDampingConstant d(final min=0, start=0) "Damping constant"; equation tau = d*w_rel; end Damper;
Spring Damper model text:
model SpringDamper "Linear 1D rotational damper" extends Interfaces.PartialCompliantWithRelativeStates; parameter SI.RotationalSpringConstant c(final min=0, start=1.0e5) "Spring constant"; parameter SI.RotationalDampingConstant d(final min=0, start=0) "Damping constant"; parameter SI.Angle phi_rel0=0 "Unstretched spring angle"; equation tau = d*w_rel; end Damper;