| 1 |
// ------------------------------------------------------------------------ |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
// Copyright (C) 2000 - 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_tensor_product_polynomials_h |
| 16 |
#define dealii_tensor_product_polynomials_h |
| 17 |
|
| 18 |
|
| 19 |
#include <deal.II/base/config.h> |
| 20 |
|
| 21 |
#include <deal.II/base/exceptions.h> |
| 22 |
#include <deal.II/base/ndarray.h> |
| 23 |
#include <deal.II/base/point.h> |
| 24 |
#include <deal.II/base/polynomial.h> |
| 25 |
#include <deal.II/base/scalar_polynomials_base.h> |
| 26 |
#include <deal.II/base/tensor.h> |
| 27 |
#include <deal.II/base/utilities.h> |
| 28 |
|
| 29 |
#include <vector> |
| 30 |
|
| 31 |
DEAL_II_NAMESPACE_OPEN |
| 32 |
|
| 33 |
// Forward declarations for friends |
| 34 |
// TODO: We may be able to modify these classes so they aren't |
| 35 |
// required to be friends |
| 36 |
template <int dim> |
| 37 |
class TensorProductPolynomialsBubbles; |
| 38 |
template <int dim> |
| 39 |
class TensorProductPolynomialsConst; |
| 40 |
|
| 41 |
/** |
| 42 |
* @addtogroup Polynomials |
| 43 |
* @{ |
| 44 |
*/ |
| 45 |
|
| 46 |
/** |
| 47 |
* Tensor product of given polynomials. |
| 48 |
* |
| 49 |
* Given a vector of <i>n</i> one-dimensional polynomials <i>P<sub>1</sub></i> |
| 50 |
* to <i>P<sub>n</sub></i>, this class generates <i>n<sup>dim</sup></i> |
| 51 |
* polynomials of the form <i>Q<sub>ijk</sub>(x,y,z) = |
| 52 |
* P<sub>i</sub>(x)P<sub>j</sub>(y)P<sub>k</sub>(z)</i>. If the base |
| 53 |
* polynomials are mutually orthogonal on the interval [-1,1] or [0,1], then |
| 54 |
* the tensor product polynomials are orthogonal on [-1,1]<sup>dim</sup> or |
| 55 |
* [0,1]<sup>dim</sup>, respectively. |
| 56 |
* |
| 57 |
* Indexing is as follows: the order of dim-dimensional polynomials is |
| 58 |
* x-coordinates running fastest, then y-coordinate, etc. The first few |
| 59 |
* polynomials are thus <i>P<sub>1</sub>(x)P<sub>1</sub>(y), |
| 60 |
* P<sub>2</sub>(x)P<sub>1</sub>(y), P<sub>3</sub>(x)P<sub>1</sub>(y), ..., |
| 61 |
* P<sub>1</sub>(x)P<sub>2</sub>(y), P<sub>2</sub>(x)P<sub>2</sub>(y), |
| 62 |
* P<sub>3</sub>(x)P<sub>2</sub>(y), ...</i> and likewise in 3d. |
| 63 |
* |
| 64 |
* The output_indices() function prints the ordering of the dim-dimensional |
| 65 |
* polynomials, i.e. for each polynomial in the polynomial space it gives the |
| 66 |
* indices i,j,k of the one-dimensional polynomials in x,y and z direction. |
| 67 |
* The ordering of the dim-dimensional polynomials can be changed by using the |
| 68 |
* set_numbering() function. |
| 69 |
* |
| 70 |
* @tparam PolynomialType A class that satisfies the required interface for computing |
| 71 |
* tensor products. Typical choices for this template argument are |
| 72 |
* Polynomials::Polynomial and Polynomials::PiecewisePolynomial. |
| 73 |
*/ |
| 74 |
template <int dim, typename PolynomialType = Polynomials::Polynomial<double>> |
| 75 |
class TensorProductPolynomials : public ScalarPolynomialsBase<dim> |
| 76 |
{ |
| 77 |
public: |
| 78 |
/** |
| 79 |
* Access to the dimension of this object, for checking and automatic |
| 80 |
* setting of dimension in other classes. |
| 81 |
*/ |
| 82 |
static constexpr unsigned int dimension = dim; |
| 83 |
|
| 84 |
/** |
| 85 |
* Constructor. <tt>pols</tt> is a vector of objects that should be derived |
| 86 |
* or otherwise convertible to one-dimensional polynomial objects of type |
| 87 |
* `PolynomialType` (template argument of class). It will be copied element |
| 88 |
* by element into a protected member variable. |
| 89 |
*/ |
| 90 |
template <class Pol> |
| 91 |
TensorProductPolynomials(const std::vector<Pol> &pols); |
| 92 |
|
| 93 |
/** |
| 94 |
* Print the list of the indices to <tt>out</tt>. |
| 95 |
*/ |
| 96 |
void |
| 97 |
output_indices(std::ostream &out) const; |
| 98 |
|
| 99 |
/** |
| 100 |
* Set the ordering of the polynomials. Requires |
| 101 |
* <tt>renumber.size()==n()</tt>. Stores a copy of <tt>renumber</tt>. |
| 102 |
*/ |
| 103 |
void |
| 104 |
set_numbering(const std::vector<unsigned int> &renumber); |
| 105 |
|
| 106 |
/** |
| 107 |
* Give read access to the renumber vector. |
| 108 |
*/ |
| 109 |
const std::vector<unsigned int> & |
| 110 |
get_numbering() const; |
| 111 |
|
| 112 |
/** |
| 113 |
* Give read access to the inverse renumber vector. |
| 114 |
*/ |
| 115 |
const std::vector<unsigned int> & |
| 116 |
get_numbering_inverse() const; |
| 117 |
|
| 118 |
/** |
| 119 |
* Compute the value and the first and second derivatives of each tensor |
| 120 |
* product polynomial at <tt>unit_point</tt>. |
| 121 |
* |
| 122 |
* The size of the vectors must either be equal 0 or equal n(). In the first |
| 123 |
* case, the function will not compute these values. |
| 124 |
* |
| 125 |
* If you need values or derivatives of all tensor product polynomials then |
| 126 |
* use this function, rather than using any of the compute_value(), |
| 127 |
* compute_grad() or compute_grad_grad() functions, see below, in a loop |
| 128 |
* over all tensor product polynomials. |
| 129 |
*/ |
| 130 |
void |
| 131 |
evaluate(const Point<dim> &unit_point, |
| 132 |
std::vector<double> &values, |
| 133 |
std::vector<Tensor<1, dim>> &grads, |
| 134 |
std::vector<Tensor<2, dim>> &grad_grads, |
| 135 |
std::vector<Tensor<3, dim>> &third_derivatives, |
| 136 |
std::vector<Tensor<4, dim>> &fourth_derivatives) const override; |
| 137 |
|
| 138 |
/** |
| 139 |
* Compute the value of the <tt>i</tt>th tensor product polynomial at |
| 140 |
* <tt>unit_point</tt>. Here <tt>i</tt> is given in tensor product |
| 141 |
* numbering. |
| 142 |
* |
| 143 |
* Note, that using this function within a loop over all tensor product |
| 144 |
* polynomials is not efficient, because then each point value of the |
| 145 |
* underlying (one-dimensional) polynomials is (unnecessarily) computed |
| 146 |
* several times. Instead use the evaluate() function with |
| 147 |
* <tt>values.size()==</tt>n() to get the point values of all tensor |
| 148 |
* polynomials all at once and in a much more efficient way. |
| 149 |
*/ |
| 150 |
double |
| 151 |
compute_value(const unsigned int i, const Point<dim> &p) const override; |
| 152 |
|
| 153 |
/** |
| 154 |
* Compute the <tt>order</tt>th derivative of the <tt>i</tt>th tensor |
| 155 |
* product polynomial at <tt>unit_point</tt>. Here <tt>i</tt> is given in |
| 156 |
* tensor product numbering. |
| 157 |
* |
| 158 |
* Note, that using this function within a loop over all tensor product |
| 159 |
* polynomials is not efficient, because then each derivative value of the |
| 160 |
* underlying (one-dimensional) polynomials is (unnecessarily) computed |
| 161 |
* several times. Instead use the evaluate() function, see above, with the |
| 162 |
* size of the appropriate parameter set to n() to get the point value of |
| 163 |
* all tensor polynomials all at once and in a much more efficient way. |
| 164 |
* |
| 165 |
* @tparam order The derivative order. |
| 166 |
*/ |
| 167 |
template <int order> |
| 168 |
Tensor<order, dim> |
| 169 |
compute_derivative(const unsigned int i, const Point<dim> &p) const; |
| 170 |
|
| 171 |
/** |
| 172 |
* @copydoc ScalarPolynomialsBase::compute_1st_derivative() |
| 173 |
*/ |
| 174 |
virtual Tensor<1, dim> |
| 175 |
compute_1st_derivative(const unsigned int i, |
| 176 |
const Point<dim> &p) const override; |
| 177 |
|
| 178 |
/** |
| 179 |
* @copydoc ScalarPolynomialsBase::compute_2nd_derivative() |
| 180 |
*/ |
| 181 |
virtual Tensor<2, dim> |
| 182 |
compute_2nd_derivative(const unsigned int i, |
| 183 |
const Point<dim> &p) const override; |
| 184 |
|
| 185 |
/** |
| 186 |
* @copydoc ScalarPolynomialsBase::compute_3rd_derivative() |
| 187 |
*/ |
| 188 |
virtual Tensor<3, dim> |
| 189 |
compute_3rd_derivative(const unsigned int i, |
| 190 |
const Point<dim> &p) const override; |
| 191 |
|
| 192 |
/** |
| 193 |
* @copydoc ScalarPolynomialsBase::compute_4th_derivative() |
| 194 |
*/ |
| 195 |
virtual Tensor<4, dim> |
| 196 |
compute_4th_derivative(const unsigned int i, |
| 197 |
const Point<dim> &p) const override; |
| 198 |
|
| 199 |
/** |
| 200 |
* Compute the grad of the <tt>i</tt>th tensor product polynomial at |
| 201 |
* <tt>unit_point</tt>. Here <tt>i</tt> is given in tensor product |
| 202 |
* numbering. |
| 203 |
* |
| 204 |
* Note, that using this function within a loop over all tensor product |
| 205 |
* polynomials is not efficient, because then each derivative value of the |
| 206 |
* underlying (one-dimensional) polynomials is (unnecessarily) computed |
| 207 |
* several times. Instead use the evaluate() function, see above, with |
| 208 |
* <tt>grads.size()==</tt>n() to get the point value of all tensor |
| 209 |
* polynomials all at once and in a much more efficient way. |
| 210 |
*/ |
| 211 |
Tensor<1, dim> |
| 212 |
compute_grad(const unsigned int i, const Point<dim> &p) const override; |
| 213 |
|
| 214 |
/** |
| 215 |
* Compute the second derivative (grad_grad) of the <tt>i</tt>th tensor |
| 216 |
* product polynomial at <tt>unit_point</tt>. Here <tt>i</tt> is given in |
| 217 |
* tensor product numbering. |
| 218 |
* |
| 219 |
* Note, that using this function within a loop over all tensor product |
| 220 |
* polynomials is not efficient, because then each derivative value of the |
| 221 |
* underlying (one-dimensional) polynomials is (unnecessarily) computed |
| 222 |
* several times. Instead use the evaluate() function, see above, with |
| 223 |
* <tt>grad_grads.size()==</tt>n() to get the point value of all tensor |
| 224 |
* polynomials all at once and in a much more efficient way. |
| 225 |
*/ |
| 226 |
Tensor<2, dim> |
| 227 |
compute_grad_grad(const unsigned int i, const Point<dim> &p) const override; |
| 228 |
|
| 229 |
/** |
| 230 |
* Return the name of the space, which is <tt>TensorProductPolynomials</tt>. |
| 231 |
*/ |
| 232 |
std::string |
| 233 |
name() const override; |
| 234 |
|
| 235 |
/** |
| 236 |
* @copydoc ScalarPolynomialsBase::clone() |
| 237 |
*/ |
| 238 |
virtual std::unique_ptr<ScalarPolynomialsBase<dim>> |
| 239 |
clone() const override; |
| 240 |
|
| 241 |
/** |
| 242 |
* Return an estimate (in bytes) for the memory consumption of this object. |
| 243 |
*/ |
| 244 |
virtual std::size_t |
| 245 |
memory_consumption() const override; |
| 246 |
|
| 247 |
/** |
| 248 |
* Return a copy of the underlying one-dimensional polynomials given to the |
| 249 |
* constructor of this class. |
| 250 |
*/ |
| 251 |
std::vector<PolynomialType> |
| 252 |
get_underlying_polynomials() const; |
| 253 |
|
| 254 |
protected: |
| 255 |
/** |
| 256 |
* Copy of the vector <tt>pols</tt> of polynomials given to the constructor. |
| 257 |
*/ |
| 258 |
std::vector<PolynomialType> polynomials; |
| 259 |
|
| 260 |
/** |
| 261 |
* Index map for reordering the polynomials. |
| 262 |
*/ |
| 263 |
std::vector<unsigned int> index_map; |
| 264 |
|
| 265 |
/** |
| 266 |
* Index map for reordering the polynomials. |
| 267 |
*/ |
| 268 |
std::vector<unsigned int> index_map_inverse; |
| 269 |
|
| 270 |
/** |
| 271 |
* Each tensor product polynomial <i>i</i> is a product of one-dimensional |
| 272 |
* polynomials in each space direction. Compute the indices of these |
| 273 |
* one-dimensional polynomials for each space direction, given the index |
| 274 |
* <i>i</i>. |
| 275 |
*/ |
| 276 |
void |
| 277 |
compute_index(const unsigned int i, |
| 278 |
std::array<unsigned int, dim> &indices) const; |
| 279 |
|
| 280 |
/** |
| 281 |
* TensorProductPolynomialsBubbles has a TensorProductPolynomials class |
| 282 |
* so we declare it as a friend class. |
| 283 |
*/ |
| 284 |
friend class TensorProductPolynomialsBubbles<dim>; |
| 285 |
|
| 286 |
/** |
| 287 |
* TensorProductPolynomialsConst has a TensorProductPolynomials class |
| 288 |
* so we declare it as a friend class. |
| 289 |
*/ |
| 290 |
friend class TensorProductPolynomialsConst<dim>; |
| 291 |
}; |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
/** |
| 296 |
* Anisotropic tensor product of given polynomials. |
| 297 |
* |
| 298 |
* Given one-dimensional polynomials $P^x_1(x), P^x_2(x), \ldots$ in |
| 299 |
* $x$-direction, $P^y_1(y), P^y_2(y), \ldots$ in $y$-direction, and |
| 300 |
* so on, this class generates polynomials of the form $Q_{ijk}(x,y,z) |
| 301 |
* = P^x_i(x)P^y_j(y)P^z_k(z)$. (With obvious generalization if @p dim |
| 302 |
* is in fact only 2. If @p dim is in fact only 1, then the result is |
| 303 |
* simply the same set of one-dimensional polynomials passed to the |
| 304 |
* constructor.) |
| 305 |
* |
| 306 |
* If the elements of each set of base polynomials are mutually |
| 307 |
* orthogonal on the interval $[-1,1]$ or $[0,1]$, then the tensor |
| 308 |
* product polynomials are orthogonal on $[-1,1]^d$ or $[0,1]^d$, |
| 309 |
* respectively. |
| 310 |
* |
| 311 |
* The resulting @p dim-dimensional tensor product polynomials are |
| 312 |
* ordered as follows: We iterate over the $x$ coordinates running |
| 313 |
* fastest, then the $y$ coordinate, etc. For example, for @p dim==2, |
| 314 |
* the first few polynomials are thus |
| 315 |
* $P^x_1(x)P^y_1(y)$, |
| 316 |
* $P^x_2(x)P^y_1(y)$, $P^x_3(x)P^y_1(y)$, ..., |
| 317 |
* $P^x_1(x)P^y_2(y)$, $P^x_2(x)P^y_2(y)$, |
| 318 |
* $P^x_3(x)P^y_2(y)$, etc. |
| 319 |
*/ |
| 320 |
template <int dim> |
| 321 |
class AnisotropicPolynomials : public ScalarPolynomialsBase<dim> |
| 322 |
{ |
| 323 |
public: |
| 324 |
/** |
| 325 |
* Constructor. @p base_polynomials is a table of one-dimensional |
| 326 |
* polynomials. The number of rows in this table (the first index |
| 327 |
* when indexing into @p base_polynomials) needs to be equal to the |
| 328 |
* space dimension, with the elements of each row (i.e., the second |
| 329 |
* index) giving the polynomials that shall be used in this |
| 330 |
* particular coordinate direction. |
| 331 |
* |
| 332 |
* Since we want to build <i>anisotropic</i> polynomials, the @p dim |
| 333 |
* sets of polynomials passed in as arguments may of course be |
| 334 |
* different, and may also vary in number. |
| 335 |
* |
| 336 |
* The number of tensor product polynomials is <tt>Nx*Ny*Nz</tt>, or with |
| 337 |
* terms dropped if the number of space dimensions is less than 3. |
| 338 |
*/ |
| 339 |
AnisotropicPolynomials( |
| 340 |
const std::vector<std::vector<Polynomials::Polynomial<double>>> |
| 341 |
&base_polynomials); |
| 342 |
|
| 343 |
/** |
| 344 |
* Set the ordering of the polynomials. Requires |
| 345 |
* <tt>renumber.size()==n()</tt>. Stores a copy of <tt>renumber</tt>. |
| 346 |
*/ |
| 347 |
void |
| 348 |
set_numbering(const std::vector<unsigned int> &renumber); |
| 349 |
|
| 350 |
/** |
| 351 |
* Give read access to the renumber vector. |
| 352 |
*/ |
| 353 |
const std::vector<unsigned int> & |
| 354 |
get_numbering() const; |
| 355 |
|
| 356 |
/** |
| 357 |
* Give read access to the inverse renumber vector. |
| 358 |
*/ |
| 359 |
const std::vector<unsigned int> & |
| 360 |
get_numbering_inverse() const; |
| 361 |
|
| 362 |
/** |
| 363 |
* Compute the value and the first and second derivatives of each tensor |
| 364 |
* product polynomial at <tt>unit_point</tt>. |
| 365 |
* |
| 366 |
* The size of the vectors must either be equal <tt>0</tt> or equal |
| 367 |
* <tt>this->n()</tt>. In the first case, the function will not compute |
| 368 |
* these values. |
| 369 |
* |
| 370 |
* If you need values or derivatives of all tensor product polynomials then |
| 371 |
* use this function, rather than using any of the <tt>compute_value</tt>, |
| 372 |
* <tt>compute_grad</tt> or <tt>compute_grad_grad</tt> functions, see below, |
| 373 |
* in a loop over all tensor product polynomials. |
| 374 |
*/ |
| 375 |
void |
| 376 |
evaluate(const Point<dim> &unit_point, |
| 377 |
std::vector<double> &values, |
| 378 |
std::vector<Tensor<1, dim>> &grads, |
| 379 |
std::vector<Tensor<2, dim>> &grad_grads, |
| 380 |
std::vector<Tensor<3, dim>> &third_derivatives, |
| 381 |
std::vector<Tensor<4, dim>> &fourth_derivatives) const override; |
| 382 |
|
| 383 |
/** |
| 384 |
* Compute the value of the <tt>i</tt>th tensor product polynomial at |
| 385 |
* <tt>unit_point</tt>. Here <tt>i</tt> is given in tensor product |
| 386 |
* numbering. |
| 387 |
* |
| 388 |
* Note, that using this function within a loop over all tensor product |
| 389 |
* polynomials is not efficient, because then each point value of the |
| 390 |
* underlying (one-dimensional) polynomials is (unnecessarily) computed |
| 391 |
* several times. Instead use the <tt>compute</tt> function, see above, |
| 392 |
* with <tt>values.size()==this->n()</tt> to get the point values of all |
| 393 |
* tensor polynomials all at once and in a much more efficient way. |
| 394 |
*/ |
| 395 |
double |
| 396 |
compute_value(const unsigned int i, const Point<dim> &p) const override; |
| 397 |
|
| 398 |
/** |
| 399 |
* Compute the <tt>order</tt>th derivative of the <tt>i</tt>th tensor |
| 400 |
* product polynomial at <tt>unit_point</tt>. Here <tt>i</tt> is given in |
| 401 |
* tensor product numbering. |
| 402 |
* |
| 403 |
* Note, that using this function within a loop over all tensor product |
| 404 |
* polynomials is not efficient, because then each derivative value of the |
| 405 |
* underlying (one-dimensional) polynomials is (unnecessarily) computed |
| 406 |
* several times. Instead use the evaluate() function, see above, with the |
| 407 |
* size of the appropriate parameter set to n() to get the point value of |
| 408 |
* all tensor polynomials all at once and in a much more efficient way. |
| 409 |
* |
| 410 |
* @tparam order The derivative order. |
| 411 |
*/ |
| 412 |
template <int order> |
| 413 |
Tensor<order, dim> |
| 414 |
compute_derivative(const unsigned int i, const Point<dim> &p) const; |
| 415 |
|
| 416 |
/** |
| 417 |
* @copydoc ScalarPolynomialsBase::compute_1st_derivative() |
| 418 |
*/ |
| 419 |
virtual Tensor<1, dim> |
| 420 |
compute_1st_derivative(const unsigned int i, |
| 421 |
const Point<dim> &p) const override; |
| 422 |
|
| 423 |
/** |
| 424 |
* @copydoc ScalarPolynomialsBase::compute_2nd_derivative() |
| 425 |
*/ |
| 426 |
virtual Tensor<2, dim> |
| 427 |
compute_2nd_derivative(const unsigned int i, |
| 428 |
const Point<dim> &p) const override; |
| 429 |
|
| 430 |
/** |
| 431 |
* @copydoc ScalarPolynomialsBase::compute_3rd_derivative() |
| 432 |
*/ |
| 433 |
virtual Tensor<3, dim> |
| 434 |
compute_3rd_derivative(const unsigned int i, |
| 435 |
const Point<dim> &p) const override; |
| 436 |
|
| 437 |
/** |
| 438 |
* @copydoc ScalarPolynomialsBase::compute_4th_derivative() |
| 439 |
*/ |
| 440 |
virtual Tensor<4, dim> |
| 441 |
compute_4th_derivative(const unsigned int i, |
| 442 |
const Point<dim> &p) const override; |
| 443 |
|
| 444 |
/** |
| 445 |
* Compute the grad of the <tt>i</tt>th tensor product polynomial at |
| 446 |
* <tt>unit_point</tt>. Here <tt>i</tt> is given in tensor product |
| 447 |
* numbering. |
| 448 |
* |
| 449 |
* Note, that using this function within a loop over all tensor product |
| 450 |
* polynomials is not efficient, because then each derivative value of the |
| 451 |
* underlying (one-dimensional) polynomials is (unnecessarily) computed |
| 452 |
* several times. Instead use the <tt>compute</tt> function, see above, |
| 453 |
* with <tt>grads.size()==this->n()</tt> to get the point value of all |
| 454 |
* tensor polynomials all at once and in a much more efficient way. |
| 455 |
*/ |
| 456 |
Tensor<1, dim> |
| 457 |
compute_grad(const unsigned int i, const Point<dim> &p) const override; |
| 458 |
|
| 459 |
/** |
| 460 |
* Compute the second derivative (grad_grad) of the <tt>i</tt>th tensor |
| 461 |
* product polynomial at <tt>unit_point</tt>. Here <tt>i</tt> is given in |
| 462 |
* tensor product numbering. |
| 463 |
* |
| 464 |
* Note, that using this function within a loop over all tensor product |
| 465 |
* polynomials is not efficient, because then each derivative value of the |
| 466 |
* underlying (one-dimensional) polynomials is (unnecessarily) computed |
| 467 |
* several times. Instead use the <tt>compute</tt> function, see above, |
| 468 |
* with <tt>grad_grads.size()==this->n()</tt> to get the point value of |
| 469 |
* all tensor polynomials all at once and in a much more efficient way. |
| 470 |
*/ |
| 471 |
Tensor<2, dim> |
| 472 |
compute_grad_grad(const unsigned int i, const Point<dim> &p) const override; |
| 473 |
|
| 474 |
/** |
| 475 |
* Return the name of the space, which is <tt>AnisotropicPolynomials</tt>. |
| 476 |
*/ |
| 477 |
std::string |
| 478 |
name() const override; |
| 479 |
|
| 480 |
/** |
| 481 |
* @copydoc ScalarPolynomialsBase::clone() |
| 482 |
*/ |
| 483 |
virtual std::unique_ptr<ScalarPolynomialsBase<dim>> |
| 484 |
clone() const override; |
| 485 |
|
| 486 |
private: |
| 487 |
/** |
| 488 |
* Copy of the vector <tt>pols</tt> of polynomials given to the constructor. |
| 489 |
*/ |
| 490 |
const std::vector<std::vector<Polynomials::Polynomial<double>>> polynomials; |
| 491 |
|
| 492 |
/** |
| 493 |
* Index map for reordering the polynomials. |
| 494 |
*/ |
| 495 |
std::vector<unsigned int> index_map; |
| 496 |
|
| 497 |
/** |
| 498 |
* Index map for reordering the polynomials. |
| 499 |
*/ |
| 500 |
std::vector<unsigned int> index_map_inverse; |
| 501 |
|
| 502 |
/** |
| 503 |
* Each tensor product polynomial $p_i$ is a product of one-dimensional |
| 504 |
* polynomials in each space direction. Compute the indices of these |
| 505 |
* one-dimensional polynomials for each space direction, given the index |
| 506 |
* <tt>i</tt>. |
| 507 |
*/ |
| 508 |
void |
| 509 |
compute_index(const unsigned int i, |
| 510 |
std::array<unsigned int, dim> &indices) const; |
| 511 |
|
| 512 |
/** |
| 513 |
* Given the input to the constructor, compute <tt>n_pols</tt>. |
| 514 |
*/ |
| 515 |
static unsigned int |
| 516 |
get_n_tensor_pols( |
| 517 |
const std::vector<std::vector<Polynomials::Polynomial<double>>> &pols); |
| 518 |
}; |
| 519 |
|
| 520 |
/** @} */ |
| 521 |
|
| 522 |
#ifndef DOXYGEN |
| 523 |
|
| 524 |
|
| 525 |
/* ---------------- template and inline functions ---------- */ |
| 526 |
|
| 527 |
|
| 528 |
template <int dim, typename PolynomialType> |
| 529 |
template <class Pol> |
| 530 |
inline TensorProductPolynomials<dim, PolynomialType>::TensorProductPolynomials( |
| 531 |
const std::vector<Pol> &pols) |
| 532 |
: ScalarPolynomialsBase<dim>(1, Utilities::fixed_power<dim>(pols.size())) |
| 533 |
, polynomials(pols.begin(), pols.end()) |
| 534 |
, index_map(this->n()) |
| 535 |
, index_map_inverse(this->n()) |
| 536 |
{ |
| 537 |
// per default set this index map to identity. This map can be changed by |
| 538 |
// the user through the set_numbering() function |
| 539 |
for (unsigned int i = 0; i < this->n(); ++i) |
| 540 |
{ |
| 541 |
index_map[i] = i; |
| 542 |
index_map_inverse[i] = i; |
| 543 |
} |
| 544 |
} |
| 545 |
|
| 546 |
|
| 547 |
template <int dim, typename PolynomialType> |
| 548 |
inline const std::vector<unsigned int> & |
| 549 |
TensorProductPolynomials<dim, PolynomialType>::get_numbering() const |
| 550 |
{ |
| 551 |
return index_map; |
| 552 |
} |
| 553 |
|
| 554 |
|
| 555 |
template <int dim, typename PolynomialType> |
| 556 |
inline const std::vector<unsigned int> & |
| 557 |
TensorProductPolynomials<dim, PolynomialType>::get_numbering_inverse() const |
| 558 |
{ |
| 559 |
return index_map_inverse; |
| 560 |
} |
| 561 |
|
| 562 |
|
| 563 |
template <int dim, typename PolynomialType> |
| 564 |
inline std::string |
| 565 |
TensorProductPolynomials<dim, PolynomialType>::name() const |
| 566 |
{ |
| 567 |
return "TensorProductPolynomials"; |
| 568 |
} |
| 569 |
|
| 570 |
|
| 571 |
template <int dim, typename PolynomialType> |
| 572 |
template <int order> |
| 573 |
Tensor<order, dim> |
| 574 |
TensorProductPolynomials<dim, PolynomialType>::compute_derivative( |
| 575 |
const unsigned int i, |
| 576 |
const Point<dim> &p) const |
| 577 |
{ |
| 578 |
std::array<unsigned int, dim> indices; |
| 579 |
compute_index(i, indices); |
| 580 |
|
| 581 |
ndarray<double, dim, 5> v; |
| 582 |
{ |
| 583 |
std::vector<double> tmp(5); |
| 584 |
for (unsigned int d = 0; d < dim; ++d) |
| 585 |
{ |
| 586 |
polynomials[indices[d]].value(p[d], tmp); |
| 587 |
v[d][0] = tmp[0]; |
| 588 |
v[d][1] = tmp[1]; |
| 589 |
v[d][2] = tmp[2]; |
| 590 |
v[d][3] = tmp[3]; |
| 591 |
v[d][4] = tmp[4]; |
| 592 |
} |
| 593 |
} |
| 594 |
|
| 595 |
if constexpr (order == 1) |
| 596 |
{ |
| 597 |
Tensor<1, dim> derivative; |
| 598 |
for (unsigned int d = 0; d < dim; ++d) |
| 599 |
{ |
| 600 |
derivative[d] = 1.; |
| 601 |
for (unsigned int x = 0; x < dim; ++x) |
| 602 |
{ |
| 603 |
unsigned int x_order = 0; |
| 604 |
if (d == x) |
| 605 |
++x_order; |
| 606 |
|
| 607 |
derivative[d] *= v[x][x_order]; |
| 608 |
} |
| 609 |
} |
| 610 |
|
| 611 |
return derivative; |
| 612 |
} |
| 613 |
else if constexpr (order == 2) |
| 614 |
{ |
| 615 |
Tensor<2, dim> derivative; |
| 616 |
for (unsigned int d1 = 0; d1 < dim; ++d1) |
| 617 |
for (unsigned int d2 = 0; d2 < dim; ++d2) |
| 618 |
{ |
| 619 |
derivative[d1][d2] = 1.; |
| 620 |
for (unsigned int x = 0; x < dim; ++x) |
| 621 |
{ |
| 622 |
unsigned int x_order = 0; |
| 623 |
if (d1 == x) |
| 624 |
++x_order; |
| 625 |
if (d2 == x) |
| 626 |
++x_order; |
| 627 |
|
| 628 |
derivative[d1][d2] *= v[x][x_order]; |
| 629 |
} |
| 630 |
} |
| 631 |
|
| 632 |
return derivative; |
| 633 |
} |
| 634 |
else if constexpr (order == 3) |
| 635 |
{ |
| 636 |
Tensor<3, dim> derivative; |
| 637 |
for (unsigned int d1 = 0; d1 < dim; ++d1) |
| 638 |
for (unsigned int d2 = 0; d2 < dim; ++d2) |
| 639 |
for (unsigned int d3 = 0; d3 < dim; ++d3) |
| 640 |
{ |
| 641 |
derivative[d1][d2][d3] = 1.; |
| 642 |
for (unsigned int x = 0; x < dim; ++x) |
| 643 |
{ |
| 644 |
unsigned int x_order = 0; |
| 645 |
if (d1 == x) |
| 646 |
++x_order; |
| 647 |
if (d2 == x) |
| 648 |
++x_order; |
| 649 |
if (d3 == x) |
| 650 |
++x_order; |
| 651 |
|
| 652 |
derivative[d1][d2][d3] *= v[x][x_order]; |
| 653 |
} |
| 654 |
} |
| 655 |
|
| 656 |
return derivative; |
| 657 |
} |
| 658 |
else if constexpr (order == 4) |
| 659 |
{ |
| 660 |
Tensor<4, dim> derivative; |
| 661 |
for (unsigned int d1 = 0; d1 < dim; ++d1) |
| 662 |
for (unsigned int d2 = 0; d2 < dim; ++d2) |
| 663 |
for (unsigned int d3 = 0; d3 < dim; ++d3) |
| 664 |
for (unsigned int d4 = 0; d4 < dim; ++d4) |
| 665 |
{ |
| 666 |
derivative[d1][d2][d3][d4] = 1.; |
| 667 |
for (unsigned int x = 0; x < dim; ++x) |
| 668 |
{ |
| 669 |
unsigned int x_order = 0; |
| 670 |
if (d1 == x) |
| 671 |
++x_order; |
| 672 |
if (d2 == x) |
| 673 |
++x_order; |
| 674 |
if (d3 == x) |
| 675 |
++x_order; |
| 676 |
if (d4 == x) |
| 677 |
++x_order; |
| 678 |
|
| 679 |
derivative[d1][d2][d3][d4] *= v[x][x_order]; |
| 680 |
} |
| 681 |
} |
| 682 |
|
| 683 |
return derivative; |
| 684 |
} |
| 685 |
else |
| 686 |
{ |
| 687 |
DEAL_II_NOT_IMPLEMENTED(); |
| 688 |
return {}; |
| 689 |
} |
| 690 |
} |
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
template <> |
| 695 |
template <int order> |
| 696 |
Tensor<order, 0> |
| 697 |
TensorProductPolynomials<0, Polynomials::Polynomial<double>>:: |
| 698 |
compute_derivative(const unsigned int, const Point<0> &) const |
| 699 |
{ |
| 700 |
AssertThrow(false, ExcNotImplemented()); |
| 701 |
|
| 702 |
return {}; |
| 703 |
} |
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
template <int dim, typename PolynomialType> |
| 708 |
inline Tensor<1, dim> |
| 709 |
TensorProductPolynomials<dim, PolynomialType>::compute_1st_derivative( |
| 710 |
const unsigned int i, |
| 711 |
const Point<dim> &p) const |
| 712 |
{ |
| 713 |
return compute_derivative<1>(i, p); |
| 714 |
} |
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
template <int dim, typename PolynomialType> |
| 719 |
inline Tensor<2, dim> |
| 720 |
TensorProductPolynomials<dim, PolynomialType>::compute_2nd_derivative( |
| 721 |
const unsigned int i, |
| 722 |
const Point<dim> &p) const |
| 723 |
{ |
| 724 |
return compute_derivative<2>(i, p); |
| 725 |
} |
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
template <int dim, typename PolynomialType> |
| 730 |
inline Tensor<3, dim> |
| 731 |
TensorProductPolynomials<dim, PolynomialType>::compute_3rd_derivative( |
| 732 |
const unsigned int i, |
| 733 |
const Point<dim> &p) const |
| 734 |
{ |
| 735 |
return compute_derivative<3>(i, p); |
| 736 |
} |
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
template <int dim, typename PolynomialType> |
| 741 |
inline Tensor<4, dim> |
| 742 |
TensorProductPolynomials<dim, PolynomialType>::compute_4th_derivative( |
| 743 |
const unsigned int i, |
| 744 |
const Point<dim> &p) const |
| 745 |
{ |
| 746 |
return compute_derivative<4>(i, p); |
| 747 |
} |
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
template <int dim> |
| 752 |
template <int order> |
| 753 |
Tensor<order, dim> |
| 754 |
AnisotropicPolynomials<dim>::compute_derivative(const unsigned int i, |
| 755 |
const Point<dim> &p) const |
| 756 |
{ |
| 757 |
std::array<unsigned int, dim> indices; |
| 758 |
compute_index(i, indices); |
| 759 |
|
| 760 |
std::vector<std::vector<double>> v(dim, std::vector<double>(order + 1)); |
| 761 |
for (unsigned int d = 0; d < dim; ++d) |
| 762 |
polynomials[d][indices[d]].value(p[d], v[d]); |
| 763 |
|
| 764 |
if constexpr (order == 1) |
| 765 |
{ |
| 766 |
Tensor<1, dim> derivative; |
| 767 |
for (unsigned int d = 0; d < dim; ++d) |
| 768 |
{ |
| 769 |
derivative[d] = 1.; |
| 770 |
for (unsigned int x = 0; x < dim; ++x) |
| 771 |
{ |
| 772 |
unsigned int x_order = 0; |
| 773 |
if (d == x) |
| 774 |
++x_order; |
| 775 |
|
| 776 |
derivative[d] *= v[x][x_order]; |
| 777 |
} |
| 778 |
} |
| 779 |
|
| 780 |
return derivative; |
| 781 |
} |
| 782 |
else if constexpr (order == 2) |
| 783 |
{ |
| 784 |
Tensor<2, dim> derivative; |
| 785 |
for (unsigned int d1 = 0; d1 < dim; ++d1) |
| 786 |
for (unsigned int d2 = 0; d2 < dim; ++d2) |
| 787 |
{ |
| 788 |
derivative[d1][d2] = 1.; |
| 789 |
for (unsigned int x = 0; x < dim; ++x) |
| 790 |
{ |
| 791 |
unsigned int x_order = 0; |
| 792 |
if (d1 == x) |
| 793 |
++x_order; |
| 794 |
if (d2 == x) |
| 795 |
++x_order; |
| 796 |
|
| 797 |
derivative[d1][d2] *= v[x][x_order]; |
| 798 |
} |
| 799 |
} |
| 800 |
|
| 801 |
return derivative; |
| 802 |
} |
| 803 |
else if constexpr (order == 3) |
| 804 |
{ |
| 805 |
Tensor<3, dim> derivative; |
| 806 |
for (unsigned int d1 = 0; d1 < dim; ++d1) |
| 807 |
for (unsigned int d2 = 0; d2 < dim; ++d2) |
| 808 |
for (unsigned int d3 = 0; d3 < dim; ++d3) |
| 809 |
{ |
| 810 |
derivative[d1][d2][d3] = 1.; |
| 811 |
for (unsigned int x = 0; x < dim; ++x) |
| 812 |
{ |
| 813 |
unsigned int x_order = 0; |
| 814 |
if (d1 == x) |
| 815 |
++x_order; |
| 816 |
if (d2 == x) |
| 817 |
++x_order; |
| 818 |
if (d3 == x) |
| 819 |
++x_order; |
| 820 |
|
| 821 |
derivative[d1][d2][d3] *= v[x][x_order]; |
| 822 |
} |
| 823 |
} |
| 824 |
|
| 825 |
return derivative; |
| 826 |
} |
| 827 |
else if constexpr (order == 4) |
| 828 |
{ |
| 829 |
Tensor<4, dim> derivative; |
| 830 |
for (unsigned int d1 = 0; d1 < dim; ++d1) |
| 831 |
for (unsigned int d2 = 0; d2 < dim; ++d2) |
| 832 |
for (unsigned int d3 = 0; d3 < dim; ++d3) |
| 833 |
for (unsigned int d4 = 0; d4 < dim; ++d4) |
| 834 |
{ |
| 835 |
derivative[d1][d2][d3][d4] = 1.; |
| 836 |
for (unsigned int x = 0; x < dim; ++x) |
| 837 |
{ |
| 838 |
unsigned int x_order = 0; |
| 839 |
if (d1 == x) |
| 840 |
++x_order; |
| 841 |
if (d2 == x) |
| 842 |
++x_order; |
| 843 |
if (d3 == x) |
| 844 |
++x_order; |
| 845 |
if (d4 == x) |
| 846 |
++x_order; |
| 847 |
|
| 848 |
derivative[d1][d2][d3][d4] *= v[x][x_order]; |
| 849 |
} |
| 850 |
} |
| 851 |
|
| 852 |
return derivative; |
| 853 |
} |
| 854 |
else |
| 855 |
{ |
| 856 |
DEAL_II_NOT_IMPLEMENTED(); |
| 857 |
return {}; |
| 858 |
} |
| 859 |
} |
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
template <> |
| 864 |
template <int order> |
| 865 |
Tensor<order, 0> |
| 866 |
AnisotropicPolynomials<0>::compute_derivative(const unsigned int, |
| 867 |
const Point<0> &) const |
| 868 |
{ |
| 869 |
AssertThrow(false, ExcNotImplemented()); |
| 870 |
|
| 871 |
return {}; |
| 872 |
} |
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
template <int dim> |
| 877 |
inline Tensor<1, dim> |
| 878 |
AnisotropicPolynomials<dim>::compute_1st_derivative(const unsigned int i, |
| 879 |
const Point<dim> &p) const |
| 880 |
{ |
| 881 |
return compute_derivative<1>(i, p); |
| 882 |
} |
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
template <int dim> |
| 887 |
inline Tensor<2, dim> |
| 888 |
AnisotropicPolynomials<dim>::compute_2nd_derivative(const unsigned int i, |
| 889 |
const Point<dim> &p) const |
| 890 |
{ |
| 891 |
return compute_derivative<2>(i, p); |
| 892 |
} |
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
template <int dim> |
| 897 |
inline Tensor<3, dim> |
| 898 |
AnisotropicPolynomials<dim>::compute_3rd_derivative(const unsigned int i, |
| 899 |
const Point<dim> &p) const |
| 900 |
{ |
| 901 |
return compute_derivative<3>(i, p); |
| 902 |
} |
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
template <int dim> |
| 907 |
inline Tensor<4, dim> |
| 908 |
AnisotropicPolynomials<dim>::compute_4th_derivative(const unsigned int i, |
| 909 |
const Point<dim> &p) const |
| 910 |
{ |
| 911 |
return compute_derivative<4>(i, p); |
| 912 |
} |
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
template <int dim> |
| 917 |
inline std::string |
| 918 |
AnisotropicPolynomials<dim>::name() const |
| 919 |
{ |
| 920 |
return "AnisotropicPolynomials"; |
| 921 |
} |
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
#endif // DOXYGEN |
| 926 |
DEAL_II_NAMESPACE_CLOSE |
| 927 |
|
| 928 |
#endif |