Assembler.cc 12 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
      ElementMatrix &m =       
116
	smallElInfo->getSubElemGradCoordsMat(rowFESpace->getBasisFcts()->getDegree());
117

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

    if (firstOrderAssemblerGrdPsi) {
123
124
125
126
127
128
129
130
131
132
133
134
      if (largeElInfo == colElInfo) {
	ERROR_EXIT("123 Not yet implemented for first order assembler!\n");
      } else {
	firstOrderAssemblerGrdPsi->calculateElementMatrix(smallElInfo, mat);

	ElementMatrix &m = 
	  smallElInfo->getSubElemCoordsMat(rowFESpace->getBasisFcts()->getDegree());
	
	tmpMat = mat * trans(m);
	
	mat = tmpMat;
      }
135
136
137
    }

    if (firstOrderAssemblerGrdPhi) {
138
      ERROR_EXIT("Not yet implemented for first order assembler!\n");
139
      firstOrderAssemblerGrdPhi->calculateElementMatrix(smallElInfo, mat);
140
    }
141

142
143
    if (zeroOrderAssembler) {
      zeroOrderAssembler->calculateElementMatrix(smallElInfo, mat);
Thomas Witkowski's avatar
Thomas Witkowski committed
144
      
145
146
      ElementMatrix &m = 
	smallElInfo->getSubElemCoordsMat(rowFESpace->getBasisFcts()->getDegree());
Thomas Witkowski's avatar
Thomas Witkowski committed
147
      
Thomas Witkowski's avatar
Thomas Witkowski committed
148
      if (smallElInfo == colElInfo)
149
150
	tmpMat = m * mat;	
      else
Thomas Witkowski's avatar
Thomas Witkowski committed
151
152
153
  	tmpMat = mat * trans(m);
      
      mat = tmpMat;
154
    }
155

156
157
    if (rememberElMat && &userMat != &elementMatrix)
      userMat += factor * elementMatrix;   
158
159
  }

160

161
  void Assembler::calculateElementVector(const ElInfo *elInfo, 
162
					 ElementVector& userVec,
163
164
165
166
					 double factor)
  {
    FUNCNAME("Assembler::calculateElementVector()");

167
    if (remember && factor != 1.0)
168
169
170
171
      rememberElVec = true;

    Element *el = elInfo->getElement();

172
    if ((el != lastMatEl && el != lastVecEl) || !operat->isOptimized())
173
      initElement(elInfo);
174
    
Thomas Witkowski's avatar
Thomas Witkowski committed
175
    if (el != lastVecEl || !operat->isOptimized()) {
176
177
178
      if (rememberElVec)
	set_to_zero(elementVector);
	
179
180
      lastVecEl = el;
    } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
181
      if (rememberElVec) {
182
	userVec += factor * elementVector;
183
184
185
	return;
      }
    }
186
187

    ElementVector& vec = rememberElVec ? elementVector : userVec;
Thomas Witkowski's avatar
Thomas Witkowski committed
188
    if (operat->uhOld && remember) {
189
      matVecAssemble(elInfo, vec);
190
      if (rememberElVec)
191
	userVec += factor * elementVector;      
192

193
194
      return;
    } 
195
196

    if (firstOrderAssemblerGrdPsi)
197
      firstOrderAssemblerGrdPsi->calculateElementVector(elInfo, vec);
198
    if (zeroOrderAssembler)
199
      zeroOrderAssembler->calculateElementVector(elInfo, vec);
200
      
201
    if (rememberElVec)
202
      userVec += factor * elementVector;    
203
204
  }

205

