Operator.inc.hpp 12 KB
Newer Older
1
2
3
4
5
6
7
#pragma once

#include <vector>

namespace AMDiS
{
  template <class MeshView>
8
9
10
    template <class RowFeSpace, class ColFeSpace>
  void Operator<MeshView>::init(RowFeSpace const& rowFeSpace,
				ColFeSpace const& colFeSpace)
11
  {
12
13
//     const auto& rowFE = rowView.tree().finiteElement();
//     const auto& colFE = colView.tree().finiteElement();
14
    
15
16
17
18
19
//     psiDegree = rowFE.localBasis().order();
//     phiDegree = colFE.localBasis().order();
      
      psiDegree = GetDegree<RowFeSpace>::value;
      phiDegree = GetDegree<ColFeSpace>::value;
20
21
22
23
24
25
26
27
28
29
30
    
    // TODO: calc quadrature degree here.
  }
  

  template <class MeshView>
    template <class RowView, class ColView, class ElementMatrix>
  void Operator<MeshView>::getElementMatrix(RowView const& rowView,
					    ColView const& colView,
					    ElementMatrix& elementMatrix)
  {    
31
32
33
34
35
36
37
38
    if (!zeroOrder.empty())
	assembleZeroOrderTerms(rowView, colView, elementMatrix);
    if (!firstOrderGrdPhi.empty())
	assembleFirstOrderTermsGrdPhi(rowView, colView, elementMatrix);
    if (!firstOrderGrdPsi.empty())
	assembleFirstOrderTermsGrdPsi(rowView, colView, elementMatrix);
    if (!secondOrder.empty())
	assembleSecondOrderTerms(rowView, colView, elementMatrix);
39
40
  }
  
41
42
43
44
45
46
  
  template <class MeshView>
    template <class RowView, class ElementVector>
  void Operator<MeshView>::getElementVector(RowView const& rowView,
					    ElementVector& elementVector)
  {    
47
48
    if (!zeroOrder.empty())
	assembleZeroOrderTerms(rowView, elementVector);
49
50
51
  }
  
      
52
53
54
55
56
57
58
59
60
61
62
  template <class MeshView>
    template <class RowView, class ColView, class ElementMatrix>
  void Operator<MeshView>::assembleZeroOrderTerms(RowView const& rowView,
						  ColView const& colView,
						  ElementMatrix& elementMatrix)
  {
    using Element = typename RowView::Element;
    auto element = rowView.element();
    const int dim = Element::dimension;
    auto geometry = element.geometry();
    
63
64
    const auto& rowLocalBasis = rowView.tree().finiteElement().localBasis();
    const auto& colLocalBasis = colView.tree().finiteElement().localBasis();
65
66
    
    int order = getQuadratureDegree(0);
67
    const auto& quad = Dune::QuadratureRules<double, dim>::rule(element.type(), order);
68
69
    
    for (auto* operatorTerm : zeroOrder)
70
	operatorTerm->init(element, quad);
71
72
73
    
    for (size_t iq = 0; iq < quad.size(); ++iq) {
	// Position of the current quadrature point in the reference element
74
	const Dune::FieldVector<double,dim>& quadPos = quad[iq].position();
75
76
77
78

	// The multiplicative factor in the integral transformation formula
	const double integrationElement = geometry.integrationElement(quadPos);
	
79
	std::vector<Dune::FieldVector<double,1> > rowShapeValues, colShapeValues;
80
	rowLocalBasis.evaluateFunction(quadPos, rowShapeValues);
81
82
83
84
	
	if (psiDegree == phiDegree)
	  colShapeValues = rowShapeValues;
	else
85
	  colLocalBasis.evaluateFunction(quadPos, colShapeValues);
86
87
88
89
90

	for (size_t i = 0; i < elementMatrix.N(); ++i) {
	    for (size_t j = 0; j < elementMatrix.M(); ++j) {
		const int local_i = rowView.tree().localIndex(i);
		const int local_j = colView.tree().localIndex(j);
91
92
93
94
		for (auto* operatorTerm : zeroOrder)
		    elementMatrix[local_i][local_j] 
			+= operatorTerm->evalZot(iq, rowShapeValues[i], colShapeValues[j]) 
			    * quad[iq].weight() * integrationElement;
95
96
97
98
99
100
	    }
	}
    }
  }
  

101
102
103
104
105
106
107
108
109
110
111
      
