I want to implement a simple unscented Kalman filter in python for some Modelica models. The models are compiled to FMU and I want to use them via the FMPy library.
I have played around a bit with the interface and I can't find a way to define a forward operator for the Kalman filter. More specifically, I don't manage to define the forward operator in such a way that it takes any state vector as input and computes the corresponding update, since I cannot arbitrarily manipulate the state vector of a model.
Apparently, co-simulation does not work, thank you @Christian Bertsch for pointing this out. I have tried to implement it using Model Exchange as follows:
def forward_operator_dirsim(x, p, tstart, tend, h, state_keys, param_keys, tol=1e-8):
start_values = {}
for (keys, vals) in zip([state_keys, param_keys], [x, p]):
for (key, val) in zip(keys, vals):
start_values[key] = val
result = simulate_fmu(FMU_PATH,
start_time=tstart,
stop_time=tend,
step_size=h,
start_values=start_values,
relative_tolerance=tol,
terminate=False,
fmi_type="ModelExchange")
return result
However, the compiler is complaining that some of the variables cannot be set. Why is that and how can I avoid this?
For Co-Simulation FMUs, this is a restriction of the current FMI Standard, see the discussion in https://github.com/modelica/fmi-standard/issues/94. The proposal is to establish a layered standard that defines an extension to FMI. However, such a solution will be a non-standard extension to the core FMI standard, and not all FMI supporting tools will support this
As a non-standard-conform extension, setting states seems to work for Dymola FMUs, see the ticket above and https://ep.liu.se/ecp/096/005/ecp14096005.pdf.
It should be possible to implement what you want with Model-Exchange FMUs, see https://ep.liu.se/ecp/063/053/ecp11063053.pdf (which was written at a very early stage of FMI; tool support is much better today).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With