Skip to content

Commit 533e413

Browse files
committed
refactoring and fixes
1 parent b00a44b commit 533e413

File tree

4 files changed

+59
-28
lines changed

4 files changed

+59
-28
lines changed

include/cosim/algorithm/ecco_algorithm.hpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,30 @@ class ecco_algorithm : public algorithm
8282

8383
/**
8484
* Retrieves the energies in the power bond for the given simulator index.add_variable_value
85-
* \param simulator_index
86-
* The index of the simulator.
85+
* \param simulator_index
86+
* The index of the simulator.
8787
*/
8888
std::vector<double> get_powerbond_energies(cosim::simulator_index simulator_index);
8989

90+
/**
91+
* Enables the step-size adjusting controller. By default the controller is enabled, so this function has no effect unless the controller has been
92+
* disabled first by calling disable_step_size_controller. This may be useful if the coupling error indicates that adjusting the step size
93+
* is necessary.
94+
*/
95+
void enable_step_size_controller();
96+
97+
/**
98+
* Disables the step-size adjusting controller. By default the controller is enabled, so this function should be called before
99+
* running the simulation if the intent is to run with it off. Toggling the flag to off in the middle of a simulation run has the effect of maintaining
100+
* the last adjusted step size value. This may be useful to improve simulation speeds when the error is sufficiently low with the current adjusted
101+
* step size.
102+
*/
103+
void disable_step_size_controller();
104+
105+
90106
private:
91107
class impl;
92-
std::unique_ptr<impl> pimpl_;
108+
std::unique_ptr<impl> pimpl_;
93109
};
94110

95111
} // namespace cosim

src/cosim/algorithm/ecco_algorithm.cpp

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ class ecco_algorithm::impl
209209
}
210210

211211
const auto stepSizeTaken = stepSize_;
212-
if (stepCounter_ >= 2) {
212+
if (stepCounter_ >= 2 && controllerEnabled_) {
213213
stepSize_ = adjust_step_size(currentT, stepSize_, params_);
214214
}
215215

@@ -228,7 +228,7 @@ class ecco_algorithm::impl
228228
transfer_variables(info.outgoingSimConnections);
229229
}
230230

231-
231+
// Have to return size of the current step, since step is taken before any step size adjustment.
232232
return {stepSizeTaken, std::move(finished)};
233233
}
234234

@@ -285,6 +285,7 @@ class ecco_algorithm::impl
285285
double max_power_residual = *std::max_element(power_residuals.begin(), power_residuals.end());
286286
const auto energy_level = max_power_residual * dt;
287287
double mean_square{};
288+
288289
for (auto power_residual : power_residuals) {
289290
const auto energy_residual = power_residual * dt;
290291
mean_square += std::pow(energy_residual / (params.abs_tolerance + params.rel_tolerance * energy_level), 2);
@@ -303,8 +304,9 @@ class ecco_algorithm::impl
303304

304305
prev_error_estimate_ = error_estimate;
305306
const auto new_step_size = to_duration(new_step_size_gain * to_double_duration(stepSize, currentTime));
306-
const auto actual_new_step_size = std::clamp(new_step_size, params.min_step_size, params.max_step_size);
307-
return actual_new_step_size;
307+
const auto new_step_size_clamped = std::clamp(new_step_size, params.min_step_size, params.max_step_size);
308+
309+
return new_step_size_clamped;
308310
}
309311

310312
void add_power_bond(cosim::variable_id input_a, cosim::variable_id output_a, cosim::variable_id input_b, cosim::variable_id output_b)
@@ -322,6 +324,16 @@ class ecco_algorithm::impl
322324
return energies_.at(simulator_index);
323325
}
324326

327+
void enable_step_size_controller()
328+
{
329+
controllerEnabled_ = true;
330+
}
331+
332+
void disable_step_size_controller()
333+
{
334+
controllerEnabled_ = false;
335+
}
336+
325337
private:
326338
std::vector<cosim::variable_id> inputVariables_{};
327339
std::vector<cosim::variable_id> outputVariables_{};
@@ -483,6 +495,7 @@ class ecco_algorithm::impl
483495
unsigned int max_threads_ = std::thread::hardware_concurrency() - 1;
484496
utility::thread_pool pool_;
485497
double prev_error_estimate_{1.0};
498+
bool controllerEnabled_ = true;
486499
};
487500

488501

@@ -597,4 +610,14 @@ std::vector<double> ecco_algorithm::get_powerbond_energies(cosim::simulator_inde
597610
return pimpl_->get_powerbond_energies(simulator_index);
598611
}
599612

613+
void ecco_algorithm::enable_step_size_controller()
614+
{
615+
pimpl_->enable_step_size_controller();
616+
}
617+
618+
void ecco_algorithm::disable_step_size_controller()
619+
{
620+
pimpl_->disable_step_size_controller();
621+
}
622+
600623
} // namespace cosim

