| 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_3D_LOCALBASIS_HH |
| 6 | #define DUNE_RANNACHER_TUREK_3D_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 | class RannacherTurek3DLocalBasis |
| 24 | { |
| 25 | static const int coefficients[ 6 ][ 6 ]; |
| 26 | |
| 27 | public: |
| 28 | typedef LocalBasisTraits< D, 3, FieldVector< D, 3 >, |
| 29 | R, 1, FieldVector< R, 1 >, |
| 30 | FieldMatrix< R, 1, 3 > > Traits; |
| 31 | |
| 32 | //! \brief number of shape functions |
| 33 | unsigned int size () const |
| 34 | { |
| 35 | return 6; |
| 36 | } |
| 37 | |
| 38 | //! \brief evaluate all shape functions |
| 39 | inline void evaluateFunction ( const typename Traits::DomainType &in, |
| 40 | std::vector< typename Traits::RangeType > &out ) const |
| 41 | { |
| 42 | typedef typename Traits::RangeFieldType RangeFieldType; |
| 43 | RangeFieldType y[ 6 ] = { 1, in[ 0 ], in[ 1 ], in[ 2 ], |
| 44 | in[ 0 ]*in[ 0 ] - in[ 1 ]*in[ 1 ], |
| 45 | in[ 1 ]*in[ 1 ] - in[ 2 ]*in[ 2 ] }; |
| 46 | out.resize( size() ); |
| 47 | for( unsigned int i = 0; i < size(); ++i ) |
| 48 | { |
| 49 | out[ i ] = RangeFieldType( 0 ); |
| 50 | for( unsigned int j = 0; j < 6; ++j ) |
| 51 | out[ i ] += coefficients[ i ][ j ]*y[ j ]; |
| 52 | out[ i ] /= RangeFieldType( 3 ); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | //! \brief evaluate jacobian of all shape functions |
| 57 | inline void evaluateJacobian ( const typename Traits::DomainType &in, |
| 58 | std::vector< typename Traits::JacobianType > &out ) const |
| 59 | { |
| 60 | typedef typename Traits::RangeFieldType RangeFieldType; |
| 61 | RangeFieldType y0[ 5 ] = { 1, 0, 0, 2*in[ 0 ], 0 }; |
| 62 | RangeFieldType y1[ 5 ] = { 0, 1, 0, -2*in[ 1 ], 2*in[ 1 ] }; |
| 63 | RangeFieldType y2[ 5 ] = { 0, 0, 1, 0, -2*in[ 2 ] }; |
| 64 | |
| 65 | out.resize( size() ); |
| 66 | for( unsigned int i = 0; i < size(); ++i ) |
| 67 | { |
| 68 | out[ i ] = RangeFieldType( 0 ); |
| 69 | for( unsigned int j = 0; j < 5; ++j ) |
| 70 | { |
| 71 | out[ i ][ 0 ][ 0 ] += coefficients[ i ][ j+1 ]*y0[ j ]; |
| 72 | out[ i ][ 0 ][ 1 ] += coefficients[ i ][ j+1 ]*y1[ j ]; |
| 73 | out[ i ][ 0 ][ 2 ] += coefficients[ i ][ j+1 ]*y2[ j ]; |
| 74 | } |
| 75 | out[ i ] /= RangeFieldType( 3 ); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | //! \brief Evaluate partial derivatives of all shape functions |
| 80 | void partial (const std::array<unsigned int, 3>& order, |
| 81 | const typename Traits::DomainType& in, // position |
| 82 | std::vector<typename Traits::RangeType>& out) const // return value |
| 83 | { |
| 84 | auto totalOrder = std::accumulate(order.begin(), order.end(), 0); |
| 85 | if (totalOrder == 0) { |
| 86 | evaluateFunction(in, out); |
| 87 | } else if (totalOrder == 1) { |
| 88 | out.resize(size()); |
| 89 | auto const direction = std::distance(order.begin(), std::find(order.begin(), order.end(), 1)); |
| 90 | |
| 91 | using RangeFieldType = typename Traits::RangeFieldType; |
| 92 | RangeFieldType y[3][5] = { { 1.0, 0.0, 0.0, 2*in[0], 0.0 }, |
| 93 | { 0.0, 1.0, 0.0, -2*in[1], 2*in[1] }, |
| 94 | { 0.0, 0.0, 1.0, 0.0, -2*in[2] } }; |
| 95 | |
| 96 | for (std::size_t i = 0; i < size(); ++i) { |
| 97 | out[i] = RangeFieldType{0}; |
| 98 | for (std::size_t j = 0; j < 5; ++j) |
| 99 | out[i] += coefficients[i][j+1] * y[direction][j]; |
| 100 | out[i] /= RangeFieldType{3}; |
| 101 | } |
| 102 | } else { |
| 103 | DUNE_THROW(NotImplemented, "Desired derivative order is not implemented"); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | //! \brief polynomial order of the shape functions |
| 108 | unsigned int order () const |
| 109 | { |
| 110 | return 2; |
| 111 | } |
| 112 | }; |
| 113 | |
| 114 | |
| 115 | |
| 116 | // RannacherTurek3DLocalBasis::coefficients |
| 117 | // ---------------------------------------- |
| 118 | |
| 119 | template< class D, class R > |
| 120 | const int RannacherTurek3DLocalBasis< D, R > |
| 121 | ::coefficients[ 6 ][ 6 ] = {{ 2, -7, 2, 2, 4, 2 }, |
| 122 | { -1, -1, 2, 2, 4, 2 }, |
| 123 | { 2, 2, -7, 2, -2, 2 }, |
| 124 | { -1, 2, -1, 2, -2, 2 }, |
| 125 | { 2, 2, 2, -7, -2, -4 }, |
| 126 | { -1, 2, 2, -1, -2, -4 }}; |
| 127 | |
| 128 | } //namespace Dune |
| 129 | |
| 130 | #endif // #ifndef DUNE_RANNACHER_TUREK_3D_LOCALBASIS_HH |