Assembler.cc 11.8 KB
Newer Older
1
2
3
#include <vector>
#include <algorithm>
#include <boost/numeric/mtl/mtl.hpp>
4
5
6
7
8
#include "Assembler.h"
#include "Operator.h"
#include "Element.h"
#include "QPsiPhi.h"
#include "DOFVector.h"
9
#include "OpenMP.h"
10
11
12

namespace AMDiS {

Thomas Witkowski's avatar
Thomas Witkowski committed
13
14
15
  Assembler::Assembler(Operator *op,
		       const FiniteElemSpace *row,
		       const FiniteElemSpace *col) 
16
    : operat(op),
Thomas Witkowski's avatar
Thomas Witkowski committed
17
18
      rowFESpace(row),
      colFESpace(col ? col : row),
19
20
21
22
23
      nRow(rowFESpace->getBasisFcts()->getNumber()),
      nCol(colFESpace->getBasisFcts()->getNumber()),
      remember(true),
      rememberElMat(false),
      rememberElVec(false),
24
25
      elementMatrix(nRow, nCol),
      elementVector(nRow),
26
      tmpMat(nRow, nCol),
27
28
29
      lastMatEl(NULL),
      lastVecEl(NULL),
      lastTraverseId(-1)
30
  {}
Thomas Witkowski's avatar
Thomas Witkowski committed
31

32

Thomas Witkowski's avatar
Thomas Witkowski committed
33
  Assembler::~Assembler()
34
  {}
35

36

37
  void Assembler::calculateElementMatrix(const ElInfo *elInfo, 
38
					 ElementMatrix& userMat,
39
40
41
42
					 double factor)
  {
    FUNCNAME("Assembler::calculateElementMatrix()");

43
    if (remember && (factor != 1.0 || operat->uhOld))
44
      rememberElMat = true;
Thomas Witkowski's avatar
Thomas Witkowski committed
45

46
    Element *el = elInfo->getElement();
Thomas Witkowski's avatar
Thomas Witkowski committed
47

48
    if ((el != lastMatEl && el != lastVecEl) || !operat->isOptimized())
49
50
51
      initElement(elInfo);

    if (el != lastMatEl || !operat->isOptimized()) {
52
53
54
      if (rememberElMat)
	set_to_zero(elementMatrix);

55
56
57
      lastMatEl = el;
    } else {
      if (rememberElMat) {
58
	userMat += factor * elementMatrix;
59
60
61
	return;
      }
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
62
 
63
    ElementMatrix& mat = rememberElMat ? elementMatrix : userMat;
64
65
66
67
68
69
70
71
72
73

    if (secondOrderAssembler)
      secondOrderAssembler->calculateElementMatrix(elInfo, mat);
    if (firstOrderAssemblerGrdPsi)
      firstOrderAssemblerGrdPsi->calculateElementMatrix(elInfo, mat);
    if (firstOrderAssemblerGrdPhi)
      firstOrderAssemblerGrdPhi->calculateElementMatrix(elInfo, mat);
    if (zeroOrderAssembler)
      zeroOrderAssembler->calculateElementMatrix(elInfo, mat);

Thomas Witkowski's avatar
Thomas Witkowski committed
74
    if (rememberElMat && &userMat != &elementMatrix)
75
      userMat += factor * elementMatrix;
76
77
  }

78

79
80
  void Assembler::calculateElementMatrix(const ElInfo *rowElInfo,
					 const ElInfo *colElInfo,
81
82
					 const ElInfo *smallElInfo,
					 const ElInfo *largeElInfo,
83
					 ElementMatrix& userMat,
84
85
86
87
					 double factor)
  {
    FUNCNAME("Assembler::calculateElementMatrix()");

88
    if (remember && (factor != 1.0 || operat->uhOld))
89
90
      rememberElMat = true;
  
Thomas Witkowski's avatar
Thomas Witkowski committed
91
    Element *el = smallElInfo->getElement();   
92
    lastVecEl = lastMatEl = NULL;
Thomas Witkowski's avatar
Thomas Witkowski committed
93
   
94
    if ((el != lastMatEl && el != lastVecEl) || !operat->isOptimized())
Thomas Witkowski's avatar
Thomas Witkowski committed
95
      initElement(smallElInfo, largeElInfo);
96
97

    if (el != lastMatEl || !operat->isOptimized()) {
98
99
100
      if (rememberElMat)
	set_to_zero(elementMatrix);

101
102
103
      lastMatEl = el;
    } else {
      if (rememberElMat) {
104
	userMat += factor * elementMatrix;
105
106
107
	return;
      }
    }
108
 
109
    ElementMatrix& mat = rememberElMat ? elementMatrix : userMat;
110

111
    if (secondOrderAssembler) {
112
      ERROR_EXIT("Da muss i noch ma testen!\n");
113
      secondOrderAssembler->calculateElementMatrix(smallElInfo, mat);
114

115
116
      ElementMatrix &m =       
	smallElInfo->getSubElemCoordsMat_so(rowFESpace->getBasisFcts()->getDegree());
117

118
      tmpMat = m * mat;
119
      mat = tmpMat;      
120
121
122
    }

    if (firstOrderAssemblerGrdPsi) {
123
      ERROR_EXIT("Not yet implemented for first order assembler!\n");
124
      firstOrderAssemblerGrdPsi->calculateElementMatrix(smallElInfo, mat);
125
126
127
    }

    if (firstOrderAssemblerGrdPhi) {
128
      ERROR_EXIT("Not yet implemented for first order assembler!\n");
129
      firstOrderAssemblerGrdPhi->calculateElementMatrix(smallElInfo, mat);
130
    }
131

132
133
    if (zeroOrderAssembler) {
      zeroOrderAssembler->calculateElementMatrix(smallElInfo, mat);
Thomas Witkowski's avatar
Thomas Witkowski committed
134
      
135
136
      ElementMatrix &m = 
	smallElInfo->getSubElemCoordsMat(rowFESpace->getBasisFcts()->getDegree());
Thomas Witkowski's avatar
Thomas Witkowski committed
137
      
Thomas Witkowski's avatar
Thomas Witkowski committed
138
      if (smallElInfo == colElInfo)
139
140
	tmpMat = m * mat;	
      else
Thomas Witkowski's avatar
Thomas Witkowski committed
141
142
143
  	tmpMat = mat * trans(m);
      
      mat = tmpMat;
144
    }
145

146
147
    if (rememberElMat && &userMat != &elementMatrix)
      userMat += factor * elementMatrix;   
148
149
  }

150

151
  void Assembler::calculateElementVector(const ElInfo *elInfo, 
152
					 ElementVector& userVec,
153
154
155
156
					 double factor)
  {
    FUNCNAME("Assembler::calculateElementVector()");

157
    if (remember && factor != 1.0)
158
159
160
161
      rememberElVec = true;

    Element *el = elInfo->getElement();

162
    if ((el != lastMatEl && el != lastVecEl) || !operat->isOptimized())
163
      initElement(elInfo);
164
    
Thomas Witkowski's avatar
Thomas Witkowski committed
165
    if (el != lastVecEl || !operat->isOptimized()) {
166
167
168
      if (rememberElVec)
	set_to_zero(elementVector);
	
169
170
      lastVecEl = el;
    } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
171
      if (rememberElVec) {
172
	userVec += factor * elementVector;
173
174
175
	return;
      }
    }
176
177

    ElementVector& vec = rememberElVec ? elementVector : userVec;
Thomas Witkowski's avatar
Thomas Witkowski committed
178
    if (operat->uhOld && remember) {
179
      matVecAssemble(elInfo, vec);
180
      if (rememberElVec)
181
	userVec += factor * elementVector;      
182

183
184
      return;
    } 
185
186

    if (firstOrderAssemblerGrdPsi)
187
      firstOrderAssemblerGrdPsi->calculateElementVector(elInfo, vec);
188
    if (zeroOrderAssembler)
189
      zeroOrderAssembler->calculateElementVector(elInfo, vec);
190
      
191
    if (rememberElVec)
192
      userVec += factor * elementVector;    
193
194
  }

195

Thomas Witkowski's avatar
Thomas Witkowski committed
196
197
198
199
  void Assembler::calculateElementVector(const ElInfo *mainElInfo, 
					 const ElInfo *auxElInfo,
					 const ElInfo *smallElInfo,
					 const ElInfo *largeElInfo,
200
					 ElementVector& userVec, 
Thomas Witkowski's avatar
Thomas Witkowski committed
201
202
203
204
					 double factor)
  {
    FUNCNAME("Assembler::calculateElementVector()");

205
    if (remember && factor != 1.0)
Thomas Witkowski's avatar
Thomas Witkowski committed
206
207
208
209
      rememberElVec = true;

    Element *el = mainElInfo->getElement();

210
    if ((el != lastMatEl && el != lastVecEl) || !operat->isOptimized())
211
212
      initElement(smallElInfo, largeElInfo);
   
Thomas Witkowski's avatar
Thomas Witkowski committed
213
    if (el != lastVecEl || !operat->isOptimized()) {
214
215
216
      if (rememberElVec)
	set_to_zero(elementVector);

Thomas Witkowski's avatar
Thomas Witkowski committed
217
218
219
      lastVecEl = el;
    } else {
      if (rememberElVec) {
220
	userVec += factor * elementVector;
Thomas Witkowski's avatar
Thomas Witkowski committed
221
222
223
	return;
      }
    }
224
    ElementVector& vec = rememberElVec ? elementVector : userVec;
Thomas Witkowski's avatar
Thomas Witkowski committed
225
226

    if (operat->uhOld && remember) {
227
      if (smallElInfo->getLevel() == largeElInfo->getLevel())
Thomas Witkowski's avatar
Thomas Witkowski committed
228
	matVecAssemble(auxElInfo, vec);
229
230
      else
	matVecAssemble(mainElInfo, auxElInfo, smallElInfo, largeElInfo, vec);      
Thomas Witkowski's avatar
Thomas Witkowski committed
231

232
      if (rememberElVec)
233
	userVec += factor * elementVector;      
234

Thomas Witkowski's avatar
Thomas Witkowski committed
235
236
237
238
239
      return;
    } 

    ERROR_EXIT("Not yet implemented!\n");

240
241
242
243
244
245
246
247
#if 0
    if (firstOrderAssemblerGrdPsi)
      firstOrderAssemblerGrdPsi->calculateElementVector(elInfo, vec);    
    if (zeroOrderAssembler)
      zeroOrderAssembler->calculateElementVector(elInfo, vec);    
    if (rememberElVec)
      axpy(factor, *elementVector, *userVec);
#endif
Thomas Witkowski's avatar
Thomas Witkowski committed
248
249
  }

250

251
  void Assembler::matVecAssemble(const ElInfo *elInfo, ElementVector& vec)
252
253
254
  {
    FUNCNAME("Assembler::matVecAssemble()");

255
    Element *el = elInfo->getElement(); 
256
    double *uhOldLoc = new double[nRow];
257

258
    operat->uhOld->getLocalVector(el, uhOldLoc);
259
    
260
    if (el != lastMatEl) {
261
      set_to_zero(elementMatrix);
262
      calculateElementMatrix(elInfo, elementMatrix);
263
264
    }

265
    for (int i = 0; i < nRow; i++) {
266
      double val = 0.0;
267
      for (int j = 0; j < nRow; j++)
268
	val += elementMatrix[i][j] * uhOldLoc[j];
269
      
270
      vec[i] += val;
271
    }   
272

Thomas Witkowski's avatar
Thomas Witkowski committed
273
274
275
    delete [] uhOldLoc;
  }

276

Thomas Witkowski's avatar
Thomas Witkowski committed
277
278
  void Assembler::matVecAssemble(const ElInfo *mainElInfo, const ElInfo *auxElInfo,
				 const ElInfo *smallElInfo, const ElInfo *largeElInfo,
279
				 ElementVector& vec)
Thomas Witkowski's avatar
Thomas Witkowski committed
280
281
282
283
284
285
286
287
288
289
290
291
292
293
  {
    FUNCNAME("Assembler::matVecAssemble()");

    TEST_EXIT(rowFESpace->getBasisFcts() == colFESpace->getBasisFcts())
      ("Works only for equal basis functions for different components!\n");

    TEST_EXIT(operat->uhOld->getFESpace()->getMesh() == auxElInfo->getMesh())
      ("Da stimmt was nicht!\n");

    Element *mainEl = mainElInfo->getElement(); 
    Element *auxEl = auxElInfo->getElement();

    const BasisFunction *basFcts = rowFESpace->getBasisFcts();
    int nBasFcts = basFcts->getNumber();
294
    std::vector<double> uhOldLoc(nBasFcts);
Thomas Witkowski's avatar
Thomas Witkowski committed
295

296
    operat->uhOld->getLocalVector(auxEl, &(uhOldLoc[0]));
Thomas Witkowski's avatar
Thomas Witkowski committed
297
298

    if (mainEl != lastMatEl) {
299
      set_to_zero(elementMatrix);
300
      calculateElementMatrix(mainElInfo, auxElInfo, smallElInfo, largeElInfo, 
301
 			     elementMatrix);    
Thomas Witkowski's avatar
Thomas Witkowski committed
302
    }
303

Thomas Witkowski's avatar
Thomas Witkowski committed
304
305
    for (int i = 0; i < nBasFcts; i++) {
      double val = 0.0;
306
      for (int j = 0; j < nBasFcts; j++)
307
 	val += elementMatrix[i][j] * uhOldLoc[j];
308
      vec[i] += val;
309
    }   
310
311
  }

312

Thomas Witkowski's avatar
Thomas Witkowski committed
313
314
315
  void Assembler::initElement(const ElInfo *smallElInfo, 
			      const ElInfo *largeElInfo,
			      Quadrature *quad)
316
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
317
    if (secondOrderAssembler) 
Thomas Witkowski's avatar
Thomas Witkowski committed
318
      secondOrderAssembler->initElement(smallElInfo, largeElInfo, quad);
Thomas Witkowski's avatar
Thomas Witkowski committed
319
    if (firstOrderAssemblerGrdPsi)
Thomas Witkowski's avatar
Thomas Witkowski committed
320
      firstOrderAssemblerGrdPsi->initElement(smallElInfo, largeElInfo, quad);
Thomas Witkowski's avatar
Thomas Witkowski committed
321
    if (firstOrderAssemblerGrdPhi)
Thomas Witkowski's avatar
Thomas Witkowski committed
322
      firstOrderAssemblerGrdPhi->initElement(smallElInfo, largeElInfo, quad);
Thomas Witkowski's avatar
Thomas Witkowski committed
323
    if (zeroOrderAssembler)
Thomas Witkowski's avatar
Thomas Witkowski committed
324
      zeroOrderAssembler->initElement(smallElInfo, largeElInfo, quad);
325
326
  }

