-
Notifications
You must be signed in to change notification settings - Fork 0
/
OED_Solver.hpp
146 lines (120 loc) · 5.1 KB
/
OED_Solver.hpp
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
#pragma once
#include <utility>
#include <cmath>
#include <vector>
#include <functional>
#include <fstream>
namespace OED_Sover
{
template <int arg_num>
std::array<double, arg_num> Euler(const std::array<std::function<double(std::array<double, arg_num>)>, arg_num-1>& equations,
const std::array<double, arg_num>& init_values,
double h)
{
int equation_num = arg_num-1;
std::array<double, arg_num> y4;
for(int i = 0;i<equation_num;++i)
y4[i+1] = init_values[i+1] + h*equations[i](init_values);
y4[0] = init_values[0] + h;
return y4;
}
template <int arg_num>
std::array<double, arg_num> Classical_Runge_Kutta(const std::array<std::function<double(std::array<double, arg_num>)>, arg_num-1>& equations,
const std::array<double, arg_num>& init_values,
double h)
{
int equation_num = arg_num-1;
double k[equation_num][4];
std::array<double, arg_num> argument;
for(int num = 0;num<equation_num;++num)
{
k[num][0] = h*equations[num](init_values);
}
argument[0] = init_values[0] + 0.5*h;
for(int i = 1;i<arg_num;++i)
argument[i] = init_values[i] + 0.5*k[i-1][0];
for(int num = 0;num<equation_num;++num)
{
k[num][1] = h*equations[num](argument);
}
for(int i = 1;i<arg_num;++i)
argument[i] = init_values[i] + 0.5*k[i-1][1];
for(int num = 0;num<equation_num;++num)
{
k[num][2] = h*equations[num](argument);
}
argument[0] = init_values[0] + h;
for(int i = 1;i<arg_num;++i)
argument[i] = init_values[i] + k[i-1][2];
for(int num = 0;num<equation_num;++num)
{
k[num][3] = h*equations[num](argument);
}
std::array<double, arg_num> y4;
for(int i = 0;i<equation_num;++i)
y4[i+1] = init_values[i+1] + 1/6.0*k[i][0] + 1/3.0*k[i][1] + 1/3.0*k[i][2] + 1/6.0*k[i][3];
y4[0] = init_values[0] + h;
return y4;
}
template <int arg_num>
std::array<double, arg_num> Runge_Kutta_Fehlberg(const std::array<std::function<double(std::array<double, arg_num>)>, arg_num-1>& equations,
const std::array<double, arg_num>& init_values,
double& h,
double error_ratio)
{
int equation_num = arg_num-1;
double k[equation_num][6];
std::array<double, arg_num> argument;
for(int num = 0;num<equation_num;++num)
{
k[num][0] = h*equations[num](init_values);
}
argument[0] = init_values[0] + 0.25*h;
for(int i = 1;i<arg_num;++i)
argument[i] = init_values[i] + 0.25*k[i-1][0];
for(int num = 0;num<equation_num;++num)
{
k[num][1] = h*equations[num](argument);
}
argument[0] = init_values[0] + 3/8.0*h;
for(int i = 1;i<arg_num;++i)
argument[i] = init_values[i] + 3/32.0*k[i-1][0] + 9/32.0*k[i-1][1];
for(int num = 0;num<equation_num;++num)
{
k[num][2] = h*equations[num](argument);
}
argument[0] = init_values[0] + 12/13.0*h;
for(int i = 1;i<arg_num;++i)
argument[i] = init_values[i] + 1932/2197.0*k[i-1][0] - 7200/2197.0*k[i-1][1] + 7296/2197.0*k[i-1][2];
for(int num = 0;num<equation_num;++num)
{
k[num][3] = h*equations[num](argument);
}
argument[0] = init_values[0] + h;
for(int i = 1;i<arg_num;++i)
argument[i] = init_values[i] + 439/216.0*k[i-1][0] - 8.0*k[i-1][1] + 3680/513.0*k[i-1][2] - 845/4104.0*k[i-1][3];
for(int num = 0;num<equation_num;++num)
{
k[num][4] = h*equations[num](argument);
}
argument[0] = init_values[0] + 0.5*h;
for(int i = 1;i<arg_num;++i)
argument[i] = init_values[i] -8/27.0*k[i-1][0] + 2.0*k[i-1][1] - 3544/2565.0*k[i-1][2] + 1859/4104.0*k[i-1][3] - 11/40.0*k[i-1][4];
for(int num = 0;num<equation_num;++num)
{
k[num][5] = h*equations[num](argument);
}
std::array<double, arg_num> y4;
std::array<double, arg_num> y5;
double temp_min = 1e5;
for(int i = 0;i<equation_num;++i)
{
y4[i+1] = init_values[i+1] + 25/216.0*k[i][0] + 1408/2565.0*k[i][2] + 2197/4104.0*k[i][3] - 0.2*k[i][4];
y5[i+1] = init_values[i+1] + 16/135.0*k[i][0] + 6656/12825.0*k[i][2] + 28561/56430.0*k[i][3] - 9/50.0*k[i][4] + 2/55.0*k[i][5];
temp_min = std::min(temp_min,(std::abs(init_values[i+1])+std::abs(k[i][0]))/std::abs(y5[i+1]-y4[i+1]));
}
y4[0] = init_values[0] + h;
h = 0.9*std::pow(temp_min*error_ratio, 0.2)*h;
return y4;
}
}