-
Notifications
You must be signed in to change notification settings - Fork 708
/
precondition_relaxation_01.cc
365 lines (278 loc) · 10.4 KB
/
precondition_relaxation_01.cc
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// ------------------------------------------------------------------------
//
// SPDX-License-Identifier: LGPL-2.1-or-later
// Copyright (C) 2022 - 2023 by the deal.II authors
//
// This file is part of the deal.II library.
//
// Part of the source code is dual licensed under Apache-2.0 WITH
// LLVM-exception OR LGPL-2.1-or-later. Detailed license information
// governing the source code and code contributions can be found in
// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
//
// ------------------------------------------------------------------------
// Test PreconditionRelaxation for different Jacobi preconditioners.
#include <deal.II/base/mpi.h>
#include <deal.II/base/quadrature_lib.h>
#include <deal.II/dofs/dof_handler.h>
#include <deal.II/dofs/dof_tools.h>
#include <deal.II/fe/fe_q.h>
#include <deal.II/fe/mapping_q1.h>
#include <deal.II/grid/grid_generator.h>
#include <deal.II/lac/dynamic_sparsity_pattern.h>
#include <deal.II/lac/precondition.h>
#include <deal.II/lac/sparse_matrix.h>
#include <deal.II/lac/vector.h>
#include <deal.II/numerics/matrix_tools.h>
#include "../tests.h"
template <typename VectorType>
class MyDiagonalMatrix
{
public:
void
vmult(VectorType &dst, const VectorType &src) const
{
diagonal_matrix.vmult(dst, src);
}
void
Tvmult(VectorType &dst, const VectorType &src) const
{
diagonal_matrix.vmult(dst, src);
}
VectorType &
get_vector()
{
return diagonal_matrix.get_vector();
}
private:
DiagonalMatrix<VectorType> diagonal_matrix;
};
template <typename VectorType>
class MyDiagonalMatrixWithPreAndPost
{
public:
void
vmult(VectorType &dst,
const VectorType &src,
const std::function<void(const unsigned int, const unsigned int)>
&operation_before_matrix_vector_product = {},
const std::function<void(const unsigned int, const unsigned int)>
&operation_after_matrix_vector_product = {}) const
{
if (operation_before_matrix_vector_product)
operation_before_matrix_vector_product(0, src.size());
if (operation_before_matrix_vector_product)
diagonal_matrix.vmult_add(dst, src);
else
diagonal_matrix.vmult(dst, src);
if (operation_after_matrix_vector_product)
operation_after_matrix_vector_product(0, src.size());
}
VectorType &
get_vector()
{
return diagonal_matrix.get_vector();
}
private:
DiagonalMatrix<VectorType> diagonal_matrix;
};
template <typename SparseMatrixType>
class MySparseMatrix : public Subscriptor
{
public:
MySparseMatrix(const SparseMatrixType &sparse_matrix)
: sparse_matrix(sparse_matrix)
{}
template <typename VectorType>
void
vmult(VectorType &dst, const VectorType &src) const
{
sparse_matrix.vmult(dst, src);
}
template <typename VectorType>
void
vmult(VectorType &dst,
const VectorType &src,
const std::function<void(const unsigned int, const unsigned int)>
&operation_before_matrix_vector_product,
const std::function<void(const unsigned int, const unsigned int)>
&operation_after_matrix_vector_product) const
{
operation_before_matrix_vector_product(0, src.size());
sparse_matrix.vmult(dst, src);
operation_after_matrix_vector_product(0, src.size());
}
private:
const SparseMatrixType &sparse_matrix;
};
template <typename PreconditionerType, typename VectorType>
std::tuple<double, double, double, double>
test(const PreconditionerType &preconditioner,
const VectorType &src,
const bool test_transposed = true)
{
VectorType dst;
dst.reinit(src);
dst = 1.0;
preconditioner.vmult(dst, src);
const double norm_0 = dst.l2_norm();
if (test_transposed)
{
dst = 1.0;
preconditioner.Tvmult(dst, src);
}
const double norm_2 = dst.l2_norm();
dst = 1.0;
preconditioner.step(dst, src);
const double norm_1 = dst.l2_norm();
if (test_transposed)
{
dst = 1.0;
preconditioner.Tstep(dst, src);
}
const double norm_3 = dst.l2_norm();
return std::tuple<double, double, double, double>{norm_0,
norm_1,
norm_2,
norm_3};
}
int
main()
{
initlog();
using Number = double;
using VectorType = Vector<Number>;
using MatrixType = SparseMatrix<Number>;
const unsigned int dim = 2;
const unsigned int degree = 1;
Triangulation<dim> tria;
GridGenerator::hyper_cube(tria);
tria.refine_global(3);
FE_Q<dim> fe(degree);
QGauss<dim> quad(degree + 1);
MappingQ1<dim> mapping;
DoFHandler<dim> dof_handler(tria);
dof_handler.distribute_dofs(fe);
MatrixType system_matrix;
DynamicSparsityPattern dsp(dof_handler.n_dofs());
DoFTools::make_sparsity_pattern(dof_handler, dsp);
SparsityPattern sparsity_pattern;
sparsity_pattern.copy_from(dsp);
system_matrix.reinit(sparsity_pattern);
MatrixCreator::create_laplace_matrix(mapping,
dof_handler,
quad,
system_matrix);
VectorType diagonal(dof_handler.n_dofs());
VectorType src(dof_handler.n_dofs());
for (const auto &entry : system_matrix)
if (entry.row() == entry.column())
diagonal[entry.row()] = 1.0 / entry.value();
src = 1.0;
std::vector<double> relaxations{0.0, 1.0, 0.9, 1.1};
std::vector<unsigned int> n_iterationss{1, 2, 3};
for (const auto relaxation : relaxations)
for (const auto n_iterations : n_iterationss)
{
std::vector<std::tuple<double, double, double, double>> results;
{
// 0) Test PreconditionJacobi
PreconditionJacobi<MatrixType> preconditioner;
PreconditionJacobi<MatrixType>::AdditionalData ad;
ad.relaxation = relaxation;
ad.n_iterations = n_iterations;
preconditioner.initialize(system_matrix, ad);
results.emplace_back(test(preconditioner, src));
}
{
// 1) Test PreconditionRelaxation + DiagonalMatrix and matrix with
// pre/post
using PreconditionerType = DiagonalMatrix<VectorType>;
using MyMatrixType = MySparseMatrix<MatrixType>;
MyMatrixType my_system_matrix(system_matrix);
PreconditionRelaxation<MyMatrixType, PreconditionerType>
preconditioner;
PreconditionRelaxation<MyMatrixType,
PreconditionerType>::AdditionalData ad;
ad.relaxation = relaxation;
ad.n_iterations = n_iterations;
ad.preconditioner = std::make_shared<PreconditionerType>();
ad.preconditioner->get_vector() = diagonal;
preconditioner.initialize(my_system_matrix, ad);
results.emplace_back(test(preconditioner, src, false));
}
{
// 2) Test PreconditionRelaxation + DiagonalMatrix and matrix without
// pre/post
using PreconditionerType = DiagonalMatrix<VectorType>;
PreconditionRelaxation<MatrixType, PreconditionerType> preconditioner;
PreconditionRelaxation<MatrixType, PreconditionerType>::AdditionalData
ad;
ad.relaxation = relaxation;
ad.n_iterations = n_iterations;
ad.preconditioner = std::make_shared<PreconditionerType>();
ad.preconditioner->get_vector() = diagonal;
preconditioner.initialize(system_matrix, ad);
results.emplace_back(test(preconditioner, src));
}
{
// 3) Test PreconditionRelaxation + arbitrary preconditioner and
// matrix without pre/post
using PreconditionerType = MyDiagonalMatrix<VectorType>;
PreconditionRelaxation<MatrixType, PreconditionerType> preconditioner;
PreconditionRelaxation<MatrixType, PreconditionerType>::AdditionalData
ad;
ad.relaxation = relaxation;
ad.n_iterations = n_iterations;
ad.preconditioner = std::make_shared<PreconditionerType>();
ad.preconditioner->get_vector() = diagonal;
preconditioner.initialize(system_matrix, ad);
results.emplace_back(test(preconditioner, src));
}
{
// 4) Test PreconditionRelaxation + preconditioner with pre/post and
// matrix without pre/post
using PreconditionerType = MyDiagonalMatrixWithPreAndPost<VectorType>;
PreconditionRelaxation<MatrixType, PreconditionerType> preconditioner;
PreconditionRelaxation<MatrixType, PreconditionerType>::AdditionalData
ad;
ad.relaxation = relaxation;
ad.n_iterations = n_iterations;
ad.preconditioner = std::make_shared<PreconditionerType>();
ad.preconditioner->get_vector() = diagonal;
preconditioner.initialize(system_matrix, ad);
results.emplace_back(test(preconditioner, src, false));
}
{
// 5) Test PreconditionRelaxation + preconditioner with pre/post and
// matrix with pre/post
using PreconditionerType = MyDiagonalMatrixWithPreAndPost<VectorType>;
using MyMatrixType = MySparseMatrix<MatrixType>;
MyMatrixType my_system_matrix(system_matrix);
PreconditionRelaxation<MyMatrixType, PreconditionerType>
preconditioner;
PreconditionRelaxation<MyMatrixType,
PreconditionerType>::AdditionalData ad;
ad.relaxation = relaxation;
ad.n_iterations = n_iterations;
ad.preconditioner = std::make_shared<PreconditionerType>();
ad.preconditioner->get_vector() = diagonal;
preconditioner.initialize(my_system_matrix, ad);
results.emplace_back(test(preconditioner, src, false));
}
if (std::all_of(results.begin(), results.end(), [&](const auto &a) {
if (std::abs(std::get<0>(a) - std::get<0>(results[0])) > 1e-6)
return false;
if (std::abs(std::get<1>(a) - std::get<1>(results[0])) > 1e-6)
return false;
if (std::abs(std::get<2>(a) - std::get<2>(results[0])) > 1e-6)
return false;
if (std::abs(std::get<3>(a) - std::get<3>(results[0])) > 1e-6)
return false;
return true;
}))
deallog << "OK!" << std::endl;
else
deallog << "ERROR!" << std::endl;
}
}