3
votes

After a successful simulation, I want to use the result at steady-state to provide the initial guess values and save them in the model, but I didn't find such an option in Dymola, it only provides an option that allows me to do initialization and then save iteration variables in the model, but there is no guarantee that Dymola would initialize the model to the expecting steady state.

enter image description here

My idea is descirbed as following: enter image description here enter image description here

My questions are:
If I wanna use a Modelica script file(.mos file) to update the iteration variables after a successful simulation, which function should I use to extract all the iteration variables used in the model and update their values?

2

2 Answers

4
votes

I see some possibilities for this:

  1. Simulate until steady-state
  2. Import the final result using Simulation>Continue>Import or using script: importInitial("dsfinal.txt"); (You might adjust the start-time.)
  3. Save start-values in model using the dialog above. It does what you want, but it will not always reliably generate good start-values. (In particular if there are initial equations, states with start-values depending on parameters etc.)

A better option is:

  1. Simulate until steady-state
  2. Script Editor > Generate Script and select Variables and Final or using script exportInitial("dsfinal.txt","MyScript.mos",true,true)

A final option (that is being improved) is to set

  1. Advanced.DefaultSteadyStateInitialization=true; And don't set unnecessary normal start-values. (This does not help with iteration variables, but it helps with states.)
3
votes

I have once written a python script to do this (see linked questions in comment section), it involves three steps:

  1. Get a list of iteration variables (or, other variables of interest)
  2. For these variables, read the values from results .mat file
  3. 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.