Search code examples
pythonmodelica

PyFMI: Restart coupled simulation from result file


I am using PyFMI to simulate a coupled simulation of two FMUs (Control.fmu and Plant.fmu) as shown below.

import matplotlib.pyplot as plt
from pyfmi import load_fmu

fmu_control = load_fmu('Control.fmu'); fmu_plant = load_fmu('Plant.fmu')

# List models
models = [fmu_control, fmu_plant]
# Set up connections between models
connections = [(fmu_control, 'y1', fmu_plant, 'u1'), (fmu_control, 'y2', fmu_plant, 'u2'),
               (fmu_plant, 'y1', fmu_control, 'u1'), (fmu_plant, 'y2', fmu_control, 'u2')]

# Create coupled co-simulation object
from pyfmi.master import Master
coupled_simulation = Master(models, connections)

opts = coupled_simulation.simulate_options()
opts['step_size'] = 0.01; opts['linear_correction'] = False
opts['block_initialization'] = True; opts['initialize'] = False

coupled_simulation.initialize(start_time = 0.0, final_time = 0.01, opts = opts)

res = coupled_simulation.simulate(options=opts, final_time = 1.0)

Plant_u1 = res[fmu_plant]['u1']; Plant_u2 = res[fmu_plant]['u2']
Plant_y1 = res[fmu_plant]['y1']; Plant_y2 = res[fmu_plant]['y2']
time = res[fmu_plant]['time']

Is there a way to implement a restart capability in the simulation? I would like to load the result files and use them to continue a simulation (i.e., start from where I left off in the previous step). I have been reading result files using:

from pyfmi.common.io import ResultDymolaTextual
res_control = ResultDymolaTextual('Control_0_result.txt')
res_plant = ResultDymolaTextual('Plant_1_result.txt')

but I don't know how to define those results as initial conditions of my next simulation step.


Solution

  • To fully support a restart capability the FMUs by themself need to support to optional feature get/set FMU state which is described in the specification, see https://fmi-standard.org/downloads/. Just using the result file to restart a simulation is in the general case not enough as there are usually internal variables in the FMU that are not exposed in the result file but have a significant impact on the actual state of the FMU. For instance boolean switches are not usually in the result file.