Skip to content

Commit 33f1de5

Browse files
committed
refactor
1 parent 6bc87c6 commit 33f1de5

File tree

9 files changed

+248
-34
lines changed

9 files changed

+248
-34
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ cmake-build-*/
99

1010
# Visual Studio Code
1111
.vscode/
12+
.lsp/
13+
.clj-kondo
1214

1315

1416
CMakeUserPresets.json

data/xsd/OspSystemStructure.xsd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
<xs:complexType name="variableEndpoint">
7878
<xs:attribute name="simulator" use="required" type="xs:string"/>
7979
<xs:attribute name="name" use="required" type="xs:string"/>
80+
<xs:attribute name="eccoparam" type="xs:string"/>
8081
</xs:complexType>
8182
<xs:complexType name="signalEndpoint">
8283
<xs:attribute name="function" use="required" type="xs:string"/>

include/cosim/system_structure.hpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,18 @@ inline std::ostream& operator<<(std::ostream& s, const full_variable_name& v)
133133
return s;
134134
}
135135

136+
/// Writes a `power_bond` to an output stream.
137+
/*
138+
inline std::ostream& operator<<(std::ostream& s, const power_bond& pb)
139+
{
140+
s << "u_a: " << pb.u_a << std::endl
141+
<< "u_b: " << pb.u_b << std::endl
142+
<< "y_a: " << pb.y_a << std::endl
143+
<< "y_b: " << pb.y_b << std::endl;
144+
return s;
145+
}
146+
*/
147+
136148
/// Returns a string representation of a `full_variable_name`.
137149
std::string to_text(const full_variable_name& v);
138150

@@ -213,8 +225,45 @@ class system_structure
213225
full_variable_name target;
214226
};
215227

228+
/// Information about a powerbond connection. For use with the ecco algorithm only.
229+
struct power_bond
230+
{
231+
/// The u_a variable in the bond, according to the description of the ecco algorithm.
232+
full_variable_name u_a{"", ""};
233+
234+
/// The u_b variable in the bond, according to the description of the ecco algorithm.
235+
full_variable_name u_b{"", ""};
236+
237+
/// The y_a variable in the bond, according to the description of the ecco algorithm.
238+
full_variable_name y_a{"", ""};
239+
240+
/// The y_b variable in the bond, according to the description of the ecco algorithm.
241+
full_variable_name y_b{"", ""};
242+
243+
void set_ua(full_variable_name& ua)
244+
{
245+
u_a = ua;
246+
}
247+
248+
void set_ub(full_variable_name& ub)
249+
{
250+
u_b = ub;
251+
}
252+
253+
void set_ya(full_variable_name& ya)
254+
{
255+
y_a = ya;
256+
}
257+
258+
void set_yb(full_variable_name& yb)
259+
{
260+
y_b = yb;
261+
}
262+
};
263+
216264
private:
217265
using entity_map = std::unordered_map<std::string, entity>;
266+
using power_bond_map = std::unordered_map<std::string, power_bond>;
218267
using connection_map =
219268
std::unordered_map<full_variable_name, full_variable_name>;
220269
using connection_transform =
@@ -251,6 +300,9 @@ class system_structure
251300
add_entity({std::string(name), type, {}, std::move(parameters)});
252301
}
253302

303+
power_bond_map get_power_bonds();
304+
void add_power_bond(std::string name, power_bond pb);
305+
254306
/**
255307
* Returns a list of the entities in the system.
256308
*
@@ -328,6 +380,8 @@ class system_structure
328380
// Connections. Target is key, source is value.
329381
connection_map connections_;
330382

383+
power_bond_map powerBonds_;
384+
331385
// Cache for fast lookup of model info, indexed by model UUID.
332386
struct model_info
333387
{

src/cosim/algorithm/ecco_algorithm.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -286,22 +286,22 @@ class ecco_algorithm::impl
286286

287287
const auto dt = to_double_duration(stepSize, currentTime);
288288

289-
for (std::size_t i = 0; i < uVariables_.size(); i+=2) {
289+
for (std::size_t i = 0; i < uVariables_.size(); i += 2) {
290290
auto u_a = uVariables_.at(i);
291-
auto u_b = uVariables_.at(i+1);
291+
auto u_b = uVariables_.at(i + 1);
292292
auto y_a = yVariables_.at(i);
293-
auto y_b = yVariables_.at(i+1);
293+
auto y_b = yVariables_.at(i + 1);
294294

295295
double u_a_value = simulators_.at(u_a.simulator).sim->get_real(u_a.reference);
296296
double u_b_value = simulators_.at(u_b.simulator).sim->get_real(u_b.reference);
297297
double y_a_value = simulators_.at(y_a.simulator).sim->get_real(y_a.reference);
298298
double y_b_value = simulators_.at(y_b.simulator).sim->get_real(y_b.reference);
299299

300300
double power_a = u_a_value * y_a_value;
301-
energies_.at(i).push_back(power_a*dt);
301+
energies_.at(i).push_back(power_a * dt);
302302

303303
double power_b = u_b_value * y_b_value;
304-
energies_.at(i+1).push_back(power_b*dt);
304+
energies_.at(i + 1).push_back(power_b * dt);
305305

306306
double power_residual = std::abs(power_a - power_b);
307307

@@ -320,7 +320,7 @@ class ecco_algorithm::impl
320320
mean_square += std::pow(energy_residual / (params.abs_tolerance + params.rel_tolerance * energy_level), 2);
321321
}
322322
const auto num_bonds = power_residuals.size(); // TODO: Still valid for multidimensial bonds?
323-
const auto error_estimate = std::sqrt(mean_square / (double) num_bonds);
323+
const auto error_estimate = std::sqrt(mean_square / (double)num_bonds);
324324

325325
// std::cout << power_a << " " << power_b << " " << energy_level << " " << energy_residual << " " << sum_power_residual << " " << error_estimate << std::endl;
326326
if (prev_error_estimate_ == 0 || error_estimate == 0) {
@@ -355,13 +355,14 @@ class ecco_algorithm::impl
355355
std::vector<cosim::variable_id> yVariables_{};
356356
std::vector<std::vector<double>> energies_{};
357357

358-
double get_mean(const std::vector<double>& elems) {
358+
double get_mean(const std::vector<double>& elems)
359+
{
359360
if (elems.empty()) return 0.0;
360361
double sum{};
361362
for (auto elem : elems) {
362363
sum += elem;
363364
}
364-
return sum / (double) elems.size();
365+
return sum / (double)elems.size();
365366
}
366367

367368
struct connection_ss
@@ -546,7 +547,8 @@ ecco_algorithm::ecco_algorithm(ecco_parameters params, std::optional<unsigned in
546547
}
547548

548549

549-
ecco_algorithm::~ecco_algorithm() noexcept {
550+
ecco_algorithm::~ecco_algorithm() noexcept
551+
{
550552
pimpl_->print_average_energies();
551553
}
552554

0 commit comments

Comments
 (0)