Written by: David Briant – Project Engineer
This is the eighth in the Dymola Basics Series, following on from a post about animation. This post explains the concept of inheritance and the effective use of it within Dymola.
Dymola models can be based on layers where models inherit properties from more basic models. Dymola uses the keyword extends to inherit properties of a base model. While this example shows the extending and replacing of a model type, the method can be used with other types within Dymola such as a function, type or record.
A base class, at the bottom level, should contain the universal properties that are shared by all the extended models reducing duplication of calculations and sub-components. To allow the base class to be more applicable to a wider range of uses it could include some conditional connectors that allow for multiple variations.
As the base model is not complete and would not be able to run, the model should be declared as partial. This declares that the base model does not have enough information to be simulated therefore it needs to be extended and completed by defining all the variables.
A new model can be declared as partial by clicking the partial declaration shown in Figure 1 or adding the definition “partial” to the model definition as shown in Figure 2.
Figure 1: Partial Model
Figure 2: Partial Model ‘PartialTwoFlanges’
The partial base class can have any type of connection, sub-component or variable declared without fully constraining the model. This requires all components extended from the partial model and that are not partial themselves to fully constrain the model by adding the relevant calculations/components/connections. Examples of base classes can be found in the Modelica Standard Library such as the Modelica.Mechanics.Rotational.Interfaces.PartialTwoFlanges used within this article.
When creating a component that is to be extended from another component, either the path of the base class can be put into the Extends input box as shown in Figure 3, or a model can be extended directly from the base component. This is done by right clicking on the base model (in this case partialTwoFlanges) and then selecting New and Extend From… as shown in Figure 4. A Create New Model dialog box with the Extends input box already populated with the selected base component is created.
Figure 3: Extend Option within the Create New Model Dialog Box
Figure 4: Extending from base component
In this example, the model inherits the two connectors shown in Figure 5 with only the extends declaration. The model is not partial as it has the connect statement that simply connects the two flanges together, which means that all variables are constrained.
Figure 5: Model extended from PartialTwoFlages with a single connection
Dymola has the ability to replace components but it is recommended that the replacement at least has the same connectors; this lends to using the extend functionality to define the interfaces at the base level. Options to redeclare a replaceable component can be controlled by constraining the replaceable declaration to the base class.
There are several methods of making a component replaceable; if using the method mentioned above, the partial model shown in Figure 2 (partialTwoFlanges) can be dragged from the Package Browser by left clicking and dragging it onto the diagram layer of the recipient model. The dialog box shown in Figure 6 is shown to ensures that the partial model is replaceable.
Figure 6: Dragging partial model into a model
A component that is already within a model can be declared as replaceable by going to the Attributes tab in the parameter Dialog box in Dymola 2017, or right clicking on a component and selecting Attributes from the drop down menu in older Dymola versions. In both instances there is a window similar to Figure 7, with the option to make the component replaceable as highlighted in red.
Figure 7: Attributes dialog for PartialTwoFlanges
The replaceable declaration can be used in conjunction with a constraint clause that only allows the component to be replaced with a component that has been extended from the model declared within the clause. Figure 7 highlights the option for the Constraining Clause of the constraining model and any modifications to the clause in the blue box. Figure 8 shows the section of the text layer that declares the replaceable component modified above in Figure 7.
Figure 8: Diagram and Text layer of a replaceable compoenet with a constraining clause
Making a component replaceable also changes how the component is displayed in the diagram layer with a grey box around the component. An indented box with the dark edges at the top left means that the component has not been redeclared from the base level, whereas the bottom right edges of the component are darkened then the component has been redeclared.
Figure 9: Redeclared replaceable component in extended model
This replaceable constraining can allow for a different type of partial base class called a template, which is a collection of replaceable components within a fully laid out structure. In a template, the connections between are made, but the components are not selected, with partial base classes often used as placeholders. The model shown by Figure 10 is a very simple template using the models made above with the replaceable model being the partialTwoFlanges created earlier. While the example uses a component that is both constrained by, and declared as, a partial model, non-partial models can be used in the same way.
While any component within a model can be partial, a model with partial components cannot be simulated.
Figure 10: Simple Template Model
To replace a component right-click on a replaceable component and select Change Class; the option to change class to any model within the Package Browser is to use the Browse… option. There is also the dropdown menu All Matching Choices that access all the components extended from the same base model as the component selected.
Figure 11: All Matching Choices tree
The parameters of the model chosen are now available within the parameter dialog box. To allow the replaceable model to be easily replaced from dialog box of the model, a drop down menu can be added that displays the same list as All Matching Choices shown in Figure 11. This functionality is available within all versions of Dymola but to apply it in Dymola 2017, the attributes tab in the parameter Dialog Box, as shown in Figure 7, includes a check box to Add Selector for All Matching Choices above the constraining clause. Furthermore, in all versions of Dymola a user can add the definition to the annotation by adding choicesAllMatching=true to the annotation for the replaceable component as shown in Figure 12.
Figure 12: Choices All Matching annotation modification
The parameter box for the model that contains the replaceable component with the choicesAllMatching annotation can be seen in Figure 13.
Figure 13: Drop down menu created by using choicesAllMatching=true
There is a Claytex blog post that elaborates on propagating the replaceable class to higher level: