Assembler.cc 12.6 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),
17
18
19
20
      rowFeSpace(row),
      colFeSpace(col ? col : row),
      nRow(rowFeSpace->getBasisFcts()->getNumber()),
      nCol(colFeSpace->getBasisFcts()->getNumber()),
21
22
23
      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()) {
95
96
97
      if (smallElInfo == colElInfo)
	initElement(smallElInfo);
      else
98
99
	initElement(smallElInfo, largeElInfo);
    }      
100
101

    if (el != lastMatEl || !operat->isOptimized()) {
102
103
104
      if (rememberElMat)
	set_to_zero(elementMatrix);

105
106
107
      lastMatEl = el;
    } else {
      if (rememberElMat) {
108
	userMat += factor * elementMatrix;
109
110
111
	return;
      }
    }
112
 
113
    ElementMatrix& mat = rememberElMat ? elementMatrix : userMat;
114

115
    if (secondOrderAssembler) {
116
      secondOrderAssembler->calculateElementMatrix(smallElInfo, mat);
117

118
      ElementMatrix &m = 
119
	smallElInfo->getSubElemGradCoordsMat(rowFeSpace->getBasisFcts()->getDegree());
120
121
122
123
124
125
126
      
      if (smallElInfo == colElInfo)
	tmpMat = m * mat;	
      else
  	tmpMat = mat * trans(m);
      
      mat = tmpMat;
127
128
129
    }

    if (firstOrderAssemblerGrdPsi) {
130
131
      firstOrderAssemblerGrdPsi->calculateElementMatrix(smallElInfo, mat);

132
      if (largeElInfo == rowElInfo) {
133
	ElementMatrix &m = 
134
	  smallElInfo->getSubElemGradCoordsMat(rowFeSpace->getBasisFcts()->getDegree());
135

136
137
	tmpMat = m * mat;
      } else {
138
	ElementMatrix &m = 
139
	  smallElInfo->getSubElemCoordsMat(rowFeSpace->getBasisFcts()->getDegree());
140
141
142
	
	tmpMat = mat * trans(m);
      }
143
144
	
      mat = tmpMat;
145
146
147
    }

    if (firstOrderAssemblerGrdPhi) {
148
      firstOrderAssemblerGrdPhi->calculateElementMatrix(smallElInfo, mat);
149

150
      if (largeElInfo == colElInfo) {
151
	ElementMatrix &m = 
152
	  smallElInfo->getSubElemGradCoordsMat(rowFeSpace->getBasisFcts()->getDegree());
153
154
155
156

	tmpMat = mat * trans(m);
      } else {
	ElementMatrix &m = 
157
	  smallElInfo->getSubElemCoordsMat(rowFeSpace->getBasisFcts()->getDegree());
158
159
160
161
162
	
	tmpMat = m * mat;	
      }

      mat = tmpMat;
163
    }
164

165
166
    if (zeroOrderAssembler) {
      zeroOrderAssembler->calculateElementMatrix(smallElInfo, mat);
Thomas Witkowski's avatar
Thomas Witkowski committed
167
      
168
      ElementMatrix &m = 
169
	smallElInfo->getSubElemCoordsMat(rowFeSpace->getBasisFcts()->getDegree());
Thomas Witkowski's avatar
Thomas Witkowski committed
170
      
Thomas Witkowski's avatar
Thomas Witkowski committed
171
      if (smallElInfo == colElInfo)
172
173
174
175
176
177
	tmpMat = m * mat;
      else 
	tmpMat = mat;

//       else
//   	tmpMat = mat * trans(m);
Thomas Witkowski's avatar
Thomas Witkowski committed
178
179
      
      mat = tmpMat;
180
    }
181

182
    if (rememberElMat && &userMat != &elementMatrix)
183
      userMat += factor * elementMatrix;       
184
185
  }

186

