Skip to content

Commit 1ad154c

Browse files
Merge branch 'add-ida-callbacks' into update-system-children
2 parents 7ef877e + 520a0ba commit 1ad154c

File tree

1 file changed

+75
-35
lines changed

1 file changed

+75
-35
lines changed

examples/PhasorDynamics/Example2/example2.cpp

Lines changed: 75 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,55 @@
2525
#include <Solver/Dynamic/Ida.hpp>
2626
#include <Utilities/Testing.hpp>
2727

28+
using scalar_type = double;
29+
using real_type = double;
30+
using index_type = size_t;
31+
32+
struct OutputData
33+
{
34+
real_type t;
35+
scalar_type gen2speed;
36+
scalar_type gen3speed;
37+
scalar_type v2mag;
38+
scalar_type v3mag;
39+
40+
OutputData operator-(const OutputData& other) const
41+
{
42+
assert(t == other.t);
43+
return {
44+
.t = t,
45+
.gen2speed = gen2speed - other.gen2speed,
46+
.gen3speed = gen3speed - other.gen3speed,
47+
.v2mag = v2mag - other.v2mag,
48+
.v3mag = v3mag - other.v3mag,
49+
};
50+
}
51+
52+
double norm() const
53+
{
54+
return std::max({
55+
std::abs(gen2speed),
56+
std::abs(gen3speed),
57+
std::abs(v2mag),
58+
std::abs(v3mag),
59+
});
60+
}
61+
};
62+
63+
std::ostream& operator<<(std::ostream& out, const OutputData& data)
64+
{
65+
out << data.t << ","
66+
<< data.gen2speed << ","
67+
<< data.gen3speed << ","
68+
<< data.v2mag << ","
69+
<< data.v3mag;
70+
return out;
71+
}
72+
2873
int main()
2974
{
3075
using namespace GridKit::PhasorDynamics;
3176
using namespace AnalysisManager::Sundials;
32-
using scalar_type = double;
33-
using real_type = double;
34-
using index_type = size_t;
3577

3678
auto error_allowed = static_cast<real_type>(1e-4);
3779

@@ -65,33 +107,40 @@ int main()
65107

66108
real_type dt = 1.0 / 4.0 / 60.0;
67109

68-
std::stringstream buffer;
110+
std::vector<OutputData> output;
111+
112+
auto output_cb = [&](real_type t)
113+
{
114+
std::vector<double>& yval = sys.y();
115+
116+
output.push_back({.t = t,
117+
.gen2speed = 1 + yval[5],
118+
.gen3speed = 1 + yval[26],
119+
.v2mag = sqrt(yval[0] * yval[0] + yval[1] * yval[1]),
120+
.v3mag = sqrt(yval[2] * yval[2] + yval[3] * yval[3])});
121+
};
69122

70123
/* Set up simulation */
71-
Ida<scalar_type, index_type> ida(&sys);
124+
Ida<scalar_type, index_type>
125+
ida(&sys);
72126
ida.configureSimulation();
73127

74128
/* Run simulation */
75129
scalar_type start = static_cast<scalar_type>(clock());
76130
ida.initializeSimulation(0.0, false);
77-
ida.runSimulationFixed(0.0, dt, 1.0, buffer);
131+
ida.runSimulation(1.0, std::round((1.0 - 0.0) / dt), output_cb);
78132
fault.setStatus(1);
79133
ida.initializeSimulation(1.0, false);
80-
ida.runSimulationFixed(1.0, dt, 1.1, buffer);
134+
ida.runSimulation(1.1, std::round((1.1 - 1.0) / dt), output_cb);
81135
fault.setStatus(0);
82136
ida.initializeSimulation(1.1, false);
83-
ida.runSimulationFixed(1.1, dt, 10.0, buffer);
137+
ida.runSimulation(10.0, std::round((10.0 - 1.1) / dt), output_cb);
84138
double stop = static_cast<double>(clock());
85139

86140
/* Check worst-case error */
87141
real_type worst_error = 0;
88142
real_type worst_error_time = 0;
89143

90-
const index_type stride = 94;
91-
const index_type nt = 2401;
92-
scalar_type results[stride];
93-
buffer.seekg(0, std::ios::beg);
94-
95144
std::ostream nullout(nullptr);
96145
std::ostream& out = nullout;
97146

@@ -103,32 +152,23 @@ int main()
103152
out << "Time,gen2speed,gen3speed,v2mag,v3mag\n";
104153
out << 0. << "," << 1. << "," << 1. << "," << 1. << "," << 1. << "\n";
105154

106-
for (index_type i = 0; i < nt - 1; ++i)
155+
for (index_type i = 0; i < output.size(); ++i)
107156
{
108-
for (index_type j = 0; j < stride; ++j)
109-
{
110-
buffer >> results[j];
111-
}
112-
real_type t = results[0];
113-
scalar_type gen2speed = 1 + results[7];
114-
scalar_type gen2speed_ref = reference_solution[i + 1][1];
115-
scalar_type gen3speed = 1 + results[28];
116-
scalar_type gen3speed_ref = reference_solution[i + 1][2];
117-
scalar_type v2mag = sqrt(results[2] * results[2] + results[3] * results[3]);
118-
scalar_type v2mag_ref = reference_solution[i + 1][4];
119-
scalar_type v3mag = sqrt(results[4] * results[4] + results[5] * results[5]);
120-
scalar_type v3mag_ref = reference_solution[i + 1][5];
121-
122-
out << t << "," << gen2speed << "," << gen3speed << "," << v2mag << "," << v3mag << "\n";
123-
124-
real_type err = std::max({std::abs(gen2speed - gen2speed_ref),
125-
std::abs(gen3speed - gen3speed_ref),
126-
std::abs(v2mag - v2mag_ref),
127-
std::abs(v3mag - v3mag_ref)});
157+
OutputData ref = {
158+
.t = reference_solution[i + 1][0],
159+
.gen2speed = reference_solution[i + 1][1],
160+
.gen3speed = reference_solution[i + 1][2],
161+
.v2mag = reference_solution[i + 1][4],
162+
.v3mag = reference_solution[i + 1][5]};
163+
OutputData out_data = output[i];
164+
165+
out << out_data << '\n';
166+
167+
real_type err = (out_data - ref).norm();
128168
if (err > worst_error)
129169
{
130170
worst_error = err;
131-
worst_error_time = t;
171+
worst_error_time = out_data.t;
132172
}
133173
}
134174
// fileout.close();

0 commit comments

Comments
 (0)