Assembler.cc 12.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
//
// Software License for AMDiS
//
// Copyright (c) 2010 Dresden University of Technology 
// All rights reserved.
// Authors: Simon Vey, Thomas Witkowski et al.
//
// This file is part of AMDiS
//
// See also license.opensource.txt in the distribution.


13
14
15
#include <vector>
#include <algorithm>
#include <boost/numeric/mtl/mtl.hpp>
16
17
18
19
20
21
22
23
#include "Assembler.h"
#include "Operator.h"
#include "Element.h"
#include "QPsiPhi.h"
#include "DOFVector.h"

namespace AMDiS {

Thomas Witkowski's avatar
Thomas Witkowski committed
24
25
26
  Assembler::Assembler(Operator *op,
		       const FiniteElemSpace *row,
		       const FiniteElemSpace *col) 
27
    : operat(op),
28
29
30
31
      rowFeSpace(row),
      colFeSpace(col ? col : row),
      nRow(rowFeSpace->getBasisFcts()->getNumber()),
      nCol(colFeSpace->getBasisFcts()->getNumber()),
32
33
34
      remember(true),
      rememberElMat(false),
      rememberElVec(false),
35
36
      elementMatrix(nRow, nCol),
      elementVector(nRow),
37
      tmpMat(nRow, nCol),
38
39
40
      lastMatEl(NULL),
      lastVecEl(NULL),
      lastTraverseId(-1)
41
  {}
Thomas Witkowski's avatar
Thomas Witkowski committed
42

43

Thomas Witkowski's avatar
Thomas Witkowski committed
44
  Assembler::~Assembler()
45
  {}
46

47

48
  void Assembler::calculateElementMatrix(const ElInfo *elInfo, 
49
					 ElementMatrix& userMat,
50
51
52
53
					 double factor)
  {
    FUNCNAME("Assembler::calculateElementMatrix()");

54
    if (remember && (factor != 1.0 || operat->uhOld))
55
      rememberElMat = true;
Thomas Witkowski's avatar
Thomas Witkowski committed
56

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

59
    if ((el != lastMatEl && el != lastVecEl) || !operat->isOptimized())
60
61
62
      initElement(elInfo);

    if (el != lastMatEl || !operat->isOptimized()) {
63
64
65
      if (rememberElMat)
	set_to_zero(elementMatrix);

66
67
68
      lastMatEl = el;
    } else {
      if (rememberElMat) {
69
	userMat += factor * elementMatrix;
70
71
72
	return;
      }
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
73
 
74
    ElementMatrix& mat = rememberElMat ? elementMatrix : userMat;
75
76
77
78
79
80
81
82
83
84

    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
85
    if (rememberElMat && &userMat != &elementMatrix)
86
      userMat += factor * elementMatrix;
87
88
  }

89

90
91
  void Assembler::calculateElementMatrix(const ElInfo *rowElInfo,
					 const ElInfo *colElInfo,
92
93
					 const ElInfo *smallElInfo,
					 const ElInfo *largeElInfo,
94
					 bool rowColFeSpaceEqual,
95
					 ElementMatrix& userMat,
96
97
98
99
					 double factor)
  {
    FUNCNAME("Assembler::calculateElementMatrix()");

100
    if (remember && (factor != 1.0 || operat->uhOld))
101
102
      rememberElMat = true;
  
Thomas Witkowski's avatar
Thomas Witkowski committed
103
    Element *el = smallElInfo->getElement();   
104
    lastVecEl = lastMatEl = NULL;
Thomas Witkowski's avatar
Thomas Witkowski committed
105
   
106
    if ((el != lastMatEl && el != lastVecEl) || !operat->isOptimized()) {
107
      if (smallElInfo == colElInfo)
108
	initElement(smallElInfo);	
109
      else
110
	initElement(smallElInfo, largeElInfo);      
111
    }      
112
113

    if (el != lastMatEl || !operat->isOptimized()) {
114
115
116
      if (rememberElMat)
	set_to_zero(elementMatrix);

117
118
119
      lastMatEl = el;
    } else {
      if (rememberElMat) {
120
	userMat += factor * elementMatrix;
121
122
123
	return;
      }
    }
124
 
125
    ElementMatrix& mat = rememberElMat ? elementMatrix : userMat;
126

127
    if (secondOrderAssembler) {
128
      secondOrderAssembler->calculateElementMatrix(smallElInfo, mat);
129

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

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

146
147
148
149
150
151
152
153
154
155
156
157
      if (!rowColFeSpaceEqual) {
	if (largeElInfo == rowElInfo) {
	  ElementMatrix &m = 
	    smallElInfo->getSubElemGradCoordsMat(rowFeSpace->getBasisFcts()->getDegree());
	  
	  tmpMat = m * mat;
	} else {
	  ElementMatrix &m = 
	    smallElInfo->getSubElemCoordsMat(rowFeSpace->getBasisFcts()->getDegree());
	  
	  tmpMat = mat * trans(m);
	}
158
	
159
	mat = tmpMat;
160
      }
161
162
163
    }

    if (firstOrderAssemblerGrdPhi) {
164
      firstOrderAssemblerGrdPhi->calculateElementMatrix(smallElInfo, mat);
165

166
167
168
169
170
171
172
173
174
175
176
177
      if (!rowColFeSpaceEqual) {
	if (largeElInfo == colElInfo) {
	  ElementMatrix &m = 
	    smallElInfo->getSubElemGradCoordsMat(rowFeSpace->getBasisFcts()->getDegree());
	  
	  tmpMat = mat * trans(m);
	} else {
	  ElementMatrix &m = 
	    smallElInfo->getSubElemCoordsMat(rowFeSpace->getBasisFcts()->getDegree());
	  
	  tmpMat = m * mat;	
	}
178
	
179
	mat = tmpMat;
180
      }
181
    }
182

183
184
    if (zeroOrderAssembler) {
      zeroOrderAssembler->calculateElementMatrix(smallElInfo, mat);
185

186
187
188
189
190
191
192
193
194
195
196
      if (!rowColFeSpaceEqual) {
	ElementMatrix &m = 
	  smallElInfo->getSubElemCoordsMat(rowFeSpace->getBasisFcts()->getDegree());
	
	if (smallElInfo == colElInfo)
	  tmpMat = m * mat;
	else 
	  tmpMat = mat * trans(m);
	
	mat = tmpMat;
      }
197
    }
198

199
    if (rememberElMat && &userMat != &elementMatrix)
200
      userMat += factor * elementMatrix;       
201
202
  }

203

204
  void Assembler::calculateElementVector(const ElInfo *elInfo, 
205
					 ElementVector& userVec,
206
207
208
209
					 double factor)
  {
    FUNCNAME("Assembler::calculateElementVector()");

210
    if (remember && factor != 1.0)
211
212
213
214
      rememberElVec = true;

    Element *el = elInfo->getElement();

215
    if ((el != lastMatEl && el != lastVecEl) || !operat->isOptimized())
216
      initElement(elInfo);
217
    
Thomas Witkowski's avatar
Thomas Witkowski committed
218
    if (el != lastVecEl || !operat->isOptimized()) {
219
220
221
      if (rememberElVec)
	set_to_zero(elementVector);
	
222
223
      lastVecEl = el;
    } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
224
      if (rememberElVec) {
225
	userVec += factor * elementVector;
226
227
228
	return;
      }
    }
229
230

    ElementVector& vec = rememberElVec ? elementVector : userVec;
231

Thomas Witkowski's avatar
Thomas Witkowski committed
232
    if (operat->uhOld && remember) {
233
      matVecAssemble(elInfo, vec);
234
      if (rememberElVec)
235
	userVec += factor * elementVector;      
236

237
238
      return;
    } 
239
240

    if (firstOrderAssemblerGrdPsi)
241
      firstOrderAssemblerGrdPsi->calculateElementVector(elInfo, vec);
242
    if (zeroOrderAssembler)
243
      zeroOrderAssembler->calculateElementVector(elInfo, vec);
244
      
245
    if (rememberElVec)
246
      userVec += factor * elementVector;    
247
248
  }

249

Thomas Witkowski's avatar
Thomas Witkowski committed
250
251
252
253
  void Assembler::calculateElementVector(const ElInfo *mainElInfo, 
					 const ElInfo *auxElInfo,
					 const ElInfo *smallElInfo,
					 const ElInfo *largeElInfo,
254
					 ElementVector& userVec, 
Thomas Witkowski's avatar
Thomas Witkowski committed
255
256
257
258
					 double factor)
  {
    FUNCNAME("Assembler::calculateElementVector()");

259
    if (remember && factor != 1.0)
Thomas Witkowski's avatar
Thomas Witkowski committed
260
261
262
263
      rememberElVec = true;

    Element *el = mainElInfo->getElement();

264
    if ((el != lastMatEl && el != lastVecEl) || !operat->isOptimized())
265
266
      initElement(smallElInfo, largeElInfo);
   
Thomas Witkowski's avatar
Thomas Witkowski committed
267
    if (el != lastVecEl || !operat->isOptimized()) {
268
269
270
      if (rememberElVec)
	set_to_zero(elementVector);

Thomas Witkowski's avatar
Thomas Witkowski committed
271
272
273
      lastVecEl = el;
    } else {
      if (rememberElVec) {
274
	userVec += factor * elementVector;
Thomas Witkowski's avatar
Thomas Witkowski committed
275
276
277
	return;
      }
    }
278
    ElementVector& vec = rememberElVec ? elementVector : userVec;
Thomas Witkowski's avatar
Thomas Witkowski committed
279
280

    if (operat->uhOld && remember) {
281
      if (smallElInfo->getLevel() == largeElInfo->getLevel())
Thomas Witkowski's avatar
Thomas Witkowski committed
282
	matVecAssemble(auxElInfo, vec);
283
284
      else
	matVecAssemble(mainElInfo, auxElInfo, smallElInfo, largeElInfo, vec);      
Thomas Witkowski's avatar
Thomas Witkowski committed
285

286
      if (rememberElVec)
287
	userVec += factor * elementVector;      
288

Thomas Witkowski's avatar
Thomas Witkowski committed
289
290
291
      return;
    } 

292
293
294
295
296
297
298
299
300
301
    if (firstOrderAssemblerGrdPsi) {
      ERROR_EXIT("Not yet implemented!\n");
    }

    if (zeroOrderAssembler) {
      zeroOrderAssembler->calculateElementVector(smallElInfo, vec);
      
      if (smallElInfo != mainElInfo) {
	ElementVector tmpVec(vec);	
	ElementMatrix &m = 
302
	  smallElInfo->getSubElemCoordsMat(rowFeSpace->getBasisFcts()->getDegree());
303
304
305
306
307

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

309
    if (rememberElVec)
310
      userVec += factor * elementVector;    
Thomas Witkowski's avatar
Thomas Witkowski committed
311
312
  }

313

314
  void Assembler::matVecAssemble(const ElInfo *elInfo, ElementVector& vec)
315
316
317
  {
    FUNCNAME("Assembler::matVecAssemble()");

318
    Element *el = elInfo->getElement(); 
319
    ElementVector uhOldLoc(operat->uhOld->getFeSpace() == rowFeSpace ? nRow : nCol);
320

321
    operat->uhOld->getLocalVector(el, uhOldLoc);
322
    
323
    if (el != lastMatEl) {
324
      set_to_zero(elementMatrix);
325
      calculateElementMatrix(elInfo, elementMatrix);
326
327
    }

328
    for (int i = 0; i < nRow; i++) {
329
      double val = 0.0;
330
      for (int j = 0; j < nCol; j++)
331
	val += elementMatrix[i][j] * uhOldLoc[j];
332
      
333
      vec[i] += val;
334
    }   
Thomas Witkowski's avatar
Thomas Witkowski committed
335
336
  }

337

Thomas Witkowski's avatar
Thomas Witkowski committed
338
339
  void Assembler::matVecAssemble(const ElInfo *mainElInfo, const ElInfo *auxElInfo,
				 const ElInfo *smallElInfo, const ElInfo *largeElInfo,
340
				 ElementVector& vec)
Thomas Witkowski's avatar
Thomas Witkowski committed
341
342
343
  {
    FUNCNAME("Assembler::matVecAssemble()");

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

347
    TEST_EXIT(operat->uhOld->getFeSpace()->getMesh() == auxElInfo->getMesh())
Thomas Witkowski's avatar
Thomas Witkowski committed
348
349
350
351
352
      ("Da stimmt was nicht!\n");

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

353
    const BasisFunction *basFcts = rowFeSpace->getBasisFcts();
Thomas Witkowski's avatar
Thomas Witkowski committed
354
    int nBasFcts = basFcts->getNumber();
355
    ElementVector uhOldLoc(nBasFcts);
Thomas Witkowski's avatar
Thomas Witkowski committed
356

357
    operat->uhOld->getLocalVector(auxEl, uhOldLoc);
Thomas Witkowski's avatar
Thomas Witkowski committed
358
359

    if (mainEl != lastMatEl) {
360
      set_to_zero(elementMatrix);
361
      calculateElementMatrix(mainElInfo, auxElInfo, smallElInfo, largeElInfo, 
362
			     false, elementMatrix);    
Thomas Witkowski's avatar
Thomas Witkowski committed
363
    }
364

Thomas Witkowski's avatar
Thomas Witkowski committed
365
366
    for (int i = 0; i < nBasFcts; i++) {
      double val = 0.0;
367
      for (int j = 0; j < nBasFcts; j++)
368
 	val += elementMatrix[i][j] * uhOldLoc[j];
369
      vec[i] += val;
370
    }   
371
372
  }

373

Thomas Witkowski's avatar
Thomas Witkowski committed
374
375
376
  void Assembler::initElement(const ElInfo *smallElInfo, 
			      const ElInfo *largeElInfo,
			      Quadrature *quad)
377
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
378
    if (secondOrderAssembler) 
Thomas Witkowski's avatar
Thomas Witkowski committed
379
      secondOrderAssembler->initElement(smallElInfo, largeElInfo, quad);
Thomas Witkowski's avatar
Thomas Witkowski committed
380
    if (firstOrderAssemblerGrdPsi)
381
      firstOrderAssemblerGrdPsi->initElement(smallElInfo, largeElInfo, quad);    
Thomas Witkowski's avatar
Thomas Witkowski committed
382
    if (firstOrderAssemblerGrdPhi)
Thomas Witkowski's avatar
Thomas Witkowski committed
383
      firstOrderAssemblerGrdPhi->initElement(smallElInfo, largeElInfo, quad);
Thomas Witkowski's avatar
Thomas Witkowski committed
384
    if (zeroOrderAssembler)
Thomas Witkowski's avatar
Thomas Witkowski committed
385
      zeroOrderAssembler->initElement(smallElInfo, largeElInfo, quad);
386
387
  }

388

389
  void Assembler::checkQuadratures()
Thomas Witkowski's avatar
Thomas Witkowski committed
390
391
  { 
    if (secondOrderAssembler) {
392
      // create quadrature
Thomas Witkowski's avatar
Thomas Witkowski committed
393
      if (!secondOrderAssembler->getQuadrature()) {
394
	int dim = rowFeSpace->getMesh()->getDim();
395
396
397
398
399
	int degree = operat->getQuadratureDegree(2);
	Quadrature *quadrature = Quadrature::provideQuadrature(dim, degree);
	secondOrderAssembler->setQuadrature(quadrature);
      }
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
400
    if (firstOrderAssemblerGrdPsi) {
401
      // create quadrature
Thomas Witkowski's avatar
Thomas Witkowski committed
402
      if (!firstOrderAssemblerGrdPsi->getQuadrature()) {
403
	int dim = rowFeSpace->getMesh()->getDim();
404
405
406
407
408
	int degree = operat->getQuadratureDegree(1, GRD_PSI);
	Quadrature *quadrature = Quadrature::provideQuadrature(dim, degree);
	firstOrderAssemblerGrdPsi->setQuadrature(quadrature);
      }
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
409
    if (firstOrderAssemblerGrdPhi) {
410
      // create quadrature
Thomas Witkowski's avatar
Thomas Witkowski committed
411
      if (!firstOrderAssemblerGrdPhi->getQuadrature()) {
412
	int dim = rowFeSpace->getMesh()->getDim();
413
414
415
416
417
	int degree = operat->getQuadratureDegree(1, GRD_PHI);
	Quadrature *quadrature = Quadrature::provideQuadrature(dim, degree);
	firstOrderAssemblerGrdPhi->setQuadrature(quadrature);
      }
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
418
    if (zeroOrderAssembler) {
419
      // create quadrature
Thomas Witkowski's avatar
Thomas Witkowski committed
420
      if (!zeroOrderAssembler->getQuadrature()) {
421
	int dim = rowFeSpace->getMesh()->getDim();
422
423
424
425
426
427
428
	int degree = operat->getQuadratureDegree(0);
	Quadrature *quadrature = Quadrature::provideQuadrature(dim, degree);
	zeroOrderAssembler->setQuadrature(quadrature);
      }
    }
  }

429

Thomas Witkowski's avatar
Thomas Witkowski committed
430
431
432
433
434
  void Assembler::finishAssembling()
  {
    lastVecEl = NULL;
    lastMatEl = NULL;
  }
Thomas Witkowski's avatar
Thomas Witkowski committed
435

436

Thomas Witkowski's avatar
Thomas Witkowski committed
437
438
439
440
441
  OptimizedAssembler::OptimizedAssembler(Operator  *op,
					 Quadrature *quad2,
					 Quadrature *quad1GrdPsi,
					 Quadrature *quad1GrdPhi,
					 Quadrature *quad0,
442
443
444
					 const FiniteElemSpace *rowFeSpace,
					 const FiniteElemSpace *colFeSpace) 
    : Assembler(op, rowFeSpace, colFeSpace)
Thomas Witkowski's avatar
Thomas Witkowski committed
445
  {
446
    bool opt = (rowFeSpace->getBasisFcts() == colFeSpace->getBasisFcts());
Thomas Witkowski's avatar
Thomas Witkowski committed
447
448
449
450
451
452
453
454
455
456
457
458
459
460

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

461

Thomas Witkowski's avatar
Thomas Witkowski committed
462
463
464
465
466
  StandardAssembler::StandardAssembler(Operator *op,
				       Quadrature *quad2,
				       Quadrature *quad1GrdPsi,
				       Quadrature *quad1GrdPhi,
				       Quadrature *quad0,
467
468
469
				       const FiniteElemSpace *rowFeSpace,
				       const FiniteElemSpace *colFeSpace) 
    : Assembler(op, rowFeSpace, colFeSpace)
Thomas Witkowski's avatar
Thomas Witkowski committed
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
  {
    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();
  }

486
}