327

328
  void Assembler::checkQuadratures()
Thomas Witkowski's avatar
Thomas Witkowski committed
329
330
  { 
    if (secondOrderAssembler) {
331
      // create quadrature
Thomas Witkowski's avatar
Thomas Witkowski committed
332
333
      if (!secondOrderAssembler->getQuadrature()) {
	int dim = rowFESpace->getMesh()->getDim();
334
335
336
337
338
	int degree = operat->getQuadratureDegree(2);
	Quadrature *quadrature = Quadrature::provideQuadrature(dim, degree);
	secondOrderAssembler->setQuadrature(quadrature);
      }
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
339
    if (firstOrderAssemblerGrdPsi) {
340
      // create quadrature
Thomas Witkowski's avatar
Thomas Witkowski committed
341
342
      if (!firstOrderAssemblerGrdPsi->getQuadrature()) {
	int dim = rowFESpace->getMesh()->getDim();
343
344
345
346
347
	int degree = operat->getQuadratureDegree(1, GRD_PSI);
	Quadrature *quadrature = Quadrature::provideQuadrature(dim, degree);
	firstOrderAssemblerGrdPsi->setQuadrature(quadrature);
      }
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
348
    if (firstOrderAssemblerGrdPhi) {
349
      // create quadrature
Thomas Witkowski's avatar
Thomas Witkowski committed
350
351
      if (!firstOrderAssemblerGrdPhi->getQuadrature()) {
	int dim = rowFESpace->getMesh()->getDim();
352
353
354
355
356
	int degree = operat->getQuadratureDegree(1, GRD_PHI);
	Quadrature *quadrature = Quadrature::provideQuadrature(dim, degree);
	firstOrderAssemblerGrdPhi->setQuadrature(quadrature);
      }
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
357
    if (zeroOrderAssembler) {
358
      // create quadrature
Thomas Witkowski's avatar
Thomas Witkowski committed
359
360
      if (!zeroOrderAssembler->getQuadrature()) {
	int dim = rowFESpace->getMesh()->getDim();
361
362
363
364
365
366
367
	int degree = operat->getQuadratureDegree(0);
	Quadrature *quadrature = Quadrature::provideQuadrature(dim, degree);
	zeroOrderAssembler->setQuadrature(quadrature);
      }
    }
  }

368

Thomas Witkowski's avatar
Thomas Witkowski committed
369
370
371
372
373
  void Assembler::finishAssembling()
  {
    lastVecEl = NULL;
    lastMatEl = NULL;
  }
Thomas Witkowski's avatar
Thomas Witkowski committed
374

375

Thomas Witkowski's avatar
Thomas Witkowski committed
376
377
378
379
380
  OptimizedAssembler::OptimizedAssembler(Operator  *op,
					 Quadrature *quad2,
					 Quadrature *quad1GrdPsi,
					 Quadrature *quad1GrdPhi,
					 Quadrature *quad0,
381
382
383
					 const FiniteElemSpace *rowFeSpace,
					 const FiniteElemSpace *colFeSpace) 
    : Assembler(op, rowFeSpace, colFeSpace)
Thomas Witkowski's avatar
Thomas Witkowski committed
384
  {
385
    bool opt = (rowFeSpace->getBasisFcts() == colFeSpace->getBasisFcts());
Thomas Witkowski's avatar
Thomas Witkowski committed
386
387
388
389
390
391
392
393
394
395
396
397
398
399

    // create sub assemblers
    secondOrderAssembler = 
      SecondOrderAssembler::getSubAssembler(op, this, quad2, opt);
    firstOrderAssemblerGrdPsi = 
      FirstOrderAssembler::getSubAssembler(op, this, quad1GrdPsi, GRD_PSI, opt);
    firstOrderAssemblerGrdPhi = 
      FirstOrderAssembler::getSubAssembler(op, this, quad1GrdPhi, GRD_PHI, opt);
    zeroOrderAssembler = 
      ZeroOrderAssembler::getSubAssembler(op, this, quad0, opt);

    checkQuadratures();
  }

400

Thomas Witkowski's avatar
Thomas Witkowski committed
401
402
403
404
405
  StandardAssembler::StandardAssembler(Operator *op,
				       Quadrature *quad2,
				       Quadrature *quad1GrdPsi,
				       Quadrature *quad1GrdPhi,
				       Quadrature *quad0,
406
407
408
				       const FiniteElemSpace *rowFeSpace,
				       const FiniteElemSpace *colFeSpace) 
    : Assembler(op, rowFeSpace, colFeSpace)
Thomas Witkowski's avatar
Thomas Witkowski committed
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
  {
    remember = false;

    // create sub assemblers
    secondOrderAssembler = 
      SecondOrderAssembler::getSubAssembler(op, this, quad2, false);
    firstOrderAssemblerGrdPsi = 
      FirstOrderAssembler::getSubAssembler(op, this, quad1GrdPsi, GRD_PSI, false);
    firstOrderAssemblerGrdPhi = 
      FirstOrderAssembler::getSubAssembler(op, this, quad1GrdPhi, GRD_PHI, false);
    zeroOrderAssembler = 
      ZeroOrderAssembler::getSubAssembler(op, this, quad0, false);

    checkQuadratures();
  }

425
}