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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
use crate::solvers::fem::basis::single_variable::{
linear_basis::LinearBasis, polynomials_1d::FirstDegreePolynomial,
};
use crate::solvers::basis::functions::{Differentiable1D, Function1D};
use crate::solvers::{solver_trait::DiffEquationSolver, matrix_solver, utils, quadrature::gauss_legendre};
use crate::Error;
use ndarray::{Array1, Array2};
#[derive(Default,Debug)]
pub struct DiffussionParamsTimeDependent {
pub mu: f64,
pub b: f64,
pub boundary_conditions: [f64;2],
pub(crate) initial_conditions: Vec<f64>
}
#[derive(Debug)]
pub struct DiffussionSolverTimeDependent {
pub boundary_conditions: [f64; 2],
pub(crate) stiffness_matrix: Array2<f64>,
pub initial_conditions: Vec<f64>,
pub(crate) mass_matrix: Array2<f64>,
pub integration_step: usize,
pub(crate) state: Array1<f64>,
pub mu: f64,
pub b: f64,
}
impl DiffussionSolverTimeDependent {
pub fn new(params: &DiffussionParamsTimeDependent, mesh: Vec<f64>, integration_step: usize) -> Result<Self,Error> {
let initial_conditions = params.initial_conditions.clone();
if initial_conditions.len() != mesh.len() - 2 {
return Err(Error::WrongDims)
}
let mut state = vec![0_f64;mesh.len()];
state[0] = params.boundary_conditions[0];
state[mesh.len() - 1] = params.boundary_conditions[1];
for i in 1..(mesh.len() - 1) {
state[i] = initial_conditions[i-1];
}
let state = Array1::from_vec(state);
let (mass_matrix, stiffness_matrix) = Self::gauss_legendre_integration(
params.mu, params.b, &mesh, integration_step)?;
Ok(Self {
boundary_conditions: params.boundary_conditions,
initial_conditions,
stiffness_matrix,
integration_step,
mass_matrix,
state,
mu: params.mu,
b: params.b,
})
}
fn gauss_legendre_integration(mu: f64, b: f64, mesh: &Vec<f64>, gauss_step: usize) -> Result<(Array2<f64>,Array2<f64>),Error> {
let linear_basis = LinearBasis::new(mesh)?;
let basis_len = linear_basis.basis.len();
let mut mass_matrix = ndarray::Array::from_elem((basis_len, basis_len), 0_f64);
let mut stiffness_matrix = ndarray::Array::from_elem((basis_len, basis_len), 0_f64);
for i in 1..(basis_len - 1) {
let derivative_phi = linear_basis.basis[i].differentiate()?;
let derivative_phi_next = linear_basis.basis[i+1].differentiate()?;
let derivative_phi_prev = linear_basis.basis[i-1].differentiate()?;
let transform_function_prev = FirstDegreePolynomial::transformation_from_m1_p1(
mesh[i-1],
mesh[i],
);
let transform_function_next = FirstDegreePolynomial::transformation_from_m1_p1(
mesh[i],
mesh[i+1],
);
let transform_function_square =
FirstDegreePolynomial::transformation_from_m1_p1(
mesh[i-1],
mesh[i+1],
);
let derivative_t_prev = transform_function_prev.differentiate()?;
let derivative_t_next = transform_function_next.differentiate()?;
let derivative_t_square = transform_function_square.differentiate()?;
let mut integral_prev_approximation_prime = 0_f64;
let mut integral_next_approximation_prime = 0_f64;
let mut integral_square_approximation_prime = 0_f64;
let mut integral_prev_approximation_half = 0_f64;
let mut integral_next_approximation_half = 0_f64;
let mut integral_square_approximation_half = 0_f64;
let mut integral_prev_approximation_mass = 0_f64;
let mut integral_next_approximation_mass = 0_f64;
let mut integral_square_approximation_mass = 0_f64;
for j in 1..gauss_step {
let (theta, w) = gauss_legendre::quad_pair(gauss_step, j)?;
let x = theta.cos();
let translated_point_prev = transform_function_prev.evaluate(x);
let translated_point_next = transform_function_next.evaluate(x);
let translated_point_square = transform_function_square.evaluate(x);
integral_prev_approximation_mass +=
linear_basis.basis[i].evaluate(translated_point_prev) *
linear_basis.basis[i-1].evaluate(translated_point_prev) * derivative_t_prev.evaluate(x) * w;
integral_square_approximation_mass +=
linear_basis.basis[i].evaluate(translated_point_square).powf(2_f64) *
derivative_t_square.evaluate(x) * w;
integral_next_approximation_mass +=
linear_basis.basis[i].evaluate(translated_point_next) *
linear_basis.basis[i+1].evaluate(translated_point_next) * derivative_t_next.evaluate(x) * w;
integral_prev_approximation_prime +=
derivative_phi.evaluate(translated_point_prev) *
derivative_phi_prev.evaluate(translated_point_prev) * derivative_t_prev.evaluate(x) * w;
integral_square_approximation_prime +=
derivative_phi.evaluate(translated_point_square).powf(2_f64) *
derivative_t_square.evaluate(x) * w;
integral_next_approximation_prime +=
derivative_phi.evaluate(translated_point_next) *
derivative_phi_next.evaluate(translated_point_next) * derivative_t_next.evaluate(x) * w;
integral_prev_approximation_half +=
linear_basis.basis[i].evaluate(translated_point_prev) *
derivative_phi_prev.evaluate(translated_point_prev) * derivative_t_prev.evaluate(x) * w;
integral_square_approximation_half +=
linear_basis.basis[i].evaluate(translated_point_square) *
derivative_phi.evaluate(translated_point_square) * derivative_t_square.evaluate(x) * w;
integral_next_approximation_half +=
linear_basis.basis[i].evaluate(translated_point_next) *
derivative_phi_next.evaluate(translated_point_next) * derivative_t_next.evaluate(x) * w;
}
mass_matrix[[i,i-1]] = integral_prev_approximation_mass;
mass_matrix[[i,i]] = integral_square_approximation_mass;
mass_matrix[[i,i+1]] = integral_next_approximation_mass;
stiffness_matrix[[i,i-1]] = - mu * integral_prev_approximation_prime -
b * integral_prev_approximation_half;
stiffness_matrix[[i,i]] = - mu * integral_square_approximation_prime -
b * integral_square_approximation_half;
stiffness_matrix[[i,i+1]] = - mu * integral_next_approximation_prime -
b * integral_next_approximation_half;
}
mass_matrix[[0,0]] = 1_f64;
mass_matrix[[basis_len-1,basis_len-1]] = 1_f64;
stiffness_matrix[[0,0]] = 1_f64;
stiffness_matrix[[basis_len-1,basis_len-1]] = 1_f64;
Ok((mass_matrix,stiffness_matrix))
}
}
impl DiffEquationSolver for DiffussionSolverTimeDependent {
fn solve(&mut self, time_step: f64) -> Result<Vec<f64>, Error> {
let b_first_part = utils::tridiagonal_matrix_vector_multiplication(
&self.stiffness_matrix, &self.state, time_step)?;
let b_second_part = utils::tridiagonal_matrix_vector_multiplication(
&self.mass_matrix, &self.state, 1_f64)?;
let b = utils::add(
&b_first_part,
&b_second_part)?;
let mut res = matrix_solver::solve_by_thomas(&self.mass_matrix, &b)?;
res[0] = self.boundary_conditions[0];
res[b.len()-1] = self.boundary_conditions[1];
self.state = Array1::from_vec(res.clone());
Ok(res)
}
}
#[cfg(test)]
mod tests {
use crate::solvers::{solver_trait::DiffEquationSolver, diffusion_solver::DiffussionParams};
use super::DiffussionSolverTimeDependent;
#[test]
fn test_matrix_and_vector_values_3p() {
let conditions = DiffussionParams::time_dependent()
.b(1_f64)
.mu(1_f64)
.boundary_conditions(0_f64, 1_f64)
.initial_conditions(vec![0_f64;1]).build();
let dif_solver = DiffussionSolverTimeDependent::new(
&conditions,
vec![0_f64,0.5,1_f64],
150)
.unwrap();
println!("{:?}",dif_solver.stiffness_matrix);
assert!(dif_solver.mass_matrix[[0,0]] == 1_f64);
assert!(dif_solver.mass_matrix[[1,0]] >= 0.08 && dif_solver.mass_matrix[[1,0]] <= 0.09);
assert!(dif_solver.mass_matrix[[1,1]] >= 0.3 && dif_solver.mass_matrix[[1,1]] <= 0.35);
assert!(dif_solver.mass_matrix[[1,2]] >= 0.08 && dif_solver.mass_matrix[[1,2]] <= 0.09);
assert!(dif_solver.mass_matrix[[2,2]] == 1_f64);
assert!(dif_solver.stiffness_matrix[[0,0]] == 1_f64);
assert!(dif_solver.stiffness_matrix[[1,0]] >= 2.4 && dif_solver.stiffness_matrix[[1,0]] <= 2.6);
assert!(dif_solver.stiffness_matrix[[1,1]] >= -4.1 && dif_solver.stiffness_matrix[[1,1]] <= -3.9);
assert!(dif_solver.stiffness_matrix[[1,2]] >= 1.4 && dif_solver.stiffness_matrix[[1,2]] <= 1.6);
assert!(dif_solver.stiffness_matrix[[2,2]] == 1_f64);
}
#[test]
fn test_matrix_solved_3p() {
let conditions = DiffussionParams::time_dependent()
.b(1_f64)
.mu(1_f64)
.boundary_conditions(1_f64, 0_f64)
.initial_conditions(vec![15_f64;1]);
let mut dif_solver = DiffussionSolverTimeDependent::new(
&conditions.build(),
vec![0_f64,0.5,1_f64],
150)
.unwrap();
for _i in 0..1000 {
dif_solver.solve(0.01).unwrap();
}
assert!(dif_solver.state[0] == 1_f64);
assert!(dif_solver.state[2] == 0_f64);
assert!(dif_solver.state[1] <= 0.65 && dif_solver.state[1] >= 0.55);
}
}