Assembler.cc 13.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/******************************************************************************
 *
 * AMDiS - Adaptive multidimensional simulations
 *
 * Copyright (C) 2013 Dresden University of Technology. All Rights Reserved.
 * Web: https://fusionforge.zih.tu-dresden.de/projects/amdis
 *
 * Authors: 
 * Simon Vey, Thomas Witkowski, Andreas Naumann, Simon Praetorius, et al.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 *
 * This file is part of AMDiS
 *
 * See also license.opensource.txt in the distribution.
 * 
 ******************************************************************************/
20
21


22
23
24
#include <vector>
#include <algorithm>
#include <boost/numeric/mtl/mtl.hpp>
25
26
27
28
29
30
31
32
#include "Assembler.h"
#include "Operator.h"
#include "Element.h"
#include "QPsiPhi.h"
#include "DOFVector.h"

namespace AMDiS {

Thomas Witkowski's avatar
Thomas Witkowski committed
33
34
35
  Assembler::Assembler(Operator *op,
		       const FiniteElemSpace *row,
		       const FiniteElemSpace *col) 
36
    : operat(op),
37
38
39
40
      rowFeSpace(row),
      colFeSpace(col ? col : row),
      nRow(rowFeSpace->getBasisFcts()->getNumber()),
      nCol(colFeSpace->getBasisFcts()->getNumber()),
41
42
43
      remember(true),
      rememberElMat(false),
      rememberElVec(false),
44
45
      elementMatrix(nRow, nCol),
      elementVector(nRow),
46
      tmpMat(nRow, nCol),
47
48
      lastMatEl(NULL),
      lastVecEl(NULL),
49
      lastTraverseId(-1)
50
  {}
Thomas Witkowski's avatar
Thomas Witkowski committed
51

52

Thomas Witkowski's avatar
Thomas Witkowski committed
53
  Assembler::~Assembler()
54
  {}
55

56

57
  void Assembler::calculateElementMatrix(const ElInfo *elInfo, 
58
					 ElementMatrix& userMat,
59
60
					 double factor)
  {
61
    if (remember && (factor != 1.0 || operat->uhOld))
62
      rememberElMat = true;
Thomas Witkowski's avatar
Thomas Witkowski committed
63

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

66
    if (el != lastMatEl || !operat->isOptimized()) {
67
      initElement(elInfo, elInfo);
68

69
70
71
      if (rememberElMat)
	set_to_zero(elementMatrix);

72
73
74
      lastMatEl = el;
    } else {
      if (rememberElMat) {
75
	userMat += factor * elementMatrix;
76
77
78
	return;
      }
    }
79

80
    ElementMatrix& mat = rememberElMat ? elementMatrix : userMat;
81
82
83
84
85
86
87
88
89
90

    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
91
    if (rememberElMat && &userMat != &elementMatrix)
92
      userMat += factor * elementMatrix;
93
94
  }

95

96
97
  void Assembler::calculateElementMatrix(const ElInfo *rowElInfo,
					 const ElInfo *colElInfo,
98
99
					 const ElInfo *smallElInfo,
					 const ElInfo *largeElInfo,
100
					 bool rowColFeSpaceEqual,
101
					 ElementMatrix& userMat,
102
103
					 double factor)
  {
104
    if (remember && (factor != 1.0 || operat->uhOld))
105
106
      rememberElMat = true;
  
Thomas Witkowski's avatar
Thomas Witkowski committed
107
    Element *el = smallElInfo->getElement();   
108
    lastVecEl = lastMatEl = NULL;
Thomas Witkowski's avatar
Thomas Witkowski committed
109
   
110
111
    if ((el != lastMatEl && el != lastVecEl) || !operat->isOptimized())
      initElement(smallElInfo, largeElInfo);
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
      // calculate element matrices always on smallest element
129
      secondOrderAssembler->calculateElementMatrix(smallElInfo, mat);
130

131
      // smallElInfo stores refinement-relation to largeElInfo
132
      ElementMatrix &m = 
133
	smallElInfo->getSubElemGradCoordsMat(rowFeSpace->getBasisFcts()->getDegree());  // muste be moved to next if-else block when generalized for multiple polynomial degrees
134
      
135
136
137
138
139
140
141
142
      if (!rowColFeSpaceEqual) {
	if (smallElInfo == colElInfo)
	  tmpMat = m * mat;	
	else
	  tmpMat = mat * trans(m);
	
	mat = tmpMat;
      }
143
144
145
    }

    if (firstOrderAssemblerGrdPsi) {
146
147
      firstOrderAssemblerGrdPsi->calculateElementMatrix(smallElInfo, mat);

148
149
150
151
152
153
154
155
156
157
158
159
      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);
	}
160
	