  template <class MeshView>
    template <class RowView, class ElementVector>
  void Operator<MeshView>::assembleZeroOrderTerms(RowView const& rowView,
						  ElementVector& elementvector)
  {
    using Element = typename RowView::Element;
    auto element = rowView.element();
    const int dim = Element::dimension;
    auto geometry = element.geometry();
    
112
    const auto& rowLocalBasis = rowView.tree().finiteElement().localBasis();
113
114
115
116
117
    
    int order = getQuadratureDegree(0);
    const auto& quad = Dune::QuadratureRules<double, dim>::rule(element.type(), order);
    
    for (auto* operatorTerm : zeroOrder)
118
	operatorTerm->init(element, quad);
119
120
121
122
123
124
125
126
127
    
    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();

	// The multiplicative factor in the integral transformation formula
	const double integrationElement = geometry.integrationElement(quadPos);
	
	std::vector<Dune::FieldVector<double,1> > rowShapeValues;
128
	rowLocalBasis.evaluateFunction(quadPos, rowShapeValues);
129
130
131

	for (size_t i = 0; i < elementvector.size(); ++i) {
	    const int local_i = rowView.tree().localIndex(i);
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
	    for (auto* operatorTerm : zeroOrder)
		elementvector[local_i] 
		    += operatorTerm->evalZot(iq, rowShapeValues[i]) 
			* quad[iq].weight() * integrationElement;
	}
    }
  }
  

  template <class MeshView>
    template <class RowView, class ColView, class ElementMatrix>
  void Operator<MeshView>::assembleFirstOrderTermsGrdPhi(RowView const& rowView,
							  ColView const& colView,
							  ElementMatrix& elementMatrix)
  {
    using Element = typename RowView::Element;
    auto element = rowView.element();
    const int dim = Element::dimension;
    auto geometry = element.geometry();
    
    const auto& rowLocalBasis = rowView.tree().finiteElement().localBasis();
    const auto& colLocalBasis = colView.tree().finiteElement().localBasis();
    
    int order = getQuadratureDegree(2);
    const auto& quad = Dune::QuadratureRules<double, dim>::rule(element.type(), order);
    
    for (auto* operatorTerm : firstOrderGrdPhi)
	operatorTerm->init(element, quad);
    
    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();

	// The transposed inverse Jacobian of the map from the reference element to the element
	const auto jacobian = geometry.jacobianInverseTransposed(quadPos);

	// The multiplicative factor in the integral transformation formula
	const double integrationElement = geometry.integrationElement(quadPos);

	std::vector<Dune::FieldVector<double,1> > rowShapeValues;
	rowLocalBasis.evaluateFunction(quadPos, rowShapeValues);
	
	// The gradients of the shape functions on the reference element
	std::vector<Dune::FieldMatrix<double,1,dim> > colReferenceGradients;
	colLocalBasis.evaluateJacobian(quadPos, colReferenceGradients);

	// Compute the shape function gradients on the real element
	std::vector<Dune::FieldVector<double,dim> > colGradients(colReferenceGradients.size());

	for (size_t i = 0; i < colGradients.size(); ++i)
	    jacobian.mv(colReferenceGradients[i][0], colGradients[i]);

	for (size_t i = 0; i < elementMatrix.N(); ++i) {
	    for (size_t j = 0; j < elementMatrix.M(); ++j) {
		const int local_i = rowView.tree().localIndex(i);
		const int local_j = colView.tree().localIndex(j);
		for (auto* operatorTerm : firstOrderGrdPhi)
		    elementMatrix[local_i][local_j] 
			+= operatorTerm->evalFot1(iq, rowShapeValues[i], colGradients[j]) 
			    * quad[iq].weight() * integrationElement;
	    }
	}
    }
  }
  

  template <class MeshView>
    template <class RowView, class ColView, class ElementMatrix>
  void Operator<MeshView>::assembleFirstOrderTermsGrdPsi(RowView const& rowView,
							  ColView const& colView,
							  ElementMatrix& elementMatrix)
  {
    using Element = typename RowView::Element;
    auto element = rowView.element();
    const int dim = Element::dimension;
    auto geometry = element.geometry();
    
    const auto& rowLocalBasis = rowView.tree().finiteElement().localBasis();
    const auto& colLocalBasis = colView.tree().finiteElement().localBasis();
    
    int order = getQuadratureDegree(2);
    const auto& quad = Dune::QuadratureRules<double, dim>::rule(element.type(), order);
    
    for (auto* operatorTerm : firstOrderGrdPsi)
	operatorTerm->init(element, quad);
    
    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();

	// The transposed inverse Jacobian of the map from the reference element to the element
	const auto jacobian = geometry.jacobianInverseTransposed(quadPos);

	// The multiplicative factor in the integral transformation formula
	const double integrationElement = geometry.integrationElement(quadPos);
	
	// The gradients of the shape functions on the reference element
	std::vector<Dune::FieldMatrix<double,1,dim> > rowReferenceGradients;
	rowLocalBasis.evaluateJacobian(quadPos, rowReferenceGradients);

	// Compute the shape function gradients on the real element
	std::vector<Dune::FieldVector<double,dim> > rowGradients(rowReferenceGradients.size());

	for (size_t i = 0; i < rowGradients.size(); ++i)
	    jacobian.mv(rowReferenceGradients[i][0], rowGradients[i]);

	std::vector<Dune::FieldVector<double,1> > colShapeValues;
	colLocalBasis.evaluateFunction(quadPos, colShapeValues);

	for (size_t i = 0; i < elementMatrix.N(); ++i) {
	    for (size_t j = 0; j < elementMatrix.M(); ++j) {
		const int local_i = rowView.tree().localIndex(i);
		const int local_j = colView.tree().localIndex(j);
		for (auto* operatorTerm : firstOrderGrdPsi)
		    elementMatrix[local_i][local_j] 
			+= operatorTerm->evalFot2(iq, rowGradients[i], colShapeValues[j]) 
			    * quad[iq].weight() * integrationElement;
	    }
250
251
252
253
254
	}
    }
  }
  

