Reusable and Configurable Models
Inheritance | Replaceable Classes |
Disabling Inherited Components | Choices for Class Redeclarations |
Replaceable Components | Composition—Component Hierarchies |
Choices for Component Redeclarations |
Reusability is an important aspect of modeling that when taken into consideration can greatly decrease the amount of time spent on model maintenance.
At times you may end up having a model that you want to create slightly different versions of. The most obvious way to achieve this could be to duplicate the model and modify each copy. While this is a simple and straightforward way to solve the problem, it has one big disadvantage, namely maintainability. With multiple models, where big parts of the models are identical, any changes to those parts means multiple changes across several models. The cost of maintenance and the risk of mistakes increase.
The following subsections describe a number of cases where features of the Modelica language can be taken advantage of to increase the reusability of models, and show how System Modeler provides easy access to these features.
For background regarding the underlying features of the Modelica language, see Conditional Component Declaration and Redeclaration.
Inheritance
Modelica is an object-oriented language, and one of the most central mechanisms in such a language is a relationship between classes, called inheritance. In Modelica, where a model is a class, inheritance allows one model to be based upon another model by extending it. This effectively makes the model appear as a copy of the extended model, but without duplicating any information. The model extending another model is considered a subclass, and the model that is being extended is the base class.
If you want to create a set of models with the same content but with different parameter values, start by creating a model in Model Center and giving it sensible default values for the parameters; this will be your base class. Next, create the models that will be the variations of the base class, each model extending the base class. Open each model and override the parameter values by modifying them in the variable view.
If some of your subclasses need to introduce another component, connection, etc., you can do it by opening the subclasses in Model Center and adding components or creating connections like with any other model. New parameters, variables and equations can also be added. However, while it is possible to make additions to a subclass, it is not possible to remove anything inherited from the base class. This is true for components as well, unless they were declared as conditional components in the base class.
Disabling Inherited Components
If you want some components of a base model to be optional for classes extending the base class, you can make them conditional components in the Component Properties dialog. Note that the enable condition for conditional components is a Boolean expression, so instead of just specifying a literal true or false value, you can specify the name of a Boolean parameter that you also add to your base class.
Your subclasses will inherit both the conditional components and the Boolean parameters. Enabling or disabling the components in your subclasses is now a matter of modifying the value of the Boolean parameter in the variable view. A disabled component, along with its modifiers and any equations involving the component, is excluded from builds and simulations. From the perspective of a simulation, there is no difference between a disabled component and a removed component.
See Disabling Components in the user guide for more information on disabling components in Model Center.
Replaceable Components
All components of a base class are inherited when extending it. But what if you want to replace one of the inherited components with another one in your subclass? For this to be allowed, the component must be made replaceable in the base class. The replaceable property can be set in the Component Properties dialog. Right-click the component in your base class, choose Properties and ensure that the Replaceable Component checkbox is checked in the General view of the dialog.
Replacing the replaceable component in the subclass can now be done by right-clicking it and choosing Redeclare Component. All components whose type allows them to replace the original component will be listed in a submenu. If there are no matching components, a dialog will be shown.
A replaceable component cannot be replaced with any other component; its type must be a subtype of the replaceable component’s constraining type (by default, the component's declared type is also its constraining type). In Modelica, this essentially means that a component replacing another component (by redeclaring it) must contain at least everything that the original component has. Consequently, all classes extending a specific class are subtypes of that class.
An explicit constraining type can be specified for a replaceable component in order to ensure that the constraining type always remains the same, regardless of if the component's type is changed.
An example of a class that is extended by many other classes in the Modelica Standard Library is Modelica.Electrical.Analog.Interfaces.OnePort. Adding such a component to your base class and making it replaceable results in it being replaceable with any other component whose type extends OnePort. This includes many of the basic electrical components of the library, such as the resistor, inductor, capacitor, etc.
Choices for Component Redeclarations
As shown in the previous section, a list with possible candidates is shown when initiating a redeclaration of a replaceable component. This list can be very long if there are many matching components, and perhaps only a few of those are of interest. To restrict the number of candidates presented in the list, redeclaration choices can be created. This is done in the Component Properties dialog. Right-click the component in your base class and choose Properties.
Select the Choices view in the dialog and set the type of Choices Annotation to Redeclaration choices: Customized redeclarations. The drop-down box in the Choices section that appears contains all possible candidates. Select the candidates you want and click Add Choice for each of them to add them as choices. Each choice can also have a description. If a description is specified for a choice, it is shown rather than the class name when redeclaring a component.
After a redeclaration choice has been added to the table of choices, it can be further customized, for instance, by adding modifiers to the redeclaration. These modifiers can be added in parentheses after the class name. Double-click the redeclaration choice to edit it.
If you do not wish to show any candidates at all when a component is being redeclared, select the None option in the Redeclaration choices drop-down box. The option All matching will show all matching candidates and is the default behavior in System Modeler if no choices annotation is present.
Replaceable Classes
Replaceable classes are useful in situations where you have multiple components of the same type in your model, and you want all components to change type whenever you replace the type of one of them.
How this is achieved is best illustrated by an example. First create a new empty model named Template. Continue by creating another model inside Template. Give it the name BasicElectricalOnePort. It should extend the model that you want to be the default type of your components; in this example, it will be Modelica.Electrical.Analog.Interfaces.OnePort. Finally, make the model replaceable. This can all be done in the New Class dialog.
Any replaceable class inside a model becomes a parameter to the model. The value of the parameter is the redeclared type. If you switch back to the Template model, you should see that there is one parameter listed in the variable view. The name of that parameter is BasicElectricalOnePort, the replaceable model you just created inside the Template model. The value of it is Modelica.Electrical.Analog.Interfaces.OnePort.
Use drag and drop to add any number of components of the type BasicElectricalOnePort to your Template model. Expand the drop-down box of the BasicElectricalOnePort parameter in the variable view to change the type of all the components in one mouse click. Note that the variables listed in the variable view depend on the selection in the Icon and Diagram views of the Class Window. If the BasicElectricalOnePort parameter is not visible, ensure that no components are selected.
Creating variations of this model is easily done by creating new models extending the Template model, i.e. subclasses of it. Each subclass will inherit the components, and their type can be changed by changing the value of the BasicElectricalOnePort parameter.
The replaceable model itself is also inherited, and additional components of it can be added to the subclasses by drag and drop of the inherited model from the Class Browser to the Diagram view. Make sure you drag and drop the inherited model and not the Template.ReplaceableModel, i.e. if your subclass is named MyModel, drag and drop the model ReplaceableModel under MyModel.extends Template.
Choices for Class Redeclarations
As shown in the previous section, a list with possible candidates is shown in the drop-down box redeclaring a replaceable class. This list can be very long if there are many matching classes, and perhaps only a few of those are of interest. To restrict the number of classes in the list, redeclaration choices can be created. This is done in the Class Properties dialog. Right-click the replaceable class in the Class Browser and choose Properties.
Select the Choices view in the dialog and set the type of Choices Annotation to Redeclaration choices: Customized redeclarations. The drop-down box in the Choices section that appears contains all possible candidates. Select the candidates you want and click Add Choice for each of them to add them as choices. Each choice can also have a description. If a description is specified for a choice, it is shown rather than the class name when redeclaring a component.
After a redeclaration choice has been added to the table of choices, it can be further customized by adding modifiers to the redeclaration. These modifiers can be added in parentheses after the component name. Double-click the redeclaration choice to edit it.
If you do not wish to show any candidates at all when a class is being redeclared, select the None option in the Redeclaration choices drop-down box. The option All matching will show all matching candidates and is the default behavior in System Modeler if no choices annotation is present.
Composition—Component Hierarchies
Everything that has just been said about modifying parameter values, disabling components using parameters and redeclaring replaceable components and classes also applies to component hierarchies. You can use the Component Browser to expand the component hierarchies of your models or visually step into the components by using the component mode (see Visualize Component Hierarchies in the user guide for more information). At any hierarchical level, you can select a component to view and edit its parameters, as well as redeclaring it if it is replaceable. It is also possible to redeclare replaceable classes inside a component.