Skip to content

Commit

Permalink
Swapped the order of setup and do_iteration in initialize function
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhjp01 committed Nov 15, 2024
1 parent 5c6e196 commit 1e54be8
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/cosim/algorithm/fixed_step_algorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,6 @@ class fixed_step_algorithm::impl

void initialize()
{
for (auto& s : simulators_) {
pool_.submit([&] {
s.second.sim->setup(startTime_, stopTime_, std::nullopt);
});
}
pool_.wait_for_tasks_to_finish();

// Run N iterations of the simulators' and functions' step/calculation
// procedures, where N is the number of simulators in the system,
// to propagate initial values.
Expand All @@ -171,6 +164,14 @@ class fixed_step_algorithm::impl
calculate_and_transfer();
}

for (auto& s : simulators_) {
pool_.submit([&] {
s.second.sim->setup(startTime_, stopTime_, std::nullopt);
});
}
pool_.wait_for_tasks_to_finish();


for (auto& s : simulators_) {
pool_.submit([&] {
s.second.sim->start_simulation();
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ set(tests
"scenario_manager_test"
"synchronized_xy_series_test"
"config_end_time_test"
"state_init_test"
)

set(unittests
Expand Down
Binary file added tests/data/fmi2/StateInitExample.fmu
Binary file not shown.
17 changes: 17 additions & 0 deletions tests/data/msmi/OspSystemStructure_StateInitExample.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<OspSystemStructure xmlns="http://opensimulationplatform.com/MSMI/OSPSystemStructure" version="0.1">
<StartTime>0.0</StartTime>
<BaseStepSize>0.01</BaseStepSize>
<Algorithm>fixedStep</Algorithm>
<Simulators>
<Simulator name="example" source="../fmi2/StateInitExample.fmu">
<InitialValues>
<InitialValue variable="Parameters.Integrator1_x0">
<Real value="10" />
</InitialValue>
</InitialValues>
</Simulator>
</Simulators>
<Connections>
</Connections>
</OspSystemStructure>
53 changes: 53 additions & 0 deletions tests/state_init_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <cosim/algorithm/fixed_step_algorithm.hpp>
#include <cosim/osp_config_parser.hpp>
#include <cosim/observer/file_observer.hpp>
#include <cosim/exception.hpp>
#include <cosim/execution.hpp>
#include <cosim/function/linear_transformation.hpp>
#include <cosim/observer/last_value_observer.hpp>
#include <cosim/system_structure.hpp>
#include <algorithm>
#include <iostream>

#define REQUIRE(test) \
if (!(test)) throw std::runtime_error("Requirement not satisfied: " #test)

int main()
{
try {
const auto testDataDir = std::getenv("TEST_DATA_DIR");
REQUIRE(!!testDataDir);

cosim::filesystem::path configPath = testDataDir;

auto resolver = cosim::default_model_uri_resolver();
const auto config = cosim::load_osp_config(configPath / "msmi" / "OspSystemStructure_StateInitExample.xml", *resolver);

auto execution = cosim::execution(
config.start_time,
std::make_shared<cosim::fixed_step_algorithm>(config.step_size));

const auto entityMaps = cosim::inject_system_structure(
execution, config.system_structure, config.initial_values);
auto lvObserver = std::make_shared<cosim::last_value_observer>();

execution.add_observer(lvObserver);
execution.simulate_until(cosim::to_time_point(0.1));

auto sim = entityMaps.simulators.at("example");
const cosim::value_reference inRef = 2;
const cosim::value_reference outRef = 3;

double initialValue = 0.0;
double outputValue = 0.0;
lvObserver->get_real(sim, gsl::make_span(&inRef, 1), gsl::make_span(&initialValue, 1));
lvObserver->get_real(sim, gsl::make_span(&outRef, 1), gsl::make_span(&outputValue, 1));

REQUIRE(initialValue == 1.0);
REQUIRE(std::fabs(outputValue - 10.1) < 1.0e-9);

} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}
}

0 comments on commit 1e54be8

Please sign in to comment.