161
	mat = tmpMat;
162
      }
163
164
165
    }

    if (firstOrderAssemblerGrdPhi) {
166
      firstOrderAssemblerGrdPhi->calculateElementMatrix(smallElInfo, mat);
167

168
169
170
171
172
173
174
175
176
177
178
179
      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;	
	}
180
	
181
	mat = tmpMat;
182
      }
183
    }
184

185
186
    if (zeroOrderAssembler) {
      zeroOrderAssembler->calculateElementMatrix(smallElInfo, mat);
187

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

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

205

206
  void Assembler::calculateElementVector(const ElInfo *elInfo, 
207
					 ElementVector& userVec,
208
209
					 double factor)
  {
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
    Element *el = elInfo->getElement(); 
317
318
    ElementVector uhOldLoc(operat->uhOld->getFeSpace() == rowFeSpace ? 
			   nRow : nCol);
319
    operat->uhOld->getLocalVector(el, uhOldLoc);
320
    
321
    if (el != lastMatEl) {
322
      set_to_zero(elementMatrix);
323
      calculateElementMatrix(elInfo, elementMatrix);
324
325
    }

326
    vec += elementMatrix*uhOldLoc;
Thomas Witkowski's avatar
Thomas Witkowski committed
327
328
  }

329

Thomas Witkowski's avatar
Thomas Witkowski committed
330
331
  void Assembler::matVecAssemble(const ElInfo *mainElInfo, const ElInfo *auxElInfo,
				 const ElInfo *smallElInfo, const ElInfo *largeElInfo,
332
				 ElementVector& vec)
Thomas Witkowski's avatar
Thomas Witkowski committed
333
334
335
  {
    FUNCNAME("Assembler::matVecAssemble()");

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

339
    Element *usedEl = NULL;
340
341
342
343
344
345
346
    if (operat->uhOld->getFeSpace()->getMesh() == auxElInfo->getMesh())
      usedEl = auxElInfo->getElement(); 
    else if (operat->uhOld->getFeSpace()->getMesh() == mainElInfo->getMesh())
      usedEl = mainElInfo->getElement();
    else
      ERROR("Mesh is incorrect.\n");
    
347
    const BasisFunction *basFcts = rowFeSpace->getBasisFcts();
Thomas Witkowski's avatar
Thomas Witkowski committed
348
    int nBasFcts = basFcts->getNumber();
349
    ElementVector uhOldLoc(nBasFcts);
Thomas Witkowski's avatar
Thomas Witkowski committed
350

351
    operat->uhOld->getLocalVector(usedEl, uhOldLoc);
Thomas Witkowski's avatar
Thomas Witkowski committed
352

353
    if (mainElInfo->getElement() != lastMatEl) {
354
      set_to_zero(elementMatrix);
355
      calculateElementMatrix(mainElInfo, auxElInfo, smallElInfo, largeElInfo, 
356
			     rowFeSpace == operat->uhOld->getFeSpace(), elementMatrix);  
Thomas Witkowski's avatar
Thomas Witkowski committed
357
    }
358

359
    vec += elementMatrix * uhOldLoc;
360
361
  }

362

Thomas Witkowski's avatar
Thomas Witkowski committed
363
364
365
  void Assembler::initElement(const ElInfo *smallElInfo, 
			      const ElInfo *largeElInfo,
			      Quadrature *quad)
366
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
367
    if (secondOrderAssembler) 
Thomas Witkowski's avatar
Thomas Witkowski committed
368
      secondOrderAssembler->initElement(smallElInfo, largeElInfo, quad);
Thomas Witkowski's avatar
Thomas Witkowski committed
369
    if (firstOrderAssemblerGrdPsi)
370
      firstOrderAssemblerGrdPsi->initElement(smallElInfo, largeElInfo, quad);    
Thomas Witkowski's avatar
Thomas Witkowski committed
371
    if (firstOrderAssemblerGrdPhi)
Thomas Witkowski's avatar
Thomas Witkowski committed
372
      firstOrderAssemblerGrdPhi->initElement(smallElInfo, largeElInfo, quad);
Thomas Witkowski's avatar
Thomas Witkowski committed
373
    if (zeroOrderAssembler)
Thomas Witkowski's avatar
Thomas Witkowski committed
374
      zeroOrderAssembler->initElement(smallElInfo, largeElInfo, quad);
375
376
  }

377

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

418

Thomas Witkowski's avatar
Thomas Witkowski committed
419
420
  void Assembler::finishAssembling()
  {
421
422
    lastVecEl = NULL;
    lastMatEl = NULL;
Thomas Witkowski's avatar
Thomas Witkowski committed
423
  }
Thomas Witkowski's avatar
Thomas Witkowski committed
424

425

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

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

450

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

475
}