Skip to content

Commit fa4fe6b

Browse files
authored
Fix warnings in callback examples (#98)
1 parent 19d1a9d commit fa4fe6b

File tree

2 files changed

+78
-55
lines changed

2 files changed

+78
-55
lines changed

examples/PhasorDynamics/Example1/example1.cpp

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ int main()
7373
// we instead narrow down exactly what we want to keep.
7474
struct OutputData
7575
{
76+
OutputData(double ti_in, double Vr_in, double Vi_in, double dw_in)
77+
: ti(ti_in), Vr(Vr_in), Vi(Vi_in), dw(dw_in)
78+
{
79+
}
80+
7681
double ti, Vr, Vi, dw;
7782
};
7883

@@ -93,46 +98,32 @@ int main()
9398
{
9499
std::vector<double>& yval = sys.y();
95100

96-
output.push_back({.ti = t,
97-
.Vr = yval[0],
98-
.Vi = yval[1],
99-
.dw = yval[3]});
101+
output.push_back(OutputData(t, yval[0], yval[1], yval[3]));
100102
};
101103

102-
// The above lambda is equivalent to writing
103-
// struct OutputCallback
104-
// {
105-
// SystemModel<double, size_t>& sys;
106-
// std::vector<OutputData>& output;
107-
//
108-
// void operator()(double t)
109-
// {
110-
// std::vector<double>& yval = sys.y();
111-
//
112-
// output.push_back({.ti = t,
113-
// .Vr = yval[0],
114-
// .Vi = yval[1],
115-
// .dw = yval[3]});
116-
// }
117-
// };
118-
//
119-
// OutputCallback output_cb = {.sys = sys, .output = output};
120-
121-
/* Set up simulation */
104+
// Set up simulation
122105
Ida<double, size_t> ida(&sys);
123106
ida.configureSimulation();
124107

125-
/* Run simulation - making sure to pass the callback to record output*/
108+
// Run simulation - making sure to pass the callback to record output
126109
double start = static_cast<double>(clock());
127-
// ida.printOutputF(0, 0, buffer);
110+
111+
// Run for 1s
128112
ida.initializeSimulation(0.0, false);
129-
ida.runSimulation(1.0, std::round((1.0 - 0.0) / dt), output_cb);
113+
int nout = static_cast<int>(std::round((1.0 - 0.0) / dt));
114+
ida.runSimulation(1.0, nout, output_cb);
115+
116+
// Introduce fault and run for the next 0.1s
130117
fault.setStatus(1);
131118
ida.initializeSimulation(1.0, false);
132-
ida.runSimulation(1.1, std::round((1.1 - 1.0) / dt), output_cb);
119+
nout = static_cast<int>(std::round((1.1 - 1.0) / dt));
120+
ida.runSimulation(1.1, nout, output_cb);
121+
122+
// Clear the fault and run until t = 10s.
133123
fault.setStatus(0);
134124
ida.initializeSimulation(1.1, false);
135-
ida.runSimulation(10.0, std::round((10.0 - 1.1) / dt), output_cb);
125+
nout = static_cast<int>(std::round((10.0 - 1.1) / dt));
126+
ida.runSimulation(10.0, nout, output_cb);
136127
double stop = static_cast<double>(clock());
137128

138129
double error_V = 0.0; // error in |V|

examples/PhasorDynamics/Example2/example2.cpp

Lines changed: 58 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,36 @@ struct OutputData
3737
scalar_type v2mag;
3838
scalar_type v3mag;
3939

40-
OutputData operator-(const OutputData& other) const
40+
OutputData(real_type t_in,
41+
scalar_type gen2speed_in,
42+
scalar_type gen3speed_in,
43+
scalar_type v2mag_in,
44+
scalar_type v3mag_in)
45+
: t(t_in),
46+
gen2speed(gen2speed_in),
47+
gen3speed(gen3speed_in),
48+
v2mag(v2mag_in),
49+
v3mag(v3mag_in)
50+
{
51+
}
52+
53+
OutputData(const OutputData& data)
54+
: t(data.t),
55+
gen2speed(data.gen2speed),
56+
gen3speed(data.gen3speed),
57+
v2mag(data.v2mag),
58+
v3mag(data.v3mag)
59+
{
60+
}
61+
62+
OutputData& operator-=(const OutputData& other)
4163
{
4264
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-
};
65+
gen2speed -= other.gen2speed;
66+
gen3speed -= other.gen3speed;
67+
v2mag -= other.v2mag;
68+
v3mag -= other.v3mag;
69+
return *this;
5070
}
5171

5272
double norm() const
@@ -60,6 +80,11 @@ struct OutputData
6080
}
6181
};
6282

83+
const OutputData operator-(const OutputData& lhs, const OutputData& rhs)
84+
{
85+
return OutputData(lhs) -= rhs;
86+
}
87+
6388
std::ostream& operator<<(std::ostream& out, const OutputData& data)
6489
{
6590
out << data.t << ","
@@ -113,28 +138,36 @@ int main()
113138
{
114139
std::vector<double>& yval = sys.y();
115140

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])});
141+
output.push_back(OutputData(t,
142+
1.0 + yval[5],
143+
1.0 + yval[26],
144+
std::sqrt(yval[0] * yval[0] + yval[1] * yval[1]),
145+
std::sqrt(yval[2] * yval[2] + yval[3] * yval[3])));
121146
};
122147

123-
/* Set up simulation */
124-
Ida<scalar_type, index_type>
125-
ida(&sys);
148+
// Set up simulation
149+
Ida<scalar_type, index_type> ida(&sys);
126150
ida.configureSimulation();
127151

128-
/* Run simulation */
152+
// Run simulation, output each `dt` interval
129153
scalar_type start = static_cast<scalar_type>(clock());
130154
ida.initializeSimulation(0.0, false);
131-
ida.runSimulation(1.0, std::round((1.0 - 0.0) / dt), output_cb);
155+
156+
// Run for 1s
157+
int nout = static_cast<int>(std::round((1.0 - 0.0) / dt));
158+
ida.runSimulation(1.0, nout, output_cb);
159+
160+
// Introduce fault to ground and run for 0.1s
132161
fault.setStatus(1);
133162
ida.initializeSimulation(1.0, false);
134-
ida.runSimulation(1.1, std::round((1.1 - 1.0) / dt), output_cb);
163+
nout = static_cast<int>(std::round((1.1 - 1.0) / dt));
164+
ida.runSimulation(1.1, nout, output_cb);
165+
166+
// Clear fault and run until t = 10s.
135167
fault.setStatus(0);
136168
ida.initializeSimulation(1.1, false);
137-
ida.runSimulation(10.0, std::round((10.0 - 1.1) / dt), output_cb);
169+
nout = static_cast<int>(std::round((10.0 - 1.1) / dt));
170+
ida.runSimulation(10.0, nout, output_cb);
138171
double stop = static_cast<double>(clock());
139172

140173
/* Check worst-case error */
@@ -154,12 +187,11 @@ int main()
154187

155188
for (index_type i = 0; i < output.size(); ++i)
156189
{
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]};
190+
OutputData ref(reference_solution[i + 1][0],
191+
reference_solution[i + 1][1],
192+
reference_solution[i + 1][2],
193+
reference_solution[i + 1][4],
194+
reference_solution[i + 1][5]);
163195
OutputData out_data = output[i];
164196

165197
out << out_data << '\n';

0 commit comments

Comments
 (0)