-
Notifications
You must be signed in to change notification settings - Fork 712
/
matrix_vector_stokes_noflux.cc
343 lines (277 loc) · 11 KB
/
matrix_vector_stokes_noflux.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
// ------------------------------------------------------------------------
//
// SPDX-License-Identifier: LGPL-2.1-or-later
// Copyright (C) 2012 - 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.
//
// ------------------------------------------------------------------------
// this function tests the correctness of the implementation of matrix free
// matrix-vector products by comparing with the result of deal.II sparse
// matrix. The mesh uses a hypershell mesh with hanging nodes and constraints
// between the vector components in the form of no-normal flux constraints on
// the Stokes equations.
#include <deal.II/base/utilities.h>
#include <deal.II/dofs/dof_handler.h>
#include <deal.II/dofs/dof_renumbering.h>
#include <deal.II/dofs/dof_tools.h>
#include <deal.II/fe/fe_q.h>
#include <deal.II/fe/fe_system.h>
#include <deal.II/fe/fe_values.h>
#include <deal.II/fe/mapping_q.h>
#include <deal.II/grid/grid_generator.h>
#include <deal.II/grid/manifold_lib.h>
#include <deal.II/grid/tria.h>
#include <deal.II/lac/affine_constraints.h>
#include <deal.II/lac/block_sparse_matrix.h>
#include <deal.II/lac/block_sparsity_pattern.h>
#include <deal.II/lac/block_vector.h>
#include <deal.II/matrix_free/fe_evaluation.h>
#include <deal.II/matrix_free/matrix_free.h>
#include <deal.II/numerics/vector_tools.h>
#include <complex>
#include <iostream>
#include "../tests.h"
template <int dim, int degree_p, typename VectorType>
class MatrixFreeTest
{
public:
using CellIterator = typename DoFHandler<dim>::active_cell_iterator;
using Number = double;
MatrixFreeTest(const MatrixFree<dim, Number> &data_in)
: data(data_in){};
void
local_apply(const MatrixFree<dim, Number> &data,
VectorType &dst,
const VectorType &src,
const std::pair<unsigned int, unsigned int> &cell_range) const
{
using vector_t = VectorizedArray<Number>;
FEEvaluation<dim, degree_p + 1, degree_p + 2, dim, Number> velocity(data,
0);
FEEvaluation<dim, degree_p, degree_p + 2, 1, Number> pressure(data, 1);
for (unsigned int cell = cell_range.first; cell < cell_range.second; ++cell)
{
velocity.reinit(cell);
velocity.read_dof_values(src.block(0));
velocity.evaluate(EvaluationFlags::gradients);
pressure.reinit(cell);
pressure.read_dof_values(src.block(1));
pressure.evaluate(EvaluationFlags::values);
for (unsigned int q = 0; q < velocity.n_q_points; ++q)
{
SymmetricTensor<2, dim, vector_t> sym_grad_u =
velocity.get_symmetric_gradient(q);
vector_t pres = pressure.get_value(q);
vector_t div = -trace(sym_grad_u);
pressure.submit_value(div, q);
// subtract p * I
for (unsigned int d = 0; d < dim; ++d)
sym_grad_u[d][d] -= pres;
velocity.submit_symmetric_gradient(sym_grad_u, q);
}
velocity.integrate(EvaluationFlags::gradients);
velocity.distribute_local_to_global(dst.block(0));
pressure.integrate(EvaluationFlags::values);
pressure.distribute_local_to_global(dst.block(1));
}
}
void
vmult(VectorType &dst, const VectorType &src) const
{
dst = 0;
data.cell_loop(&MatrixFreeTest<dim, degree_p, VectorType>::local_apply,
this,
dst,
src);
};
private:
const MatrixFree<dim, Number> &data;
};
template <int dim, int fe_degree>
void
test()
{
SphericalManifold<dim> manifold;
Triangulation<dim> triangulation;
GridGenerator::hyper_shell(triangulation, Point<dim>(), 0.5, 1., 96, true);
triangulation.set_all_manifold_ids(0);
triangulation.set_manifold(0, manifold);
triangulation.begin_active()->set_refine_flag();
triangulation.last()->set_refine_flag();
triangulation.execute_coarsening_and_refinement();
triangulation.refine_global(3 - dim);
triangulation.last()->set_refine_flag();
triangulation.execute_coarsening_and_refinement();
MappingQ<dim> mapping(3);
FE_Q<dim> fe_u_scal(fe_degree + 1);
FESystem<dim> fe_u(fe_u_scal, dim);
FE_Q<dim> fe_p(fe_degree);
FESystem<dim> fe(fe_u_scal, dim, fe_p, 1);
DoFHandler<dim> dof_handler_u(triangulation);
DoFHandler<dim> dof_handler_p(triangulation);
DoFHandler<dim> dof_handler(triangulation);
MatrixFree<dim, double> mf_data;
AffineConstraints<double> constraints, constraints_u, constraints_p;
BlockSparsityPattern sparsity_pattern;
BlockSparseMatrix<double> system_matrix;
BlockVector<double> solution;
BlockVector<double> system_rhs;
BlockVector<double> mf_solution;
dof_handler.distribute_dofs(fe);
dof_handler_u.distribute_dofs(fe_u);
dof_handler_p.distribute_dofs(fe_p);
std::vector<unsigned int> stokes_sub_blocks(dim + 1, 0);
stokes_sub_blocks[dim] = 1;
DoFRenumbering::component_wise(dof_handler, stokes_sub_blocks);
const std::set<types::boundary_id> no_normal_flux_boundaries = {0, 1};
DoFTools::make_hanging_node_constraints(dof_handler, constraints);
VectorTools::compute_no_normal_flux_constraints(
dof_handler, 0, no_normal_flux_boundaries, constraints, mapping);
constraints.close();
DoFTools::make_hanging_node_constraints(dof_handler_u, constraints_u);
VectorTools::compute_no_normal_flux_constraints(
dof_handler_u, 0, no_normal_flux_boundaries, constraints_u, mapping);
constraints_u.close();
DoFTools::make_hanging_node_constraints(dof_handler_p, constraints_p);
constraints_p.close();
const std::vector<types::global_dof_index> dofs_per_block =
DoFTools::count_dofs_per_fe_block(dof_handler, stokes_sub_blocks);
// std::cout << "Number of active cells: "
// << triangulation.n_active_cells()
// << std::endl
// << "Number of degrees of freedom: "
// << dof_handler.n_dofs()
// << " (" << n_u << '+' << n_p << ')'
// << std::endl;
{
BlockDynamicSparsityPattern csp(2, 2);
for (unsigned int d = 0; d < 2; ++d)
for (unsigned int e = 0; e < 2; ++e)
csp.block(d, e).reinit(dofs_per_block[d], dofs_per_block[e]);
csp.collect_sizes();
DoFTools::make_sparsity_pattern(dof_handler, csp, constraints, false);
sparsity_pattern.copy_from(csp);
}
system_matrix.reinit(sparsity_pattern);
// this is from step-22
{
QGauss<dim> quadrature_formula(fe_degree + 2);
FEValues<dim> fe_values(mapping,
fe,
quadrature_formula,
update_values | update_JxW_values |
update_gradients);
const unsigned int dofs_per_cell = fe.dofs_per_cell;
const unsigned int n_q_points = quadrature_formula.size();
FullMatrix<double> local_matrix(dofs_per_cell, dofs_per_cell);
std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);
const FEValuesExtractors::Vector velocities(0);
const FEValuesExtractors::Scalar pressure(dim);
std::vector<SymmetricTensor<2, dim>> phi_grads_u(dofs_per_cell);
std::vector<double> div_phi_u(dofs_per_cell);
std::vector<double> phi_p(dofs_per_cell);
typename DoFHandler<dim>::active_cell_iterator cell =
dof_handler.begin_active(),
endc = dof_handler.end();
for (; cell != endc; ++cell)
{
fe_values.reinit(cell);
local_matrix = 0;
for (unsigned int q = 0; q < n_q_points; ++q)
{
for (unsigned int k = 0; k < dofs_per_cell; ++k)
{
phi_grads_u[k] = fe_values[velocities].symmetric_gradient(k, q);
div_phi_u[k] = fe_values[velocities].divergence(k, q);
phi_p[k] = fe_values[pressure].value(k, q);
}
for (unsigned int i = 0; i < dofs_per_cell; ++i)
{
for (unsigned int j = 0; j <= i; ++j)
{
local_matrix(i, j) +=
(phi_grads_u[i] * phi_grads_u[j] -
div_phi_u[i] * phi_p[j] - phi_p[i] * div_phi_u[j]) *
fe_values.JxW(q);
}
}
}
for (unsigned int i = 0; i < dofs_per_cell; ++i)
for (unsigned int j = i + 1; j < dofs_per_cell; ++j)
local_matrix(i, j) = local_matrix(j, i);
cell->get_dof_indices(local_dof_indices);
constraints.distribute_local_to_global(local_matrix,
local_dof_indices,
system_matrix);
}
}
solution.reinit(2);
for (unsigned int d = 0; d < 2; ++d)
solution.block(d).reinit(dofs_per_block[d]);
solution.collect_sizes();
system_rhs.reinit(solution);
mf_solution.reinit(solution);
// fill system_rhs with random numbers
for (unsigned int j = 0; j < system_rhs.block(0).size(); ++j)
if (constraints_u.is_constrained(j) == false)
{
const double val = -1 + 2. * random_value<double>();
system_rhs.block(0)(j) = val;
}
for (unsigned int j = 0; j < system_rhs.block(1).size(); ++j)
if (constraints_p.is_constrained(j) == false)
{
const double val = -1 + 2. * random_value<double>();
system_rhs.block(1)(j) = val;
}
// setup matrix-free structure
{
std::vector<const DoFHandler<dim> *> dofs;
dofs.push_back(&dof_handler_u);
dofs.push_back(&dof_handler_p);
std::vector<const AffineConstraints<double> *> constraints;
constraints.push_back(&constraints_u);
constraints.push_back(&constraints_p);
QGauss<1> quad(fe_degree + 2);
// no parallelism
mf_data.reinit(mapping,
dofs,
constraints,
quad,
typename MatrixFree<dim>::AdditionalData(
MatrixFree<dim>::AdditionalData::none));
}
system_matrix.vmult(solution, system_rhs);
MatrixFreeTest<dim, fe_degree, BlockVector<double>> mf(mf_data);
mf.vmult(mf_solution, system_rhs);
// Verification
mf_solution -= solution;
const double error = mf_solution.linfty_norm();
const double relative = solution.linfty_norm();
deallog << "Verification fe degree " << fe_degree << ": " << error / relative
<< std::endl
<< std::endl;
}
int
main()
{
initlog();
deallog << std::setprecision(3);
{
deallog << std::endl << "Test with doubles" << std::endl << std::endl;
deallog.push("2d");
test<2, 1>();
test<2, 2>();
test<2, 3>();
deallog.pop();
deallog.push("3d");
test<3, 1>();
deallog.pop();
}
}