-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_chapter_05.cpp
199 lines (149 loc) · 6.96 KB
/
main_chapter_05.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include "MMLBase.h"
#include "interfaces/IODESystem.h"
#include "base/BaseUtils.h"
#include "base/Vector.h"
#include "tools/ConsolePrinter.h"
#include "tools/Visualizer.h"
#include "tools/Serializer.h"
#include "algorithms/ODESystemSolver.h"
#include "algorithms/ODESystemStepCalculators.h"
#include "algorithms/ODESystemSteppers.h"
#include "algorithms/RootFinding.h"
#include "algorithms/Statistics.h"
using namespace MML;
using namespace MML::RootFinding;
class PendulumODE : public IODESystem
{
Real _Length;
public:
PendulumODE(Real length) : _Length(length) {}
int getDim() const override { return 2; }
void derivs(const Real t, const MML::Vector<Real>& x, MML::Vector<Real>& dxdt) const override
{
dxdt[0] = x[1];
dxdt[1] = -9.81 / _Length * sin(x[0]);
}
};
double calculatePendulumPeriod(double length, double initialAngle)
{
const double g = 9.81;
double k = std::sin(initialAngle / 2);
double ellipticIntegral = std::comp_ellint_1(k);
double period = 4 * std::sqrt(length / g) * ellipticIntegral;
return period;
}
Real calcFunctionPeriod(const IRealFunction& func, Real t1, Real t2)
{
int numFoundRoots;
Vector<Real> root_brack_x1(10), root_brack_x2(10);
FindRootBrackets(func, t1, t2, 4000, root_brack_x1, root_brack_x2, numFoundRoots);
Vector<Real> roots(numFoundRoots);
Vector<Real> rootDiffs(numFoundRoots - 1);
for (int i = 0; i < numFoundRoots; i++)
{
roots[i] = FindRootBisection(func, root_brack_x1[i], root_brack_x2[i], 1e-7);
if (i > 0)
rootDiffs[i - 1] = roots[i] - roots[i - 1];
}
return Statistics::Avg(rootDiffs);
}
void Demo_SimplePendulum()
{
ODESystem pendSys(2, [](Real t, const Vector<Real>& x, Vector<Real>& dxdt)
{
Real Length = 1.0;
dxdt[0] = x[1];
dxdt[1] = -9.81 / Length * sin(x[0]);
}
);
Real pendulumLen = 1.0;
PendulumODE sys = PendulumODE(pendulumLen);
Real t1 = 0.0, t2 = 10.0;
int expectNumSteps = 100;
Real minSaveInterval = (t2 - t1) / expectNumSteps;
Real initAngle = 0.5;
Vector<Real> initCond{ initAngle, 0.0 };
ODESystemFixedStepSolver fixedSolver(pendSys, StepCalculators::RK4_Basic);
ODESystemSolution solFixed = fixedSolver.integrate(initCond, t1, t2, expectNumSteps);
ODESystemSolver<RK5_CashKarp_Stepper> adaptSolver(sys);
ODESystemSolution solAdapt = adaptSolver.integrate(initCond, t1, t2, minSaveInterval / 1.21, 1e-06, 0.01);
Vector<Real> x_fixed = solFixed.getTValues();
Vector<Real> y1_fixed = solFixed.getXValues(0);
Vector<Real> y2_fixed = solFixed.getXValues(1);
Vector<Real> x_adapt = solAdapt.getTValues();
Vector<Real> y1_adapt = solAdapt.getXValues(0);
Vector<Real> y2_adapt = solAdapt.getXValues(1);
std::cout << "\n\n**** Runge-Kutta 4th order - fixed stepsize ********** Runge-Kutta 4th order - adaptive stepper ****\n";
std::vector<ColDesc> vecNames{ ColDesc("t", 11, 2, 'F'), ColDesc("angle", 15, 8, 'F'), ColDesc("ang.vel.", 15, 8, 'F'),
ColDesc("t", 22, 2, 'F'), ColDesc("angle", 12, 8, 'F'), ColDesc("ang.vel.", 12, 8, 'F') };
std::vector<Vector<Real>*> vecVals{ &x_fixed, &y1_fixed, &y2_fixed,
&x_adapt, &y1_adapt, &y2_adapt };
VerticalVectorPrinter vvp(vecNames, vecVals);
vvp.Print();
// getting solutions as polynomials
PolynomInterpRealFunc solFixedPolyInterp0 = solFixed.getSolutionAsPolyInterp(0, 3);
PolynomInterpRealFunc solFixedPolyInterp1 = solFixed.getSolutionAsPolyInterp(1, 3);
PolynomInterpRealFunc solAdaptPolyInterp0 = solAdapt.getSolutionAsPolyInterp(0, 3);
PolynomInterpRealFunc solAdaptPolyInterp1 = solAdapt.getSolutionAsPolyInterp(1, 3);
SplineInterpRealFunc solSplineInterp0 = solAdapt.getSolutionAsSplineInterp(0);
Visualizer::VisualizeRealFunction(solAdaptPolyInterp0, "Pendulum - angle in time",
0.0, 10.0, 200, "pendulum_angle.txt");
// shown together
Visualizer::VisualizeMultiRealFunction({ &solAdaptPolyInterp0, &solAdaptPolyInterp1 },
"Pendulum - both variables", 0.0, 10.0, 200,
"pendulum_multi_real_func.txt");
// extracting period T from solutions
Real periodLinear = 2.0 * Constants::PI * sqrt(pendulumLen / 9.81);
std::cout << "Pendulum period approx. linear : " << periodLinear << std::endl;
Real simulPeriodFixed = calcFunctionPeriod(solFixedPolyInterp0, t1, t2);
std::cout << "Pendulum period RK4 fixed step : " << 2 * simulPeriodFixed << std::endl;
Real simulPeriodAdapt = calcFunctionPeriod(solAdaptPolyInterp0, t1, t2);
std::cout << "Pendulum period RK4 adapt.step : " << 2 * simulPeriodAdapt << std::endl;
// calculate exact period for pendulum of length L, and given initial angle phi
Real exactPeriod = calculatePendulumPeriod(pendulumLen, initAngle);
std::cout << "Pendulum period analytic exact : " << exactPeriod << std::endl;
// Comparing periods for different initial angles
Vector<Real> initAnglesDeg{ 1, 2, 5, 10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0, 80.0 };
Vector<Real> initAngles(initAnglesDeg.size()); // convert to radians
for (int i = 0; i < initAngles.size(); i++)
initAngles[i] = initAnglesDeg[i] * Constants::PI / 180.0;
Vector<Real> periods(initAngles.size());
Real periodLin, periodSimulFixed, periodSimulAdapt, periodExact;
std::cout << "\nAngle Linear. Exact Diff % Fix.step.sim Adapt.step.sim" << std::endl;
for (int i = 0; i < initAngles.size(); i++)
{
initCond[0] = initAngles[i];
// calculate period from fixed solution
ODESystemSolution solF = fixedSolver.integrate(initCond, t1, t2, expectNumSteps);
PolynomInterpRealFunc solFInterp = solF.getSolutionAsPolyInterp(0, 3);
periodSimulFixed = calcFunctionPeriod(solFInterp, t1, t2);
// calculate period from adaptive solution
ODESystemSolution solA = adaptSolver.integrate(initCond, t1, t2, minSaveInterval, 1e-06, 0.01);
PolynomInterpRealFunc solAInterp = solA.getSolutionAsPolyInterp(0, 3);
periodSimulAdapt = calcFunctionPeriod(solAInterp, t1, t2);
// analytical formulas
periodLin = 2.0 * Constants::PI * sqrt(pendulumLen / 9.81);
periodExact = calculatePendulumPeriod(pendulumLen, initAngles[i]);
double diffPercent = Abs(periodLin - periodExact) / periodExact * 100;
std::cout << std::setw(2) << initAnglesDeg[i] << " deg: " << periodLin << " "
<< periodExact << " " << std::setw(5) << diffPercent << " " << 2 * periodSimulFixed << " " << 2 * periodSimulAdapt << std::endl;
}
// Comparing number of adaptive steps needed, for given accuracy
Vector<Real> acc{ 1e-2, 1e-3, 1e-4, 1e-5, 1e-6, 1e-7, 1e-8 };
Vector<Real> numSteps(acc.size());
std::cout << "\nAccuracy Num steps OK steps Bad steps" << std::endl;
for (int i = 0; i < acc.size(); i++)
{
ODESystemSolver<RK5_CashKarp_Stepper> adaptSolver(sys);
ODESystemSolution solAdapt = adaptSolver.integrate(initCond, t1, t2, minSaveInterval, acc[i], 0.01);
numSteps[i] = solAdapt.getTotalNumSteps();
std::cout << std::setw(7) << acc[i] << " " << std::setw(3) << numSteps[i] << " "
<< std::setw(3) << solAdapt.getNumStepsOK() << " "
<< std::setw(3) << solAdapt.getNumStepsBad() << std::endl;
}
}
int main()
{
Demo_SimplePendulum();
return 0;
}