open-simulation-platform / libcosim

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

Initial subsimulator variable values are not available after `setup()` #762

Closed kyllingstad closed 2 months ago

kyllingstad commented 2 months ago

Let us say that we have an FMU instance with an output variable x which has a default initial value of 1.0. Furthermore, assume we have an object of type std::shared_ptr<slave> named instance which refers to this instance. Now, consider the following:

const auto xVarReference = cosim::find_variable(instance->modelDescription(), "x")->reference;
auto sim = cosim::slave_simulator(instance, "my_simulator");
sim.expose_for_getting(cosim::variable_type::real, xVarReference);
sim.setup({}, {}, {});
const auto xValue = sim.get_real(xVarReference);
assert(xValue == 1.0); // FAILS

The assertion in the last line should pass, but it will fail.

The reason is that we don't populate the output variable cache in slave_simulator in its setup() method. We should, because at the end of that method call, the (FMI 2.0) FMU instance will be in initialisation mode, where output variables are gettable.

kyllingstad commented 2 months ago

The fix is trivial: Just add the line

get_variables(duration::zero());

at the end of slave_simulator::impl::setup().

I'll do this while working on #756, as I'm making changes to slave_simulator and writing a unittest for it there anyway.

markaren commented 2 months ago

Potential fix for https://github.com/open-simulation-platform/libcosim/issues/609 I reckon

kyllingstad commented 2 months ago

Potential fix for #609 I reckon

I tried to run the quarter-car case with the new code in #765, and it seems unfortunately not. The line I added there to fix this issue only transfers subsimulator variables to the master so they're available there; it doesn't push them out to the subsimulators again.