Dymola tips and tricks series (VI)John Batteh
A flexible approach for loading and managing time-series data
The Modelica CombiTimeTable block provides the ability to load time-series data into a model. In this post, we will show you how to flexibly manage the data both in Modelica and in compiled models.
Part VI: Loading External Time-Series Data
In a previous post from our Dymola tips and tricks series, we covered a way to parameterize models using external data with the DataAccess package included with the Modelon Base Library. Providing model data externally is a powerful and popular approach that allows the separation of model from data.
Many of our customers leverage DataAccess to not only parameterize the model with external data files but also change the model parameterization in the data files with compiled models either as executables or Functional Mockup Units (FMUs).
In this post, we will share an approach to load time series data into Dymola from a file and also maintain the flexibility to change the file name reference along with the actual data in the file after compilation. The CombiTimeTable block in the Modelica Standard Library provides the basic ability to load time series data into Dymola.
The component allows data to be entered directly into the model or read from a file by setting the tableOnFile parameter to true. With tableOnFile set to true, the user can enter the fileName and tableName parameters which give the file reference and the specific table reference to be used for a given table instance. The info layer for the model provides details on all the configuration options and external file formats supported.
While there are many different source blocks that can provide inputs to a model, an external file input can provide maximum flexibility in that you can simply create whatever input signal you want in the file, perhaps loading experimental data or even constructing input signals from requirements.
CoolingLoop example model and input data files
For this demonstration, we will take the CoolingLoop example from Liquid Cooling Library and modify it so that the inputs are provided in an external file via the CombiTimeTable block. The model is shown below.
Figure 1. CoolingLoop example from Liquid Cooling Library with external file inputs via CombiTimeTable blocks
To make it convenient to load the same data file into each CombiTimeTable block, we can propagate a string parameter for the file name as shown in the parameter dialog for the heat_input block and in the source code snippet below. The pump_speed and airflow blocks reference the same string parameter.
Figure 2. CoolingLoop example from Liquid Cooling Library with external file inputs via CombiTimeTable blocks
Figure 3. Modelica source code for cooling loop example with file inputs
In the source code, the fileName parameter has a default file name “cooling_loop_ramps.txt”. As a first step, let’s create this input file with values corresponding to the same ramps as the original model in Liquid Cooling. The file is shown below.
Figure 4. Sample external input file with ramp data (cooling_loop_ramps.txt)
So now we have a model that references an external file for inputs. While we can always change the file reference via the filename parameter in the Modelica code for simulating in Dymola, this approach requires the model to be re-compiled. Furthermore, when we are using the model in compiled form (i.e. as an executable or FMU), we need the flexibility to change the data file after the model is compiled.
In Dymola 2016 FD01, we do have the ability to access and modify string parameters, but you will need to enable an advanced option for string parameter handling. Let’s go through the workflow. First, we will create a second input file called cooling_loop_newinputs.txt with different values for the inputs. Note that both the size of the data traces and the actual data have changed.
Figure 5. New external input file (cooling_loop_newinputs.txt)
Model compilation and file reference handling
Now let’s go through the steps to compile the model and change the data file reference. These steps are described below and also shown in the Dymola command window for clarity.
- The first step is to set the flag Advanced.AllowStringParameters=true so that we have string parameters available to access. This setting will need to be enabled once per Dymola session but can be set in a startup script if desired by default.
- Translate and simulate the original model in Dymola as you would normally. This simulation will use the default input file “cooling_loop_ramps.txt” specified in the source code.
- You may notice that no string parameter appears in the result browser, but the parameter can still be accessed if it is at the top level of the model (note that this behavior may be a temporary limitation in Dymola). In the command window, simply issue the command to change the filename parameter to our new input file “cooling_loops_newinputs.txt” as shown in the image below. This command will update the file name parameter in the dsin.txt file.
- Without recompiling, simulate the model again and now the run will be based on the new input file. You have now changed the data file reference without recompiling by simply changing the file name!!
Figure 6. Dymola command window showing the modification of the fileName string parameter after compilation
To confirm that the model ran with the different input files, the plot below shows the engine volume temperature (i.e. input to the radiator) and the prescribed pump speed specified in the input file for the two simulations.
Figure 7. Comparison of results from the two different input files in Dymola
File reference handling in compiled FMUs
Now let’s test the model as compiled into an FMU. First, we can create an FMU from our model and then import into FMI Add-in for Excel to run the simulations in Excel with different file inputs by changing the string parameter in Excel. With the string support in our Excel tool, the screenshot below shows the experiment sheet with the string parameter for the fileName set to our two different input files for Case1 and Case 2 and then a comparison of the results. So far so good- we are getting the same behavior in an FMU as expected.
Figure 8. String parameter handling in FMI Add-in for Excel and results comparison
Finally, let’s load the FMU into FMI Toolbox for MATLAB/Simulink. As shown in the screenshot below, the fileName parameter appears in the parameter dialog for the FMU. And when the string value is changed to cooling_loop_newinputs.txt, the model results change as expected. So with the FMU exported from Dymola with string parameters enabled, we can certainly conclude that our FMI tools handle the string parameters properly and that this approach works both inside Dymola and when the models are compiled into FMUs.
Figure 9. String parameter handling in FMI Toolbox for MATLAB/Simulink and results from new input file
The ability to change input files by changing the file reference in the CombiTimeTable after compilation (rather than just changing the data in the default file as was the limitation without proper string parameter support) is a common request, and now it can certainly be done in the latest version of Dymola. Will you utilize this approach? Feel free to leave a comment as I’d love to hear from you.
Find out more
Contact us if you would like more information on the use of external data to streamline modeling and simulation workflows, including the use of DataAccess for model parameterization using external data. DataAccess is part of the Modelon Base Library which is provided along with nearly all Modelon commercial libraries.