Assembler.cc 12.5 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())
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
      secondOrderAssembler->calculateElementMatrix(smallElInfo, mat);
113

114
      ElementMatrix &m = 
115
	smallElInfo->getSubElemGradCoordsMat(rowFeSpace->getBasisFcts()->getDegree());
116
117
118
119
120
121
122
      
      if (smallElInfo == colElInfo)
	tmpMat = m * mat;	
      else
  	tmpMat = mat * trans(m);
      
      mat = tmpMat;
123
124
125
    }

    if (firstOrderAssemblerGrdPsi) {
126
127
      firstOrderAssemblerGrdPsi->calculateElementMatrix(smallElInfo, mat);

128
      if (largeElInfo == rowElInfo) {
129
	ElementMatrix &m = 
130
	  smallElInfo->getSubElemGradCoordsMat(rowFeSpace->getBasisFcts()->getDegree());
131

132
133
	tmpMat = m * mat;
      } else {
134
	ElementMatrix &m = 
135
	  smallElInfo->getSubElemCoordsMat(rowFeSpace->getBasisFcts()->getDegree());
136
137
138
	
	tmpMat = mat * trans(m);
      }
139
140
	
      mat = tmpMat;
141
142
143
    }

    if (firstOrderAssemblerGrdPhi) {
144
      firstOrderAssemblerGrdPhi->calculateElementMatrix(smallElInfo, mat);
145

146
      if (largeElInfo == colElInfo) {
147
	ElementMatrix &m = 
148
	  smallElInfo->getSubElemGradCoordsMat(rowFeSpace->getBasisFcts()->getDegree());
149
150
151
152

	tmpMat = mat * trans(m);
      } else {
	ElementMatrix &m = 
153
	  smallElInfo->getSubElemCoordsMat(rowFeSpace->getBasisFcts()->getDegree());
154
155
156
157
158
	
	tmpMat = m * mat;	
      }

      mat = tmpMat;
159
    }
160

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

175
176
    if (rememberElMat && &userMat != &elementMatrix)
      userMat += factor * elementMatrix;   
177
178
  }

179

180
  void Assembler::calculateElementVector(const ElInfo *elInfo, 
181
					 ElementVector& userVec,
182
183
184
185
					 double factor)
  {
    FUNCNAME("Assembler::calculateElementVector()");

186
    if (remember && factor != 1.0)
187
188
189
190
      rememberElVec = true;

    Element *el = elInfo->getElement();

191
    if ((el != lastMatEl && el != lastVecEl) || !operat->isOptimized())
192
      initElement(elInfo);
193
    
Thomas Witkowski's avatar
Thomas Witkowski committed
194
    if (el != lastVecEl || !operat->isOptimized()) {
195
196
197
      if (rememberElVec)
	set_to_zero(elementVector);
	
198
199
      lastVecEl = el;
    } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
200
      if (rememberElVec) {
201
	userVec += factor * elementVector;
202
203
204
	return;
      }
    }
205
206

    ElementVector& vec = rememberElVec ? elementVector : userVec;
207

Thomas Witkowski's avatar
Thomas Witkowski committed
208
    if (operat->uhOld && remember) {
209
      matVecAssemble(elInfo, vec);
210
      if (rememberElVec)
211
	userVec += factor * elementVector;      
212

213
214
      return;
    } 
215
216

    if (firstOrderAssemblerGrdPsi)
217
      firstOrderAssemblerGrdPsi->calculateElementVector(elInfo, vec);
218
    if (zeroOrderAssembler)
219
      zeroOrderAssembler->calculateElementVector(elInfo, vec);
220
      
221
    if (rememberElVec)
222
      userVec += factor * elementVector;    
223
224
  }

225

