| 1 |
// ------------------------------------------------------------------------ |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
// Copyright (C) 2016 - 2024 by the deal.II authors |
| 5 |
// |
| 6 |
// This file is part of the deal.II library. |
| 7 |
// |
| 8 |
// Part of the source code is dual licensed under Apache-2.0 WITH |
| 9 |
// LLVM-exception OR LGPL-2.1-or-later. Detailed license information |
| 10 |
// governing the source code and code contributions can be found in |
| 11 |
// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II. |
| 12 |
// |
| 13 |
// ------------------------------------------------------------------------ |
| 14 |
|
| 15 |
#ifndef dealii_fe_series_h |
| 16 |
#define dealii_fe_series_h |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#include <deal.II/base/config.h> |
| 21 |
|
| 22 |
#include <deal.II/base/enable_observer_pointer.h> |
| 23 |
#include <deal.II/base/exceptions.h> |
| 24 |
#include <deal.II/base/table.h> |
| 25 |
#include <deal.II/base/table_indices.h> |
| 26 |
#include <deal.II/base/tensor.h> |
| 27 |
|
| 28 |
#include <deal.II/hp/fe_collection.h> |
| 29 |
#include <deal.II/hp/q_collection.h> |
| 30 |
|
| 31 |
#include <deal.II/lac/full_matrix.h> |
| 32 |
#include <deal.II/lac/vector.h> |
| 33 |
|
| 34 |
#include <deal.II/numerics/vector_tools_common.h> |
| 35 |
|
| 36 |
#include <memory> |
| 37 |
#include <string> |
| 38 |
#include <vector> |
| 39 |
|
| 40 |
|
| 41 |
DEAL_II_NAMESPACE_OPEN |
| 42 |
|
| 43 |
|
| 44 |
/** |
| 45 |
* @addtogroup fe |
| 46 |
* @{ |
| 47 |
*/ |
| 48 |
|
| 49 |
|
| 50 |
/** |
| 51 |
* This namespace offers functions to calculate expansion series of the |
| 52 |
* solution on the reference element. Coefficients of expansion are often used |
| 53 |
* to estimate local smoothness of the underlying FiniteElement field to decide |
| 54 |
* on h- or p-adaptive refinement strategy. |
| 55 |
*/ |
| 56 |
namespace FESeries |
| 57 |
{ |
| 58 |
/** |
| 59 |
* A class to calculate expansion of a scalar FE (or a single component |
| 60 |
* of vector-valued FE) field into Fourier series on a reference element. |
| 61 |
* The exponential form of the Fourier series is based on completeness |
| 62 |
* and Hermitian orthogonality of the set of exponential |
| 63 |
* functions $ \phi_{\bf k}({\bf x}) = \exp(2 \pi i\, {\bf k} \cdot {\bf x})$. |
| 64 |
* For example in 1d the L2-orthogonality condition reads |
| 65 |
* @f[ |
| 66 |
* \int_0^1 \phi_k(x) \phi_l^\ast(x) dx=\delta_{kl}. |
| 67 |
* @f] |
| 68 |
* Note that $ \phi_{\bf k} = \phi_{-\bf k}^\ast $. |
| 69 |
* |
| 70 |
* The arbitrary scalar FE field on the reference element can be expanded in |
| 71 |
* the complete orthogonal exponential basis as |
| 72 |
* @f[ |
| 73 |
* u({\bf x}) |
| 74 |
* = \sum_{\bf k} c_{\bf k} \phi_{\bf k}({\bf x}). |
| 75 |
* @f] |
| 76 |
* From the orthogonality property of the basis, it follows that |
| 77 |
* @f[ |
| 78 |
* c_{\bf k} = |
| 79 |
* \int_{[0,1]^d} u({\bf x}) \phi_{\bf k}^\ast ({\bf x}) d{\bf x}\,. |
| 80 |
* @f] |
| 81 |
* It is this complex-valued expansion coefficients, that are calculated by |
| 82 |
* this class. Note that $ u({\bf x}) = \sum_i u_i N_i({\bf x})$, |
| 83 |
* where $ N_i({\bf x}) $ are real-valued FiniteElement shape functions. |
| 84 |
* Consequently $ c_{\bf k} \equiv c_{-\bf k}^\ast $ and |
| 85 |
* we only need to compute $ c_{\bf k} $ for positive indices |
| 86 |
* $ \bf k $ . |
| 87 |
*/ |
| 88 |
template <int dim, int spacedim = dim> |
| 89 |
class Fourier : public EnableObserverPointer |
| 90 |
{ |
| 91 |
public: |
| 92 |
using CoefficientType = typename std::complex<double>; |
| 93 |
|
| 94 |
/** |
| 95 |
* Constructor that initializes all required data structures. |
| 96 |
* |
| 97 |
* The @p n_coefficients_per_direction defines the number of coefficients in |
| 98 |
* each direction, @p fe_collection is the hp::FECollection for which |
| 99 |
* expansion will be used and @p q_collection is the hp::QCollection used to |
| 100 |
* integrate the expansion for each FiniteElement in @p fe_collection. |
| 101 |
* |
| 102 |
* As the Fourier expansion can only be performed on scalar fields, this |
| 103 |
* class does not operate on vector-valued finite elements and will |
| 104 |
* therefore throw an assertion. However, each component of a finite element |
| 105 |
* field can be treated as a scalar field, respectively, on which Fourier |
| 106 |
* expansions are again possible. For this purpose, the optional parameter |
| 107 |
* @p component defines which component of each FiniteElement will be used. |
| 108 |
* The default value of @p component only applies to scalar FEs, in which |
| 109 |
* case it indicates that the sole component is to be decomposed. For |
| 110 |
* vector-valued FEs, a non-default value must be explicitly provided. |
| 111 |
*/ |
| 112 |
Fourier(const std::vector<unsigned int> &n_coefficients_per_direction, |
| 113 |
const hp::FECollection<dim, spacedim> &fe_collection, |
| 114 |
const hp::QCollection<dim> &q_collection, |
| 115 |
const unsigned int component = numbers::invalid_unsigned_int); |
| 116 |
|
| 117 |
/** |
| 118 |
* Calculate @p fourier_coefficients of the cell vector field given by |
| 119 |
* @p local_dof_values corresponding to FiniteElement with |
| 120 |
* @p cell_active_fe_index . |
| 121 |
*/ |
| 122 |
template <typename Number> |
| 123 |
void |
| 124 |
calculate(const dealii::Vector<Number> &local_dof_values, |
| 125 |
const unsigned int cell_active_fe_index, |
| 126 |
Table<dim, CoefficientType> &fourier_coefficients); |
| 127 |
|
| 128 |
/** |
| 129 |
* Return the number of coefficients in each coordinate direction for the |
| 130 |
* finite element associated with @p index in the provided hp::FECollection. |
| 131 |
*/ |
| 132 |
unsigned int |
| 133 |
get_n_coefficients_per_direction(const unsigned int index) const; |
| 134 |
|
| 135 |
/** |
| 136 |
* Calculate all transformation matrices to transfer the finite element |
| 137 |
* solution to the series expansion representation. |
| 138 |
* |
| 139 |
* These matrices will be generated on demand by calling calculate() and |
| 140 |
* stored for recurring purposes. Usually, this operation consumes a lot of |
| 141 |
* workload. With this function, all matrices will be calculated in advance. |
| 142 |
* This way, we can separate their costly generation from the actual |
| 143 |
* application. |
| 144 |
*/ |
| 145 |
void |
| 146 |
precalculate_all_transformation_matrices(); |
| 147 |
|
| 148 |
/** |
| 149 |
* Write all transformation matrices of this object to a stream for the |
| 150 |
* purpose of serialization. |
| 151 |
* |
| 152 |
* Since any of its transformation matrices has to be generated only once |
| 153 |
* for a given scenario, it is common practice to determine them in advance |
| 154 |
* calling precalculate_all_transformation_matrices() and keep them via |
| 155 |
* serialization. |
| 156 |
*/ |
| 157 |
template <class Archive> |
| 158 |
void |
| 159 |
save_transformation_matrices(Archive &ar, const unsigned int version); |
| 160 |
|
| 161 |
/** |
| 162 |
* Read all transformation matrices from a stream and recover them for this |
| 163 |
* object. |
| 164 |
*/ |
| 165 |
template <class Archive> |
| 166 |
void |
| 167 |
load_transformation_matrices(Archive &ar, const unsigned int version); |
| 168 |
|
| 169 |
/** |
| 170 |
* Test for equality of two series expansion objects. |
| 171 |
*/ |
| 172 |
bool |
| 173 |
operator==(const Fourier<dim, spacedim> &fourier) const; |
| 174 |
|
| 175 |
private: |
| 176 |
/** |
| 177 |
* Number of coefficients in each direction for each finite element in the |
| 178 |
* registered hp::FECollection. |
| 179 |
*/ |
| 180 |
const std::vector<unsigned int> n_coefficients_per_direction; |
| 181 |
|
| 182 |
/** |
| 183 |
* hp::FECollection for which transformation matrices will be calculated. |
| 184 |
*/ |
| 185 |
ObserverPointer<const hp::FECollection<dim, spacedim>> fe_collection; |
| 186 |
|
| 187 |
/** |
| 188 |
* hp::QCollection used in calculation of transformation matrices. |
| 189 |
*/ |
| 190 |
const hp::QCollection<dim> q_collection; |
| 191 |
|
| 192 |
/** |
| 193 |
* Angular frequencies $ 2 \pi {\bf k} $ . |
| 194 |
*/ |
| 195 |
Table<dim, Tensor<1, dim>> k_vectors; |
| 196 |
|
| 197 |
/** |
| 198 |
* Transformation matrices for each FiniteElement. |
| 199 |
*/ |
| 200 |
std::vector<FullMatrix<CoefficientType>> fourier_transform_matrices; |
| 201 |
|
| 202 |
/** |
| 203 |
* Auxiliary vector to store unrolled coefficients. |
| 204 |
*/ |
| 205 |
std::vector<CoefficientType> unrolled_coefficients; |
| 206 |
|
| 207 |
/** |
| 208 |
* Which component of FiniteElement should be used to calculate the |
| 209 |
* expansion. |
| 210 |
*/ |
| 211 |
const unsigned int component; |
| 212 |
}; |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
/** |
| 217 |
* A class to calculate expansion of a scalar FE (or a single component |
| 218 |
* of vector-valued FE) field into series of Legendre functions on a |
| 219 |
* reference element. |
| 220 |
* |
| 221 |
* Legendre functions are solutions to Legendre's differential equation |
| 222 |
* @f[ |
| 223 |
* \frac{d}{dx}\left([1-x^2] \frac{d}{dx} P_n(x)\right) + |
| 224 |
* n[n+1] P_n(x) = 0 |
| 225 |
* @f] |
| 226 |
* and can be expressed using Rodrigues' formula |
| 227 |
* @f[ |
| 228 |
* P_n(x) = \frac{1}{2^n n!} \frac{d^n}{dx^n}[x^2-1]^n. |
| 229 |
* @f] |
| 230 |
* These polynomials are orthogonal with respect to the $ L^2 $ inner |
| 231 |
* product on the interval $ [-1;1] $ |
| 232 |
* @f[ |
| 233 |
* \int_{-1}^1 P_m(x) P_n(x) = \frac{2}{2n + 1} \delta_{mn} |
| 234 |
* @f] |
| 235 |
* and are complete. |
| 236 |
* A family of $ L^2 $-orthogonal polynomials on $ [0;1] $ can be |
| 237 |
* constructed via |
| 238 |
* @f[ |
| 239 |
* \widetilde P_m = \sqrt{2} P_m(2x-1). |
| 240 |
* @f] |
| 241 |
* |
| 242 |
* |
| 243 |
* An arbitrary scalar FE field on the reference element $ [0;1] $ can be |
| 244 |
* expanded in the complete orthogonal basis as |
| 245 |
* @f[ |
| 246 |
* u(x) |
| 247 |
* = \sum_{m} c_m \widetilde P_{m}(x). |
| 248 |
* @f] |
| 249 |
* From the orthogonality property of the basis, it follows that |
| 250 |
* @f[ |
| 251 |
* c_m = \frac{2m+1}{2} |
| 252 |
* \int_0^1 u(x) \widetilde P_m(x) dx . |
| 253 |
* @f] |
| 254 |
* This class calculates coefficients $ c_{\bf k} $ using |
| 255 |
* $ dim $-dimensional Legendre polynomials constructed from |
| 256 |
* $ \widetilde P_m(x) $ using tensor product rule. |
| 257 |
*/ |
| 258 |
template <int dim, int spacedim = dim> |
| 259 |
class Legendre : public EnableObserverPointer |
| 260 |
{ |
| 261 |
public: |
| 262 |
using CoefficientType = double; |
| 263 |
|
| 264 |
/** |
| 265 |
* Constructor that initializes all required data structures. |
| 266 |
* |
| 267 |
* The @p n_coefficients_per_direction defines the number of coefficients in |
| 268 |
* each direction, @p fe_collection is the hp::FECollection for which |
| 269 |
* expansion will be used and @p q_collection is the hp::QCollection used to |
| 270 |
* integrate the expansion for each FiniteElement in @p fe_collection. |
| 271 |
* |
| 272 |
* As the Legendre expansion can only be performed on scalar fields, this |
| 273 |
* class does not operate on vector-valued finite elements and will |
| 274 |
* therefore throw an assertion. However, each component of a finite element |
| 275 |
* field can be treated as a scalar field, respectively, on which Legendre |
| 276 |
* expansions are again possible. For this purpose, the optional parameter |
| 277 |
* @p component defines which component of each FiniteElement will be used. |
| 278 |
* The default value of @p component only applies to scalar FEs, in which |
| 279 |
* case it indicates that the sole component is to be decomposed. For |
| 280 |
* vector-valued FEs, a non-default value must be explicitly provided. |
| 281 |
*/ |
| 282 |
Legendre(const std::vector<unsigned int> &n_coefficients_per_direction, |
| 283 |
const hp::FECollection<dim, spacedim> &fe_collection, |
| 284 |
const hp::QCollection<dim> &q_collection, |
| 285 |
const unsigned int component = numbers::invalid_unsigned_int); |
| 286 |
|
| 287 |
/** |
| 288 |
* Calculate @p legendre_coefficients of the cell vector field given by |
| 289 |
* @p local_dof_values corresponding to FiniteElement with |
| 290 |
* @p cell_active_fe_index . |
| 291 |
*/ |
| 292 |
template <typename Number> |
| 293 |
void |
| 294 |
calculate(const dealii::Vector<Number> &local_dof_values, |
| 295 |
const unsigned int cell_active_fe_index, |
| 296 |
Table<dim, CoefficientType> &legendre_coefficients); |
| 297 |
|
| 298 |
/** |
| 299 |
* Return the number of coefficients in each coordinate direction for the |
| 300 |
* finite element associated with @p index in the provided hp::FECollection. |
| 301 |
*/ |
| 302 |
unsigned int |
| 303 |
get_n_coefficients_per_direction(const unsigned int index) const; |
| 304 |
|
| 305 |
/** |
| 306 |
* Calculate all transformation matrices to transfer the finite element |
| 307 |
* solution to the series expansion representation. |
| 308 |
* |
| 309 |
* These matrices will be generated on demand by calling calculate() and |
| 310 |
* stored for recurring purposes. Usually, this operation consumes a lot of |
| 311 |
* workload. With this function, all matrices will be calculated in advance. |
| 312 |
* This way, we can separate their costly generation from the actual |
| 313 |
* application. |
| 314 |
*/ |
| 315 |
void |
| 316 |
precalculate_all_transformation_matrices(); |
| 317 |
|
| 318 |
/** |
| 319 |
* Write all transformation matrices of this object to a stream for the |
| 320 |
* purpose of serialization. |
| 321 |
* |
| 322 |
* Since any of its transformation matrices has to be generated only once |
| 323 |
* for a given scenario, it is common practice to determine them in advance |
| 324 |
* calling precalculate_all_transformation_matrices() and keep them via |
| 325 |
* serialization. |
| 326 |
*/ |
| 327 |
template <class Archive> |
| 328 |
void |
| 329 |
save_transformation_matrices(Archive &ar, const unsigned int version); |
| 330 |
|
| 331 |
/** |
| 332 |
* Read all transformation matrices from a stream and recover them for this |
| 333 |
* object. |
| 334 |
*/ |
| 335 |
template <class Archive> |
| 336 |
void |
| 337 |
load_transformation_matrices(Archive &ar, const unsigned int version); |
| 338 |
|
| 339 |
/** |
| 340 |
* Test for equality of two series expansion objects. |
| 341 |
*/ |
| 342 |
bool |
| 343 |
operator==(const Legendre<dim, spacedim> &legendre) const; |
| 344 |
|
| 345 |
private: |
| 346 |
/** |
| 347 |
* Number of coefficients in each direction for each finite element in the |
| 348 |
* registered hp::FECollection. |
| 349 |
*/ |
| 350 |
const std::vector<unsigned int> n_coefficients_per_direction; |
| 351 |
|
| 352 |
/** |
| 353 |
* hp::FECollection for which transformation matrices will be calculated. |
| 354 |
*/ |
| 355 |
ObserverPointer<const hp::FECollection<dim, spacedim>> fe_collection; |
| 356 |
|
| 357 |
/** |
| 358 |
* hp::QCollection used in calculation of transformation matrices. |
| 359 |
*/ |
| 360 |
const hp::QCollection<dim> q_collection; |
| 361 |
|
| 362 |
/** |
| 363 |
* Transformation matrices for each FiniteElement. |
| 364 |
*/ |
| 365 |
std::vector<FullMatrix<CoefficientType>> legendre_transform_matrices; |
| 366 |
|
| 367 |
/** |
| 368 |
* Auxiliary vector to store unrolled coefficients. |
| 369 |
*/ |
| 370 |
std::vector<CoefficientType> unrolled_coefficients; |
| 371 |
|
| 372 |
/** |
| 373 |
* Which component of FiniteElement should be used to calculate the |
| 374 |
* expansion. |
| 375 |
*/ |
| 376 |
const unsigned int component; |
| 377 |
}; |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
/** |
| 382 |
* Calculate the @p norm of subsets of @p coefficients defined by |
| 383 |
* @p predicate being constant. Return the pair of vectors of predicate values |
| 384 |
* and the vector of calculated subset norms. |
| 385 |
* |
| 386 |
* @p predicate should return a pair of <code>bool</code> and <code>unsigned |
| 387 |
* int</code>. The former is a flag whether a given TableIndices should be |
| 388 |
* used in calculation, whereas the latter is the unrolled value of indices |
| 389 |
* according to which the subsets of coefficients will be formed. |
| 390 |
* |
| 391 |
* Only those coefficients will be considered which are larger than |
| 392 |
* @p smallest_abs_coefficient. |
| 393 |
* |
| 394 |
* @note Only the following values of @p norm_type are implemented and make |
| 395 |
* sense in this case: mean, L1_norm, L2_norm, Linfty_norm. The mean norm ca |
| 396 |
* only be applied to real valued coefficients. |
| 397 |
*/ |
| 398 |
template <int dim, typename CoefficientType> |
| 399 |
std::pair<std::vector<unsigned int>, std::vector<double>> |
| 400 |
process_coefficients(const Table<dim, CoefficientType> &coefficients, |
| 401 |
const std::function<std::pair<bool, unsigned int>( |
| 402 |
const TableIndices<dim> &)> &predicate, |
| 403 |
const VectorTools::NormType norm_type, |
| 404 |
const double smallest_abs_coefficient = 1e-10); |
| 405 |
|
| 406 |
/** |
| 407 |
* Linear regression least-square fit of $y = k \, x + b$. |
| 408 |
* The size of the input vectors should be equal and more than 1. |
| 409 |
* The returned pair will contain $k$ (first) and $b$ (second). |
| 410 |
*/ |
| 411 |
std::pair<double, double> |
| 412 |
linear_regression(const std::vector<double> &x, const std::vector<double> &y); |
| 413 |
|
| 414 |
} // namespace FESeries |
| 415 |
|
| 416 |
/** @} */ |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
#ifndef DOXYGEN |
| 421 |
|
| 422 |
// ------------------- inline and template functions ---------------- |
| 423 |
|
| 424 |
namespace internal |
| 425 |
{ |
| 426 |
namespace FESeriesImplementation |
| 427 |
{ |
| 428 |
template <int dim, typename CoefficientType> |
| 429 |
void |
| 430 |
fill_map_index( |
| 431 |
const Table<dim, CoefficientType> &coefficients, |
| 432 |
const TableIndices<dim> &ind, |
| 433 |
const std::function< |
| 434 |
std::pair<bool, unsigned int>(const TableIndices<dim> &)> &predicate, |
| 435 |
std::map<unsigned int, std::vector<CoefficientType>> &pred_to_values) |
| 436 |
{ |
| 437 |
const std::pair<bool, unsigned int> pred_pair = predicate(ind); |
| 438 |
// don't add a value if predicate is false |
| 439 |
if (pred_pair.first == false) |
| 440 |
return; |
| 441 |
|
| 442 |
const unsigned int pred_value = pred_pair.second; |
| 443 |
const CoefficientType &coeff_value = coefficients(ind); |
| 444 |
// If pred_value is not in the pred_to_values map, the element will be |
| 445 |
// created. Otherwise a reference to the existing element is returned. |
| 446 |
pred_to_values[pred_value].push_back(coeff_value); |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
template <typename CoefficientType> |
| 452 |
void |
| 453 |
fill_map( |
| 454 |
const Table<1, CoefficientType> &coefficients, |
| 455 |
const std::function< |
| 456 |
std::pair<bool, unsigned int>(const TableIndices<1> &)> &predicate, |
| 457 |
std::map<unsigned int, std::vector<CoefficientType>> &pred_to_values) |
| 458 |
{ |
| 459 |
for (unsigned int i = 0; i < coefficients.size(0); ++i) |
| 460 |
{ |
| 461 |
const TableIndices<1> ind(i); |
| 462 |
fill_map_index(coefficients, ind, predicate, pred_to_values); |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
template <typename CoefficientType> |
| 469 |
void |
| 470 |
fill_map( |
| 471 |
const Table<2, CoefficientType> &coefficients, |
| 472 |
const std::function< |
| 473 |
std::pair<bool, unsigned int>(const TableIndices<2> &)> &predicate, |
| 474 |
std::map<unsigned int, std::vector<CoefficientType>> &pred_to_values) |
| 475 |
{ |
| 476 |
for (unsigned int i = 0; i < coefficients.size(0); ++i) |
| 477 |
for (unsigned int j = 0; j < coefficients.size(1); ++j) |
| 478 |
{ |
| 479 |
const TableIndices<2> ind(i, j); |
| 480 |
fill_map_index(coefficients, ind, predicate, pred_to_values); |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
template <typename CoefficientType> |
| 487 |
void |
| 488 |
fill_map( |
| 489 |
const Table<3, CoefficientType> &coefficients, |
| 490 |
const std::function< |
| 491 |
std::pair<bool, unsigned int>(const TableIndices<3> &)> &predicate, |
| 492 |
std::map<unsigned int, std::vector<CoefficientType>> &pred_to_values) |
| 493 |
{ |
| 494 |
for (unsigned int i = 0; i < coefficients.size(0); ++i) |
| 495 |
for (unsigned int j = 0; j < coefficients.size(1); ++j) |
| 496 |
for (unsigned int k = 0; k < coefficients.size(2); ++k) |
| 497 |
{ |
| 498 |
const TableIndices<3> ind(i, j, k); |
| 499 |
fill_map_index(coefficients, ind, predicate, pred_to_values); |
| 500 |
} |
| 501 |
} |
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
template <typename Number> |
| 506 |
double |
| 507 |
complex_mean_value(const Number &value) |
| 508 |
{ |
| 509 |
return value; |
| 510 |
} |
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
template <typename Number> |
| 515 |
double |
| 516 |
complex_mean_value(const std::complex<Number> &value) |
| 517 |
{ |
| 518 |
AssertThrow(false, |
| 519 |
ExcMessage( |
| 520 |
"FESeries::process_coefficients() can not be used with " |
| 521 |
"complex-valued coefficients and VectorTools::mean norm.")); |
| 522 |
return std::abs(value); |
| 523 |
} |
| 524 |
} // namespace FESeriesImplementation |
| 525 |
} // namespace internal |
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
template <int dim, typename CoefficientType> |
| 530 |
std::pair<std::vector<unsigned int>, std::vector<double>> |
| 531 |
FESeries::process_coefficients( |
| 532 |
const Table<dim, CoefficientType> &coefficients, |
| 533 |
const std::function<std::pair<bool, unsigned int>(const TableIndices<dim> &)> |
| 534 |
&predicate, |
| 535 |
const VectorTools::NormType norm_type, |
| 536 |
const double smallest_abs_coefficient) |
| 537 |
{ |
| 538 |
Assert(smallest_abs_coefficient >= 0., |
| 539 |
ExcMessage("smallest_abs_coefficient should be non-negative.")); |
| 540 |
|
| 541 |
std::vector<unsigned int> predicate_values; |
| 542 |
std::vector<double> norm_values; |
| 543 |
|
| 544 |
// first, parse all table elements into a map of predicate values and |
| 545 |
// coefficients. We could have stored (predicate values ->TableIndices) map, |
| 546 |
// but its processing would have been much harder later on. |
| 547 |
std::map<unsigned int, std::vector<CoefficientType>> pred_to_values; |
| 548 |
internal::FESeriesImplementation::fill_map(coefficients, |
| 549 |
predicate, |
| 550 |
pred_to_values); |
| 551 |
|
| 552 |
// now go through the map and populate the @p norm_values based on @p norm: |
| 553 |
for (const auto &pred_to_value : pred_to_values) |
| 554 |
{ |
| 555 |
Vector<CoefficientType> values(pred_to_value.second.cbegin(), |
| 556 |
pred_to_value.second.cend()); |
| 557 |
|
| 558 |
double norm_value = 0; |
| 559 |
switch (norm_type) |
| 560 |
{ |
| 561 |
case VectorTools::L2_norm: |
| 562 |
{ |
| 563 |
norm_value = values.l2_norm(); |
| 564 |
break; |
| 565 |
} |
| 566 |
case VectorTools::L1_norm: |
| 567 |
{ |
| 568 |
norm_value = values.l1_norm(); |
| 569 |
break; |
| 570 |
} |
| 571 |
case VectorTools::Linfty_norm: |
| 572 |
{ |
| 573 |
norm_value = values.linfty_norm(); |
| 574 |
break; |
| 575 |
} |
| 576 |
case VectorTools::mean: |
| 577 |
{ |
| 578 |
norm_value = internal::FESeriesImplementation::complex_mean_value( |
| 579 |
values.mean_value()); |
| 580 |
break; |
| 581 |
} |
| 582 |
default: |
| 583 |
AssertThrow(false, ExcNotImplemented()); |
| 584 |
break; |
| 585 |
} |
| 586 |
|
| 587 |
// will use all non-zero coefficients |
| 588 |
if (std::abs(norm_value) > smallest_abs_coefficient) |
| 589 |
{ |
| 590 |
predicate_values.push_back(pred_to_value.first); |
| 591 |
norm_values.push_back(norm_value); |
| 592 |
} |
| 593 |
} |
| 594 |
|
| 595 |
return std::make_pair(predicate_values, norm_values); |
| 596 |
} |
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
template <int dim, int spacedim> |
| 601 |
template <class Archive> |
| 602 |
inline void |
| 603 |
FESeries::Fourier<dim, spacedim>::save_transformation_matrices( |
| 604 |
Archive &ar, |
| 605 |
const unsigned int /*version*/) |
| 606 |
{ |
| 607 |
// Store information about those resources which have been used to generate |
| 608 |
// the transformation matrices. |
| 609 |
// mode vector |
| 610 |
ar &n_coefficients_per_direction; |
| 611 |
|
| 612 |
// finite element collection |
| 613 |
unsigned int size = fe_collection->size(); |
| 614 |
ar &size; |
| 615 |
for (unsigned int i = 0; i < size; ++i) |
| 616 |
ar &(*fe_collection)[i].get_name(); |
| 617 |
|
| 618 |
// quadrature collection |
| 619 |
size = q_collection.size(); |
| 620 |
ar &size; |
| 621 |
for (unsigned int i = 0; i < size; ++i) |
| 622 |
ar &q_collection[i]; |
| 623 |
|
| 624 |
// Store the actual transform matrices. |
| 625 |
ar &fourier_transform_matrices; |
| 626 |
} |
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
template <int dim, int spacedim> |
| 631 |
template <class Archive> |
| 632 |
inline void |
| 633 |
FESeries::Fourier<dim, spacedim>::load_transformation_matrices( |
| 634 |
Archive &ar, |
| 635 |
const unsigned int /*version*/) |
| 636 |
{ |
| 637 |
// Check whether the currently registered resources are compatible with |
| 638 |
// the transformation matrices to load. |
| 639 |
// mode vector |
| 640 |
std::vector<unsigned int> compare_coefficients; |
| 641 |
ar &compare_coefficients; |
| 642 |
Assert(compare_coefficients == n_coefficients_per_direction, |
| 643 |
ExcMessage("A different number of coefficients vector has been used " |
| 644 |
"to generate the transformation matrices you are about " |
| 645 |
"to load!")); |
| 646 |
|
| 647 |
// finite element collection |
| 648 |
unsigned int size; |
| 649 |
ar &size; |
| 650 |
AssertDimension(size, fe_collection->size()); |
| 651 |
std::string name; |
| 652 |
for (unsigned int i = 0; i < size; ++i) |
| 653 |
{ |
| 654 |
ar &name; |
| 655 |
Assert(name.compare((*fe_collection)[i].get_name()) == 0, |
| 656 |
ExcMessage("A different FECollection has been used to generate " |
| 657 |
"the transformation matrices you are about to load!")); |
| 658 |
} |
| 659 |
|
| 660 |
// quadrature collection |
| 661 |
ar &size; |
| 662 |
AssertDimension(size, q_collection.size()); |
| 663 |
Quadrature<dim> quadrature; |
| 664 |
for (unsigned int i = 0; i < size; ++i) |
| 665 |
{ |
| 666 |
ar &quadrature; |
| 667 |
Assert(quadrature == q_collection[i], |
| 668 |
ExcMessage("A different QCollection has been used to generate " |
| 669 |
"the transformation matrices you are about to load!")); |
| 670 |
} |
| 671 |
|
| 672 |
// Restore the transform matrices since all prerequisites are fulfilled. |
| 673 |
ar &fourier_transform_matrices; |
| 674 |
} |
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
template <int dim, int spacedim> |
| 679 |
template <class Archive> |
| 680 |
inline void |
| 681 |
FESeries::Legendre<dim, spacedim>::save_transformation_matrices( |
| 682 |
Archive &ar, |
| 683 |
const unsigned int /*version*/) |
| 684 |
{ |
| 685 |
// Store information about those resources which have been used to generate |
| 686 |
// the transformation matrices. |
| 687 |
// mode vector |
| 688 |
ar &n_coefficients_per_direction; |
| 689 |
|
| 690 |
// finite element collection |
| 691 |
unsigned int size = fe_collection->size(); |
| 692 |
ar &size; |
| 693 |
for (unsigned int i = 0; i < size; ++i) |
| 694 |
ar &(*fe_collection)[i].get_name(); |
| 695 |
|
| 696 |
// quadrature collection |
| 697 |
size = q_collection.size(); |
| 698 |
ar &size; |
| 699 |
for (unsigned int i = 0; i < size; ++i) |
| 700 |
ar &q_collection[i]; |
| 701 |
|
| 702 |
// Store the actual transform matrices. |
| 703 |
ar &legendre_transform_matrices; |
| 704 |
} |
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
template <int dim, int spacedim> |
| 709 |
template <class Archive> |
| 710 |
inline void |
| 711 |
FESeries::Legendre<dim, spacedim>::load_transformation_matrices( |
| 712 |
Archive &ar, |
| 713 |
const unsigned int /*version*/) |
| 714 |
{ |
| 715 |
// Check whether the currently registered resources are compatible with |
| 716 |
// the transformation matrices to load. |
| 717 |
// mode vector |
| 718 |
std::vector<unsigned int> compare_coefficients; |
| 719 |
ar &compare_coefficients; |
| 720 |
Assert(compare_coefficients == n_coefficients_per_direction, |
| 721 |
ExcMessage("A different number of coefficients vector has been used " |
| 722 |
"to generate the transformation matrices you are about " |
| 723 |
"to load!")); |
| 724 |
|
| 725 |
// finite element collection |
| 726 |
unsigned int size; |
| 727 |
ar &size; |
| 728 |
AssertDimension(size, fe_collection->size()); |
| 729 |
std::string name; |
| 730 |
for (unsigned int i = 0; i < size; ++i) |
| 731 |
{ |
| 732 |
ar &name; |
| 733 |
Assert(name.compare((*fe_collection)[i].get_name()) == 0, |
| 734 |
ExcMessage("A different FECollection has been used to generate " |
| 735 |
"the transformation matrices you are about to load!")); |
| 736 |
} |
| 737 |
|
| 738 |
// quadrature collection |
| 739 |
ar &size; |
| 740 |
AssertDimension(size, q_collection.size()); |
| 741 |
Quadrature<dim> quadrature; |
| 742 |
for (unsigned int i = 0; i < size; ++i) |
| 743 |
{ |
| 744 |
ar &quadrature; |
| 745 |
Assert(quadrature == q_collection[i], |
| 746 |
ExcMessage("A different QCollection has been used to generate " |
| 747 |
"the transformation matrices you are about to load!")); |
| 748 |
} |
| 749 |
|
| 750 |
// Restore the transform matrices since all prerequisites are fulfilled. |
| 751 |
ar &legendre_transform_matrices; |
| 752 |
} |
| 753 |
|
| 754 |
|
| 755 |
#endif // DOXYGEN |
| 756 |
|
| 757 |
DEAL_II_NAMESPACE_CLOSE |
| 758 |
|
| 759 |
#endif // dealii_fe_series_h |