kdavies4 / ModelicaRes

Set up, analyze, and plot Modelica simulations in Python
http://kdavies4.github.io/ModelicaRes/
Other
51 stars 20 forks source link

Adjust SimResList.plot() to account for continued simulations #26

Closed arnoutaertgeerts closed 9 years ago

arnoutaertgeerts commented 9 years ago

The SimResList class does an excellent job of plotting simulation results of different simulations with the same start and stop time.

For continued simulations, it is unnecessary to divide the plot in different segments and give each segment a new name and color. For example:

image

I see two solutions:

kdavies4 commented 9 years ago

I see your point. This is tricky. We could handle that in SimResList.plot(), but it makes that method more complicated and SimResList isn't really intended to concatenate results anyway. We could add a merge() to SimRes or as a standalone function, but the details to truely merge the data depend on the format it was loaded from. I wasn't planning to add a method to write or rewrite simulation result files (although SimRes.to_pandas() can export to some standard formats).

The easiest way might be to create a dedicated class that extends from SimRes. There are two abstract methods in SimRes: values and times. As it stands these are populated by the Dymola file loader (_modelicares.io.dymola.readsim). We could create a SimResSequence class that extends from SimRes but has an __init__ method similar to that of SimResList. It would define the values() and times() methods to pull from the SimRes instances stored within it. In theory, everything else (its plot(), min, max, etc.) would fall into place.

What do you think?

arnoutaertgeerts commented 9 years ago

Good thinking, that could be an easy solution!

kdavies4 commented 9 years ago

Would you consider taking the first shot at it?

arnoutaertgeerts commented 9 years ago

Sure, I was planning to ;)

arnoutaertgeerts commented 9 years ago

I followed your approach and everything works :) see https://github.com/kdavies4/ModelicaRes/commit/75b565b892196b2b845244424cf53dfffb1d3b27. However, I did had to change values in NamedTuple instances which is something you should normally not do.

If you are okay with my implementation, we can close this.

PS: I think the continue_run() method is a great addition to the module. It enables developers to use external control methods on Dymola models (as is also possible in Matlab). I think the FMU approach works best but I did not yet succeed in running FMUs with the Dymola solvers.

arnoutaertgeerts commented 9 years ago

Some considerations after I tried this with a large simulations:

Note that when working with continued simulations, the file writing is the bottleneck, especially when the iterations are short. This can cause the following overhead:

kdavies4 commented 9 years ago

I changed the approach to generate new NamedTuple instances for each variable (179571d). I'm not sure if this will reduce the memory load but it could.

kdavies4 commented 9 years ago

We could speed up the dsin.txt writing, but it would take some work. Options:

  1. Update the file in place. This is probably not safe because the length of the new values may be longer than the old ones and the existing whitespace.
  2. Use binary files (dsin.mat) generated from dymosim with the -ib option. We'd need to create functions to read and write to those (using scipy's loadmat and savemat).
  3. Write only the modified parameters to a new dsin file. Dymosim apparently has an option to use two files where one overrides the other. Edit: Looking at the documentation now, I think I was wrong on this.
kdavies4 commented 9 years ago

I think I'll close this because we've solved the original issue. I'll open a new issue (#27) related to file size and processing time.