open-simulation-platform / libcosim

OSP C++ co-simulation library
https://open-simulation-platform.github.io/libcosim
Mozilla Public License 2.0
61 stars 10 forks source link

Enable saving and restoring full simulation state #777

Open kyllingstad opened 2 weeks ago

kyllingstad commented 2 weeks ago

This closes #768. Some changes may warrant a bit of extra explanation:

execution and algorithm: I have taken one step towards prohibiting adding/removing sub-simulators after the co-simulation has begun, as decided in #771. In particular, I've added the execution::initialize() function, which marks the point where such changes are no longer allowed. (This is a backwards-compatible change, because it gets automatically called by execution::step() if it hasn't been called manually.)

slave_simulator: The changes in slave_simulator.cpp really ought to have been included in #769. Unfortunately, I didn't realise they were necessary before it was all put into the context of saving algorithm and execution state. Basically, I thought I could get away with just saving each FMU's internal state, but it turns out that we also need to save the slave_simulator "get" and "set" caches.

(For those interested in the nitty-gritties, this is due to some subtleties regarding exactly when the cache values are set in the course of a co-simulation, relative to when the values are passed to the FMU. At the end of an "algorithm step", the "set cache" is out of sync with the FMUs input variables, and won't be synced before the next step. Simply performing an additional sync prior to saving the state is not sufficient, because that could have an effect on the FMUs output variables, thus invalidating the "get cache". That could in principle be updated too, but then the slave_simulator is in a whole different state from where it was when we started to save the state.)

davidhjp01 commented 2 days ago

Just leaving some ideas here - I have used nlohman_json before and I liked it, and seems it has support to encode/decode json data to/from CBOR. May be worth to consider switching it from boost ptree? Also I like its documentation :) https://json.nlohmann.me/api/basic_json/

Update There are some limitations using nlohman_json for CBOR encoding/decoding it is documented here: https://json.nlohmann.me/features/binary_formats/cbor/, but does not seem critical for our use case?