diff --git a/src/cosim/slave_simulator.cpp b/src/cosim/slave_simulator.cpp index 22838bb5..c16f2d17 100644 --- a/src/cosim/slave_simulator.cpp +++ b/src/cosim/slave_simulator.cpp @@ -13,6 +13,7 @@ #include #include #include +#include namespace cosim @@ -219,7 +220,7 @@ class set_variable_cache std::pair, gsl::span> modify_and_get( duration deltaT, - std::optional> filter = std::nullopt) + std::optional> filter = std::nullopt) { if (!hasRunModifiers_) { for (const auto& entry : modifiers_) { @@ -243,9 +244,11 @@ class set_variable_cache for (size_t i = 0; i < references_.size(); i++) { auto& ref = references_.at(i); - if ((*filter)(ref)) { + auto& value = values_.at(i); + + if ((*filter)(ref, value)) { references_filtered_.push_back(ref); - values_filtered_.push_back(values_.at(i)); + values_filtered_.push_back(value); } } @@ -546,19 +549,25 @@ class slave_simulator::impl void initialize_start_values() { auto deltaT = duration::zero(); - auto filter = [&](variable_type vt) { - return [&vt, this](const value_reference &vr) { - const auto &vd = this->find_variable_description(vr, vt); - /// FMI Specification 2.0.4 - Section 4.2.4 - return vd.variability != variable_variability::constant && - vd.causality != variable_causality::input; - }; + auto filter = [this](const value_reference& vr, const std::variant& vt) { + variable_type type; + std::visit( + visitor( + [&](double) { type = variable_type::real; }, + [&](int) { type = variable_type::integer; }, + [&](bool) { type = variable_type::boolean; }, + [&](const std::string&) { type = variable_type::string; }), + vt); + const auto& vd = this->find_variable_description(vr, type); + /// FMI Specification 2.0.4 - Section 4.2.4 + return vd.variability != variable_variability::constant && + vd.causality != variable_causality::input; }; - const auto [realRefs, realValues] = realSetCache_.modify_and_get(deltaT, filter(variable_type::real)); - const auto [integerRefs, integerValues] = integerSetCache_.modify_and_get(deltaT, filter(variable_type::integer)); - const auto [booleanRefs, booleanValues] = booleanSetCache_.modify_and_get(deltaT, filter(variable_type::boolean)); - const auto [stringRefs, stringValues] = stringSetCache_.modify_and_get(deltaT, filter(variable_type::string)); + const auto [realRefs, realValues] = realSetCache_.modify_and_get(deltaT, filter); + const auto [integerRefs, integerValues] = integerSetCache_.modify_and_get(deltaT, filter); + const auto [booleanRefs, booleanValues] = booleanSetCache_.modify_and_get(deltaT, filter); + const auto [stringRefs, stringValues] = stringSetCache_.modify_and_get(deltaT, filter); slave_->set_variables( gsl::make_span(realRefs),