| 1 | // SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root |
| 2 | // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception |
| 3 | #ifndef DUNE_LOCALFUNCTIONS_BREZZIDOUGLASFORTINMARINI_CUBE_LOCALINTERPOLATION_HH |
| 4 | #define DUNE_LOCALFUNCTIONS_BREZZIDOUGLASFORTINMARINI_CUBE_LOCALINTERPOLATION_HH |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <array> |
| 8 | #include <bitset> |
| 9 | #include <vector> |
| 10 | #include <limits> |
| 11 | |
| 12 | #include <dune/common/fvector.hh> |
| 13 | #include <dune/common/math.hh> |
| 14 | #include <dune/common/rangeutilities.hh> |
| 15 | #include <dune/common/typetraits.hh> |
| 16 | |
| 17 | #include <dune/geometry/quadraturerules.hh> |
| 18 | |
| 19 | |
| 20 | namespace Dune |
| 21 | { |
| 22 | |
| 23 | /** |
| 24 | * \ingroup BrezziDouglasFortinMariniImpl |
| 25 | * \brief Interpolation for Brezzi-Douglas-Fortin-Marini shape functions on cubes. |
| 26 | * |
| 27 | * \tparam D Type of represent the field in the domain. |
| 28 | * \tparam R Type of represent the field in the domain. |
| 29 | * \tparam dim dimension of the reference element, must be >= 2. |
| 30 | * \tparam order order of the element, must be >= 1. |
| 31 | * |
| 32 | * \nosubgrouping |
| 33 | */ |
| 34 | template<class D, class R, unsigned int dim, unsigned int order> |
| 35 | class BDFMCubeLocalInterpolation |
| 36 | { |
| 37 | using DomainType = FieldVector<D, dim>; |
| 38 | using FaceDomainType = FieldVector<D, dim-1>; |
| 39 | using RangeType = FieldVector<R, dim>; |
| 40 | using DomainFieldType = D; |
| 41 | using RangeFieldType = R; |
| 42 | |
| 43 | static constexpr std::size_t numFaces = 2*dim; |
| 44 | |
| 45 | static constexpr unsigned int interiorDofs = dim*binomial(dim+order-2, order-2); |
| 46 | static constexpr unsigned int faceDofs = binomial(dim+order-2, order-1); |
| 47 | |
| 48 | /** |
| 49 | * \brief compute the i'th shifted Legendre function on [0,1] |
| 50 | * |
| 51 | * \param i index of function to compute |
| 52 | * \param x Position |
| 53 | */ |
| 54 | inline static auto legendre( unsigned int i, const DomainFieldType& x ) |
| 55 | -> RangeFieldType |
| 56 | { |
| 57 | switch( i ) |
| 58 | { |
| 59 | case 0: |
| 60 | return 1; |
| 61 | case 1: |
| 62 | return 2*x-1; |
| 63 | case 2: |
| 64 | return 6*x*x-6*x+1; |
| 65 | case 3: |
| 66 | return 20*x*x*x-30*x*x+12*x-1; |
| 67 | default: |
| 68 | return ((2*i-1)*(2*x-1)*legendre(x,i-1) - (i-1)*legendre(x,i-2)) / i; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * \brief calculate the i`th multi index in graded lexigraphic order |
| 74 | * |
| 75 | * \tparam d number of components of the multi index |
| 76 | * \tparam kMax maximum absolute value to consider. (default is `unlimited`) |
| 77 | * |
| 78 | * \param i index |
| 79 | * |
| 80 | */ |
| 81 | template<std::size_t d, std::size_t kMax = std::numeric_limits<std::size_t>::max()> |
| 82 | constexpr inline static auto unrank (std::size_t i) |
| 83 | -> std::array<std::size_t, d> |
| 84 | { |
| 85 | assert( i < binomial(d+kMax, kMax)); |
| 86 | |
| 87 | std::array<std::size_t, d> mi{}; |
| 88 | if (i == 0) |
| 89 | return mi; |
| 90 | |
| 91 | std::size_t k = 0, b = 1; |
| 92 | for(;k <= kMax && b <= i; ++k, b = binomial(d+k-1, k)) |
| 93 | i -= b; |
| 94 | |
| 95 | std::size_t p = 0; |
| 96 | for(; p < d && i > 0; ++p) |
| 97 | { |
| 98 | std::size_t m = 0, c = 1; |
| 99 | for(; m <= k && c <= i; ++m, c = binomial(d-p+m-2, m)) |
| 100 | i -= c; |
| 101 | |
| 102 | mi[p] = k-m; |
| 103 | k = m; |
| 104 | } |
| 105 | mi[p] = k; |
| 106 | |
| 107 | return mi; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * \brief embed the a local position on a face into the reference element |
| 112 | * |
| 113 | * \param face index of the face |
| 114 | * \param x local position |
| 115 | */ |
| 116 | inline static auto embed (unsigned int face, const FaceDomainType& x ) |
| 117 | -> DomainType |
| 118 | { |
| 119 | DomainType y; |
| 120 | |
| 121 | std::size_t j = 0; |
| 122 | for (auto i : range(dim)) |
| 123 | if (i == face/2) |
| 124 | y[i] = face%2; |
| 125 | else |
| 126 | y[i] = x[j++]; |
| 127 | |
| 128 | return y; |
| 129 | } |
| 130 | |
| 131 | public: |
| 132 | //! \brief Standard constructor |
| 133 | BDFMCubeLocalInterpolation () |
| 134 | { |
| 135 | std::fill(sign_.begin(), sign_.end(), 1.0); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * \brief Make set number s, where 0 <= s < 2^(2*dim) |
| 140 | * |
| 141 | * \param s Edge orientation indicator |
| 142 | */ |
| 143 | BDFMCubeLocalInterpolation (std::bitset<numFaces> s) |
| 144 | { |
| 145 | for (auto i : range(numFaces)) |
| 146 | { |
| 147 | sign_[i] = s[i] ? -1 : 1; |
| 148 | normal_[i][i/2] = i%2 ? 1 : -1; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * \brief Interpolate a given function with shape functions |
| 154 | * |
| 155 | * \tparam F Function type for function which should be interpolated |
| 156 | * \tparam C Coefficient vector type |
| 157 | * |
| 158 | * \param f function which should be interpolated |
| 159 | * \param out return value, vector of coefficients |
| 160 | */ |
| 161 | template<class F, class C> |
| 162 | void interpolate (const F& f, C& out) const |
| 163 | { |
| 164 | out.resize(numFaces*faceDofs + interiorDofs); |
| 165 | std::fill(out.begin(),out.end(), 0.0); |
| 166 | |
| 167 | for(auto i : range(numFaces)) |
| 168 | trace(i, f, out); |
| 169 | |
| 170 | interior(f, out); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * \brief Interpolate a given function with shape functions on a face of the reference element |
| 175 | * |
| 176 | * \tparam F Function type for function which should be interpolated |
| 177 | * \tparam C Coefficient vector type |
| 178 | * |
| 179 | * \param face index of the face on which to interpolate |
| 180 | * \param f function which should be interpolated |
| 181 | * \param out return value, vector of coefficients |
| 182 | * |
| 183 | */ |
| 184 | template<class F, class C> |
| 185 | void trace (unsigned int face, const F& f, C& out) const |
| 186 | { |
| 187 | assert(out.size() >= numFaces*faceDofs + interiorDofs); |
| 188 | assert(face < numFaces); |
| 189 | |
| 190 | const auto o = face*faceDofs; |
| 191 | const auto& n = normal_[face]; |
| 192 | const auto& s = sign_[face]; |
| 193 | const auto& c = n[face/2]; |
| 194 | |
| 195 | const auto& rule = QuadratureRules<DomainFieldType, dim-1>::rule(GeometryTypes::cube(dim-1), order+(order-1)+5); |
| 196 | for (const auto& qp : rule) |
| 197 | { |
| 198 | const auto& x = qp.position(); |
| 199 | auto y = f(embed(face, x)); |
| 200 | |
| 201 | for (auto i : range(faceDofs)) |
| 202 | { |
| 203 | auto mi = unrank<dim-1,order-1>(i); |
| 204 | |
| 205 | RangeFieldType phi = 1.; |
| 206 | for (auto j : range(dim-1)) |
| 207 | phi *= legendre(mi[j], x[j]); |
| 208 | |
| 209 | out[o+i] += (y*n) * phi * qp.weight() * (i%2 ? c : s); |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * \brief Interpolate a given function with shape functions in the interior of the reference element |
| 216 | * |
| 217 | * \tparam F Function type for function which should be interpolated |
| 218 | * \tparam C Coefficient vector type |
| 219 | * |
| 220 | * \param f function which should be interpolated |
| 221 | * \param out return value, vector of coefficients |
| 222 | */ |
| 223 | template<class F, class C> |
| 224 | void interior (const F& f, C& out) const |
| 225 | { |
| 226 | assert(out.size() >= numFaces*faceDofs + interiorDofs); |
| 227 | |
| 228 | const auto o = numFaces*faceDofs; |
| 229 | |
| 230 | const auto& rule = QuadratureRules<DomainFieldType, dim>::rule(GeometryTypes::cube(dim), order+std::max((int)order-2,(int)0)+5); |
| 231 | for(const auto& qp : rule) |
| 232 | { |
| 233 | const auto& x = qp.position(); |
| 234 | auto y = f(x); |
| 235 | |
| 236 | for (auto i : range(interiorDofs/dim)) |
| 237 | { |
| 238 | auto mi = unrank<dim,order-2>(i); |
| 239 | |
| 240 | RangeFieldType phi = 1.; |
| 241 | for (auto j : range(dim)) |
| 242 | phi *= legendre(mi[j], x[j]); |
| 243 | |
| 244 | for (auto j : range(dim)) |
| 245 | out[o+dim*i+j] += y[j]* phi * qp.weight(); |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | private: |
| 251 | std::array<RangeFieldType, numFaces> sign_; |
| 252 | std::array<DomainType, numFaces> normal_; |
| 253 | }; |
| 254 | |
| 255 | |
| 256 | template<class D, class R, unsigned int dim, unsigned int order> |
| 257 | constexpr std::size_t BDFMCubeLocalInterpolation<D, R, dim, order>::numFaces; |
| 258 | |
| 259 | template<class D, class R, unsigned int dim, unsigned int order> |
| 260 | constexpr unsigned int BDFMCubeLocalInterpolation<D, R, dim, order>::interiorDofs; |
| 261 | |
| 262 | template<class D, class R, unsigned int dim, unsigned int order> |
| 263 | constexpr unsigned int BDFMCubeLocalInterpolation<D, R, dim, order>::faceDofs; |
| 264 | |
| 265 | |
| 266 | #ifndef DOXYGEN |
| 267 | template<class D, class R, unsigned int dim> |
| 268 | class BDFMCubeLocalInterpolation<D, R, dim, 0> |
| 269 | { |
| 270 | static_assert(AlwaysFalse<D>::value, |
| 271 | "`BDFMCubeLocalCoefficients` not defined for order 0."); |
| 272 | }; |
| 273 | #endif //#ifndef DOXYGEN |
| 274 | |
| 275 | } // namespace Dune |
| 276 | |
| 277 | #endif // #ifndef DUNE_LOCALFUNCTIONS_BREZZIDOUGLASFORTINMARINI_CUBE_LOCALINTERPOLATION_HH |