255
256
257
258
259
260
261
262
263
264
265
  template <class MeshView>
    template <class RowView, class ColView, class ElementMatrix>
  void Operator<MeshView>::assembleSecondOrderTerms(RowView const& rowView,
						    ColView const& colView,
						    ElementMatrix& elementMatrix)
  {
    using Element = typename RowView::Element;
    auto element = rowView.element();
    const int dim = Element::dimension;
    auto geometry = element.geometry();
    
266
267
    const auto& rowLocalBasis = rowView.tree().finiteElement().localBasis();
    const auto& colLocalBasis = colView.tree().finiteElement().localBasis();
268
    
269
    int order = getQuadratureDegree(2);
270
    const auto& quad = Dune::QuadratureRules<double, dim>::rule(element.type(), order);
271
272
    
    for (auto* operatorTerm : secondOrder)
273
	operatorTerm->init(element, quad);
274
275
276
277
278
279
    
    // TODO: currently only the implementation for equal fespaces
    assert( psiDegree == phiDegree );
    
    for (size_t iq = 0; iq < quad.size(); ++iq) {
	// Position of the current quadrature point in the reference element
280
	const Dune::FieldVector<double,dim>& quadPos = quad[iq].position();
281
282
283
284
285
286
287
288

	// The transposed inverse Jacobian of the map from the reference element to the element
	const auto jacobian = geometry.jacobianInverseTransposed(quadPos);

	// The multiplicative factor in the integral transformation formula
	const double integrationElement = geometry.integrationElement(quadPos);

	// The gradients of the shape functions on the reference element
289
	std::vector<Dune::FieldMatrix<double,1,dim> > referenceGradients;
290
	rowLocalBasis.evaluateJacobian(quadPos, referenceGradients);
291
292

	// Compute the shape function gradients on the real element
293
	std::vector<Dune::FieldVector<double,dim> > gradients(referenceGradients.size());
294
295
296
297
298
299
300
301

	for (size_t i = 0; i < gradients.size(); ++i)
	    jacobian.mv(referenceGradients[i][0], gradients[i]);

	for (size_t i = 0; i < elementMatrix.N(); ++i) {
	    for (size_t j = 0; j < elementMatrix.M(); ++j) {
		const int local_i = rowView.tree().localIndex(i);
		const int local_j = colView.tree().localIndex(j);
302
303
304
305
		for (auto* operatorTerm : secondOrder)
		    elementMatrix[local_i][local_j] 
			+= operatorTerm->evalSot(iq, gradients[i], gradients[j]) 
			    * quad[iq].weight() * integrationElement;
306
307
308
309
310
311
312
	    }
	}
    }
  }
  

  template <class MeshView>
313
  int Operator<MeshView>::getQuadratureDegree(int order, FirstOrderType firstOrderType)
314
  {
315
    std::list<OperatorTermType*>* terms = NULL;
316
317
318
319
320
321

    switch(order)
    {
    case 0:
      terms = &zeroOrder;
      break;
322
323
324
325
326
327
    case 1:
      if (firstOrderType == GRD_PHI)
        terms = &firstOrderGrdPhi;
      else
        terms = &firstOrderGrdPsi;
      break;
328
329
330
331
332
333
334
    case 2:
      terms = &secondOrder;
      break;
    }
    int maxTermDegree = 0;

    for (OperatorTermType* term : *terms)
335
	maxTermDegree = std::max(maxTermDegree, term->getDegree());
336
337
338
339
340

    return psiDegree + phiDegree - order + maxTermDegree;
  }
  
} // end namespace AMDiS