OperatorTerm.hpp 9.38 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
#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>

12
13
14
15
16
17
#include "OperatorTermBase.hpp"

namespace AMDiS {
    
template <class MeshView>
class OperatorTerm
18
{
19
protected:
20
21
22
23
24
25
    using Codim0      = typename MeshView::template Codim<0>;
    using Element     = typename Codim0::Entity;
    
    static constexpr int dim = Element::dimension;
    
    using QuadratureRule = Dune::QuadratureRule<double, dim>;
26
    using PointList = std::vector<Dune::QuadraturePoint<double, dim>>;
27
    
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
public:
    virtual void init(Element const& element, 
		      PointList const& points) = 0;
		      
    virtual double evalZot(size_t iq, 
	Dune::FieldVector<double,1> const& test, 
	Dune::FieldVector<double,1> const trial = 1.0) const = 0;
	
    virtual double evalFot1(size_t iq, 
	Dune::FieldVector<double,1> const& test, 
	Dune::FieldVector<double,dim> const& grad_trial) const = 0;
	
    virtual double evalFot2(size_t iq, 
	Dune::FieldVector<double,dim> const& grad_test, 
	Dune::FieldVector<double,1> const trial = 1.0) const = 0;
	
    virtual double evalSot(size_t iq, 
	Dune::FieldVector<double,dim> const& grad_test, 
	Dune::FieldVector<double,dim> const& grad_trial) const = 0;
47
48
			   
    virtual int getDegree() const = 0;
49
};
50
51
52
  
  
  
53
54
55
56
57
58
59
60
template <class MeshView, class Term, class Traits = _none>
class GenericOperatorTerm 
    : public OperatorTerm<MeshView>
    , public OperatorEvaluation
{
    using Super   = OperatorTerm<MeshView>;
    using Element = typename Super::Element;
    using PointList = typename Super::PointList;
61
    
62
63
64
    static constexpr int dim = Element::dimension;

public:
65
    GenericOperatorTerm(Term const& term)
66
	: term(term)
67
    {}
68
69
70

    virtual void init(Element const& element, 
		      PointList const& points) override
71
    {
72
73
74
75
76
77
	term.init(element, points);
	
	// cache term evaluation
	values.resize(points.size());
	for (size_t iq = 0; iq < points.size(); ++iq)
	    values[iq] = term[iq];
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
		      
    virtual double evalZot(size_t iq, 
	Dune::FieldVector<double,1> const& test, 
	Dune::FieldVector<double,1> const trial = 1.0) const override
    {
	return this->evalZotImpl(_cat{}, _traits{}, values[iq], test, trial);
    }
	
    virtual double evalFot1(size_t iq, 
	Dune::FieldVector<double,1> const& test, 
	Dune::FieldVector<double,dim> const& grad_trial) const override
    {
	return this->evalFotImpl(_cat{}, _traits{}, values[iq], grad_trial, test);
    }
	
    virtual double evalFot2(size_t iq, 
	Dune::FieldVector<double,dim> const& grad_test, 
	Dune::FieldVector<double,1> const trial = 1.0) const override
    {
	return this->evalFotImpl(_cat{}, _traits{}, values[iq], grad_test, trial);
    }
	
    virtual double evalSot(size_t iq, 
	Dune::FieldVector<double,dim> const& grad_test, 
	Dune::FieldVector<double,dim> const& grad_trial) const override
    {
	return this->evalSotImpl(_cat{}, _traits{}, values[iq], grad_test, grad_trial);
    }
			    
108
109
110
111
    virtual int getDegree() const override
    {
	return term.getDegree();
    }
112
113

private:
114
    Term term;
115
116
117
118
119
120
121
    
    using value_type = std::decay_t< decltype( std::declval<Term>()[std::declval<size_t>()] ) >;
    using _cat    = ValueCategory_t<value_type>;
    using _traits = Traits;
    
    std::vector<value_type> values;
};
122
123
  

124
125
126
127
128
129
130
131
132
133
134
// some example terms
// -----------------------------------------------------------------------------


template <class ValueType>
class ConstantTerm
{
public:
    using value_type = ValueType;
    
    ConstantTerm(value_type value)
135
136
137
      : value(value)
    {}
    
138
139
140
141
142
    template <class Element, class PointList>
    void init(Element const& element, 
	      PointList const& points) {}
    
    value_type operator[](size_t iq) const
143
    {
144
	return value;
145
146
147
148
    }
    
    int getDegree() const
    {
149
	return 0;
150
151
    }
    
152
153
154
private:
    value_type value;
};
155
156
  
  
157
158
159
160
// generator function for coordinate expressions
template <class T>
std::enable_if_t< std::is_arithmetic<T>::value, ConstantTerm<T> >
constant(T value) { return {value}; }
161
162
163
  
  
  
164
165
166
167
168
169
template <class Functor>
class CoordsTerm
{
public:
    using value_type = typename std::result_of<Functor(Dune::FieldVector<double, 2>)>::type;
    
170
171
172
173
174
175
176
    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)
    {}
    
177
178
179
180
181
182
183
184
185
186
    template <class Element, class PointList>
    void init(Element const& element, 
	      PointList const& points)
    {
	values.resize(points.size());
	for (size_t iq = 0; iq < points.size(); ++iq)
	    values[iq] = fct(element.geometry().global(points[iq].position()));
    }
    
    value_type const& operator[](size_t iq) const
187
    {
188
	return values[iq];
189
190
191
192
    }
    
    int getDegree() const
    {
193
	return degree;
194
195
    }
    
196
private:
197
198
    Functor fct;
    int degree;
199
200
201
    
    std::vector<value_type> values;
};
202
203
  
  
204
205
206
// generator function for coordinate expressions
template <class F>
CoordsTerm< std::decay_t<F> > eval(F&& f) { return {std::forward<F>(f)}; }
207
208
  
  
209
210
211
template <class FeSpace> struct GetDegree : int_<1> {};
template <class GV, int k, class ST>
struct GetDegree<Dune::Functions::PQkNodalBasis<GV, k, ST> > : int_<k> {};
212
213
  
  
214
215
216
template <class DOFVectorType>
class DOFVectorTerm
{
217
    using Basis = typename DOFVectorType::FeSpace;
218
219
220
    using field_type = typename DOFVectorType::field_type;
    
public:
221
222
    using value_type = typename DOFVectorType::value_type;
    
223
224
225
    template <class DOFVectorType_>
    DOFVectorTerm(DOFVectorType_&& dofvector, double factor = 1.0)
      : vector(dofvector.getVector())
226
227
228
229
230
      , factor(factor)
      , localView(dofvector.getFeSpace().localView())
      , localIndexSet(dofvector.getFeSpace().localIndexSet())
    {}
    
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
    template <class Element, class PointList>
    void init(Element const& element, 
	      PointList const& points)
    {
	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];
	}
	
