| 1 |
// ------------------------------------------------------------------------ |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
// Copyright (C) 2002 - 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_polynomial_space_h |
| 16 |
#define dealii_polynomial_space_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/observer_pointer.h> |
| 24 |
#include <deal.II/base/point.h> |
| 25 |
#include <deal.II/base/polynomial.h> |
| 26 |
#include <deal.II/base/scalar_polynomials_base.h> |
| 27 |
#include <deal.II/base/tensor.h> |
| 28 |
|
| 29 |
#include <vector> |
| 30 |
|
| 31 |
DEAL_II_NAMESPACE_OPEN |
| 32 |
|
| 33 |
/** |
| 34 |
* Representation of the space of polynomials of degree at most n in higher |
| 35 |
* dimensions. |
| 36 |
* |
| 37 |
* Given a vector of <i>n</i> one-dimensional polynomials <i>P<sub>0</sub></i> |
| 38 |
* to <i>P<sub>n</sub></i>, where <i>P<sub>i</sub></i> has degree <i>i</i>, |
| 39 |
* this class generates all dim-dimensional polynomials of the form <i> |
| 40 |
* P<sub>ijk</sub>(x,y,z) = |
| 41 |
* P<sub>i</sub>(x)P<sub>j</sub>(y)P<sub>k</sub>(z)</i>, where the sum of |
| 42 |
* <i>i</i>, <i>j</i> and <i>k</i> is less than or equal <i>n</i>. |
| 43 |
* |
| 44 |
* The output_indices() function prints the ordering of the polynomials, i.e. |
| 45 |
* for each dim-dimensional polynomial in the polynomial space it gives the |
| 46 |
* indices i,j,k of the one-dimensional polynomials in x,y and z direction. |
| 47 |
* The ordering of the dim-dimensional polynomials can be changed by using the |
| 48 |
* set_numbering() function. |
| 49 |
* |
| 50 |
* The standard ordering of polynomials is that indices for the first space |
| 51 |
* dimension vary fastest and the last space dimension is slowest. In |
| 52 |
* particular, if we take for simplicity the vector of monomials |
| 53 |
* <i>x<sup>0</sup>, x<sup>1</sup>, x<sup>2</sup>,..., x<sup>n</sup></i>, we |
| 54 |
* get |
| 55 |
* |
| 56 |
* <dl> <dt> 1d <dd> <i> x<sup>0</sup>, x<sup>1</sup>,...,x<sup>n</sup></i> |
| 57 |
* <dt> 2d: <dd> <i> x<sup>0</sup>y<sup>0</sup>, |
| 58 |
* x<sup>1</sup>y<sup>0</sup>,..., x<sup>n</sup>y<sup>0</sup>, |
| 59 |
* <br> |
| 60 |
* x<sup>0</sup>y<sup>1</sup>, x<sup>1</sup>y<sup>1</sup>,..., |
| 61 |
* x<sup>n-1</sup>y<sup>1</sup>, |
| 62 |
* <br> |
| 63 |
* x<sup>0</sup>y<sup>2</sup>,... x<sup>n-2</sup>y<sup>2</sup>, |
| 64 |
* <br> |
| 65 |
* ... |
| 66 |
* <br> |
| 67 |
* x<sup>0</sup>y<sup>n-1</sup>, x<sup>1</sup>y<sup>n-1</sup>, |
| 68 |
* <br> |
| 69 |
* x<sup>0</sup>y<sup>n</sup> </i> <dt> 3d: <dd> <i> |
| 70 |
* x<sup>0</sup>y<sup>0</sup>z<sup>0</sup>,..., |
| 71 |
* x<sup>n</sup>y<sup>0</sup>z<sup>0</sup>, |
| 72 |
* <br> |
| 73 |
* x<sup>0</sup>y<sup>1</sup>z<sup>0</sup>,..., |
| 74 |
* x<sup>n-1</sup>y<sup>1</sup>z<sup>0</sup>, |
| 75 |
* <br> |
| 76 |
* ... |
| 77 |
* <br> |
| 78 |
* x<sup>0</sup>y<sup>n</sup>z<sup>0</sup>, |
| 79 |
* <br> |
| 80 |
* x<sup>0</sup>y<sup>0</sup>z<sup>1</sup>,... |
| 81 |
* x<sup>n-1</sup>y<sup>0</sup>z<sup>1</sup>, |
| 82 |
* <br> |
| 83 |
* ... |
| 84 |
* <br> |
| 85 |
* x<sup>0</sup>y<sup>n-1</sup>z<sup>1</sup>, |
| 86 |
* <br> |
| 87 |
* x<sup>0</sup>y<sup>0</sup>z<sup>2</sup>,... |
| 88 |
* x<sup>n-2</sup>y<sup>0</sup>z<sup>2</sup>, |
| 89 |
* <br> |
| 90 |
* ... |
| 91 |
* <br> |
| 92 |
* x<sup>0</sup>y<sup>0</sup>z<sup>n</sup> </i> </dl> |
| 93 |
* |
| 94 |
* @ingroup Polynomials |
| 95 |
*/ |
| 96 |
template <int dim> |
| 97 |
class PolynomialSpace : public ScalarPolynomialsBase<dim> |
| 98 |
{ |
| 99 |
public: |
| 100 |
/** |
| 101 |
* Access to the dimension of this object, for checking and automatic |
| 102 |
* setting of dimension in other classes. |
| 103 |
*/ |
| 104 |
static constexpr unsigned int dimension = dim; |
| 105 |
|
| 106 |
/** |
| 107 |
* Constructor. <tt>pols</tt> is a vector of pointers to one-dimensional |
| 108 |
* polynomials and will be copied into a private member variable. The static |
| 109 |
* type of the template argument <tt>pols</tt> needs to be convertible to |
| 110 |
* Polynomials::Polynomial@<double@>, i.e. should usually be a derived class |
| 111 |
* of Polynomials::Polynomial@<double@>. |
| 112 |
*/ |
| 113 |
template <class Pol> |
| 114 |
PolynomialSpace(const std::vector<Pol> &pols); |
| 115 |
|
| 116 |
/** |
| 117 |
* Prints the list of the indices to <tt>out</tt>. |
| 118 |
*/ |
| 119 |
template <typename StreamType> |
| 120 |
void |
| 121 |
output_indices(StreamType &out) const; |
| 122 |
|
| 123 |
/** |
| 124 |
* Set the ordering of the polynomials. Requires |
| 125 |
* <tt>renumber.size()==n()</tt>. Stores a copy of <tt>renumber</tt>. |
| 126 |
*/ |
| 127 |
void |
| 128 |
set_numbering(const std::vector<unsigned int> &renumber); |
| 129 |
|
| 130 |
/** |
| 131 |
* Compute the value and the first and second derivatives of each |
| 132 |
* polynomial at <tt>unit_point</tt>. |
| 133 |
* |
| 134 |
* The size of the vectors must either be equal 0 or equal n(). In the first |
| 135 |
* case, the function will not compute these values, i.e. you indicate what |
| 136 |
* you want to have computed by resizing those vectors which you want |
| 137 |
* filled. |
| 138 |
* |
| 139 |
* If you need values or derivatives of all polynomials then use this |
| 140 |
* function, rather than using any of the compute_value(), compute_grad() or |
| 141 |
* compute_grad_grad() functions, see below, in a loop over all polynomials. |
| 142 |
*/ |
| 143 |
void |
| 144 |
evaluate(const Point<dim> &unit_point, |
| 145 |
std::vector<double> &values, |
| 146 |
std::vector<Tensor<1, dim>> &grads, |
| 147 |
std::vector<Tensor<2, dim>> &grad_grads, |
| 148 |
std::vector<Tensor<3, dim>> &third_derivatives, |
| 149 |
std::vector<Tensor<4, dim>> &fourth_derivatives) const override; |
| 150 |
|
| 151 |
/** |
| 152 |
* Compute the value of the <tt>i</tt>th polynomial at unit point |
| 153 |
* <tt>p</tt>. |
| 154 |
* |
| 155 |
* Consider using evaluate() instead. |
| 156 |
*/ |
| 157 |
double |
| 158 |
compute_value(const unsigned int i, const Point<dim> &p) const override; |
| 159 |
|
| 160 |
/** |
| 161 |
* Compute the <tt>order</tt>th derivative of the <tt>i</tt>th polynomial |
| 162 |
* at unit point <tt>p</tt>. |
| 163 |
* |
| 164 |
* Consider using evaluate() instead. |
| 165 |
* |
| 166 |
* @tparam order The order of the derivative. |
| 167 |
*/ |
| 168 |
template <int order> |
| 169 |
Tensor<order, dim> |
| 170 |
compute_derivative(const unsigned int i, const Point<dim> &p) const; |
| 171 |
|
| 172 |
/** |
| 173 |
* @copydoc ScalarPolynomialsBase::compute_1st_derivative() |
| 174 |
*/ |
| 175 |
virtual Tensor<1, dim> |
| 176 |
compute_1st_derivative(const unsigned int i, |
| 177 |
const Point<dim> &p) const override; |
| 178 |
|
| 179 |
/** |
| 180 |
* @copydoc ScalarPolynomialsBase::compute_2nd_derivative() |
| 181 |
*/ |
| 182 |
virtual Tensor<2, dim> |
| 183 |
compute_2nd_derivative(const unsigned int i, |
| 184 |
const Point<dim> &p) const override; |
| 185 |
|
| 186 |
/** |
| 187 |
* @copydoc ScalarPolynomialsBase::compute_3rd_derivative() |
| 188 |
*/ |
| 189 |
virtual Tensor<3, dim> |
| 190 |
compute_3rd_derivative(const unsigned int i, |
| 191 |
const Point<dim> &p) const override; |
| 192 |
|
| 193 |
/** |
| 194 |
* @copydoc ScalarPolynomialsBase::compute_4th_derivative() |
| 195 |
*/ |
| 196 |
virtual Tensor<4, dim> |
| 197 |
compute_4th_derivative(const unsigned int i, |
| 198 |
const Point<dim> &p) const override; |
| 199 |
|
| 200 |
/** |
| 201 |
* Compute the gradient of the <tt>i</tt>th polynomial at unit point |
| 202 |
* <tt>p</tt>. |
| 203 |
* |
| 204 |
* Consider using evaluate() instead. |
| 205 |
*/ |
| 206 |
Tensor<1, dim> |
| 207 |
compute_grad(const unsigned int i, const Point<dim> &p) const override; |
| 208 |
|
| 209 |
/** |
| 210 |
* Compute the second derivative (grad_grad) of the <tt>i</tt>th polynomial |
| 211 |
* at unit point <tt>p</tt>. |
| 212 |
* |
| 213 |
* Consider using evaluate() instead. |
| 214 |
*/ |
| 215 |
Tensor<2, dim> |
| 216 |
compute_grad_grad(const unsigned int i, const Point<dim> &p) const override; |
| 217 |
|
| 218 |
/** |
| 219 |
* Return the number of polynomials spanning the space represented by this |
| 220 |
* class. Here, if <tt>N</tt> is the number of one-dimensional polynomials |
| 221 |
* given, then the result of this function is <i>N</i> in 1d, |
| 222 |
* <i>N(N+1)/2</i> in 2d, and <i>N(N+1)(N+2)/6</i> in 3d. |
| 223 |
*/ |
| 224 |
static unsigned int |
| 225 |
n_polynomials(const unsigned int n); |
| 226 |
|
| 227 |
/** |
| 228 |
* Return the name of the space, which is <tt>PolynomialSpace</tt>. |
| 229 |
*/ |
| 230 |
std::string |
| 231 |
name() const override; |
| 232 |
|
| 233 |
/** |
| 234 |
* @copydoc ScalarPolynomialsBase::clone() |
| 235 |
*/ |
| 236 |
virtual std::unique_ptr<ScalarPolynomialsBase<dim>> |
| 237 |
clone() const override; |
| 238 |
|
| 239 |
protected: |
| 240 |
/** |
| 241 |
* Compute numbers in x, y and z direction. Given an index <tt>n</tt> in the |
| 242 |
* d-dimensional polynomial space, return the indices i,j,k such that |
| 243 |
* <i>p<sub>n</sub>(x,y,z) = |
| 244 |
* p<sub>i</sub>(x)p<sub>j</sub>(y)p<sub>k</sub>(z)</i>. |
| 245 |
* |
| 246 |
* In 1d and 2d, obviously only i and i,j are returned. |
| 247 |
*/ |
| 248 |
std::array<unsigned int, dim> |
| 249 |
compute_index(const unsigned int n) const; |
| 250 |
|
| 251 |
private: |
| 252 |
/** |
| 253 |
* Copy of the vector <tt>pols</tt> of polynomials given to the constructor. |
| 254 |
*/ |
| 255 |
const std::vector<Polynomials::Polynomial<double>> polynomials; |
| 256 |
|
| 257 |
/** |
| 258 |
* Index map for reordering the polynomials. |
| 259 |
*/ |
| 260 |
std::vector<unsigned int> index_map; |
| 261 |
|
| 262 |
/** |
| 263 |
* Index map for reordering the polynomials. |
| 264 |
*/ |
| 265 |
std::vector<unsigned int> index_map_inverse; |
| 266 |
}; |
| 267 |
|
| 268 |
|
| 269 |
/* -------------- declaration of explicit specializations --- */ |
| 270 |
|
| 271 |
template <> |
| 272 |
std::array<unsigned int, 1> |
| 273 |
PolynomialSpace<1>::compute_index(const unsigned int n) const; |
| 274 |
template <> |
| 275 |
std::array<unsigned int, 2> |
| 276 |
PolynomialSpace<2>::compute_index(const unsigned int n) const; |
| 277 |
template <> |
| 278 |
std::array<unsigned int, 3> |
| 279 |
PolynomialSpace<3>::compute_index(const unsigned int n) const; |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
/* -------------- inline and template functions ------------- */ |
| 284 |
|
| 285 |
template <int dim> |
| 286 |
template <class Pol> |
| 287 |
PolynomialSpace<dim>::PolynomialSpace(const std::vector<Pol> &pols) |
| 288 |
: ScalarPolynomialsBase<dim>(pols.size(), n_polynomials(pols.size())) |
| 289 |
, polynomials(pols.begin(), pols.end()) |
| 290 |
, index_map(n_polynomials(pols.size())) |
| 291 |
, index_map_inverse(n_polynomials(pols.size())) |
| 292 |
{ |
| 293 |
// per default set this index map |
| 294 |
// to identity. This map can be |
| 295 |
// changed by the user through the |
| 296 |
// set_numbering function |
| 297 |
for (unsigned int i = 0; i < this->n(); ++i) |
| 298 |
{ |
| 299 |
index_map[i] = i; |
| 300 |
index_map_inverse[i] = i; |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
template <int dim> |
| 307 |
inline std::string |
| 308 |
PolynomialSpace<dim>::name() const |
| 309 |
{ |
| 310 |
return "PolynomialSpace"; |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
template <int dim> |
| 315 |
template <typename StreamType> |
| 316 |
void |
| 317 |
PolynomialSpace<dim>::output_indices(StreamType &out) const |
| 318 |
{ |
| 319 |
for (unsigned int i = 0; i < this->n(); ++i) |
| 320 |
{ |
| 321 |
const std::array<unsigned int, dim> ix = compute_index(i); |
| 322 |
out << i << "\t"; |
| 323 |
for (unsigned int d = 0; d < dim; ++d) |
| 324 |
out << ix[d] << ' '; |
| 325 |
out << std::endl; |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
template <int dim> |
| 330 |
template <int order> |
| 331 |
Tensor<order, dim> |
| 332 |
PolynomialSpace<dim>::compute_derivative(const unsigned int i, |
| 333 |
const Point<dim> &p) const |
| 334 |
{ |
| 335 |
const std::array<unsigned int, dim> indices = compute_index(i); |
| 336 |
|
| 337 |
ndarray<double, dim, order + 1> v; |
| 338 |
{ |
| 339 |
std::vector<double> tmp(order + 1); |
| 340 |
for (unsigned int d = 0; d < dim; ++d) |
| 341 |
{ |
| 342 |
polynomials[indices[d]].value(p[d], tmp); |
| 343 |
for (unsigned int j = 0; j < order + 1; ++j) |
| 344 |
v[d][j] = tmp[j]; |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
if constexpr (order == 1) |
| 349 |
{ |
| 350 |
Tensor<1, dim> derivative; |
| 351 |
for (unsigned int d = 0; d < dim; ++d) |
| 352 |
{ |
| 353 |
derivative[d] = 1.; |
| 354 |
for (unsigned int x = 0; x < dim; ++x) |
| 355 |
{ |
| 356 |
unsigned int x_order = 0; |
| 357 |
if (d == x) |
| 358 |
++x_order; |
| 359 |
|
| 360 |
derivative[d] *= v[x][x_order]; |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
return derivative; |
| 365 |
} |
| 366 |
else if constexpr (order == 2) |
| 367 |
{ |
| 368 |
Tensor<2, dim> derivative; |
| 369 |
for (unsigned int d1 = 0; d1 < dim; ++d1) |
| 370 |
for (unsigned int d2 = 0; d2 < dim; ++d2) |
| 371 |
{ |
| 372 |
derivative[d1][d2] = 1.; |
| 373 |
for (unsigned int x = 0; x < dim; ++x) |
| 374 |
{ |
| 375 |
unsigned int x_order = 0; |
| 376 |
if (d1 == x) |
| 377 |
++x_order; |
| 378 |
if (d2 == x) |
| 379 |
++x_order; |
| 380 |
|
| 381 |
derivative[d1][d2] *= v[x][x_order]; |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
return derivative; |
| 386 |
} |
| 387 |
else if constexpr (order == 3) |
| 388 |
{ |
| 389 |
Tensor<3, dim> derivative; |
| 390 |
for (unsigned int d1 = 0; d1 < dim; ++d1) |
| 391 |
for (unsigned int d2 = 0; d2 < dim; ++d2) |
| 392 |
for (unsigned int d3 = 0; d3 < dim; ++d3) |
| 393 |
{ |
| 394 |
derivative[d1][d2][d3] = 1.; |
| 395 |
for (unsigned int x = 0; x < dim; ++x) |
| 396 |
{ |
| 397 |
unsigned int x_order = 0; |
| 398 |
if (d1 == x) |
| 399 |
++x_order; |
| 400 |
if (d2 == x) |
| 401 |
++x_order; |
| 402 |
if (d3 == x) |
| 403 |
++x_order; |
| 404 |
|
| 405 |
derivative[d1][d2][d3] *= v[x][x_order]; |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
return derivative; |
| 410 |
} |
| 411 |
else if constexpr (order == 4) |
| 412 |
{ |
| 413 |
Tensor<4, dim> derivative; |
| 414 |
for (unsigned int d1 = 0; d1 < dim; ++d1) |
| 415 |
for (unsigned int d2 = 0; d2 < dim; ++d2) |
| 416 |
for (unsigned int d3 = 0; d3 < dim; ++d3) |
| 417 |
for (unsigned int d4 = 0; d4 < dim; ++d4) |
| 418 |
{ |
| 419 |
derivative[d1][d2][d3][d4] = 1.; |
| 420 |
for (unsigned int x = 0; x < dim; ++x) |
| 421 |
{ |
| 422 |
unsigned int x_order = 0; |
| 423 |
if (d1 == x) |
| 424 |
++x_order; |
| 425 |
if (d2 == x) |
| 426 |
++x_order; |
| 427 |
if (d3 == x) |
| 428 |
++x_order; |
| 429 |
if (d4 == x) |
| 430 |
++x_order; |
| 431 |
|
| 432 |
derivative[d1][d2][d3][d4] *= v[x][x_order]; |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
return derivative; |
| 437 |
} |
| 438 |
else |
| 439 |
{ |
| 440 |
DEAL_II_NOT_IMPLEMENTED(); |
| 441 |
return {}; |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
template <int dim> |
| 448 |
inline Tensor<1, dim> |
| 449 |
PolynomialSpace<dim>::compute_1st_derivative(const unsigned int i, |
| 450 |
const Point<dim> &p) const |
| 451 |
{ |
| 452 |
return compute_derivative<1>(i, p); |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
template <int dim> |
| 458 |
inline Tensor<2, dim> |
| 459 |
PolynomialSpace<dim>::compute_2nd_derivative(const unsigned int i, |
| 460 |
const Point<dim> &p) const |
| 461 |
{ |
| 462 |
return compute_derivative<2>(i, p); |
| 463 |
} |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
template <int dim> |
| 468 |
inline Tensor<3, dim> |
| 469 |
PolynomialSpace<dim>::compute_3rd_derivative(const unsigned int i, |
| 470 |
const Point<dim> &p) const |
| 471 |
{ |
| 472 |
return compute_derivative<3>(i, p); |
| 473 |
} |
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
template <int dim> |
| 478 |
inline Tensor<4, dim> |
| 479 |
PolynomialSpace<dim>::compute_4th_derivative(const unsigned int i, |
| 480 |
const Point<dim> &p) const |
| 481 |
{ |
| 482 |
return compute_derivative<4>(i, p); |
| 483 |
} |
| 484 |
|
| 485 |
DEAL_II_NAMESPACE_CLOSE |
| 486 |
|
| 487 |
#endif |