Reputation: 315
what is in your opinion the best way to get data (measure date for example) into modelica (dymola)? Is it possible, to import data from python to modelica (for example into a combi-time-table)? My idea would be as follows:
I would appreciate any suggestions.
Upvotes: 1
Views: 1980
Reputation: 1084
I have a simple example using FMU and compare simulation with data and adjust three parameters for the best fit. All is in a Jupyter notebook that you can run from the web-browser without installation using Google Colab. The notebook can either be run using PyFMI or FMPy.
https://github.com/janpeter19/BPL_TEST2_Batch_calibration
The only thing you need is to have Google account and starting the script means you "borrow" from Google a virtual Linux machine that install all packages needed and then run the script that perform the example. So just, see the example evolve on your screen. Installation (on the virtual machine) takes a few minutes. Afterwards you can continue to interact with the FMU. Note that interaction in the notebook is with a set of commands that provide a simplified "high-level" approach. If you like to see the details of Python code just look at the setup files in the repository.
Upvotes: 0
Reputation: 101
There are two basic approaches:
Which one is the best choice for you depends on your desired workflow.
If you want to add the original measurement data to your models as part of a documentation, you might prefer to store the data in the model. If you want to run larger simulation studies, you might want to export the model and use another language or tool to execute the simulation and use your measurement data.
If you follow the approach FMU Export: Clarify if you’re licensed to export FMUs, use the libraries you plan to use, and if those FMUs shall perform a runtime license check. Simulating FMUs in Python can be done with fmpy (https://pypi.org/project/FMPy/). pyFMI has not been updated in the recent 6 years (https://pypi.org/project/PyFMI/#history) and does not support FMI 3.0 yet. FMUs can also be imported into various environments such as Matlab/Simulink. However, you have to take care of storing the measurement data files and running the simulation. This might be a pro or con.
If you follow the "Pure" Modelica approach, you can preprocess the measurement data and use it in Modelica. To load the files, you can use the MSL CombiTable, or the Open Source ExternData library (https://github.com/modelica-3rdparty/ExternData). There are also commercial tools such as the TILFileReader (packaged with the TIL library http://til-suite.com) to facilitate the import, specifically addressing the name/column index mapping. When you reference external files from Modelica, make sure to use “modelica://” URI paths (https://specification.modelica.org/maint/3.6/packages.html#external-resources) and the Modelica.Utilities.Files.loadResource() function. This function will make sure that the files are also exported to FMUs, the files can be stored in the Modelica package directory and paths will be adapted dynamically. You can find an example in Modelica.Utilities.Examples.ReadRealMatrixFromFile.
The simulation in Modelica is often faster, as the data table is part of the model, and does not require a solver reset at each grid point. If you use a 2.0 FMU, usually the solver state is reset each time you change the input values. To avoid this, there is an input smoothing tool provided by TLK-Thermo (tlk_fmuinputsmoother, https://www.tlk-thermo.com), and a similar implementation will be included in future Dymola versions. This issue is also addressed by the callback function of FMI 3 which enables providing time dependent input trajectories.
Upvotes: 0
Reputation: 847
As Rene Just Nielsen pointed out, this is primarily opinion based.
To give another way of accomplishing your goal, try the DymolaInterface
. You could either set the table-parameter via the Interface in python, our use a .txt-file which you create and alter in python, and Modelica just knows the path to the file. The interface comes with your Dymola installation under Modelica\Library\python_interface\dymola.egg
where you will also find documentation for the functions.
Another python-package for using FMU's is FMPy. I image both FMPy
and PyFMI
have their pros and cons.
The last option which does not require any external python package would be to use mos-files to execute simulations and use the .txt-files to read in the data. If the task you described is the only thing you want to accomplish, mos-scripts are quite sufficient.
Upvotes: 0
Reputation: 3403
That's probably a matter of opinion. But since you have to do much of your data post- and preprocessing in Python I would definitely export my (plant) model from Dymola as a co-simulation FMU and run it in Python.
In Dymola you can export FMU's and 'execute' them on the same pc that holds the Dymola license file. If you need to run the FMU on another pc you'll have to buy a special binary export license.
There is a free Python package called PyFMI (www.pyfmi.org) which makes it easy to run an FMU in Python. See the examples at http://www.jmodelica.org/page/4924.
PyFMI can be a bit tricky to get up and running (with the right Python package dependencies and so on). So if you are not an experienced Python user I would suggest that you download the installer for JModelica.org which will do much the setting up for you.
Best regards,
Rene Just Nielsen
Upvotes: 4