187
  void Assembler::calculateElementVector(const ElInfo *elInfo, 
188
					 ElementVector& userVec,
189
190
191
192
					 double factor)
  {
    FUNCNAME("Assembler::calculateElementVector()");

193
    if (remember && factor != 1.0)
194
195
196
197
      rememberElVec = true;

    Element *el = elInfo->getElement();

198
    if ((el != lastMatEl && el != lastVecEl) || !operat->isOptimized())
199
      initElement(elInfo);
200
    
Thomas Witkowski's avatar
Thomas Witkowski committed
201
    if (el != lastVecEl || !operat->isOptimized()) {
202
203
204
      if (rememberElVec)
	set_to_zero(elementVector);
	
205
206
      lastVecEl = el;
    } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
207
      if (rememberElVec) {
208
	userVec += factor * elementVector;
209
210
211
	return;
      }
    }
212
213

    ElementVector& vec = rememberElVec ? elementVector : userVec;
214

Thomas Witkowski's avatar
Thomas Witkowski committed
215
    if (operat->uhOld && remember) {
216
      matVecAssemble(elInfo, vec);
217
      if (rememberElVec)
218
	userVec += factor * elementVector;      
219

220
221
      return;
    } 
222
223

    if (firstOrderAssemblerGrdPsi)
224
      firstOrderAssemblerGrdPsi->calculateElementVector(elInfo, vec);
225
    if (zeroOrderAssembler)
226
      zeroOrderAssembler->calculateElementVector(elInfo, vec);
227
      
228
    if (rememberElVec)
229
      userVec += factor * elementVector;    
230
231
  }

232

