Abstract: This article is aimed to present Modelica-based simple tic and toc functions and their implementation in Dymola. These functions are created to estimate the total elapsed computer processing unit (CPU) time of repeated model experimentations in Dymola. For this purpose, the getTime() function from Modelica Standard Library (MSL) is elected to retrieve the local time at the time instant of this function when called. It is shown that by carefully interpreting and processing the results obtained from the execution of this function just before and after an experimentation study, the total elapsed CPU time of this study can be easily estimated. For demonstrating the usefulness of these created functions, an automated Modelica model experimentation example is adopted from a previous blog article. The pros and cons of these created functions are also discussed.
Keywords: Modelica, Tic, Toc, Function, Dymola, Model, Experimentation, Estimate, Elapsed, CPU, Time.
Software/Tool/Language used: Dymola 2018, MSL 3.2.2
Download a copy of this article as a pdf: Modelica based Tic & Toc Functions in Dymola
Introduction
For developing a reasonably accurate and efficient Modelica model in Dymola, it is important that modellers perform a model experimentation with their model(s). In other words, a model in its developing stage must go through a series of simulations to ascertain an acceptable level of accuracy and efficiency. However, there is always a trade-off between fidelity and simulation speed of a created model. Among others, there are at least two methods that can be used to improve not only the accuracy but also the efficiency of a model. (1) Understand which part of the model is consuming highest elapsed computer processing unit (CPU) time during the simulation; by using this information one can improve the efficiency of the model. (2) Run the model multiple times with various input signals (e.g., a parametric sweep study); by which to attain a stable and/or accurate model. For the former, we can use Dymola built-in function called ‘CPUtime’ [1]. This function can be activated by selecting a tick box on the Simulation > Setup…> Translation tab whilst in the Dymola Simulation Window which is shown in Figure 1 below.
Figure 1- Translation tab of the Simulation Setup dialog box in Dymola
With the help of this Dymola built-in feature, the Dymola user is not only able to obtain a cumulative CPU time during simulation but also identify which part of the model has a lot of iteration (also known as an EventCounter). For the latter, it is quite convenient, if we can automate the model experimentation process using either a Modelica function or by calling a script in Dymola. A methodology for automating the experimentation of Modelica models using a Modelica function in Dymola is given in a previous blog article [2]. For a parametric sweep study, especially one provided in [2], it is valuable to estimate the total elapsed CPU time of the full iteration process. The Dymola User Guide Volume 1 and 2 [1, 3] and Modelica Standard Library (MSL) [4] do not provide information on functions/features to estimate the total elapsed CPU time of repeated model experimentations either by achieving this task using a Modelica function or by a Dymola script. Therefore, this blog article is aimed to present Modelica-based simple tic and toc functions and their implementation methods in Dymola.
Methodology
The methodology of this blog article has three stages; they are as follows:
- Create Modelica-based simple tic and toc functions with the help of Modelica.Utilities.System.getTime function available in MSL.
- Step-up an experimental study adopted from a previous blog article, and modified to run an extended parametric sweep study.
- Use the created tic and toc functions in Step 1 with the extended parametric sweep study in Step 2 to demonstrate the aim of this blog article.
Modelica-based tic and toc Functions in Dymola
As aforementioned, as of the latest version of Dymola 2018, there is no function/feature to estimate total elapsed CPU time of a repeated or cycled experimentation of a Modelica model within Dymola. This section of the blog article begins by describing the getTime() function available in MSL. Followed by, guiding through a procedure to utilise this function to obtain some useful results. Then, processing these results, a step-by-step procedure to create Modelica-based tic and toc functions is also explained.
MSL getTime() Function
The Modelica.Utilities.System.getTime is a Modelica-based function available in MSL that can be used to get the local time at the time instant this function was called. Please follow the step-by-step procedure below to get some useful results by executing this function;
- By right-clicking the Modelica.Utilities.System.getTime in MSL, then selecting Call Function, a pop-up dialog box will open, which looks like the one shown in Figure 2.
Figure 2- Dialog box of the Modelica.Utilities.System.getTime function in Dymola
By executing the above function at an arbitrary instance of time, a result will be shown in the command log pane of Dymola Command Window. An example of which is given below;
= 966, 22, 28, 13, 4, 12, 2017
According to the Documentation layer of this function, the above results can be interpreted as
966, 22, 28, 13, 4, 12, 2017 = Dec. 4, 2017 at 13:28 after 22.966 s
Please note that the time (13: 28) interpreted from the above result is in 24-hour clock time.
A Modelica-based Simple Tic Function
For the sake of demonstration purposes, both tic and toc functions are made very simple. It is assumed for this example that a Modelica model experimentation study neither exceeds a day nor is conducted overnight, by which the date of the experiments does not change.
After taking the above assumptions into consideration, a simple Modelica-based tic function can be coded as shown below;
function tic
“Function to record the internal time just before the experiment starts in [s]”
output Modelica.SIunits.Time tic
“Record the internal time at the execution of this (tic) function”;
algorithm
(ms_tic,sec_tic,min_tic,hour_tic,day_tic,mon_tic,year_tic) :=
Modelica.Utilities.System.getTime();
tic := (ms_tic*0.001) + (sec_tic) + (min_tic*60) + (hour_tic*3600);
Modelica.Utilities.Streams.print(“tic = ” + String(tic) + ” [s]”);
end tic;
Using the above created function, one can record the internal clock time at the execution of this function just before the model experimentation starts.
By right-clicking the above function in Dymola Package Browser, the following dialog box will open;
Figure 3- Dialog box of a simple ‘tic’ function.
By executing the above tic function at an arbitrary time instant, get the following output:
AutomatedModelExperimentsWithTicToC.ElapsedTime.tic;
tic = 39992.5 [s]
= 39992.532
A Modelica-based Simple Toc Function
This sub-section describes how to create a simple toc function from the lesson learned from the previous sub-section. The toc function can be coded as shown below;
function toc
“Function to record the internal time just after the experiment ends in [s]”
output Modelica.SIunits.Time toc
“Record the internal time at the execution of this (toc) function”;
algorithm
(ms_toc,sec_toc,min_toc,hour_toc,day_toc,mon_toc,year_toc) :=
Modelica.Utilities.System.getTime();
toc := (ms_toc*0.001) + (sec_toc) + (min_toc*60) + (hour_toc*3600);
Modelica.Utilities.Streams.print(“toc = “ + String(toc) + ” [s]“);
end toc;
This created ‘toc’ function is a replica of ‘tic’ function presented in the last sub-section. In the ‘toc’ function however, the output variable names of the Modelica.Utilities.System.getTime and the annotations part are altered.
By calling the created ‘toc’ function from the Dymola Package Browser, a dialog box like the one shown below appears;
Figure 4- Dialog box of a simple ‘toc’ function.
Again, by arbitrarily executing the above ‘toc’ function after about 10 mins, we get the following output on Dymola Command Log Window:
AutomatedModelExperimentsWithTicToC.ElapsedTime.toc;
tic = 40567.1 [s]
= 40567.075
Finally, the elapsed time between execution of these two functions (tic and toc) can be calculated with a simple calculation result in units of seconds as below,
ElapsedTime = abs(toc-tic) = abs(40567.075 -39992.532) = 574.543s = 9.58 mins ~= 10 mins
For analysing the real benefits of these created functions, in the next section, a simple Modelica model experimentations example provided in a previous blog article [2] is adopted.
Example: An Extended Automated Modelica Model Experimentation
This section is only intended to cover a brief overview about a Modelica package adopted from [2] and experimentation setup to demonstrate the estimation of the total elapsed CPU time. For gaining more insight into how to automate Modelica model experimentations using a Modelica function in Dymola and the discussion about the physical meaning of the simulation results that can be obtained from the present study are beyond the scope of this article. So, those who are interested, please refer to [2, 5] for further details.
For performing an automated model experimentation task, a Modelica package (AutomatedModelExperimentsWithTicToc) is created. It has a simple RC circuit model (ModularRC_Modified), a modified automated Modelica model experimentation function (ModelExperimentWithTicTocFn), these are basically adopted from [2], and a separate subpackage (ElapsedTime) with the newly created tic and toc functions show in the previous section of this blog article. This modified Modelica package can be downloaded from the bottom of this article.
The experimentation studies considered in this article are provided in Table 1. There are three case studies in Table 1. However, each case study iterates 161 times with different input excitation frequencies (Vin_freqHZ). So, one can argue that there are 483 experiments in total.
It is also important to note that both Case 2 and 3 are selected herein, only to emphasise the usefulness of the created functions/method (tic-toc/estimating total elapsed CPU time) and not indented to have any physical meaning.
Table 1: Experimentation studies
Case no. | ts [s] | Vin_v [V] | Vin_freqHz |
1 | 30 | 4.2 | 0:0.01:1.6 |
2 | 300 | 4.2 | 0:0.01:1.6 |
3 | 3000 | 4.2 | 0:0.01:1.6 |
In Table 1, ts, Vin_v and Vin_freqHZ are three input parameters defined in the ModularRC_Modified model in AutomatedModelExperimentsWithTicToc package. In Table 1, Case 1, the study includes a simple RC circuit model which is simulated with a sinusoidal input voltage of 4.2V and simulation time of 30s. The excitation frequency is iterated incrementally in steps of 0.01 Hz from 0 to 1.6 Hz. The results obtained from each (total of 161) iteration are stored in individual .mat files. By making appropriate adjustment to create plot setup section of ModelExperimentWithTicTocFn, one can plot the simulation results of interest using the already saved .mat file in Dymola. For knowing more details about how to plot results from saved .mat files, please refer to [2] and do not repeat here. Both Case 2 and 3 experiment studies are exactly the same as Case 1 and repeated manually, with changes in Simulation Stop Time (ts) of 300s and 3000s respectively.
Results and Discussions
The three different output results obtained by executing the ModelExperiment_ModifiedWithTicTocFn() on Dymola Command Log Window are provided below;
ElapsedCPUtime = 73.636 s
ElapsedCPUtime = 345.782 s
ElapsedCPUtime = 2644.21 s
These results are also plotted using a bar chart in Figure 5.
Figure 5- Results obtained from the three cases in Table 1 using a bar chart.
By looking at a bar chart in Figure 5 and comparing with Table 1, it is evident that by increasing the simulation time of the experimentation study, the total elapsed CPU time also increases. In Case 1, with a ts of 30s, the total elapsed CPU time for 161 iterations is 73.636s, whereas that of Case 2 (ts=300s) and Case 3(ts=3000s) took about 345.782s and 2644.21s respectively. From this experiment, the usefulness of the above created functions can be interpreted in two-ways. On the one hand, without the help of the created tic and toc functions, it is hard to sit with a stopwatch and record the simulation time manually, especially in Case 3 study. Imagine, if someone is carrying out an experimentation study which lasts for several hours (please do take care with limitations put on the created functions), it’s hardly feasible to estimate the total elapsed CPU time accurately. On the other hand, by combining Dymola built-in function, ‘CPUtime’ mentioned in the introduction section with tic and toc functions created in this article, one can develop an enhanced Modelica function or Dymola script to improve efficiency and/or accuracy of the model in a most systematic and automated manner. It should also be worth mentioning that the getTime() function has seven outputs (please refer to Figure 2), but only four of them are used in this article. By adopting the presented tic and toc functions and utilising all seven outputs from the getTime() function, one can create a general-purpose tic and toc functions in Dymola.
Conclusion
This article presented Modelica-based simple tic and toc functions in Dymola. With the aid of the getTime() function from MSL, the implementation procedure for how to create simple tic and toc functions in Dymola is demonstrated. By modifying an automated Modelica model experimentation example from a previous blog article and combining this with the newly created tic and toc functions, it allows discussing benefits of the tic and toc functions using the specific example in this article. The presented example shows that using the created functions, a Dymola user is not only able to easily estimate the total elapsed CPU time of automated model experimentations but also has a potential to improve the model efficiently by understanding the model behaviour clearly in a systematic and useful manner.
The Modelica package with model and functions used in this article can be downloaded here.
References
- Page no. 603, Dymola – Dynamic modelling laboratory, User Manual Volume 1, version 2018, Copyright © Dassault Systèmes, 1992–2017, March 2017.
- Raees B. K. Parambu, How to Automate Modelica Models Experimentation in Dymola, https://www.claytex.com/blog/how-to-automate-modelica-models-experimentation-in-dymola/ . (05 Dec. 17, last accessed).
- Dymola – Dynamic Modelling Laboratory, User Manual Volume 2, version 2018, Copyright © Dassault Systèmes, 1992–2017, March 2017.
- Modelica language specifications version 3.3 Revision 1, Copyright © 1998–2014 Modelica Association July 11, 2014, https://www.modelica.org/documents (03 Oct. 17, last accessed).
- Raees B. K. Parambu & S. Harrisons, A State-Space Model of a simple RC Network in Dymola & potential Applications, https://www.claytex.com/blog/a-state-space-model-of-a-simple-rc-network-in-dymola-potential-applications/ (03 Oct. 17, last accessed).
Written by: Raees B. K. Parambu — Systems (Project) Engineer
Please get in touch if you have any questions or have got a topic in mind that you would like us to write about. You can submit your questions / topics via: Tech Blog Questions / Topic Suggestion