Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable exporting and importing subsimulator state #769

Open
wants to merge 2 commits into
base: dev/state-persistence
Choose a base branch
from

Conversation

kyllingstad
Copy link
Member

@kyllingstad kyllingstad commented Jul 4, 2024

This is a follow-up to #765 and the second and final step to close #756. It is part of a PR series which will culminate in the implementation of #757, and until that point, I am targeting the dev/state-persistence branch rather than master.

Here, I've implemented functionality to export the internal state of individual subsimulators in a generic, structured form, and to import it again later.

This exported form is intended as an intermediate step before serialisation to disk. The idea was to create a type that can be inspected and serialised to almost any file format we'd like.

The type is defined by cosim::serialization::node in cosim/serialization.hpp. It is a hierarchical, dynamic data type with support for a variety of primitive scalar types and a few aggregate types: strings, arrays of nodes, dictionaries of nodes, and binary blobs. (Think JSON-like structure, only in-memory and with more types.)

Edit: node was originally a homebrew type. Now, it is based on Boost.PropertyTree. Otherwise, the description above still fits pretty well.

This is a follow-up to #765 and the second and final step to close #756.

Here, I've implemented functionality to export the internal state of
individual subsimulators in a generic, structured form, and to import
them again later.

This exported form is intended as an intermediate step before
serialisation and disk storage.  The idea was to create a type that can
be inspected and serialised to almost any file format we'd like.

The type is defined by `cosim::serialization::node` in
`cosim/serialization.hpp`.  It is a hierarchical, dynamic data type with
support for a variety of primitive scalar types and a few aggregate
types: strings, arrays of nodes, dictionaries of nodes, and binary
blobs. (Think JSON, only with more types.)
@kyllingstad kyllingstad added the enhancement New feature or request label Jul 4, 2024
@kyllingstad kyllingstad self-assigned this Jul 4, 2024
@kyllingstad kyllingstad marked this pull request as draft July 4, 2024 17:09
@kyllingstad
Copy link
Member Author

Hm. Seems like my little trick of storing an incomplete node type in an unordered_map doesn't work on GCC 9. It worked locally for me, but I use a more recent compiler. I've changed this PR to "draft" status while I figure out what to do about it.

@kyllingstad
Copy link
Member Author

kyllingstad commented Jul 9, 2024

Ok, I made it work now.

It seems that to make a recursive map-like data type in standard C++, one has to hide the internal type, for example by messing about with void* pointers. Rather than do this myself, I have replaced the homemade cosim::serialization::node class with an alias for boost::property_tree::basic_ptree. The downside of this is a slightly less elegant API, but this is more than outweighed by the confidence we get from using tried-and-true code. Plus, we're already using ptree elsewhere in libcosim, so there's no extra dependency baggage involved.

@kyllingstad kyllingstad marked this pull request as ready for review July 9, 2024 15:38
@@ -290,8 +291,6 @@ class slave
* `state_index`. The index is only valid for this particular slave.
*
* The function may be called at any point after `setup()` has been called.
*
* \pre `this->model_description().can_save_state`
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and the removal on lines 304–305 was just something I missed in #765.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add interfaces to FMI functions for saving/serialising/deserialising/restoring FMU state
1 participant