/
advect_1d.cc
326 lines (267 loc) · 10.1 KB
/
advect_1d.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
// ---------------------------------------------------------------------
//
// Copyright (C) 2018 by the deal.II authors
//
// This file is part of the deal.II library.
//
// The deal.II library is free software; you can use it, redistribute
// it, and/or modify it under the terms of the GNU Lesser General
// Public License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// The full text of the license can be found in the file LICENSE.md at
// the top level directory of deal.II.
//
// ---------------------------------------------------------------------
// similar to matrix_vector_faces_15 and the code in matrix_vector_common but
// for 1D
#include <deal.II/base/function.h>
#include <deal.II/base/quadrature_lib.h>
#include <deal.II/fe/fe_dgq.h>
#include <deal.II/grid/grid_generator.h>
#include <deal.II/grid/grid_tools.h>
#include <deal.II/grid/tria.h>
#include <deal.II/lac/la_parallel_vector.h>
#include <deal.II/matrix_free/fe_evaluation.h>
#include <deal.II/matrix_free/matrix_free.h>
#include <deal.II/matrix_free/operators.h>
#include <deal.II/numerics/vector_tools.h>
#include "../tests.h"
template <int dim,
int fe_degree,
int n_q_points_1d = fe_degree + 1,
typename number = double,
typename VectorType = Vector<number>,
int n_components = 1>
class MatrixFreeAdvectionBasic
{
public:
MatrixFreeAdvectionBasic(const MatrixFree<dim, number> &data,
const bool zero_within_loop = true,
const unsigned int start_vector_component = 0)
: data(data)
, zero_within_loop(zero_within_loop)
, start_vector_component(start_vector_component)
{
for (unsigned int d = 0; d < dim; ++d)
advection[d] = 0.4 + 0.12 * d;
}
void
vmult(VectorType &dst, const VectorType &src) const
{
if (!zero_within_loop)
dst = 0;
data.loop(&MatrixFreeAdvectionBasic::local_apply,
&MatrixFreeAdvectionBasic::local_apply_face,
&MatrixFreeAdvectionBasic::local_apply_boundary_face,
this,
dst,
src,
zero_within_loop,
MatrixFree<dim, number>::DataAccessOnFaces::values,
MatrixFree<dim, number>::DataAccessOnFaces::values);
FEEvaluation<dim, fe_degree, fe_degree + 1, n_components, number> phi(data);
const unsigned int dofs_per_cell = phi.dofs_per_cell;
AlignedVector<VectorizedArray<number>> coefficients(phi.dofs_per_cell);
MatrixFreeOperators::
CellwiseInverseMassMatrix<dim, fe_degree, n_components, number>
inverse(phi);
for (unsigned int cell = 0; cell < data.n_cell_batches(); ++cell)
{
phi.reinit(cell);
phi.read_dof_values(dst);
inverse.fill_inverse_JxW_values(coefficients);
inverse.apply(coefficients,
n_components,
phi.begin_dof_values(),
phi.begin_dof_values());
phi.set_dof_values(dst);
}
}
private:
void
local_apply(const MatrixFree<dim, number> & data,
VectorType & dst,
const VectorType & src,
const std::pair<unsigned int, unsigned int> &cell_range) const
{
FEEvaluation<dim, fe_degree, n_q_points_1d, n_components, number> phi(
data, 0, 0, start_vector_component);
for (unsigned int cell = cell_range.first; cell < cell_range.second; ++cell)
{
phi.reinit(cell);
phi.read_dof_values(src);
phi.evaluate(true, false);
for (unsigned int q = 0; q < phi.n_q_points; ++q)
phi.submit_gradient(advection * phi.get_value(q), q);
phi.integrate(false, true);
phi.distribute_local_to_global(dst);
}
}
void
local_apply_face(
const MatrixFree<dim, number> & data,
VectorType & dst,
const VectorType & src,
const std::pair<unsigned int, unsigned int> &face_range) const
{
FEFaceEvaluation<dim, fe_degree, n_q_points_1d, n_components, number> phi_m(
data, true, 0, 0, start_vector_component);
FEFaceEvaluation<dim, fe_degree, n_q_points_1d, n_components, number> phi_p(
data, false, 0, 0, start_vector_component);
typedef typename FEFaceEvaluation<dim,
fe_degree,
n_q_points_1d,
n_components,
number>::value_type value_type;
for (unsigned int face = face_range.first; face < face_range.second; face++)
{
phi_m.reinit(face);
phi_m.read_dof_values(src);
phi_m.evaluate(true, false);
phi_p.reinit(face);
phi_p.read_dof_values(src);
phi_p.evaluate(true, false);
for (unsigned int q = 0; q < phi_m.n_q_points; ++q)
{
value_type u_minus = phi_m.get_value(q),
u_plus = phi_p.get_value(q);
const VectorizedArray<number> normal_times_advection =
advection * phi_m.get_normal_vector(q);
const value_type flux_times_normal =
make_vectorized_array<number>(0.5) *
((u_minus + u_plus) * normal_times_advection +
std::abs(normal_times_advection) * (u_minus - u_plus));
phi_m.submit_value(-flux_times_normal, q);
phi_p.submit_value(flux_times_normal, q);
}
phi_m.integrate(true, false);
phi_m.distribute_local_to_global(dst);
phi_p.integrate(true, false);
phi_p.distribute_local_to_global(dst);
}
}
void
local_apply_boundary_face(
const MatrixFree<dim, number> & data,
VectorType & dst,
const VectorType & src,
const std::pair<unsigned int, unsigned int> &face_range) const
{
FEFaceEvaluation<dim, fe_degree, n_q_points_1d, n_components, number>
fe_eval(data, true, 0, 0, start_vector_component);
typedef typename FEFaceEvaluation<dim,
fe_degree,
n_q_points_1d,
n_components,
number>::value_type value_type;
for (unsigned int face = face_range.first; face < face_range.second; face++)
{
fe_eval.reinit(face);
fe_eval.read_dof_values(src);
fe_eval.evaluate(true, false);
for (unsigned int q = 0; q < fe_eval.n_q_points; ++q)
{
value_type u_minus = fe_eval.get_value(q);
value_type u_plus = -u_minus;
const VectorizedArray<number> normal_times_advection =
advection * fe_eval.get_normal_vector(q);
const value_type flux_times_normal =
make_vectorized_array<number>(0.5) *
((u_minus + u_plus) * normal_times_advection +
std::abs(normal_times_advection) * (u_minus - u_plus));
fe_eval.submit_value(-flux_times_normal, q);
}
fe_eval.integrate(true, false);
fe_eval.distribute_local_to_global(dst);
}
}
const MatrixFree<dim, number> & data;
const bool zero_within_loop;
const unsigned int start_vector_component;
Tensor<1, dim, VectorizedArray<number>> advection;
};
template <int dim>
class AnalyticFunction : public Function<dim>
{
public:
static_assert(dim == 1, "Only 1D implemented");
AnalyticFunction()
: Function<dim>(1)
{}
virtual double
value(const Point<dim> &p, const unsigned int) const override
{
return std::sin(3 * numbers::PI * p[0] / 0.8);
}
};
template <int dim>
class AnalyticDerivative : public Function<dim>
{
public:
static_assert(dim == 1, "Only 1D implemented");
AnalyticDerivative()
: Function<dim>(1)
{}
virtual double
value(const Point<dim> &p, const unsigned int) const override
{
Tensor<1, dim> advection;
for (unsigned int d = 0; d < dim; ++d)
advection[d] = 0.4 + 0.12 * d;
return -std::cos(3 * numbers::PI * p[0] / 0.8) * advection[0] * 3 *
numbers::PI / 0.8;
}
};
template <int dim, int fe_degree>
void
test(const unsigned int n_refine)
{
Triangulation<dim> tria;
GridGenerator::hyper_cube(tria, 0, 0.8);
tria.refine_global(n_refine);
FE_DGQ<dim> fe(fe_degree);
DoFHandler<dim> dof(tria);
dof.distribute_dofs(fe);
AffineConstraints<double> constraints;
constraints.close();
if (n_refine == 3)
{
deallog << "Testing " << dof.get_fe().get_name();
deallog << std::endl;
}
LinearAlgebra::distributed::Vector<double> in, out;
const QGauss<1> quad(fe_degree + 1);
typename MatrixFree<dim, double>::AdditionalData data;
data.tasks_parallel_scheme = MatrixFree<dim, double>::AdditionalData::none;
data.mapping_update_flags_inner_faces =
(update_gradients | update_JxW_values);
data.mapping_update_flags_boundary_faces =
(update_gradients | update_JxW_values);
MatrixFree<dim, double> mf_data;
mf_data.reinit(dof, constraints, quad, data);
mf_data.initialize_dof_vector(in);
mf_data.initialize_dof_vector(out);
VectorTools::interpolate(dof, AnalyticFunction<dim>(), in);
MatrixFreeAdvectionBasic<dim,
fe_degree,
fe_degree + 1,
double,
LinearAlgebra::distributed::Vector<double>>
mf2(mf_data);
mf2.vmult(out, in);
VectorTools::interpolate(dof, AnalyticDerivative<dim>(), in);
out -= in;
double diff_norm = out.linfty_norm();
deallog << "Norm of difference: " << diff_norm << " " << std::endl;
}
int
main()
{
initlog();
for (unsigned int r = 3; r < 9; ++r)
test<1, 2>(r);
for (unsigned int r = 3; r < 9; ++r)
test<1, 4>(r);
for (unsigned int r = 3; r < 9; ++r)
test<1, 5>(r);
}