OperatorTerm.hpp 5.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#pragma once

#include <algorithm>
#include <vector>
#include <type_traits>

#include <dune/geometry/quadraturerules.hh>
#include <dune/istl/bvector.hh>
#include <dune/functions/common/functionconcepts.hh>
#include <dune/functions/functionspacebases/pqknodalbasis.hh>

namespace AMDiS
{
  template <class MeshView>
  class OperatorTerm
  {
  protected:
    using Codim0      = typename MeshView::template Codim<0>;
    using Element     = typename Codim0::Entity;
    using WorldVector = typename Codim0::Geometry::GlobalCoordinate;
    
    static constexpr int dim = Element::dimension;
    
    using QuadratureRule = Dune::QuadratureRule<double, dim>;
    
  public:
    virtual void evalAtQPs(Element const& element, 
			   QuadratureRule const& quad, 
			   std::vector<double>& functionValues) const = 0;
			   
    virtual int getDegree() const = 0;
  };
  
  
  
  template <class MeshView, class Term>
  class GenericOperatorTerm : public OperatorTerm<MeshView>
  {
    using Super = OperatorTerm<MeshView>;
    using Element        = typename Super::Element;
    using QuadratureRule = typename Super::QuadratureRule;
    
  public:
    GenericOperatorTerm(Term const& term)
      : term(term)
    {}
    
    virtual void evalAtQPs(Element const& element, 
			   QuadratureRule const& quad, 
			   std::vector<double>& functionValues) const override
    {
	term.evalAtQPs(element, quad, functionValues);
    }
			   
    virtual int getDegree() const override
    {
	return term.getDegree();
    }
    
  private:
    Term term;
  };
  

  
  // some example terms
  
  template <class ValueType>
  class ConstantTerm
  {
  public:
    ConstantTerm(ValueType value)
      : value(value)
    {}
    
    template <class Element, class QuadratureRule>
    void evalAtQPs(Element const& element, 
		   QuadratureRule const& quad, 
		   std::vector<double>& functionValues) const
    {
      double value0 = value; // TODO: generalize functionValues!
      std::fill(functionValues.begin(), functionValues.end(), value0);
    }
    
    int getDegree() const
    {
      return 0;
    }
    
  private:
    ValueType value;
  };
  
  
  // generator function for coordinate expressions
  template <class T>
  std::enable_if_t< std::is_arithmetic<T>::value, ConstantTerm<T> >
  constant(T value) { return {value}; }
  
  
  
  template <class Functor>
  class CoordsTerm
  {
  public:
    template <class F,
      class = std::enable_if_t<std::is_same<Functor, std::decay_t<F>>::value> >
    CoordsTerm(F&& f, int degree = 1)
      : fct(std::forward<F>(f))
      , degree(degree)
    {}
    
    template <class Element, class QuadratureRule>
    void evalAtQPs(Element const& element, 
		   QuadratureRule const& quad, 
		   std::vector<double>& functionValues) const
    {
      static constexpr int dim = Element::dimension;
      for (size_t iq = 0; iq < quad.size(); ++iq) {
	  // Position of the current quadrature point in the reference element
	  const Dune::FieldVector<double,dim>& quadPos = quad[iq].position();
	  functionValues[iq] += fct(element.geometry().global(quadPos));
      }
    }
    
    int getDegree() const
    {
      return degree;
    }
    
  private:
    Functor fct;
    int degree;
  };
  
  
  // generator function for coordinate expressions
  template <class F>
  CoordsTerm< std::decay_t<F> > eval(F&& f) { return {std::forward<F>(f)}; }
  
  
  template <class FeSpace> struct GetDegree : int_<1> {};
  template <class GV, int k, class ST>
  struct GetDegree<Dune::Functions::PQkNodalBasis<GV, k, ST> > : int_<k> {};
  
  
  template <class DOFVectorType>
  class DOFVectorTerm
  {
    using Basis = typename DOFVectorType::FeSpace;
    using value_type = typename DOFVectorType::value_type;
    
  public:
    DOFVectorTerm(DOFVectorType const& dofvector, double factor = 1.0)
      : vector(dofvector)
      , factor(factor)
      , localView(dofvector.getFeSpace().localView())
      , localIndexSet(dofvector.getFeSpace().localIndexSet())
    {}
    
    template <class Element, class QuadratureRule>
    void evalAtQPs(Element const& element, 
		   QuadratureRule const& quad, 
		   std::vector<double>& functionValues) const
    {      
      static constexpr int dim = Element::dimension;
      
      localView.bind(element);
      localIndexSet.bind(localView);
      
      const auto& localFiniteElem = localView.tree().finiteElement();
      const size_t nBasisFct = localFiniteElem.size();
      
      std::vector<Dune::FieldVector<double,1> > shapeValues(nBasisFct);
      std::vector<value_type> localVec(nBasisFct);
      
      for (size_t j = 0; j < nBasisFct; ++j) {
	  const auto global_idx = localIndexSet.index(j);
	  localVec[j] = vector[global_idx];
      }
      
      for (size_t iq = 0; iq < quad.size(); ++iq) {
	  // Position of the current quadrature point in the reference element
	  const Dune::FieldVector<double, dim>& quadPos = quad[iq].position();
      
	  localFiniteElem.localBasis().evaluateFunction(quadPos, shapeValues);
	  for (size_t j = 0; j < shapeValues.size(); ++j)
	      functionValues[iq] += factor * localVec[j] * shapeValues[j];
      }
    }
    
    int getDegree() const
    {
      return degree;
    }
    
  private:
    DOFVectorType vector;
    double factor;
    
    mutable typename Basis::LocalView localView;
    mutable typename Basis::LocalIndexSet localIndexSet;
    
    int degree = GetDegree<Basis>::value;
  };
  
  
  
  template <class DOFVectorType>
  DOFVectorTerm<DOFVectorType>
  valueOf(DOFVectorType const& vector, double factor = 1.0)
  {
      return {vector, factor};
  }
  
  
} // end namespace AMDiS