Thomas Witkowski's avatar
Thomas Witkowski committed
233
234
235
236
  void Assembler::calculateElementVector(const ElInfo *mainElInfo, 
					 const ElInfo *auxElInfo,
					 const ElInfo *smallElInfo,
					 const ElInfo *largeElInfo,
237
					 ElementVector& userVec, 
Thomas Witkowski's avatar
Thomas Witkowski committed
238
239
240
241
					 double factor)
  {
    FUNCNAME("Assembler::calculateElementVector()");

242
    if (remember && factor != 1.0)
Thomas Witkowski's avatar
Thomas Witkowski committed
243
244
245
246
      rememberElVec = true;

    Element *el = mainElInfo->getElement();

247
    if ((el != lastMatEl && el != lastVecEl) || !operat->isOptimized())
248
249
      initElement(smallElInfo, largeElInfo);
   
Thomas Witkowski's avatar
Thomas Witkowski committed
250
    if (el != lastVecEl || !operat->isOptimized()) {
251
252
253
      if (rememberElVec)
	set_to_zero(elementVector);

Thomas Witkowski's avatar
Thomas Witkowski committed
254
255
256
      lastVecEl = el;
    } else {
      if (rememberElVec) {
257
	userVec += factor * elementVector;
Thomas Witkowski's avatar
Thomas Witkowski committed
258
259
260
	return;
      }
    }
261
    ElementVector& vec = rememberElVec ? elementVector : userVec;
Thomas Witkowski's avatar
Thomas Witkowski committed
262
263

    if (operat->uhOld && remember) {
264
      if (smallElInfo->getLevel() == largeElInfo->getLevel())
Thomas Witkowski's avatar
Thomas Witkowski committed
265
	matVecAssemble(auxElInfo, vec);
266
267
      else
	matVecAssemble(mainElInfo, auxElInfo, smallElInfo, largeElInfo, vec);      
Thomas Witkowski's avatar
Thomas Witkowski committed
268

269
      if (rememberElVec)
270
	userVec += factor * elementVector;      
271

Thomas Witkowski's avatar
Thomas Witkowski committed
272
273
274
      return;
    } 

275
276
277
278
279
280
281
282
283
284
    if (firstOrderAssemblerGrdPsi) {
      ERROR_EXIT("Not yet implemented!\n");
    }

    if (zeroOrderAssembler) {
      zeroOrderAssembler->calculateElementVector(smallElInfo, vec);
      
      if (smallElInfo != mainElInfo) {
	ElementVector tmpVec(vec);	
	ElementMatrix &m = 
285
	  smallElInfo->getSubElemCoordsMat(rowFeSpace->getBasisFcts()->getDegree());
286
287
288
289
290

	tmpVec = m * vec;	
	vec = tmpVec;
      }      
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
291

292
    if (rememberElVec)
293
      userVec += factor * elementVector;    
Thomas Witkowski's avatar
Thomas Witkowski committed
294
295
  }

296

297
  void Assembler::matVecAssemble(const ElInfo *elInfo, ElementVector& vec)
298
299
300
  {
    FUNCNAME("Assembler::matVecAssemble()");

301
    Element *el = elInfo->getElement(); 
302
    ElementVector uhOldLoc(operat->uhOld->getFeSpace() == rowFeSpace ? nRow : nCol);
303

304
    operat->uhOld->getLocalVector(el, uhOldLoc);
305
    
306
    if (el != lastMatEl) {
307
      set_to_zero(elementMatrix);
308
      calculateElementMatrix(elInfo, elementMatrix);
309
310
    }

311
    for (int i = 0; i < nRow; i++) {
312
      double val = 0.0;
313
      for (int j = 0; j < nCol; j++)
314
	val += elementMatrix[i][j] * uhOldLoc[j];
315
      
316
      vec[i] += val;
317
    }   
Thomas Witkowski's avatar
Thomas Witkowski committed
318
319
  }

320

Thomas Witkowski's avatar
Thomas Witkowski committed
321
322
  void Assembler::matVecAssemble(const ElInfo *mainElInfo, const ElInfo *auxElInfo,
				 const ElInfo *smallElInfo, const ElInfo *largeElInfo,
323
				 ElementVector& vec)
Thomas Witkowski's avatar
Thomas Witkowski committed
324
325
326
  {
    FUNCNAME("Assembler::matVecAssemble()");

327
    TEST_EXIT(rowFeSpace->getBasisFcts() == colFeSpace->getBasisFcts())
Thomas Witkowski's avatar
Thomas Witkowski committed
328
329
      ("Works only for equal basis functions for different components!\n");

330
    TEST_EXIT(operat->uhOld->getFeSpace()->getMesh() == auxElInfo->getMesh())
Thomas Witkowski's avatar
Thomas Witkowski committed
331
332
333
334
335
      ("Da stimmt was nicht!\n");

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

336
    const BasisFunction *basFcts = rowFeSpace->getBasisFcts();
Thomas Witkowski's avatar
Thomas Witkowski committed
337
    int nBasFcts = basFcts->getNumber();
338
    ElementVector uhOldLoc(nBasFcts);
Thomas Witkowski's avatar
Thomas Witkowski committed
339

340
    operat->uhOld->getLocalVector(auxEl, uhOldLoc);
Thomas Witkowski's avatar
Thomas Witkowski committed
341
342

    if (mainEl != lastMatEl) {
343
      set_to_zero(elementMatrix);
344
      calculateElementMatrix(mainElInfo, auxElInfo, smallElInfo, largeElInfo, 
345
 			     elementMatrix);    
Thomas Witkowski's avatar
Thomas Witkowski committed
346
    }
347

Thomas Witkowski's avatar
Thomas Witkowski committed
348
349
    for (int i = 0; i < nBasFcts; i++) {
      double val = 0.0;
350
      for (int j = 0; j < nBasFcts; j++)
351
 	val += elementMatrix[i][j] * uhOldLoc[j];
352
      vec[i] += val;
353
    }   
354
355
  }

356

Thomas Witkowski's avatar
Thomas Witkowski committed
357
358
359
  void Assembler::initElement(const ElInfo *smallElInfo, 
			      const ElInfo *largeElInfo,
			      Quadrature *quad)
360
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
361
    if (secondOrderAssembler) 
Thomas Witkowski's avatar
Thomas Witkowski committed
362
      secondOrderAssembler->initElement(smallElInfo, largeElInfo, quad);
Thomas Witkowski's avatar
Thomas Witkowski committed
363
    if (firstOrderAssemblerGrdPsi)
Thomas Witkowski's avatar
Thomas Witkowski committed
364
      firstOrderAssemblerGrdPsi->initElement(smallElInfo, largeElInfo, quad);
Thomas Witkowski's avatar
Thomas Witkowski committed
365
    if (firstOrderAssemblerGrdPhi)
Thomas Witkowski's avatar
Thomas Witkowski committed
366
      firstOrderAssemblerGrdPhi->initElement(smallElInfo, largeElInfo, quad);
Thomas Witkowski's avatar
Thomas Witkowski committed
367
    if (zeroOrderAssembler)
Thomas Witkowski's avatar
Thomas Witkowski committed
368
      zeroOrderAssembler->initElement(smallElInfo, largeElInfo, quad);
369
370
  }