Thomas Witkowski's avatar
Thomas Witkowski committed
226
227
228
229
  void Assembler::calculateElementVector(const ElInfo *mainElInfo, 
					 const ElInfo *auxElInfo,
					 const ElInfo *smallElInfo,
					 const ElInfo *largeElInfo,
230
					 ElementVector& userVec, 
Thomas Witkowski's avatar
Thomas Witkowski committed
231
232
233
234
					 double factor)
  {
    FUNCNAME("Assembler::calculateElementVector()");

235
    if (remember && factor != 1.0)
Thomas Witkowski's avatar
Thomas Witkowski committed
236
237
238
239
      rememberElVec = true;

    Element *el = mainElInfo->getElement();

240
    if ((el != lastMatEl && el != lastVecEl) || !operat->isOptimized())
241
242
      initElement(smallElInfo, largeElInfo);
   
Thomas Witkowski's avatar
Thomas Witkowski committed
243
    if (el != lastVecEl || !operat->isOptimized()) {
244
245
246
      if (rememberElVec)
	set_to_zero(elementVector);

Thomas Witkowski's avatar
Thomas Witkowski committed
247
248
249
      lastVecEl = el;
    } else {
      if (rememberElVec) {
250
	userVec += factor * elementVector;
Thomas Witkowski's avatar
Thomas Witkowski committed
251
252
253
	return;
      }
    }
254
    ElementVector& vec = rememberElVec ? elementVector : userVec;
Thomas Witkowski's avatar
Thomas Witkowski committed
255
256

    if (operat->uhOld && remember) {
257
      if (smallElInfo->getLevel() == largeElInfo->getLevel())
Thomas Witkowski's avatar
Thomas Witkowski committed
258
	matVecAssemble(auxElInfo, vec);
259
260
      else
	matVecAssemble(mainElInfo, auxElInfo, smallElInfo, largeElInfo, vec);      
Thomas Witkowski's avatar
Thomas Witkowski committed
261

262
      if (rememberElVec)
263
	userVec += factor * elementVector;      
264

Thomas Witkowski's avatar
Thomas Witkowski committed
265
266
267
      return;
    } 

268
269
270
271
272
273
274
275
276
277
    if (firstOrderAssemblerGrdPsi) {
      ERROR_EXIT("Not yet implemented!\n");
    }

    if (zeroOrderAssembler) {
      zeroOrderAssembler->calculateElementVector(smallElInfo, vec);
      
      if (smallElInfo != mainElInfo) {
	ElementVector tmpVec(vec);	
	ElementMatrix &m = 
278
	  smallElInfo->getSubElemCoordsMat(rowFeSpace->getBasisFcts()->getDegree());
279
280
281
282
283

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

285
    if (rememberElVec)
286
      userVec += factor * elementVector;    
Thomas Witkowski's avatar
Thomas Witkowski committed
287
288
  }

289

290
  void Assembler::matVecAssemble(const ElInfo *elInfo, ElementVector& vec)
291
292
293
  {
    FUNCNAME("Assembler::matVecAssemble()");

294
    Element *el = elInfo->getElement(); 
295
    std::vector<double> uhOldLoc(operat->uhOld->getFeSpace() == rowFeSpace ? nRow : nCol);
296

297
    operat->uhOld->getLocalVector(el, &(uhOldLoc[0]));
298
    
299
    if (el != lastMatEl) {
300
      set_to_zero(elementMatrix);
301
      calculateElementMatrix(elInfo, elementMatrix);
302
303
    }

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

313

Thomas Witkowski's avatar
Thomas Witkowski committed
314
315
  void Assembler::matVecAssemble(const ElInfo *mainElInfo, const ElInfo *auxElInfo,
				 const ElInfo *smallElInfo, const ElInfo *largeElInfo,
316
				 ElementVector& vec)
Thomas Witkowski's avatar
Thomas Witkowski committed
317
318
319
  {
    FUNCNAME("Assembler::matVecAssemble()");

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

323
    TEST_EXIT(operat->uhOld->getFeSpace()->getMesh() == auxElInfo->getMesh())
Thomas Witkowski's avatar
Thomas Witkowski committed
324
325
326
327
328
      ("Da stimmt was nicht!\n");

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

329
    const BasisFunction *basFcts = rowFeSpace->getBasisFcts();
Thomas Witkowski's avatar
Thomas Witkowski committed
330
    int nBasFcts = basFcts->getNumber();
331
    std::vector<double> uhOldLoc(nBasFcts);
Thomas Witkowski's avatar
Thomas Witkowski committed
332

333
    operat->uhOld->getLocalVector(auxEl, &(uhOldLoc[0]));
Thomas Witkowski's avatar
Thomas Witkowski committed
334
335

    if (mainEl != lastMatEl) {
336
      set_to_zero(elementMatrix);
337
      calculateElementMatrix(mainElInfo, auxElInfo, smallElInfo, largeElInfo, 
338
 			     elementMatrix);    
Thomas Witkowski's avatar
Thomas Witkowski committed
339
    }
340

Thomas Witkowski's avatar
Thomas Witkowski committed
341
342
    for (int i = 0; i < nBasFcts; i++) {
      double val = 0.0;
343
      for (int j = 0; j < nBasFcts; j++)
344
 	val += elementMatrix[i][j] * uhOldLoc[j];
345
      vec[i] += val;
346
    }   
347
348
  }

349

Thomas Witkowski's avatar
Thomas Witkowski committed
350
351
352
  void Assembler::initElement(const ElInfo *smallElInfo, 
			      const ElInfo *largeElInfo,
			      Quadrature *quad)
353
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
354
    if (secondOrderAssembler) 
Thomas Witkowski's avatar
Thomas Witkowski committed
355
      secondOrderAssembler->initElement(smallElInfo, largeElInfo, quad);
Thomas Witkowski's avatar
Thomas Witkowski committed
356
    if (firstOrderAssemblerGrdPsi)
Thomas Witkowski's avatar
Thomas Witkowski committed
357
      firstOrderAssemblerGrdPsi->initElement(smallElInfo, largeElInfo, quad);
Thomas Witkowski's avatar
Thomas Witkowski committed
358
    if (firstOrderAssemblerGrdPhi)
Thomas Witkowski's avatar
Thomas Witkowski committed
359
      firstOrderAssemblerGrdPhi->initElement(smallElInfo, largeElInfo, quad);
Thomas Witkowski's avatar
Thomas Witkowski committed
360
    if (zeroOrderAssembler)
Thomas Witkowski's avatar
Thomas Witkowski committed
361
      zeroOrderAssembler->initElement(smallElInfo, largeElInfo, quad);
362
363
  }

364

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

405

Thomas Witkowski's avatar
Thomas Witkowski committed
406
407
408
409
410
  void Assembler::finishAssembling()
  {
    lastVecEl = NULL;
    lastMatEl = NULL;
  }
Thomas Witkowski's avatar
Thomas Witkowski committed
411

412

Thomas Witkowski's avatar
Thomas Witkowski committed
413
414
415
416
417
  OptimizedAssembler::OptimizedAssembler(Operator  *op,
					 Quadrature *quad2,
					 Quadrature *quad1GrdPsi,
					 Quadrature *quad1GrdPhi,
					 Quadrature *quad0,
418
419
420
					 const FiniteElemSpace *rowFeSpace,
					 const FiniteElemSpace *colFeSpace) 
    : Assembler(op, rowFeSpace, colFeSpace)
Thomas Witkowski's avatar
Thomas Witkowski committed
421
  {
422
    bool opt = (rowFeSpace->getBasisFcts() == colFeSpace->getBasisFcts());
Thomas Witkowski's avatar
Thomas Witkowski committed
423
424
425
426
427
428
429
430
431
432
433
434
435
436

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

437

Thomas Witkowski's avatar
Thomas Witkowski committed
438
439
440
441
442
  StandardAssembler::StandardAssembler(Operator *op,
				       Quadrature *quad2,
				       Quadrature *quad1GrdPsi,
				       Quadrature *quad1GrdPhi,
				       Quadrature *quad0,
443
444
445
				       const FiniteElemSpace *rowFeSpace,
				       const FiniteElemSpace *colFeSpace) 
    : Assembler(op, rowFeSpace, colFeSpace)
Thomas Witkowski's avatar
Thomas Witkowski committed
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
  {
    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();
  }

462
}