I have once written a python script to do this (see linked questions in comment section), it involves three steps:
- Get a list of iteration variables (or, other variables of interest)
- For these variables, read the values from results .mat file
- Write out a .mos file
Step 1 turned out to be the most difficult one, mostly because Dymola prints statistics and logs to txt files that need to be parsed! This would be much much easier if the log/statistics files were in a structured format like .json.
- I used the Dymola flag
Advanced.LogNonLinearIterationVariables=true
to make Dymola print out the variables to a file dsmodelIterationSelect.mof
. This file can be read into a python list with low effort, but I later on saw some discrepancy between the IVs reported in this file, and IVs reported in dsmodel.mof
.
- So, I also tried reading IVs from
dsmodel.mof
, but it feels like a waste of time to parse txt files. Still, this is the approach I would trust most.
- Yet another approach could be to use
modelDescription.xml
, generated with the flag Advanced.FMI.GenerateModelDescriptionInterface2
, not sure what information is available exactly, but that is documented somewhere.
- You can use
alist.exe -b dsfinal.txt
to convert the txt file to a mat file, that is easier to load.
- Yet another approach could be to use
Advanced.TranslationInCommandLog
in combination with translateModel()
and savelog("translate.txt");
and then parse this log file.
Step 2 is easy with the python package DyMat. There are some examples here on stackoverflow. Nice python trick: Index 0
gives first entry of a list, Index -1
gives last entry of the list. So, terminal value has index -1
.
Step 3 is more or less trivial, just write variable = value;
to a file, this step needs less than 10 lines of python code.