Skip to content

Commit a515ea1

Browse files
committed
fix merge issues with state saving branch
1 parent 3f7a18f commit a515ea1

File tree

4 files changed

+37
-12
lines changed

4 files changed

+37
-12
lines changed

include/cosim/algorithm/ecco_algorithm.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ class ecco_algorithm : public algorithm
7070
void setup(time_point startTime, std::optional<time_point> stopTime) override;
7171
void initialize() override;
7272
std::pair<duration, std::unordered_set<simulator_index>> do_step(time_point currentT) override;
73+
serialization::node export_current_state() const override;
74+
void import_state(const serialization::node& exportedState) override;
7375

7476
/**
7577
* Adds a variable pair for the power residual calculation.

src/cosim/algorithm/ecco_algorithm.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,16 @@ class ecco_algorithm::impl
232232
return {stepSizeTaken, std::move(finished)};
233233
}
234234

235+
serialization::node export_current_state() const
236+
{
237+
throw error(make_error_code(errc::unsupported_feature), "State saving not yet supported by the ECCO algorithm!");
238+
}
239+
240+
void import_state([[maybe_unused]] const serialization::node& exportedState)
241+
{
242+
throw error(make_error_code(errc::unsupported_feature), "State saving not yet supported by the ECCO algorithm!");
243+
}
244+
235245
void get_energies()
236246
{
237247
for (std::size_t i = 0; i < energies_.size(); ++i) {
@@ -567,6 +577,16 @@ std::pair<duration, std::unordered_set<simulator_index>> ecco_algorithm::do_step
567577
return pimpl_->do_step(currentT);
568578
}
569579

580+
serialization::node ecco_algorithm::export_current_state() const
581+
{
582+
return pimpl_->export_current_state();
583+
}
584+
585+
void ecco_algorithm::import_state(const serialization::node& exportedState)
586+
{
587+
pimpl_->import_state(exportedState);
588+
}
589+
570590
void ecco_algorithm::add_power_bond(cosim::variable_id input_a, cosim::variable_id output_a, cosim::variable_id input_b, cosim::variable_id output_b)
571591
{
572592
pimpl_->add_power_bond(input_a, output_a, input_b, output_b);

tests/mock_slave.hpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class mock_slave : public cosim::slave
3838
std::function<double(cosim::time_point, cosim::duration, double)> realOp = nullptr,
3939
std::function<int(cosim::time_point, cosim::duration, int)> intOp = nullptr,
4040
std::function<bool(cosim::time_point, cosim::duration, bool)> boolOp = nullptr,
41-
std::function<std::string(cosim::time_point, cosim::duration, std::string_view)> stringOp = nullptr,
41+
std::function<std::string(cosim::time_point, cosim::duration, std::string_view)> stringOp = nullptr,
4242
std::function<void(cosim::time_point, cosim::duration)> stepAction = nullptr)
4343
: realOp_(std::move(realOp))
4444
, intOp_(std::move(intOp))
@@ -71,7 +71,6 @@ class mock_slave : public cosim::slave
7171
return [op = std::move(timeIndependentOp)](cosim::time_point, cosim::duration, T... value) { return op(value...); };
7272
}
7373

74-
7574
cosim::model_description model_description() const override
7675
{
7776
cosim::model_description md;
@@ -106,7 +105,8 @@ class mock_slave : public cosim::slave
106105

107106
cosim::step_result do_step(cosim::time_point currentT, cosim::duration deltaT) override
108107
{
109-
if (stepAction_) stepAction_(currentT);
108+
state_.currentStepSize = deltaT;
109+
if (stepAction_) stepAction_(currentT, deltaT);
110110
state_.currentTime = currentT + deltaT;
111111
return cosim::step_result::complete;
112112
}
@@ -117,7 +117,7 @@ class mock_slave : public cosim::slave
117117
{
118118
for (std::size_t i = 0; i < variables.size(); ++i) {
119119
if (variables[i] == real_out_reference) {
120-
values[i] = realOp_ ? realOp_(state_.currentTime, state_.realIn) : state_.realIn;
120+
values[i] = realOp_ ? realOp_(state_.currentTime, state_.currentStepSize, state_.realIn) : state_.realIn;
121121
} else if (variables[i] == real_in_reference) {
122122
values[i] = state_.realIn;
123123
} else {
@@ -132,7 +132,7 @@ class mock_slave : public cosim::slave
132132
{
133133
for (std::size_t i = 0; i < variables.size(); ++i) {
134134
if (variables[i] == integer_out_reference) {
135-
values[i] = intOp_ ? intOp_(state_.currentTime, state_.intIn) : state_.intIn;
135+
values[i] = intOp_ ? intOp_(state_.currentTime, state_.currentStepSize, state_.intIn) : state_.intIn;
136136
} else if (variables[i] == integer_in_reference) {
137137
values[i] = state_.intIn;
138138
} else {
@@ -147,7 +147,7 @@ class mock_slave : public cosim::slave
147147
{
148148
for (std::size_t i = 0; i < variables.size(); ++i) {
149149
if (variables[i] == boolean_out_reference) {
150-
values[i] = boolOp_ ? boolOp_(state_.currentTime, state_.boolIn) : state_.boolIn;
150+
values[i] = boolOp_ ? boolOp_(state_.currentTime, state_.currentStepSize, state_.boolIn) : state_.boolIn;
151151
} else if (variables[i] == boolean_in_reference) {
152152
values[i] = state_.boolIn;
153153
} else {
@@ -162,7 +162,7 @@ class mock_slave : public cosim::slave
162162
{
163163
for (std::size_t i = 0; i < variables.size(); ++i) {
164164
if (variables[i] == string_out_reference) {
165-
values[i] = stringOp_ ? stringOp_(state_.currentTime, state_.stringIn) : state_.stringIn;
165+
values[i] = stringOp_ ? stringOp_(state_.currentTime, state_.currentStepSize, state_.stringIn) : state_.stringIn;
166166
} else if (variables[i] == string_in_reference) {
167167
values[i] = state_.stringIn;
168168
} else {
@@ -248,7 +248,8 @@ class mock_slave : public cosim::slave
248248
{
249249
const auto& ss = savedStates_.at(state);
250250
cosim::serialization::node es;
251-
es.put("currentTime", ss.currentTime.time_since_epoch().count());
251+
es.put("currentTime", cosim::to_double_time_point(ss.currentTime));
252+
es.put("currentStepSize", cosim::to_double_duration(ss.currentStepSize, ss.currentTime));
252253
es.put("realIn", ss.realIn);
253254
es.put("intIn", ss.intIn);
254255
es.put("boolIn", ss.boolIn);
@@ -259,8 +260,8 @@ class mock_slave : public cosim::slave
259260
state_index import_state(const cosim::serialization::node& exportedState) override
260261
{
261262
state ss;
262-
ss.currentTime = cosim::time_point(cosim::time_point::duration(
263-
exportedState.get<cosim::time_point::rep>("currentTime")));
263+
ss.currentTime = cosim::time_point(cosim::time_point::duration(exportedState.get<cosim::time_point::rep>("currentTime")));
264+
ss.currentStepSize = cosim::to_duration(exportedState.get<double>("currentStepSize"));
264265
ss.realIn = exportedState.get<decltype(ss.realIn)>("realIn");
265266
ss.intIn = exportedState.get<decltype(ss.intIn)>("intIn");
266267
ss.boolIn = exportedState.get<decltype(ss.boolIn)>("boolIn");
@@ -279,6 +280,7 @@ class mock_slave : public cosim::slave
279280
struct state
280281
{
281282
cosim::time_point currentTime;
283+
cosim::duration currentStepSize{1000};
282284

283285
double realIn = 0.0;
284286
int intIn = 0;

tests/save_state_test.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,10 @@ int main()
5656

5757
auto resolver = cosim::default_model_uri_resolver();
5858
const auto config = cosim::load_osp_config(configPath, *resolver);
59+
auto algorithm_cfg = std::get<cosim::fixed_step_algorithm_params>(config.algorithm_configuration);
5960
auto execution = cosim::execution(
6061
config.start_time,
61-
std::make_shared<cosim::fixed_step_algorithm>(config.step_size));
62+
std::make_shared<cosim::fixed_step_algorithm>(algorithm_cfg));
6263

6364
const auto entityMaps = cosim::inject_system_structure(
6465
execution, config.system_structure, config.initial_values);
@@ -129,7 +130,7 @@ int main()
129130
std::vector<cosim::simulator_index> simulators;
130131
simulators.push_back(
131132
execution.add_slave(
132-
std::make_unique<mock_slave>([](cosim::time_point t, double) {
133+
std::make_unique<mock_slave>([](cosim::time_point t, [[maybe_unused]] cosim::duration d, double) {
133134
return cosim::to_double_time_point(t);
134135
}),
135136
"clock"));

0 commit comments

Comments
 (0)