@@ -71,13 +71,28 @@ int main()
71
71
72
72
double dt = 1.0 / 4.0 / 60.0 ;
73
73
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.
74
78
struct OutputData
75
79
{
76
80
double ti, Vr, Vi, dw;
77
81
};
78
82
83
+ // A list of output for each time step.
79
84
std::vector<OutputData> output;
80
85
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.
81
96
auto output_cb = [&](double t)
82
97
{
83
98
std::vector<double >& yval = sys.y ();
@@ -88,11 +103,27 @@ int main()
88
103
.dw = yval[3 ]});
89
104
};
90
105
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
+
91
122
/* Set up simulation */
92
123
Ida<double , size_t > ida (&sys);
93
124
ida.configureSimulation ();
94
125
95
- /* Run simulation */
126
+ /* Run simulation - making sure to pass the callback to record output */
96
127
double start = static_cast <double >(clock ());
97
128
// ida.printOutputF(0, 0, buffer);
98
129
ida.initializeSimulation (0.0 , false );
@@ -107,7 +138,9 @@ int main()
107
138
108
139
double error_V = 0.0 ; // error in |V|
109
140
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.
111
144
for (size_t i = 0 ; i < output.size (); i++)
112
145
{
113
146
OutputData data = output[i];
0 commit comments