src/cosim/osp_config_parser.cpp

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -798,13 +798,13 @@ void validate_power_bond(system_structure& systemStructure, std::string name, co
798798

799799
if (source_variable.causality != variable_causality::output) {
800800
std::ostringstream oss;
801-
oss << "Failed validating powerbond: source causality is not output for variable " << source_variable.name;
801+
oss << "Failed validating powerbond: causality is not output for source variable " << source_variable.name;
802802
throw std::runtime_error(oss.str());
803803
}
804804

805805
if (target_variable.causality != variable_causality::input) {
806806
std::ostringstream oss;
807-
oss << "Failed validating powerbond: source causality is not input for variable " << target_variable.name;
807+
oss << "Failed validating powerbond: causality is not input for target variable " << target_variable.name;
808808
throw std::runtime_error(oss.str());
809809
}
810810
}
@@ -816,6 +816,7 @@ void add_power_bonds(const std::vector<osp_config_parser::PowerBondConnection>&
816816
std::set<std::string> uniquePowerBondNames;
817817
for (const auto& pbConnection : pbConnections) {
818818
powerBondNames.emplace_back(pbConnection.name);
819+
std::cout << "Inserting powerbond " << pbConnection.name << std::endl;
819820
uniquePowerBondNames.insert(pbConnection.name);
820821
}
821822

@@ -836,14 +837,6 @@ void add_power_bonds(const std::vector<osp_config_parser::PowerBondConnection>&
836837

837838
assert(connectionA.name == connectionB.name);
838839

839-
/*
840-
auto variableA = cosim::full_variable_name{connectionA.connection.variableA.simulator, connectionA.connection.variableA.name};
841-
auto variableB = cosim::full_variable_name{connectionA.connection.variableB.simulator, connectionA.connection.variableB.name};
842-
auto variableC = cosim::full_variable_name{connectionB.connection.variableA.simulator, connectionB.connection.variableA.name};
843-
auto variableD = cosim::full_variable_name{connectionB.connection.variableB.simulator, connectionB.connection.variableB.name};
844-
*/
845-
846-
847840
auto connection_a_variable_a = cosim::full_variable_name{connectionA.connection.variableA.simulator, connectionA.connection.variableA.name};
848841
auto connection_a_variable_b = cosim::full_variable_name{connectionA.connection.variableB.simulator, connectionA.connection.variableB.name};
849842
auto connection_b_variable_a = cosim::full_variable_name{connectionB.connection.variableA.simulator, connectionB.connection.variableA.name};
@@ -854,19 +847,17 @@ void add_power_bonds(const std::vector<osp_config_parser::PowerBondConnection>&
854847

855848
auto powerbond = cosim::system_structure::power_bond{connection_a, connection_b};
856849

857-
validate_power_bond(systemStructure, pbName, powerbond);
858850
systemStructure.add_power_bond(pbName, powerbond);
851+
validate_power_bond(systemStructure, pbName, powerbond);
852+
}
859853

860-
// Check that the number of unique power bond names is equal to the number of power bonds. Otherwise, it is not possible to correctly connect the bonds.
861-
std::sort(powerBondNames.begin(), powerBondNames.end());
862-
auto uniqueCount = static_cast<int>(std::unique(powerBondNames.begin(), powerBondNames.end()) - powerBondNames.begin());
863-
auto numPowerBonds = static_cast<int>(systemStructure.get_power_bonds().size());
854+
auto uniquePowerBonds = static_cast<int>(uniquePowerBondNames.size());
855+
auto numPowerBonds = static_cast<int>(systemStructure.get_power_bonds().size());
864856

865-
if (uniqueCount != numPowerBonds) {
866-
std::ostringstream oss;
867-
oss << "The number of powerbonds (" << numPowerBonds << ") is not equal to the number of unique power bond names (" << uniqueCount << ") found in the configured system. Power bond names must be unique pr. bond, that is found on only and exactly the two VariableConnections that form the bond.";
868-
throw std::runtime_error(oss.str());
869-
}
857+
if (uniquePowerBonds != numPowerBonds) {
858+
std::ostringstream oss;
859+
oss << "The number of powerbonds (" << numPowerBonds << ") is not equal to the number of unique power bond names (" << uniquePowerBonds << ") found in the configured system. Power bond names must be unique pr. bond, that is found on only and exactly the two VariableConnections that form the bond.";
860+
throw std::runtime_error(oss.str());
870861
}
871862
}
872863

src/cosim/system_structure.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <cctype>
1414
#include <cstddef>
1515
#include <iomanip>
16+
#include <iostream>
1617
#include <sstream>
1718
#include <unordered_map>
1819

@@ -136,7 +137,7 @@ system_structure::power_bond_map system_structure::get_power_bonds() const noexc
136137
}
137138

138139
void system_structure::add_power_bond(std::string name, system_structure::power_bond pb)
139-
{
140+
{
140141
powerBonds_.emplace(name, pb);
141142
}
142143

0 commit comments

Comments
 (0)