In some interactive simulation work it is convenient to simulate up to a time point, change some parameters, and then continue simulation.
Using FMU for simulation then you need to reload or reset the FMU, and then enter new initial values for the state of the system that is the final values of the states from the previous simulation. You also need to enter the values for parameters that are different from default values in a similar way as for the previous simulation.
When you use PyFMI there is a convenient associated function get_states_list() as:
from pyfmi import load_fmu
...
model = load_fmu('file of fmu')
stateDict = model.get_states_list()
The dictionary stateDict contains the continuous time states and this is very helpful to further automate the update of the initial state values with the final state values from the previous simulation. The possible discrete time states is not included though, and needs to be handled manually, as far as I understand.
How do I get a similar dictionary of (continuous time) states using FMPy? Or even better, include also discrete time states?
With FMPy you can extract a list of the continuous states from the model description by following the
derivativeattribute of the unknowns inModelStructure/Derivatives(see FMI Specification 2.0.4, p.57 & p. 61).Note that it is not allowed to set the values of variables with causality
local(see FMI Specification 2.0.4, p. 108). This includes the continuous states of a Co-Simulation FMU.To retrieve and restore the state of a Co-Simulation FMU you have to use the FMU State API (see FMI Specification 2.0.4, p. 26).