Skip to content

Commit 51251d0

Browse files
Document example more
1 parent 3f5cb6f commit 51251d0

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

examples/PhasorDynamics/Example1/example1.cpp

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,28 @@ int main()
7171

7272
double dt = 1.0 / 4.0 / 60.0;
7373

74+
// A data structure to keep track of the data we want to
75+
// compare to the reference solution. Rather than keeping
76+
// the entire solution vector at every time step around,
77+
// we instead narrow down exactly what we want to keep.
7478
struct OutputData
7579
{
7680
double ti, Vr, Vi, dw;
7781
};
7882

83+
// A list of output for each time step.
7984
std::vector<OutputData> output;
8085

86+
// A callback which will be called by the integrator after
87+
// each time step. It will be told the time of the current
88+
// state, and it is allowed to access the up-to-date state
89+
// of the components, which are captured by a closure
90+
// due to the [&] notation (every variable that is referenced
91+
// by the callback that is external to the callback itself -
92+
// here output, bus1, and gen - will be considered a
93+
// reference to that variable inside the callback). We select
94+
// the subset of the output we're interested in recording and
95+
// push it into output, which is updated outside the callback.
8196
auto output_cb = [&](double t)
8297
{
8398
std::vector<double>& yval = sys.y();
@@ -88,11 +103,27 @@ int main()
88103
.dw = yval[3]});
89104
};
90105

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

95-
/* Run simulation */
126+
/* Run simulation - making sure to pass the callback to record output*/
96127
double start = static_cast<double>(clock());
97128
// ida.printOutputF(0, 0, buffer);
98129
ida.initializeSimulation(0.0, false);
@@ -107,7 +138,9 @@ int main()
107138

108139
double error_V = 0.0; // error in |V|
109140

110-
// Read through the simulation data storred in the buffer
141+
// Read through the simulation data stored in the buffer.
142+
// Since we captured by reference, output should be available
143+
// for us to read here, outside the callback.
111144
for (size_t i = 0; i < output.size(); i++)
112145
{
113146
OutputData data = output[i];

0 commit comments

Comments
 (0)