Thomas Witkowski's avatar
Thomas Witkowski committed
206
207
208
209
  void Assembler::calculateElementVector(const ElInfo *mainElInfo, 
					 const ElInfo *auxElInfo,
					 const ElInfo *smallElInfo,
					 const ElInfo *largeElInfo,
210
					 ElementVector& userVec, 
Thomas Witkowski's avatar
Thomas Witkowski committed
211
212
213
214
					 double factor)
  {
    FUNCNAME("Assembler::calculateElementVector()");

215
    if (remember && factor != 1.0)
Thomas Witkowski's avatar
Thomas Witkowski committed
216
217
218
219
      rememberElVec = true;

    Element *el = mainElInfo->getElement();

220
    if ((el != lastMatEl && el != lastVecEl) || !operat->isOptimized())
221
222
      initElement(smallElInfo, largeElInfo);
   
Thomas Witkowski's avatar
Thomas Witkowski committed
223
    if (el != lastVecEl || !operat->isOptimized()) {
224
225
226
      if (rememberElVec)
	set_to_zero(elementVector);

Thomas Witkowski's avatar
Thomas Witkowski committed
227
228
229
      lastVecEl = el;
    } else {
      if (rememberElVec) {
230
	userVec += factor * elementVector;
Thomas Witkowski's avatar
Thomas Witkowski committed
231
232
233
	return;
      }
    }
234
    ElementVector& vec = rememberElVec ? elementVector : userVec;
Thomas Witkowski's avatar
Thomas Witkowski committed
235
236

    if (operat->uhOld && remember) {
237
      if (smallElInfo->getLevel() == largeElInfo->getLevel())
Thomas Witkowski's avatar
Thomas Witkowski committed
238
	matVecAssemble(auxElInfo, vec);
239
240
      else
	matVecAssemble(mainElInfo, auxElInfo, smallElInfo, largeElInfo, vec);      
Thomas Witkowski's avatar
Thomas Witkowski committed
241

242
      if (rememberElVec)
243
	userVec += factor * elementVector;      
244

Thomas Witkowski's avatar
Thomas Witkowski committed
245
246
247
248
249
      return;
    } 

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

250
251
252
253
254
255
256
257
#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
258
259
  }

260

261
  void Assembler::matVecAssemble(const ElInfo *elInfo, ElementVector& vec)
262
263
264
  {
    FUNCNAME("Assembler::matVecAssemble()");

265
    Element *el = elInfo->getElement(); 
266
    double *uhOldLoc = new double[nRow];
267

268
    operat->uhOld->getLocalVector(el, uhOldLoc);
269
    
270
    if (el != lastMatEl) {
271
      set_to_zero(elementMatrix);
272
      calculateElementMatrix(elInfo, elementMatrix);
273
274
    }

275
    for (int i = 0; i < nRow; i++) {
276
      double val = 0.0;
277
      for (int j = 0; j < nRow; j++)
278
	val += elementMatrix[i][j] * uhOldLoc[j];
279
      
280
      vec[i] += val;
281
    }   
282

Thomas Witkowski's avatar
Thomas Witkowski committed
283
284
285
    delete [] uhOldLoc;
  }

286

Thomas Witkowski's avatar
Thomas Witkowski committed
287
288
  void Assembler::matVecAssemble(const ElInfo *mainElInfo, const ElInfo *auxElInfo,
				 const ElInfo *smallElInfo, const ElInfo *largeElInfo,
289
				 ElementVector& vec)
Thomas Witkowski's avatar
Thomas Witkowski committed
290
291
292
293
294
295
296
297
298
299
300
301
302
303
  {
    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();
304
    std::vector<double> uhOldLoc(nBasFcts);
Thomas Witkowski's avatar
Thomas Witkowski committed
305

306
    operat->uhOld->getLocalVector(auxEl, &(uhOldLoc[0]));
Thomas Witkowski's avatar
Thomas Witkowski committed
307
308

    if (mainEl != lastMatEl) {
309
      set_to_zero(elementMatrix);
310
      calculateElementMatrix(mainElInfo, auxElInfo, smallElInfo, largeElInfo, 
311
 			     elementMatrix);    
Thomas Witkowski's avatar
Thomas Witkowski committed
312
    }
313

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

322

Thomas Witkowski's avatar
Thomas Witkowski committed
323
324
325
  void Assembler::initElement(const ElInfo *smallElInfo, 
			      const ElInfo *largeElInfo,
			      Quadrature *quad)
326
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
327
    if (secondOrderAssembler) 
Thomas Witkowski's avatar
Thomas Witkowski committed
328
      secondOrderAssembler->initElement(smallElInfo, largeElInfo, quad);
Thomas Witkowski's avatar
Thomas Witkowski committed
329
    if (firstOrderAssemblerGrdPsi)
Thomas Witkowski's avatar
Thomas Witkowski committed
330
      firstOrderAssemblerGrdPsi->initElement(smallElInfo, largeElInfo, quad);
Thomas Witkowski's avatar
Thomas Witkowski committed
331
    if (firstOrderAssemblerGrdPhi)
Thomas Witkowski's avatar
Thomas Witkowski committed
332
      firstOrderAssemblerGrdPhi->initElement(smallElInfo, largeElInfo, quad);
Thomas Witkowski's avatar
Thomas Witkowski committed
333
    if (zeroOrderAssembler)
Thomas Witkowski's avatar
Thomas Witkowski committed
334
      zeroOrderAssembler->initElement(smallElInfo, largeElInfo, quad);
335
336
  }

