I am trying to run an FMU for a nominal input and at the same time step for a perturbed input using the same states as the original FMU.
The main difficulty here is making a copy of the FMU i.e. saving the numerical data and setting it again in an FMU.
At the moment, I have been working with fmpy and this package does have several functionalities for saving and setting the states. However, none of these are supported by the FMU that I am working with at the moment. So I would need to look if there is another way around this to access to numerical data.
Additionally, I tried the CancelStep functionality to try to reset the states of the FMU, but this provided an 'error 3' output.
from fmpy import *
from fmpy.fmi2 import FMU2Slave
fmu_filename = 'BouncingBall.fmu'
Ts = 0.01
# load fmu
model_description = read_model_description(fmu_filename)
vrs = {}
for variable in model_description.modelVariables:
vrs[variable.name] = variable.valueReference
v = vrs['v']
y = vrs['h']
unzipdir = extract(fmu_filename)
fmu = FMU2Slave(guid=model_description.guid,
unzipDirectory=unzipdir,
modelIdentifier='BouncingBall',
instanceName='instance1')
fmu.instantiate()
fmu.setupExperiment(startTime=0.)
fmu.enterInitializationMode()
fmu.exitInitializationMode()
v_t0 = 0
y_t0 = 2
dv = 0.1
fmu.setReal([v], [v_t0])
fmu.setReal([y], [y_t0])
fmu.doStep(currentCommunicationPoint=0., communicationStepSize=Ts)
print(fmu.getReal([y]))
print(fmu.getReal([v]))
fmu.cancelStep()
fmu.setReal([v], [v_t0 + dv])
fmu.doStep(currentCommunicationPoint=0., communicationStepSize=Ts)
print(fmu.getReal([y]))
print(fmu.getReal([v]))