| 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_RANNACHER_TUREK_2D_LOCALBASIS_HH |
| 6 | #define DUNE_RANNACHER_TUREK_2D_LOCALBASIS_HH |
| 7 | |
| 8 | #include <numeric> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include <dune/common/fvector.hh> |
| 12 | #include <dune/common/fmatrix.hh> |
| 13 | |
| 14 | #include <dune/localfunctions/common/localbasis.hh> |
| 15 | |
| 16 | namespace Dune |
| 17 | { |
| 18 | |
| 19 | /** |
| 20 | * \ingroup RannacherTurekImpl |
| 21 | */ |
| 22 | template< class D, class R > |
| 23 | struct RannacherTurek2DLocalBasis |
| 24 | { |
| 25 | typedef LocalBasisTraits< D, 2, FieldVector< D, 2 >, |
| 26 | R, 1, FieldVector< R, 1 >, |
| 27 | FieldMatrix< R, 1, 2 > > Traits; |
| 28 | |
| 29 | //! \brief number of shape functions |
| 30 | unsigned int size () const |
| 31 | { |
| 32 | return 4; |
| 33 | } |
| 34 | |
| 35 | //! \brief evaluate all shape functions |
| 36 | inline void evaluateFunction ( const typename Traits::DomainType &in, |
| 37 | std::vector< typename Traits::RangeType > &out ) const |
| 38 | { |
| 39 | out.resize(4); |
| 40 | typename Traits::DomainFieldType qbase = in[0]*in[0]-in[1]*in[1]; |
| 41 | out[0] = .75 - 2*in[0] + in[1] + qbase; |
| 42 | out[1] = -.25 + in[1] + qbase; |
| 43 | out[2] = .75 + in[0] - 2*in[1] - qbase; |
| 44 | out[3] = -.25 + in[0] - qbase; |
| 45 | } |
| 46 | |
| 47 | //! \brief evaluate jacobian of all shape functions |
| 48 | inline void evaluateJacobian ( const typename Traits::DomainType &in, |
| 49 | std::vector< typename Traits::JacobianType > &out ) const |
| 50 | { |
| 51 | out.resize(4); |
| 52 | |
| 53 | // see http://www.dune-project.org/doc/doxygen/html/classDune_1_1C1LocalBasisInterface.html#d6f8368f8aa43439cc7ef10419f6e2ea |
| 54 | // out[i][j][k] = d_k \phi^i_j , where \phi^i_j is the j'th component of the i'th shape function. |
| 55 | |
| 56 | out[0][0][0] = -2 + 2*in[0]; out[0][0][1] = 1 - 2*in[1]; |
| 57 | out[1][0][0] = 2*in[0]; out[1][0][1] = 1 - 2*in[1]; |
| 58 | out[2][0][0] = 1 - 2*in[0]; out[2][0][1] = -2 + 2*in[1]; |
| 59 | out[3][0][0] = 1 - 2*in[0]; out[3][0][1] = 2*in[1]; |
| 60 | } |
| 61 | |
| 62 | //! \brief Evaluate partial derivatives of all shape functions |
| 63 | void partial (const std::array<unsigned int, 2>& order, |
| 64 | const typename Traits::DomainType& in, // position |
| 65 | std::vector<typename Traits::RangeType>& out) const // return value |
| 66 | { |
| 67 | auto totalOrder = std::accumulate(order.begin(), order.end(), 0); |
| 68 | if (totalOrder == 0) { |
| 69 | evaluateFunction(in, out); |
| 70 | } else if (totalOrder == 1) { |
| 71 | auto const direction = std::distance(order.begin(), std::find(order.begin(), order.end(), 1)); |
| 72 | out.resize(size()); |
| 73 | |
| 74 | switch (direction) { |
| 75 | case 0: |
| 76 | out[0] = -2 + 2*in[0]; |
| 77 | out[1] = 2*in[0]; |
| 78 | out[2] = 1 - 2*in[0]; |
| 79 | out[3] = 1 - 2*in[0]; |
| 80 | break; |
| 81 | case 1: |
| 82 | out[0] = 1 - 2*in[1]; |
| 83 | out[1] = 1 - 2*in[1]; |
| 84 | out[2] = -2 + 2*in[1]; |
| 85 | out[3] = 2*in[1]; |
| 86 | break; |
| 87 | default: |
| 88 | DUNE_THROW(RangeError, "Component out of range."); |
| 89 | } |
| 90 | } else if (totalOrder == 2) { |
| 91 | auto const direction = std::distance(order.begin(), std::find(order.begin(), order.end(), 2)); |
| 92 | out.resize(size()); |
| 93 | |
| 94 | switch (direction) { |
| 95 | case 0: |
| 96 | out[0] = out[1] = 2; |
| 97 | out[2] = out[3] =-2; |
| 98 | break; |
| 99 | case 1: |
| 100 | out[0] = out[1] =-2; |
| 101 | out[2] = out[3] = 2; |
| 102 | break; |
| 103 | default: |
| 104 | out[0] = out[1] = out[2] = out[3] = 0; |
| 105 | break; |
| 106 | } |
| 107 | } else { |
| 108 | out[0] = out[1] = out[2] = out[3] = 0; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | //! \brief polynomial order of the shape functions |
| 113 | unsigned int order () const |
| 114 | { |
| 115 | // must be 2 here since it contains x^2 and x^2 |
| 116 | return 2; |
| 117 | } |
| 118 | }; |
| 119 | |
| 120 | } //namespace Dune |
| 121 | |
| 122 | #endif // #ifndef DUNE_RANNACHER_TUREK_2D_LOCALBASIS_HH |