2

Is there an easy way to extract a list of all variables with start attribute from a Modelica model? The ultimate goal is to run a simulation until it reaches steady-state, then run a python script that compares the values of start attribute against the steady-state value, so that I can identify start values that were chosen badly.

In the Dymola Python interface I could not find such a functionality. Another approach could be to generate the modelDescription.xml and parse it, I assume the information is available somewhere in there, but for that approach I also feel I need help to get started.

Priyanka
  • 559
  • 3
  • 13
  • After each simulation, there is a list of iteration variables in the dsin.txt, so it might be easier to extract the information for the txt file. – Jack Apr 25 '20 at 09:37
  • I would like to avoid parsing txt files. – Priyanka Apr 27 '20 at 09:36
  • related:https://stackoverflow.com/questions/49065763/extract-types-classnames-from-flat-modelica-code, except the method of parsing XML or fmu, there are also some python parsers for Modelica. – Jack May 07 '20 at 18:45
  • Use alist.exe to convert dsin.txt and dsfinal.txt to mat files - much easier to load into python, without parsing txt files. – matth Aug 11 '20 at 07:57

3 Answers3

3

The files dsin.txt and dsfinal.txt might help you around with this. They have the same structure, with values at the start and at the end of the simulation; by renaming dsfinal.txt to dsin.txt you can start your simulation from the (e.g. steady-state) values you computed in a previous run.

  • It might be worthy working with these two files if you have in mind already to use such values for running other simulations.
  • They give you information about solvers/simulation settings, that you won't find in the .mat result files (if they're of any interest for your case)

However, if it is only a comparison between start and final values of variables that are present in the result files anyway, a better choice might be to use python and a library to read the result.mat file (dymat, modelicares, etc). It is then a matter of comparing start-end values of the signals of interest.

  • Thanks. Can I somehow filter this to only show variables with start attribute? – Priyanka Apr 27 '20 at 09:39
  • 1
    Sort of. If you scroll toward the end of a dsin.txt/dsfinal.txt there's a doc explaining the initial values calculation, it seems you can infer which variables have been defined with a start value. But then again, it requires you to parse a txt file, which I understand you prefer not to do –  Apr 27 '20 at 09:57
2

After some trial and error, I came up with this python code snippet to get that information from modelDescription.xml:

import xml.etree.ElementTree as ET
root = ET.parse('modelDescription.xml').getroot()

for ScalarVariable in root.findall('ModelVariables/ScalarVariable'):
    varStart = ScalarVariable.find('*[@start]')
    if varStart is not None:
        name = ScalarVariable.get('name')
        value = varStart.get('start')
        print(f"{name} = {value};")

To generate the modelDescription.xml file, run Dymola translation with the flag
Advanced.FMI.GenerateModelDescriptionInterface2 = true;

Python standard library has several modules for processing XML:
https://docs.python.org/3/library/xml.html
This snippet uses ElementTree.

This is just a first step, not sure if I missed something basic.

Priyanka
  • 559
  • 3
  • 13
2

Similar to this answer, you can extract that info easily from the modelDescription.xml inside a FMU with FMPy.

Here is a small runnable example:

from fmpy import read_model_description
from fmpy.util import download_test_file
from pprint import pprint

fmu_filename = 'CoupledClutches.fmu'

download_test_file('2.0', 'CoSimulation', 'MapleSim', '2016.2', 'CoupledClutches', fmu_filename)

model_description = read_model_description(fmu_filename)

start_vars = [v for v in model_description.modelVariables if v.start and v.causality == 'local']

pprint(start_vars)
marco
  • 5,944
  • 10
  • 22