337

338
  void Assembler::checkQuadratures()
Thomas Witkowski's avatar
Thomas Witkowski committed
339
340
  { 
    if (secondOrderAssembler) {
341
      // create quadrature
Thomas Witkowski's avatar
Thomas Witkowski committed
342
343
      if (!secondOrderAssembler->getQuadrature()) {
	int dim = rowFESpace->getMesh()->getDim();
344
345
346
347
348
	int degree = operat->getQuadratureDegree(2);
	Quadrature *quadrature = Quadrature::provideQuadrature(dim, degree);
	secondOrderAssembler->setQuadrature(quadrature);
      }
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
349
    if (firstOrderAssemblerGrdPsi) {
350
      // create quadrature
Thomas Witkowski's avatar
Thomas Witkowski committed
351
352
      if (!firstOrderAssemblerGrdPsi->getQuadrature()) {
	int dim = rowFESpace->getMesh()->getDim();
353
354
355
356
357
	int degree = operat->getQuadratureDegree(1, GRD_PSI);
	Quadrature *quadrature = Quadrature::provideQuadrature(dim, degree);
	firstOrderAssemblerGrdPsi->setQuadrature(quadrature);
      }
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
358
    if (firstOrderAssemblerGrdPhi) {
359
      // create quadrature
Thomas Witkowski's avatar
Thomas Witkowski committed
360
361
      if (!firstOrderAssemblerGrdPhi->getQuadrature()) {
	int dim = rowFESpace->getMesh()->getDim();
362
363
364
365
366
	int degree = operat->getQuadratureDegree(1, GRD_PHI);
	Quadrature *quadrature = Quadrature::provideQuadrature(dim, degree);
	firstOrderAssemblerGrdPhi->setQuadrature(quadrature);
      }
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
367
    if (zeroOrderAssembler) {
368
      // create quadrature
Thomas Witkowski's avatar
Thomas Witkowski committed
369
370
      if (!zeroOrderAssembler->getQuadrature()) {
	int dim = rowFESpace->getMesh()->getDim();
371
372
373
374
375
376
377
	int degree = operat->getQuadratureDegree(0);
	Quadrature *quadrature = Quadrature::provideQuadrature(dim, degree);
	zeroOrderAssembler->setQuadrature(quadrature);
      }
    }
  }

378

Thomas Witkowski's avatar
Thomas Witkowski committed
379
380
381
382
383
  void Assembler::finishAssembling()
  {
    lastVecEl = NULL;
    lastMatEl = NULL;
  }
Thomas Witkowski's avatar
Thomas Witkowski committed
384

385

Thomas Witkowski's avatar
Thomas Witkowski committed
386
387
388
389
390
  OptimizedAssembler::OptimizedAssembler(Operator  *op,
					 Quadrature *quad2,
					 Quadrature *quad1GrdPsi,
					 Quadrature *quad1GrdPhi,
					 Quadrature *quad0,
391
392
393
					 const FiniteElemSpace *rowFeSpace,
					 const FiniteElemSpace *colFeSpace) 
    : Assembler(op, rowFeSpace, colFeSpace)
Thomas Witkowski's avatar
Thomas Witkowski committed
394
  {
395
    bool opt = (rowFeSpace->getBasisFcts() == colFeSpace->getBasisFcts());
Thomas Witkowski's avatar
Thomas Witkowski committed
396
397
398
399
400
401
402
403
404
405
406
407
408
409

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

410

Thomas Witkowski's avatar
Thomas Witkowski committed
411
412
413
414
415
  StandardAssembler::StandardAssembler(Operator *op,
				       Quadrature *quad2,
				       Quadrature *quad1GrdPsi,
				       Quadrature *quad1GrdPhi,
				       Quadrature *quad0,
416
417
418
				       const FiniteElemSpace *rowFeSpace,
				       const FiniteElemSpace *colFeSpace) 
    : Assembler(op, rowFeSpace, colFeSpace)
Thomas Witkowski's avatar
Thomas Witkowski committed
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
  {
    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();
  }

435
}