371

372
  void Assembler::checkQuadratures()
Thomas Witkowski's avatar
Thomas Witkowski committed
373
374
  { 
    if (secondOrderAssembler) {
375
      // create quadrature
Thomas Witkowski's avatar
Thomas Witkowski committed
376
      if (!secondOrderAssembler->getQuadrature()) {
377
	int dim = rowFeSpace->getMesh()->getDim();
378
379
380
381
382
	int degree = operat->getQuadratureDegree(2);
	Quadrature *quadrature = Quadrature::provideQuadrature(dim, degree);
	secondOrderAssembler->setQuadrature(quadrature);
      }
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
383
    if (firstOrderAssemblerGrdPsi) {
384
      // create quadrature
Thomas Witkowski's avatar
Thomas Witkowski committed
385
      if (!firstOrderAssemblerGrdPsi->getQuadrature()) {
386
	int dim = rowFeSpace->getMesh()->getDim();
387
388
389
390
391
	int degree = operat->getQuadratureDegree(1, GRD_PSI);
	Quadrature *quadrature = Quadrature::provideQuadrature(dim, degree);
	firstOrderAssemblerGrdPsi->setQuadrature(quadrature);
      }
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
392
    if (firstOrderAssemblerGrdPhi) {
393
      // create quadrature
Thomas Witkowski's avatar
Thomas Witkowski committed
394
      if (!firstOrderAssemblerGrdPhi->getQuadrature()) {
395
	int dim = rowFeSpace->getMesh()->getDim();
396
397
398
399
400
	int degree = operat->getQuadratureDegree(1, GRD_PHI);
	Quadrature *quadrature = Quadrature::provideQuadrature(dim, degree);
	firstOrderAssemblerGrdPhi->setQuadrature(quadrature);
      }
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
401
    if (zeroOrderAssembler) {
402
      // create quadrature
Thomas Witkowski's avatar
Thomas Witkowski committed
403
      if (!zeroOrderAssembler->getQuadrature()) {
404
	int dim = rowFeSpace->getMesh()->getDim();
405
406
407
408
409
410
411
	int degree = operat->getQuadratureDegree(0);
	Quadrature *quadrature = Quadrature::provideQuadrature(dim, degree);
	zeroOrderAssembler->setQuadrature(quadrature);
      }
    }
  }

412

Thomas Witkowski's avatar
Thomas Witkowski committed
413
414
415
416
417
  void Assembler::finishAssembling()
  {
    lastVecEl = NULL;
    lastMatEl = NULL;
  }
Thomas Witkowski's avatar
Thomas Witkowski committed
418

419

Thomas Witkowski's avatar
Thomas Witkowski committed
420
421
422
423
424
  OptimizedAssembler::OptimizedAssembler(Operator  *op,
					 Quadrature *quad2,
					 Quadrature *quad1GrdPsi,
					 Quadrature *quad1GrdPhi,
					 Quadrature *quad0,
425
426
427
					 const FiniteElemSpace *rowFeSpace,
					 const FiniteElemSpace *colFeSpace) 
    : Assembler(op, rowFeSpace, colFeSpace)
Thomas Witkowski's avatar
Thomas Witkowski committed
428
  {
429
    bool opt = (rowFeSpace->getBasisFcts() == colFeSpace->getBasisFcts());
Thomas Witkowski's avatar
Thomas Witkowski committed
430
431
432
433
434
435
436
437
438
439
440
441
442
443

    // 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();
  }

444

Thomas Witkowski's avatar
Thomas Witkowski committed
445
446
447
448
449
  StandardAssembler::StandardAssembler(Operator *op,
				       Quadrature *quad2,
				       Quadrature *quad1GrdPsi,
				       Quadrature *quad1GrdPhi,
				       Quadrature *quad0,
450
451
452
				       const FiniteElemSpace *rowFeSpace,
				       const FiniteElemSpace *colFeSpace) 
    : Assembler(op, rowFeSpace, colFeSpace)
Thomas Witkowski's avatar
Thomas Witkowski committed
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
  {
    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();
  }

469
}