// ------------------------------------------------------------------------ // // SPDX-License-Identifier: LGPL-2.1-or-later // Copyright (C) 2002 - 2024 by the deal.II authors // // This file is part of the deal.II library. // // Part of the source code is dual licensed under Apache-2.0 WITH // LLVM-exception OR LGPL-2.1-or-later. Detailed license information // governing the source code and code contributions can be found in // LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II. // // ------------------------------------------------------------------------ #ifndef dealii_polynomial_space_h #define dealii_polynomial_space_h #include #include #include #include #include #include #include #include #include DEAL_II_NAMESPACE_OPEN /** * Representation of the space of polynomials of degree at most n in higher * dimensions. * * Given a vector of n one-dimensional polynomials P0 * to Pn, where Pi has degree i, * this class generates all dim-dimensional polynomials of the form * Pijk(x,y,z) = * Pi(x)Pj(y)Pk(z), where the sum of * i, j and k is less than or equal n. * * The output_indices() function prints the ordering of the polynomials, i.e. * for each dim-dimensional polynomial in the polynomial space it gives the * indices i,j,k of the one-dimensional polynomials in x,y and z direction. * The ordering of the dim-dimensional polynomials can be changed by using the * set_numbering() function. * * The standard ordering of polynomials is that indices for the first space * dimension vary fastest and the last space dimension is slowest. In * particular, if we take for simplicity the vector of monomials * x0, x1, x2,..., xn, we * get * *
1d
x0, x1,...,xn *
2d:
x0y0, * x1y0,..., xny0, *
* x0y1, x1y1,..., * xn-1y1, *
* x0y2,... xn-2y2, *
* ... *
* x0yn-1, x1yn-1, *
* x0yn
3d:
* x0y0z0,..., * xny0z0, *
* x0y1z0,..., * xn-1y1z0, *
* ... *
* x0ynz0, *
* x0y0z1,... * xn-1y0z1, *
* ... *
* x0yn-1z1, *
* x0y0z2,... * xn-2y0z2, *
* ... *
* x0y0zn
* * @ingroup Polynomials */ template class PolynomialSpace : public ScalarPolynomialsBase { public: /** * Access to the dimension of this object, for checking and automatic * setting of dimension in other classes. */ static constexpr unsigned int dimension = dim; /** * Constructor. pols is a vector of pointers to one-dimensional * polynomials and will be copied into a private member variable. The static * type of the template argument pols needs to be convertible to * Polynomials::Polynomial@, i.e. should usually be a derived class * of Polynomials::Polynomial@. */ template PolynomialSpace(const std::vector &pols); /** * Prints the list of the indices to out. */ template void output_indices(StreamType &out) const; /** * Set the ordering of the polynomials. Requires * renumber.size()==n(). Stores a copy of renumber. */ void set_numbering(const std::vector &renumber); /** * Compute the value and the first and second derivatives of each * polynomial at unit_point. * * The size of the vectors must either be equal 0 or equal n(). In the first * case, the function will not compute these values, i.e. you indicate what * you want to have computed by resizing those vectors which you want * filled. * * If you need values or derivatives of all polynomials then use this * function, rather than using any of the compute_value(), compute_grad() or * compute_grad_grad() functions, see below, in a loop over all polynomials. */ void evaluate(const Point &unit_point, std::vector &values, std::vector> &grads, std::vector> &grad_grads, std::vector> &third_derivatives, std::vector> &fourth_derivatives) const override; /** * Compute the value of the ith polynomial at unit point * p. * * Consider using evaluate() instead. */ double compute_value(const unsigned int i, const Point &p) const override; /** * Compute the orderth derivative of the ith polynomial * at unit point p. * * Consider using evaluate() instead. * * @tparam order The order of the derivative. */ template Tensor compute_derivative(const unsigned int i, const Point &p) const; /** * @copydoc ScalarPolynomialsBase::compute_1st_derivative() */ virtual Tensor<1, dim> compute_1st_derivative(const unsigned int i, const Point &p) const override; /** * @copydoc ScalarPolynomialsBase::compute_2nd_derivative() */ virtual Tensor<2, dim> compute_2nd_derivative(const unsigned int i, const Point &p) const override; /** * @copydoc ScalarPolynomialsBase::compute_3rd_derivative() */ virtual Tensor<3, dim> compute_3rd_derivative(const unsigned int i, const Point &p) const override; /** * @copydoc ScalarPolynomialsBase::compute_4th_derivative() */ virtual Tensor<4, dim> compute_4th_derivative(const unsigned int i, const Point &p) const override; /** * Compute the gradient of the ith polynomial at unit point * p. * * Consider using evaluate() instead. */ Tensor<1, dim> compute_grad(const unsigned int i, const Point &p) const override; /** * Compute the second derivative (grad_grad) of the ith polynomial * at unit point p. * * Consider using evaluate() instead. */ Tensor<2, dim> compute_grad_grad(const unsigned int i, const Point &p) const override; /** * Return the number of polynomials spanning the space represented by this * class. Here, if N is the number of one-dimensional polynomials * given, then the result of this function is N in 1d, * N(N+1)/2 in 2d, and N(N+1)(N+2)/6 in 3d. */ static unsigned int n_polynomials(const unsigned int n); /** * Return the name of the space, which is PolynomialSpace. */ std::string name() const override; /** * @copydoc ScalarPolynomialsBase::clone() */ virtual std::unique_ptr> clone() const override; protected: /** * Compute numbers in x, y and z direction. Given an index n in the * d-dimensional polynomial space, return the indices i,j,k such that * pn(x,y,z) = * pi(x)pj(y)pk(z). * * In 1d and 2d, obviously only i and i,j are returned. */ std::array compute_index(const unsigned int n) const; private: /** * Copy of the vector pols of polynomials given to the constructor. */ const std::vector> polynomials; /** * Index map for reordering the polynomials. */ std::vector index_map; /** * Index map for reordering the polynomials. */ std::vector index_map_inverse; }; /* -------------- declaration of explicit specializations --- */ template <> std::array PolynomialSpace<1>::compute_index(const unsigned int n) const; template <> std::array PolynomialSpace<2>::compute_index(const unsigned int n) const; template <> std::array PolynomialSpace<3>::compute_index(const unsigned int n) const; /* -------------- inline and template functions ------------- */ template template PolynomialSpace::PolynomialSpace(const std::vector &pols) : ScalarPolynomialsBase(pols.size(), n_polynomials(pols.size())) , polynomials(pols.begin(), pols.end()) , index_map(n_polynomials(pols.size())) , index_map_inverse(n_polynomials(pols.size())) { // per default set this index map // to identity. This map can be // changed by the user through the // set_numbering function for (unsigned int i = 0; i < this->n(); ++i) { index_map[i] = i; index_map_inverse[i] = i; } } template inline std::string PolynomialSpace::name() const { return "PolynomialSpace"; } template template void PolynomialSpace::output_indices(StreamType &out) const { for (unsigned int i = 0; i < this->n(); ++i) { const std::array ix = compute_index(i); out << i << "\t"; for (unsigned int d = 0; d < dim; ++d) out << ix[d] << ' '; out << std::endl; } } template template Tensor PolynomialSpace::compute_derivative(const unsigned int i, const Point &p) const { const std::array indices = compute_index(i); ndarray v; { std::vector tmp(order + 1); for (unsigned int d = 0; d < dim; ++d) { polynomials[indices[d]].value(p[d], tmp); for (unsigned int j = 0; j < order + 1; ++j) v[d][j] = tmp[j]; } } if constexpr (order == 1) { Tensor<1, dim> derivative; for (unsigned int d = 0; d < dim; ++d) { derivative[d] = 1.; for (unsigned int x = 0; x < dim; ++x) { unsigned int x_order = 0; if (d == x) ++x_order; derivative[d] *= v[x][x_order]; } } return derivative; } else if constexpr (order == 2) { Tensor<2, dim> derivative; for (unsigned int d1 = 0; d1 < dim; ++d1) for (unsigned int d2 = 0; d2 < dim; ++d2) { derivative[d1][d2] = 1.; for (unsigned int x = 0; x < dim; ++x) { unsigned int x_order = 0; if (d1 == x) ++x_order; if (d2 == x) ++x_order; derivative[d1][d2] *= v[x][x_order]; } } return derivative; } else if constexpr (order == 3) { Tensor<3, dim> derivative; for (unsigned int d1 = 0; d1 < dim; ++d1) for (unsigned int d2 = 0; d2 < dim; ++d2) for (unsigned int d3 = 0; d3 < dim; ++d3) { derivative[d1][d2][d3] = 1.; for (unsigned int x = 0; x < dim; ++x) { unsigned int x_order = 0; if (d1 == x) ++x_order; if (d2 == x) ++x_order; if (d3 == x) ++x_order; derivative[d1][d2][d3] *= v[x][x_order]; } } return derivative; } else if constexpr (order == 4) { Tensor<4, dim> derivative; for (unsigned int d1 = 0; d1 < dim; ++d1) for (unsigned int d2 = 0; d2 < dim; ++d2) for (unsigned int d3 = 0; d3 < dim; ++d3) for (unsigned int d4 = 0; d4 < dim; ++d4) { derivative[d1][d2][d3][d4] = 1.; for (unsigned int x = 0; x < dim; ++x) { unsigned int x_order = 0; if (d1 == x) ++x_order; if (d2 == x) ++x_order; if (d3 == x) ++x_order; if (d4 == x) ++x_order; derivative[d1][d2][d3][d4] *= v[x][x_order]; } } return derivative; } else { DEAL_II_NOT_IMPLEMENTED(); return {}; } } template inline Tensor<1, dim> PolynomialSpace::compute_1st_derivative(const unsigned int i, const Point &p) const { return compute_derivative<1>(i, p); } template inline Tensor<2, dim> PolynomialSpace::compute_2nd_derivative(const unsigned int i, const Point &p) const { return compute_derivative<2>(i, p); } template inline Tensor<3, dim> PolynomialSpace::compute_3rd_derivative(const unsigned int i, const Point &p) const { return compute_derivative<3>(i, p); } template inline Tensor<4, dim> PolynomialSpace::compute_4th_derivative(const unsigned int i, const Point &p) const { return compute_derivative<4>(i, p); } DEAL_II_NAMESPACE_CLOSE #endif