deal.II version GIT relicensing-6834-g5b78e6bcdf 2026-10-01 11:20:01+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Loading...
Searching...
No Matches
fe_series_fourier.cc
Go to the documentation of this file.
1// -----------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
4// Copyright (C) 2018 - 2025 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Detailed license information governing the source code and contributions
9// can be found in LICENSE.md and CONTRIBUTING.md at the top level directory.
10//
11// -----------------------------------------------------------------------------
12
13
14
17
19
20#include <iostream>
21
22
24
25namespace
26{
27 void
28 set_k_vectors(Table<1, Tensor<1, 1>> &k_vectors, const unsigned int N)
29 {
30 k_vectors.reinit(TableIndices<1>(N));
31 for (unsigned int i = 0; i < N; ++i)
32 k_vectors(i)[0] = 2. * numbers::PI * i;
33 }
34
35 void
36 set_k_vectors(Table<2, Tensor<1, 2>> &k_vectors, const unsigned int N)
37 {
38 k_vectors.reinit(TableIndices<2>(N, N));
39 for (unsigned int i = 0; i < N; ++i)
40 for (unsigned int j = 0; j < N; ++j)
41 {
42 k_vectors(i, j)[0] = 2. * numbers::PI * i;
43 k_vectors(i, j)[1] = 2. * numbers::PI * j;
44 }
45 }
46
47 void
48 set_k_vectors(Table<3, Tensor<1, 3>> &k_vectors, const unsigned int N)
49 {
50 k_vectors.reinit(TableIndices<3>(N, N, N));
51 for (unsigned int i = 0; i < N; ++i)
52 for (unsigned int j = 0; j < N; ++j)
53 for (unsigned int k = 0; k < N; ++k)
54 {
55 k_vectors(i, j, k)[0] = 2. * numbers::PI * i;
56 k_vectors(i, j, k)[1] = 2. * numbers::PI * j;
57 k_vectors(i, j, k)[2] = 2. * numbers::PI * k;
58 }
59 }
60
61
62
63 template <int dim, int spacedim>
64 std::complex<double>
65 integrate(const FiniteElement<dim, spacedim> &fe,
66 const Quadrature<dim> &quadrature,
67 const Tensor<1, dim> &k_vector,
68 const unsigned int j,
69 const unsigned int component)
70 {
71 std::complex<double> sum = 0;
72 for (unsigned int q = 0; q < quadrature.size(); ++q)
73 {
74 const Point<dim> &x_q = quadrature.point(q);
75 sum += std::exp(std::complex<double>(0, 1) * (k_vector * x_q)) *
76 fe.shape_value_component(j, x_q, component) *
77 quadrature.weight(q);
78 }
79 return sum;
80 }
81
82
83
84 /*
85 * Ensure that the transformation matrix for FiniteElement index
86 * @p fe_index is calculated. If not, calculate it.
87 */
88 template <int spacedim>
89 void
90 ensure_existence(
91 const std::vector<unsigned int> &n_coefficients_per_direction,
92 const hp::FECollection<1, spacedim> &fe_collection,
93 const hp::QCollection<1> &q_collection,
94 const Table<1, Tensor<1, 1>> &k_vectors,
95 const unsigned int fe,
96 const unsigned int component,
97 std::vector<FullMatrix<std::complex<double>>> &fourier_transform_matrices)
98 {
99 AssertIndexRange(fe, fe_collection.size());
100
101 if (fourier_transform_matrices[fe].m() == 0)
102 {
103 fourier_transform_matrices[fe].reinit(
104 n_coefficients_per_direction[fe],
105 fe_collection[fe].n_dofs_per_cell());
106
107 for (unsigned int k = 0; k < n_coefficients_per_direction[fe]; ++k)
108 for (unsigned int j = 0; j < fe_collection[fe].n_dofs_per_cell(); ++j)
109 fourier_transform_matrices[fe](k, j) = integrate(
110 fe_collection[fe], q_collection[fe], k_vectors(k), j, component);
111 }
112 }
113
114 template <int spacedim>
115 void
116 ensure_existence(
117 const std::vector<unsigned int> &n_coefficients_per_direction,
118 const hp::FECollection<2, spacedim> &fe_collection,
119 const hp::QCollection<2> &q_collection,
120 const Table<2, Tensor<1, 2>> &k_vectors,
121 const unsigned int fe,
122 const unsigned int component,
123 std::vector<FullMatrix<std::complex<double>>> &fourier_transform_matrices)
124 {
125 AssertIndexRange(fe, fe_collection.size());
126
127 if (fourier_transform_matrices[fe].m() == 0)
128 {
129 fourier_transform_matrices[fe].reinit(
130 Utilities::fixed_power<2>(n_coefficients_per_direction[fe]),
131 fe_collection[fe].n_dofs_per_cell());
132
133 unsigned int k = 0;
134 for (unsigned int k1 = 0; k1 < n_coefficients_per_direction[fe]; ++k1)
135 for (unsigned int k2 = 0; k2 < n_coefficients_per_direction[fe];
136 ++k2, ++k)
137 for (unsigned int j = 0; j < fe_collection[fe].n_dofs_per_cell();
138 ++j)
139 fourier_transform_matrices[fe](k, j) =
140 integrate(fe_collection[fe],
141 q_collection[fe],
142 k_vectors(k1, k2),
143 j,
144 component);
145 }
146 }
147
148 template <int spacedim>
149 void
150 ensure_existence(
151 const std::vector<unsigned int> &n_coefficients_per_direction,
152 const hp::FECollection<3, spacedim> &fe_collection,
153 const hp::QCollection<3> &q_collection,
154 const Table<3, Tensor<1, 3>> &k_vectors,
155 const unsigned int fe,
156 const unsigned int component,
157 std::vector<FullMatrix<std::complex<double>>> &fourier_transform_matrices)
158 {
159 AssertIndexRange(fe, fe_collection.size());
160
161 if (fourier_transform_matrices[fe].m() == 0)
162 {
163 fourier_transform_matrices[fe].reinit(
164 Utilities::fixed_power<3>(n_coefficients_per_direction[fe]),
165 fe_collection[fe].n_dofs_per_cell());
166
167 unsigned int k = 0;
168 for (unsigned int k1 = 0; k1 < n_coefficients_per_direction[fe]; ++k1)
169 for (unsigned int k2 = 0; k2 < n_coefficients_per_direction[fe]; ++k2)
170 for (unsigned int k3 = 0; k3 < n_coefficients_per_direction[fe];
171 ++k3, ++k)
172 for (unsigned int j = 0; j < fe_collection[fe].n_dofs_per_cell();
173 ++j)
174 fourier_transform_matrices[fe](k, j) =
175 integrate(fe_collection[fe],
176 q_collection[fe],
177 k_vectors(k1, k2, k3),
178 j,
179 component);
180 }
181 }
182} // namespace
183
184
185
186namespace FESeries
187{
188 template <int dim, int spacedim>
190 const std::vector<unsigned int> &n_coefficients_per_direction,
191 const hp::FECollection<dim, spacedim> &fe_collection,
192 const hp::QCollection<dim> &q_collection,
193 const unsigned int component_)
194 : n_coefficients_per_direction(n_coefficients_per_direction)
195 , fe_collection(&fe_collection)
196 , q_collection(q_collection)
197 , fourier_transform_matrices(fe_collection.size())
198 , component(component_ != numbers::invalid_unsigned_int ? component_ : 0)
199 {
202 ExcMessage("All parameters are supposed to have the same size."));
203
204 if (fe_collection[0].n_components() > 1)
205 Assert(
206 component_ != numbers::invalid_unsigned_int,
208 "For vector-valued problems, you need to explicitly specify for "
209 "which vector component you will want to do a Fourier decomposition "
210 "by setting the 'component' argument of this constructor."));
211
212 AssertIndexRange(component, fe_collection[0].n_components());
213
214 const unsigned int max_n_coefficients_per_direction =
215 *std::max_element(n_coefficients_per_direction.cbegin(),
217 set_k_vectors(k_vectors, max_n_coefficients_per_direction);
218
219 // reserve sufficient memory
220 unrolled_coefficients.reserve(k_vectors.n_elements());
221 }
222
223
224
225 template <int dim, int spacedim>
226 bool
228 const Fourier<dim, spacedim> &fourier) const
229 {
230 return (
231 (n_coefficients_per_direction == fourier.n_coefficients_per_direction) &&
232 (*fe_collection == *(fourier.fe_collection)) &&
233 (q_collection == fourier.q_collection) &&
234 (k_vectors == fourier.k_vectors) &&
235 (fourier_transform_matrices == fourier.fourier_transform_matrices) &&
236 (component == fourier.component));
237 }
238
239
240
241 template <int dim, int spacedim>
242 void
244 {
245 Threads::TaskGroup<> task_group;
246 for (unsigned int fe = 0; fe < fe_collection->size(); ++fe)
247 task_group += Threads::new_task([&, fe]() {
248 ensure_existence(n_coefficients_per_direction,
249 *fe_collection,
250 q_collection,
251 k_vectors,
252 fe,
253 component,
254 fourier_transform_matrices);
255 });
256
257 task_group.join_all();
258 }
259
260
261
262 template <int dim, int spacedim>
263 unsigned int
265 const unsigned int index) const
266 {
267 return n_coefficients_per_direction[index];
268 }
269
270
271
272 template <int dim, int spacedim>
273 template <typename Number>
274 void
276 const Vector<Number> &local_dof_values,
277 const unsigned int cell_active_fe_index,
278 Table<dim, CoefficientType> &fourier_coefficients)
279 {
280 for (unsigned int d = 0; d < dim; ++d)
281 AssertDimension(fourier_coefficients.size(d),
282 n_coefficients_per_direction[cell_active_fe_index]);
283
284 ensure_existence(n_coefficients_per_direction,
285 *fe_collection,
286 q_collection,
287 k_vectors,
288 cell_active_fe_index,
289 component,
290 fourier_transform_matrices);
291
292 const FullMatrix<CoefficientType> &matrix =
293 fourier_transform_matrices[cell_active_fe_index];
294
295 unrolled_coefficients.resize(Utilities::fixed_power<dim>(
296 n_coefficients_per_direction[cell_active_fe_index]));
297 std::fill(unrolled_coefficients.begin(),
298 unrolled_coefficients.end(),
299 CoefficientType(0.));
300
301 Assert(unrolled_coefficients.size() == matrix.m(), ExcInternalError());
302
303 Assert(local_dof_values.size() == matrix.n(),
304 ExcDimensionMismatch(local_dof_values.size(), matrix.n()));
305
306 for (unsigned int i = 0; i < unrolled_coefficients.size(); ++i)
307 for (unsigned int j = 0; j < local_dof_values.size(); ++j)
308 unrolled_coefficients[i] += matrix[i][j] * local_dof_values[j];
309
310 fourier_coefficients.fill(unrolled_coefficients.begin());
311 }
312} // namespace FESeries
313
314
315// explicit instantiations
316#include "fe/fe_series_fourier.inst"
317
void calculate(const ::Vector< Number > &local_dof_values, const unsigned int cell_active_fe_index, Table< dim, CoefficientType > &fourier_coefficients)
Table< dim, Tensor< 1, dim > > k_vectors
Definition fe_series.h:194
std::vector< CoefficientType > unrolled_coefficients
Definition fe_series.h:204
const unsigned int component
Definition fe_series.h:210
typename std::complex< double > CoefficientType
Definition fe_series.h:91
unsigned int get_n_coefficients_per_direction(const unsigned int index) const
const std::vector< unsigned int > n_coefficients_per_direction
Definition fe_series.h:179
ObserverPointer< const hp::FECollection< dim, spacedim > > fe_collection
Definition fe_series.h:184
bool operator==(const Fourier< dim, spacedim > &fourier) const
std::vector< FullMatrix< CoefficientType > > fourier_transform_matrices
Definition fe_series.h:199
const hp::QCollection< dim > q_collection
Definition fe_series.h:189
Fourier(const std::vector< unsigned int > &n_coefficients_per_direction, const hp::FECollection< dim, spacedim > &fe_collection, const hp::QCollection< dim > &q_collection, const unsigned int component=numbers::invalid_unsigned_int)
void precalculate_all_transformation_matrices()
virtual double shape_value_component(const unsigned int i, const Point< dim > &p, const unsigned int component) const
Definition point.h:111
const Point< dim > & point(const unsigned int i) const
double weight(const unsigned int i) const
unsigned int size() const
virtual size_type size() const override
unsigned int size() const
Definition collection.h:314
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:38
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:39
#define Assert(cond, exc)
#define AssertDimension(dim1, dim2)
#define AssertIndexRange(index, range)
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
static ::ExceptionBase & ExcMessage(std::string arg1)
Task< RT > new_task(const std::function< RT()> &function)
std::size_t size
Definition mpi.cc:733
constexpr char N
T sum(const T &t, const MPI_Comm mpi_communicator)
constexpr double PI
Definition numbers.h:240
constexpr unsigned int invalid_unsigned_int
Definition types.h:228
::VectorizedArray< Number, width > exp(const ::VectorizedArray< Number, width > &)