	values.resize(points.size());
	for (size_t iq = 0; iq < points.size(); ++iq) {      
	    localFiniteElem.localBasis().evaluateFunction(points[iq].position(), shapeValues);
	    value_type result = 0;
	    for (size_t j = 0; j < shapeValues.size(); ++j)
		result += localVec[j] * (factor * shapeValues[j]);
	    values[iq] = result;
	}
    }
    
    value_type const& operator[](size_t iq) const
    {
	return values[iq];
262
263
264
265
    }
    
    int getDegree() const
    {
266
	return degree;
267
268
    }
    
269
private:
270
    typename DOFVectorType::BaseVector const& vector;
271
272
    double factor;
    
273
274
    typename Basis::LocalView localView;
    typename Basis::LocalIndexSet localIndexSet;
275
276
    
    int degree = GetDegree<Basis>::value;
277
278
279
    
    std::vector<value_type> values;
};
280
281
282
  
  
  
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
template <class DOFVectorType, class F>
class DOFVectorFuncTerm
{
    using Basis = typename DOFVectorType::FeSpace;
    using field_type = typename DOFVectorType::field_type;
    
public:
    using value_type = typename DOFVectorType::value_type;
    
    template <class DOFVectorType_, class F_>
    DOFVectorFuncTerm(DOFVectorType_&& dofvector, F_&& f, int f_deg)
      : vector(dofvector.getVector())
      , f(f)
      , localView(dofvector.getFeSpace().localView())
      , localIndexSet(dofvector.getFeSpace().localIndexSet())
      , f_deg(f_deg)
    {}
    
    template <class Element, class PointList>
    void init(Element const& element, 
	      PointList const& points)
    {
	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];
	}
	
	values.resize(points.size());
	for (size_t iq = 0; iq < points.size(); ++iq) {      
	    localFiniteElem.localBasis().evaluateFunction(points[iq].position(), shapeValues);
	    value_type result = 0;
	    for (size_t j = 0; j < shapeValues.size(); ++j)
		result += localVec[j] * shapeValues[j];
	    
	    values[iq] = f(result);
	}
    }
    
    value_type const& operator[](size_t iq) const
    {
	return values[iq];
    }
    
    int getDegree() const
    {
	return degree * f_deg;
    }
    
private:
    typename DOFVectorType::BaseVector const& vector;
    F f;
    
    typename Basis::LocalView localView;
    typename Basis::LocalIndexSet localIndexSet;
    
    int degree = GetDegree<Basis>::value;
    int f_deg;
    
    std::vector<value_type> values;
};

  
template <class DOFVectorType>
DOFVectorTerm<std::decay_t<DOFVectorType>>
valueOf(DOFVectorType&& vector, double factor = 1.0)
{
    return {std::forward<DOFVectorType>(vector), factor};
}

  
template <class DOFVectorType, class F>
DOFVectorFuncTerm<std::decay_t<DOFVectorType>, std::decay_t<F>>
valueOfFunc(DOFVectorType&& vector, F&& f, int deg = 1)
{
    return {std::forward<DOFVectorType>(vector), std::forward<F>(f), deg};
}
368
369
370
  
  
} // end namespace AMDiS