SecondOrderPartialTestPartialTrial.hpp 4.58 KB
Newer Older
1
2
3
4
#pragma once

#include <type_traits>

5
#include <amdis/GridFunctionOperator.hpp>
6
#include <amdis/LocalBasisCache.hpp>
7
8
#include <amdis/Output.hpp>
#include <amdis/common/ValueCategory.hpp>
9
10
11

namespace AMDiS
{
12
13
14
15
16
  /**
   * \addtogroup operators
   * @{
   **/

17
18
19
20
  namespace tag
  {
    struct partialtest_partialtrial
    {
21
22
      std::size_t comp_test; // i
      std::size_t comp_trial; // j
23
24
25
26
    };
  }


27
  /// second-order operator \f$ \langle\partial_i\psi, c\,\partial_j\phi\rangle \f$
28
29
30
31
  template <class LocalContext, class GridFct>
  class GridFunctionOperator<tag::partialtest_partialtrial, LocalContext, GridFct>
      : public GridFunctionOperatorBase<GridFunctionOperator<tag::partialtest_partialtrial, LocalContext, GridFct>,
                                        LocalContext, GridFct>
32
  {
33
    using Self = GridFunctionOperator;
34
    using Super = GridFunctionOperatorBase<Self, LocalContext, GridFct>;
35

36
    static_assert( Category::Scalar<typename GridFct::Range>, "Expression must be of scalar type." );
37
38

  public:
39
40
    GridFunctionOperator(tag::partialtest_partialtrial tag, GridFct const& expr)
      : Super(expr, 2)
41
42
43
44
      , compTest_(tag.comp_test)
      , compTrial_(tag.comp_trial)
    {}

45
46
47
48
49
    template <class Context, class RowNode, class ColNode, class ElementMatrix>
    void getElementMatrix(Context const& context,
                          RowNode const& rowNode,
                          ColNode const& colNode,
                          ElementMatrix& elementMatrix)
50
    {
51
52
      static_assert(RowNode::isLeaf && ColNode::isLeaf,
        "Operator can be applied to Leaf-Nodes only.");
53
54
55
56

      auto const& rowLocalFE = rowNode.finiteElement();
      auto const& colLocalFE = colNode.finiteElement();

57
58
      using RowLocalBasisType = typename std::decay_t<decltype(rowLocalFE)>::Traits::LocalBasisType;
      using ColLocalBasisType = typename std::decay_t<decltype(colLocalFE)>::Traits::LocalBasisType;
59

60
61
62
63
      using RowFieldType = typename RowLocalBasisType::Traits::RangeFieldType;
      using ColFieldType = typename ColLocalBasisType::Traits::RangeFieldType;

      auto const& quad = this->getQuadratureRule(context.type(), rowNode, colNode);
64
65
66
67
68
69
70

      LocalBasisCache<RowLocalBasisType> rowCache(rowLocalFE.localBasis());
      LocalBasisCache<ColLocalBasisType> colCache(colLocalFE.localBasis());

      auto const& rowGradientsCache = rowCache.evaluateJacobianAtQp(context, quad);
      auto const& colGradientsCache = colCache.evaluateJacobianAtQp(context, quad);
      for (std::size_t iq = 0; iq < quad.size(); ++iq) {
71
        // Position of the current quadrature point in the reference element
72
        decltype(auto) local = context.local(quad[iq].position());
73
74

        // The transposed inverse Jacobian of the map from the reference element to the element
75
        const auto jacobian = context.geometry().jacobianInverseTransposed(local);
76
77

        // The multiplicative factor in the integral transformation formula
78
        const auto factor = context.integrationElement(quad[iq].position()) * quad[iq].weight();
79
        const auto c = Super::coefficient(local);
80
81

        // The gradients of the shape functions on the reference element
82
83
        auto const& rowShapeGradients = rowGradientsCache[iq];
        auto const& colShapeGradients = colGradientsCache[iq];
84
85

        // Compute the shape function gradients on the real element
86
87
        thread_local std::vector<RowFieldType> rowPartial;
        rowPartial.resize(rowShapeGradients.size());
88
        for (std::size_t i = 0; i < rowPartial.size(); ++i) {
89
          rowPartial[i] = jacobian[compTest_][0] * rowShapeGradients[i][0][0];
90
          for (std::size_t j = 1; j < jacobian.cols(); ++j)
91
            rowPartial[i] += jacobian[compTest_][j] * rowShapeGradients[i][0][j];
92
93
        }

94
95
        thread_local std::vector<ColFieldType> colPartial;
        colPartial.resize(colShapeGradients.size());
96
        for (std::size_t i = 0; i < colPartial.size(); ++i) {
97
          colPartial[i] = jacobian[compTrial_][0] * colShapeGradients[i][0][0];
98
          for (std::size_t j = 1; j < jacobian.cols(); ++j)
99
            colPartial[i] += jacobian[compTrial_][j] * colShapeGradients[i][0][j];
100
101
102
        }

        for (std::size_t j = 0; j < colLocalFE.size(); ++j) {
103
104
          const auto local_j = colNode.localIndex(j);
          const auto value = factor * (c * colPartial[j]);
105
          for (std::size_t i = 0; i < rowLocalFE.size(); ++i) {
106
            const auto local_i = rowNode.localIndex(i);
107
108
109
110
111
112
113
114
            elementMatrix[local_i][local_j] += value * rowPartial[i];
          }
        }
      }

    }

  private:
115
116
    std::size_t compTest_;
    std::size_t compTrial_;
117
118
  };

119
120
  /** @} **/

121
} // end namespace AMDiS