| 1 | // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 | // vi: set et ts=4 sw=2 sts=2: |
| 3 | // SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root |
| 4 | // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception |
| 5 | #ifndef DUNE_LOCALFUNCTIONS_BREZZIDOUGLASFORTINMARINI_CUBE_LOCALBASIS_HH |
| 6 | #define DUNE_LOCALFUNCTIONS_BREZZIDOUGLASFORTINMARINI_CUBE_LOCALBASIS_HH |
| 7 | |
| 8 | #include <algorithm> |
| 9 | #include <array> |
| 10 | #include <bitset> |
| 11 | #include <numeric> |
| 12 | #include <vector> |
| 13 | |
| 14 | #include <dune/common/fmatrix.hh> |
| 15 | #include <dune/common/fvector.hh> |
| 16 | #include <dune/common/math.hh> |
| 17 | #include <dune/common/rangeutilities.hh> |
| 18 | #include <dune/common/typetraits.hh> |
| 19 | |
| 20 | #include <dune/localfunctions/common/localbasis.hh> |
| 21 | |
| 22 | namespace Dune |
| 23 | { |
| 24 | /** |
| 25 | * \ingroup BrezziDouglasFortinMariniImpl |
| 26 | * \brief Brezzi-Douglas-Fortin-Marini shape functions on a reference cube. |
| 27 | * |
| 28 | * \tparam D Type to represent the field in the domain. |
| 29 | * \tparam R Type to represent the field in the range. |
| 30 | * \tparam dim dimension of the reference element, must be >= 2. |
| 31 | * \tparam order order of the element, must be >= 1. |
| 32 | * |
| 33 | * \nosubgrouping |
| 34 | */ |
| 35 | template<class D, class R, unsigned int dim, unsigned int order> |
| 36 | class BDFMCubeLocalBasis |
| 37 | { |
| 38 | static_assert( AlwaysFalse<D>::value, |
| 39 | "`BDFMCubeLocalBasis` not implemented for chosen `dim` and `order`." ); |
| 40 | }; |
| 41 | |
| 42 | |
| 43 | #ifndef DOXYGEN |
| 44 | template<class D, class R, unsigned int dim> |
| 45 | class BDFMCubeLocalBasis<D, R, dim, 0> |
| 46 | { |
| 47 | static_assert( AlwaysFalse<D>::value, |
| 48 | "`BDFMCubeLocalBasis` not defined for order 0." ); |
| 49 | }; |
| 50 | #endif // #ifndef DOXYGEN |
| 51 | |
| 52 | |
| 53 | /** |
| 54 | * \brief First order Brezzi-Douglas-Fortin-Marini shape functions on the reference quadrialteral. |
| 55 | * |
| 56 | * \nosubgrouping |
| 57 | */ |
| 58 | template<class D, class R > |
| 59 | class BDFMCubeLocalBasis<D, R, 2, 1> |
| 60 | { |
| 61 | using DomainType = FieldVector<D, 2>; |
| 62 | using RangeType = FieldVector<R, 2>; |
| 63 | using JacobianType = FieldMatrix<R, 2, 2>; |
| 64 | |
| 65 | public: |
| 66 | using Traits = LocalBasisTraits<D, 2, DomainType, R, 2, RangeType, JacobianType>; |
| 67 | |
| 68 | //! \brief Standard constructor |
| 69 | BDFMCubeLocalBasis () |
| 70 | { |
| 71 | std::fill(s_.begin(), s_.end(), 1); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * \brief Make set number s, where 0<= s < 16 |
| 76 | * |
| 77 | * \param s Edge orientation indicator |
| 78 | */ |
| 79 | BDFMCubeLocalBasis (std::bitset<4> s) |
| 80 | { |
| 81 | for (auto i : range(4)) |
| 82 | s_[i] = s[i] ? -1 : 1; |
| 83 | } |
| 84 | |
| 85 | //! \brief number of shape functions |
| 86 | unsigned int size () const { return 4; } |
| 87 | |
| 88 | /** |
| 89 | * \brief Evaluate all shape functions |
| 90 | * |
| 91 | * \param in Position |
| 92 | * \param out return value |
| 93 | */ |
| 94 | inline void evaluateFunction (const DomainType& in, std::vector<RangeType>& out) const |
| 95 | { |
| 96 | out.resize(4); |
| 97 | |
| 98 | out[0] = {s_[0]*(in[0]-1), 0 }; |
| 99 | out[1] = {s_[1]*(in[0]) , 0 }; |
| 100 | out[2] = {0, s_[2]*(in[1]-1)}; |
| 101 | out[3] = {0, s_[3]*(in[1]) }; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * \brief Evaluate Jacobian of all shape functions |
| 106 | * |
| 107 | * \param in Position |
| 108 | * \param out return value |
| 109 | */ |
| 110 | inline void evaluateJacobian (const DomainType& in, std::vector<JacobianType>& out) const |
| 111 | { |
| 112 | out.resize(4); |
| 113 | |
| 114 | out[0] = {{s_[0], 0}, {0, 0}}; |
| 115 | out[1] = {{s_[1], 0}, {0, 0}}; |
| 116 | out[2] = {{0, 0}, {0, s_[2]}}; |
| 117 | out[3] = {{0, 0}, {0, s_[3]}}; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * \brief Evaluate all partial derivatives of all shape functions |
| 122 | * |
| 123 | * \param order order the partial derivative |
| 124 | * \param in Position |
| 125 | * \param out return value |
| 126 | */ |
| 127 | void partial (const std::array<unsigned int, 2>& order, |
| 128 | const DomainType& in, |
| 129 | std::vector<RangeType>& out) const |
| 130 | { |
| 131 | if (std::accumulate(order.begin(), order.end(), 0) == 0) |
| 132 | evaluateFunction(in, out); |
| 133 | else |
| 134 | DUNE_THROW(NotImplemented, "Desired derivative order is not implemented"); |
| 135 | } |
| 136 | |
| 137 | //! \brief Polynomial order of the shape functions |
| 138 | unsigned int order () const { return 1; } |
| 139 | |
| 140 | private: |
| 141 | std::array<R, 4> s_; |
| 142 | }; |
| 143 | |
| 144 | |
| 145 | /** |
| 146 | * \brief Second order Brezzi-Douglas-Fortin-Marini shape functions on the reference quadrialteral. |
| 147 | * |
| 148 | * \nosubgrouping |
| 149 | */ |
| 150 | template<class D, class R > |
| 151 | class BDFMCubeLocalBasis<D, R, 2, 2> |
| 152 | { |
| 153 | using DomainType = FieldVector<D, 2>; |
| 154 | using RangeType = FieldVector<R, 2>; |
| 155 | using JacobianType = FieldMatrix<R, 2, 2>; |
| 156 | |
| 157 | public: |
| 158 | using Traits = LocalBasisTraits<D, 2, DomainType, R, 2, RangeType, JacobianType>; |
| 159 | |
| 160 | //! \brief Standard constructor |
| 161 | BDFMCubeLocalBasis () |
| 162 | { |
| 163 | std::fill(s_.begin(), s_.end(), 1); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * \brief Make set number s, where 0<= s < 16 |
| 168 | * |
| 169 | * \param s Edge orientation indicator |
| 170 | */ |
| 171 | BDFMCubeLocalBasis (std::bitset<4> s) |
| 172 | { |
| 173 | for (auto i : range(4)) |
| 174 | s_[i] = s[i] ? -1 : 1; |
| 175 | } |
| 176 | |
| 177 | //! \brief number of shape functions |
| 178 | unsigned int size () const { return 10; } |
| 179 | |
| 180 | /** |
| 181 | * \brief Evaluate all shape functions |
| 182 | * |
| 183 | * \param in Position |
| 184 | * \param out return value |
| 185 | */ |
| 186 | inline void evaluateFunction (const DomainType& in, std::vector<RangeType>& out) const |
| 187 | { |
| 188 | out.resize(10); |
| 189 | |
| 190 | const auto& x = in[0]; |
| 191 | const auto& y = in[1]; |
| 192 | |
| 193 | auto pre = 1-x; |
| 194 | out[0] = {pre*s_[0]*(3*x-1), 0}; |
| 195 | out[1] = { pre*3*(2*y-1), 0}; |
| 196 | |
| 197 | pre = x; |
| 198 | out[2] = {pre*s_[1]*(3*x-2), 0}; |
| 199 | out[3] = {pre* 3*(2*y-1), 0}; |
| 200 | |
| 201 | pre = 1-y; |
| 202 | out[4] = {0, pre*s_[2]*(3*y-1)}; |
| 203 | out[5] = {0, pre*3*(2*x-1)}; |
| 204 | |
| 205 | pre = y; |
| 206 | out[6] = {0, pre*s_[3]*(3*y-2)}; |
| 207 | out[7] = {0, pre*3*(2*x-1)}; |
| 208 | |
| 209 | out[8] = {6*x*(1-x), 0}; |
| 210 | |
| 211 | out[9] = {0, 6*y*(1-y)}; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * \brief Evaluate Jacobian of all shape functions |
| 216 | * |
| 217 | * \param in Position |
| 218 | * \param out return value |
| 219 | */ |
| 220 | inline void evaluateJacobian (const DomainType& in, std::vector<JacobianType>& out) const |
| 221 | { |
| 222 | out.resize(10); |
| 223 | |
| 224 | const auto& x = in[0]; |
| 225 | const auto& y = in[1]; |
| 226 | |
| 227 | out[0] = {{s_[0]*(4-6*x), 0}, {0, 0}}; |
| 228 | out[1] = {{3-6*y, 6-6*x}, {0, 0}}; |
| 229 | |
| 230 | out[2] = {{s_[1]*(6*x-2), 0}, {0, 0}}; |
| 231 | out[3] = {{6*y-3, 6*x}, {0, 0}}; |
| 232 | |
| 233 | out[4] = {{0, 0}, {0, s_[2]*(4-6*y)}}; |
| 234 | out[5] = {{0, 0}, {6-6*y, 3-6*x}}; |
| 235 | |
| 236 | out[6] = {{0, 0}, {0, s_[3]*(6*y-2)}}; |
| 237 | out[7] = {{0, 0}, {6*y, 6*x-3}}; |
| 238 | |
| 239 | out[8] = {{6-12*x, 0}, {0, 0}}; |
| 240 | |
| 241 | out[9] = {{0, 0}, {0, 6-12*y}}; |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * \brief Evaluate all partial derivatives of all shape functions |
| 246 | * |
| 247 | * \param order order the partial derivative |
| 248 | * \param in Position |
| 249 | * \param out return value |
| 250 | */ |
| 251 | void partial (const std::array<unsigned int, 2>& order, |
| 252 | const DomainType& in, |
| 253 | std::vector<RangeType>& out) const |
| 254 | { |
| 255 | if (std::accumulate(order.begin(), order.end(), 0) == 0) |
| 256 | evaluateFunction(in, out); |
| 257 | else |
| 258 | DUNE_THROW(NotImplemented, "Desired derivative order is not implemented"); |
| 259 | } |
| 260 | |
| 261 | //! \brief Polynomial order of the shape functions |
| 262 | unsigned int order () const { return 2; } |
| 263 | |
| 264 | private: |
| 265 | std::array<R, 4> s_; |
| 266 | }; |
| 267 | |
| 268 | |
| 269 | /** |
| 270 | * \brief Third order Brezzi-Douglas-Fortin-Marini shape functions on the reference quadrialteral. |
| 271 | * |
| 272 | * \nosubgrouping |
| 273 | */ |
| 274 | template<class D, class R > |
| 275 | class BDFMCubeLocalBasis<D, R, 2, 3> |
| 276 | { |
| 277 | using DomainType = FieldVector<D, 2>; |
| 278 | using RangeType = FieldVector<R, 2>; |
| 279 | using JacobianType = FieldMatrix<R, 2, 2>; |
| 280 | |
| 281 | public: |
| 282 | using Traits = LocalBasisTraits<D, 2, DomainType, R, 2, RangeType, JacobianType>; |
| 283 | |
| 284 | //! \brief Standard constructor |
| 285 | BDFMCubeLocalBasis () |
| 286 | { |
| 287 | std::fill(s_.begin(), s_.end(), 1); |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * \brief Make set number s, where 0<= s < 16 |
| 292 | * |
| 293 | * \param s Edge orientation indicator |
| 294 | */ |
| 295 | BDFMCubeLocalBasis (std::bitset<4> s) |
| 296 | { |
| 297 | for (auto i : range(4)) |
| 298 | s_[i] = s[i] ? -1 : 1; |
| 299 | } |
| 300 | |
| 301 | //! \brief number of shape functions |
| 302 | unsigned int size () const { return 18; } |
| 303 | |
| 304 | /** |
| 305 | * \brief Evaluate all shape functions |
| 306 | * |
| 307 | * \param in Position |
| 308 | * \param out return value |
| 309 | */ |
| 310 | inline void evaluateFunction (const DomainType& in, std::vector<RangeType>& out) const |
| 311 | { |
| 312 | out.resize(18); |
| 313 | |
| 314 | const auto& x = in[0]; |
| 315 | const auto& y = in[1]; |
| 316 | |
| 317 | auto pre = 1-x; |
| 318 | out[0] = {pre*s_[0]*-1*(10*x*x-8*x+1), 0}; |
| 319 | out[1] = {pre* 3*(1-3*x)*(2*y-1), 0}; |
| 320 | out[2] = {pre* s_[0]*-5*(6*y*y-6*y+1), 0}; |
| 321 | |
| 322 | pre = x; |
| 323 | out[3] = {pre*s_[1]*(10*x*x-12*x+3), 0}; |
| 324 | out[4] = {pre* 3*(3*x-2)*(2*y-1), 0}; |
| 325 | out[5] = {pre*s_[1]*5*(6*y*y-6*y+1), 0}; |
| 326 | |
| 327 | pre = 1-y; |
| 328 | out[6] = {0, pre*s_[2]*-1*(10*y*y-8*y+1)}; |
| 329 | out[7] = {0, pre* 3*(1-3*y)*(2*x-1)}; |
| 330 | out[8] = {0, pre*s_[2]*-5*( 6*x*x-6*x+1)}; |
| 331 | |
| 332 | pre = y; |
| 333 | out[9] = {0, pre*s_[3]*(10*y*y-12*y+3)}; |
| 334 | out[10] = {0, pre* 3*(3*y-2)*(2*x-1)}; |
| 335 | out[11] = {0, pre*s_[3]*5*(6*x*x-6*x+1)}; |
| 336 | |
| 337 | pre = x*(1-x); |
| 338 | out[12] = {pre* 6 , 0}; |
| 339 | out[14] = {pre*30*(2*x-1), 0}; |
| 340 | out[16] = {pre*18*(2*y-1), 0}; |
| 341 | |
| 342 | pre = y*(1-y); |
| 343 | out[13] = {0, pre* 6 }; |
| 344 | out[15] = {0, pre*18*(2*x-1)}; |
| 345 | out[17] = {0, pre*30*(2*y-1)}; |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * \brief Evaluate Jacobian of all shape functions |
| 350 | * |
| 351 | * \param in Position |
| 352 | * \param out return value |
| 353 | */ |
| 354 | inline void evaluateJacobian (const DomainType& in, std::vector<JacobianType>& out) const |
| 355 | { |
| 356 | out.resize(18); |
| 357 | |
| 358 | const auto& x = in[0]; |
| 359 | const auto& y = in[1]; |
| 360 | |
| 361 | out[0] = {{s_[0]*(30*x*x-36*x+9), 0}, {0, 0}}; |
| 362 | out[1] = {{ 6*(6*x*y-3*x-4*y+2), 6*(3*x*x-4*x+1)}, {0, 0}}; |
| 363 | out[2] = {{s_[0]*5*(6*y*y-6*y+1), s_[0]*30*(x-1)*(2*y-1)}, {0, 0}}; |
| 364 | |
| 365 | out[3] = {{ s_[1]*30*x*x-24*x+3, 0}, {0, 0}}; |
| 366 | out[4] = {{ 6*(3*x-1)*(2*y-1), 6*x*(3*x-2)}, {0, 0}}; |
| 367 | out[5] = {{s_[1]*5*(6*y*y-6*y+1), s_[1]*30*x*(2*y-1)}, {0, 0}}; |
| 368 | |
| 369 | out[6] = {{0, 0}, { 0, s_[2]*(30*y*y-36*y+9)}}; |
| 370 | out[7] = {{0, 0}, { 6*(3*y*y-4*y+1), 6*(6*y*x-3*y-4*x+2)}}; |
| 371 | out[8] = {{0, 0}, {s_[2]*30*(y-1)*(2*x-1), s_[2]*5*(6*x*x-6*x+1)}}; |
| 372 | |
| 373 | out[9] = {{0, 0}, { 0, s_[3]*30*y*y-24*y+3}}; |
| 374 | out[10] = {{0, 0}, { 6*y*(3*y-2), 6*(3*y-1)*(2*x-1)}}; |
| 375 | out[11] = {{0, 0}, {s_[3]*30*y*(2*x-1), s_[3]*5*(6*x*x-6*x+1)}}; |
| 376 | |
| 377 | out[12] = {{ -6*(2*x-1), 0}, {0, 0}}; |
| 378 | out[14] = {{ -30*(6*x*x-6*x+1), 0}, {0, 0}}; |
| 379 | out[16] = {{-18*(2*x-1)*(2*y-1), 36*x*(1-x)}, {0, 0}}; |
| 380 | |
| 381 | out[13] = {{0, 0}, { 0, -6*(2*y-1)}}; |
| 382 | out[15] = {{0, 0}, {36*y*(1-y), -18*(2*y-1)*(2*x-1)}}; |
| 383 | out[17] = {{0, 0}, { 0, -30*(6*y*y-6*y+1)}}; |
| 384 | } |
| 385 | |
| 386 | /** |
| 387 | * \brief Evaluate all partial derivatives of all shape functions |
| 388 | * |
| 389 | * \param order order the partial derivative |
| 390 | * \param in Position |
| 391 | * \param out return value |
| 392 | */ |
| 393 | void partial (const std::array<unsigned int, 2>& order, |
| 394 | const DomainType& in, |
| 395 | std::vector<RangeType>& out) const |
| 396 | { |
| 397 | if (std::accumulate(order.begin(), order.end(), 0) == 0) |
| 398 | evaluateFunction(in, out); |
| 399 | else |
| 400 | DUNE_THROW(NotImplemented, "Desired derivative order is not implemented"); |
| 401 | } |
| 402 | |
| 403 | //! \brief Polynomial order of the shape functions |
| 404 | unsigned int order () const { return 3; } |
| 405 | |
| 406 | private: |
| 407 | std::array<R, 4> s_; |
| 408 | }; |
| 409 | |
| 410 | } // namespace Dune |
| 411 | |
| 412 | #endif // #ifndef DUNE_LOCALFUNCTIONS_BREZZIDOUGLASFORTINMARINI_CUBE_LOCALBASIS_HH |