-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab11.cpp
More file actions
197 lines (154 loc) · 5.62 KB
/
Copy pathLab11.cpp
File metadata and controls
197 lines (154 loc) · 5.62 KB
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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <vector>
using Real = long double;
using Vector = std::vector<Real>;
using Matrix = std::vector<Vector>;
constexpr Real PI = std::numbers::pi_v<Real>;
constexpr Real T_MAX = 1.0L;
constexpr Real D = 1.0L;
constexpr Real X_A = 0.0L;
constexpr Real X_B = 1.0L;
constexpr Real LAMBDA_EXPLICIT = 0.4L;
constexpr Real LAMBDA_IMPLICIT = 1.0L;
void thomas_algorithm(Vector d, const Vector &l, const Vector &u, Vector &b, Vector &x);
void lu_decomposition(Matrix &M);
void solve_lu_equation(const Matrix &M, Vector &b);
Real analytical_solution(const Real x, const Real t) {
return (1.0L - std::exp(-PI * PI * D * t)) * std::sin(PI * x);
}
Real source_term(const Real dt, const Real h, const int i) {
const Real x_i = X_A + i * h;
return dt * D * PI * PI * std::sin(PI * x_i);
}
Matrix explicit_method(const Real h) {
const Real dt = LAMBDA_EXPLICIT * h * h / D;
const int x_num = static_cast<int>(std::round((X_B - X_A) / h)) + 1;
const int t_num = static_cast<int>(std::round(T_MAX / dt)) + 1;
Matrix u(t_num, Vector(x_num, 0.0L));
for (int k = 0; k < t_num - 1; k++)
for (int i = 1; i < x_num - 1; i++)
u[k + 1][i] =
LAMBDA_EXPLICIT * u[k][i - 1] +
(1.0L - 2.0L * LAMBDA_EXPLICIT) * u[k][i] +
LAMBDA_EXPLICIT * u[k][i + 1] +
source_term(dt, h, i);
return u;
}
Matrix implicit_method_thomas(const Real h) {
const Real dt = LAMBDA_IMPLICIT * h * h / D;
constexpr Real diagonal_value = -(1.0L + 2.0L * LAMBDA_IMPLICIT);
const int x_num = static_cast<int>(std::round((X_B - X_A) / h)) + 1;
const int t_num = static_cast<int>(std::round(T_MAX / dt)) + 1;
Matrix u(t_num, Vector(x_num, 0.0L));
Vector diagonal(x_num, diagonal_value);
diagonal[0] = 1.0L;
diagonal[x_num - 1] = 1.0L;
Vector lower(x_num - 1, LAMBDA_IMPLICIT);
lower[x_num - 2] = 0.0L;
Vector upper(x_num - 1, LAMBDA_IMPLICIT);
upper[0] = 0.0L;
for (int t = 1; t < t_num; t++) {
Vector b(x_num), x(x_num);
b[0] = 0.0L;
b[x_num - 1] = 0.0L;
for (int i = 1; i < x_num - 1; ++i) b[i] = -u[t - 1][i] - source_term(dt, h, i);
thomas_algorithm(diagonal, lower, upper, b, x);
for (int i = 0; i < x_num; ++i) u[t][i] = x[i];
}
return u;
}
Matrix implicit_method_lu(const Real h) {
const Real dt = LAMBDA_IMPLICIT * h * h / D;
constexpr Real diagonal_value = -(1.0L + 2.0L * LAMBDA_IMPLICIT);
const int x_num = static_cast<int>(std::round((X_B - X_A) / h)) + 1;
const int t_num = static_cast<int>(std::round(T_MAX / dt)) + 1;
Matrix u(t_num, Vector(x_num, 0.0L));
Matrix A(x_num, Vector(x_num, 0.0L));
Vector b(x_num);
for (int i = 0; i < x_num; ++i) {
A[i][i] = diagonal_value;
if (i > 0) A[i][i - 1] = LAMBDA_IMPLICIT;
if (i < x_num - 1) A[i][i + 1] = LAMBDA_IMPLICIT;
}
A[0][0] = 1.0L;
A[0][1] = 0.0L;
A[x_num - 1][x_num - 1] = 1.0L;
A[x_num - 1][x_num - 2] = 0.0L;
lu_decomposition(A);
for (int t = 1; t < t_num; t++) {
b[0] = 0.0L;
b[x_num - 1] = 0.0L;
for (int i = 1; i < x_num - 1; i++) b[i] = -u[t - 1][i] - source_term(dt, h, i);
solve_lu_equation(A, b);
for (int i = 0; i < x_num; i++) u[t][i] = b[i];
}
return u;
}
int main() {
constexpr int precision = 16;
constexpr Real h = 0.05L;
constexpr Real dt = LAMBDA_EXPLICIT * h * h / D;
std::cout << dt << std::endl;
Vector t_values = {0.001L, 0.01L, 0.1L, 1.0L};
constexpr int x_num = static_cast<int>((X_B - X_A) / h) + 1;
std::ofstream file("../data/Lab11/explicit_values.txt");
file << std::scientific << std::setprecision(precision);
// for (int i = 0; i <= 10000; i++) {
// const Real x_i = X_A + i * 0.0001;
// file << x_i;
// for (const Real t: t_values) file << "\t" << analytical_solution(x_i, t);
// file << std::endl;
// }
Matrix u = explicit_method(h);
for (int i = 0; i < x_num; i++) {
const Real x_i = X_A + i * h;
file << x_i;
for (const Real t: t_values) {
const int t_index = static_cast<int>(std::round(t / dt));
std::cout << t_index << std::endl;
file << "\t" << u[t_index][i];
}
file << std::endl;
}
return 0;
}
void thomas_algorithm(Vector d, const Vector &l, const Vector &u, Vector &b, Vector &x) {
const int n = static_cast<int>(d.size());
if (n == 0) return;
for (int i = 1; i < n; i++) {
const Real m = l[i - 1] / d[i - 1];
d[i] = d[i] - m * u[i - 1];
b[i] = b[i] - m * b[i - 1];
}
x[n - 1] = b[n - 1] / d[n - 1];
for (int i = n - 2; i >= 0; i--) x[i] = (b[i] - u[i] * x[i + 1]) / d[i];
}
void lu_decomposition(Matrix &M) {
const int n = static_cast<int>(M.size());
if (n == 0) return;
for (int i = 0; i < n - 1; i++) {
for (int k = i + 1; k < n; k++) {
const Real quotient = M[k][i] / M[i][i];
M[k][i] = quotient;
for (int j = i + 1; j < n; j++)
M[k][j] -= M[i][j] * quotient;
}
}
}
void solve_lu_equation(const Matrix &M, Vector &b) {
const int n = static_cast<int>(b.size());
Real y[n];
for (int i = 0; i < n; i++) {
Real sum = 0.0;
for (int j = 0; j < i; j++) sum += M[i][j] * y[j];
y[i] = b[i] - sum;
}
for (int i = n - 1; i >= 0; i--) {
Real sum = 0.0;
for (int j = i + 1; j < n; j++) sum += M[i][j] * b[j];
b[i] = (y[i] - sum) / M[i][i];
}
}