This blog post presents a simple method for reducing the total number of ‘input’ calls when developing a Modelica function in Dymola. Modelica function only acts on input values given to it and returns output value (s) without showing its intermediate results, like a “black box” model. However, with the aids of a Modelica pre-defined class, the ‘Record’, it not only helps to store input values of the function separately but also serves as a convenient way to reduce a total number of ‘input’ calls in the main function.
While creating a Modelica function, depending on the input values required in its algorithm section, it is recommended to use a Modelica class called the Record. By using this method, we will improve the readability of the code and reduce the total number of ‘input’ calls in the main function. For the illustration purpose, a simple code with five ‘input’ calls available in Modelica Standard Library (MSL) is selected and shown in Figure 1.
Figure 1: An MSL function available at Modelica.Fluid.Dissipation.Utilities.Functions.General.CubicInterpolation_DP.
The five inputs used in the function, CubicInterpolation_DP, are either directly or indirectly used in its algorithm section. However, by right-clicking on this function, in Figure 1, and selecting ‘Call Function’, a dialog box will appear which is shown in Figure 2.
Figure 2 depicts the input(s) and output declared in CubicInterpolation_DP, which is organised in a single dialog box.
Figure 2: Dialog box of function CubicInterpolation_DP available in MSL.
In order to reduce the number of ‘input’ calls in the selected function above, the following method can be used.
- Open Dymola and create a new package called ReducedInputCalls in the Package Browser.
- Duplicate the function, CubicInterpolation_DP, available in MSL, and place into the newly created package which is shown in Figure 3.
Figure 3: A New package, ReducedInputCalls, with a function duplicated from MSL.
- Create a new Record in this package and named as CubicInter_DP_Record.
- Followed by, Cut and Paste all the lines start with ‘input’ of this function in Figure 3 to the newly created record.
However, the copied code in the new record needs to be modified slightly to comply with Modelica syntax. Because the code is no longer in a function, we should declare each of the five ‘input’ lines in the copied code as ‘parameter’. The result is shown in the code below:
record CubicInterDP_Record parameter Real Re_turbulent; parameter Modelica.SIunits.ReynoldsNumber Re1; parameter Modelica.SIunits.ReynoldsNumber Re2; parameter Real Delta; parameter Real lambda2; a end CubicInterDP_Record;
- Then, edit the duplicated function by adding a line which starts with an ‘input’ command, and followed by drag and drops the CubicInter_DP_Record from the package browser into this line. Please note that do not forget to terminate the line by inserting a semi-column ‘;’ at the end of this line.
- Rename the duplicated/modified function as CubicInter_DP_Modified in the ReducedInputCalls package.
- Finally, the newly created package ReducedInputCalls needs to be saved in your preferred location for the future use.
The result of the last three steps in the above methodology is shown in Figure 4.
Figure 4: The created new package, ReducedInputCalls, with a modified function and a record.
Now, by right-clicking the modified function, CubicInter_DP_Modified, and selecting ‘Call Function’, a pop-up will appear shown in Figure 5.a. Clicking on the ‘CubicInter_DP_Rec’ on the left-hand side of Figure 5.a, a separate tab will appear which is shown in Figure 5.b.
Comparing to the dialog box appeared in Figure 2 using the original MSL function, ‘input’ and ’output’ declaration in the code are now appeared in two dialog boxes in Figure 5.a and 5.b separately.
This post presented a simple method to reduce the number of ‘input’ calls in a Modelica function by using a pre-defined Modelica class, called the ‘Record’ in Dymola. The effectiveness of this approach is not emphasised to a high level because it is only a simple example selected from MSL and employed to illustrate the presented method. However, by looking at the differences in the appearance of the dialog boxes in Figure 2 and Figure 5.a & b, it has a potential to create a more complicated function with a large number of ‘input’ values displayed in a well organised and readable manner. It is also important to note that, by using the same example itself, by adding ‘annotations’ such as input descriptions and categories to each line of code in the newly created record, the dialog windows in Figure 5 will look more organised than the presented method. This modified method will be provided in the upcoming post.
Written by: Raees B. K. Parambu – Project Engineer