ZeroOrderAssembler.cc 13.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <vector>
#include "Assembler.h"
#include "ZeroOrderAssembler.h"
#include "Operator.h"
#include "QPsiPhi.h"
#include "FiniteElemSpace.h"
#include "Quadrature.h"
#include "DOFVector.h"
#include "ElementMatrix.h"
#include "OpenMP.h"

namespace AMDiS {

  std::vector<SubAssembler*> ZeroOrderAssembler::optimizedSubAssemblers;
  std::vector<SubAssembler*> ZeroOrderAssembler::standardSubAssemblers;

  ZeroOrderAssembler::ZeroOrderAssembler(Operator *op,
					 Assembler *assembler,
					 Quadrature *quad,
					 bool optimized)
    : SubAssembler(op, assembler, quad, 0, optimized)
  {}

  ZeroOrderAssembler* ZeroOrderAssembler::getSubAssembler(Operator* op,
							  Assembler *assembler,
							  Quadrature *quad,
							  bool optimized)
  {
    // check if a assembler is needed at all
    if (op->zeroOrder.size() == 0) {
      return NULL;
    }

    ZeroOrderAssembler *newAssembler;

    std::vector<SubAssembler*> *subAssemblers =
37
      optimized ? &optimizedSubAssemblers : &standardSubAssemblers;
38
39

    int myRank = omp_get_thread_num();
40
    std::vector<OperatorTerm*> opTerms = op->zeroOrder[myRank];
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149

    sort(opTerms.begin(), opTerms.end());

    // check if a new assembler is needed
    if (quad) {
      for (int i = 0; i < static_cast<int>( subAssemblers->size()); i++) {
	std::vector<OperatorTerm*> assTerms = *((*subAssemblers)[i]->getTerms());

	sort(assTerms.begin(), assTerms.end());

	if ((opTerms == assTerms) && 
	    ((*subAssemblers)[i]->getQuadrature() == quad)) {
	
	  return dynamic_cast<ZeroOrderAssembler*>((*subAssemblers)[i]);
	}
      }
    }
 
    // check if all terms are pw_const
    bool pwConst = true;
    for (int i = 0; i < static_cast<int>( op->zeroOrder[myRank].size()); i++) {
      if (!op->zeroOrder[myRank][i]->isPWConst()) {
	pwConst = false;
	break;
      }
    }  

    // create new assembler
    if (!optimized) {
      newAssembler = NEW StandardZOA(op, assembler, quad);
    } else {
      if (pwConst) {
	newAssembler = NEW PrecalcZOA(op, assembler, quad);
      } else {
	newAssembler = NEW FastQuadZOA(op, assembler, quad);
      }
    }

    subAssemblers->push_back(newAssembler);
    return newAssembler;
  }

  StandardZOA::StandardZOA(Operator *op, Assembler *assembler, Quadrature *quad)
    : ZeroOrderAssembler(op, assembler, quad, false)
  {
  }

  void StandardZOA::calculateElementMatrix(const ElInfo *elInfo, ElementMatrix *mat)
  {
    const BasisFunction *psi = owner->getRowFESpace()->getBasisFcts();
    const BasisFunction *phi = owner->getColFESpace()->getBasisFcts();

    double *phival = GET_MEMORY(double, nCol);
    int nPoints = quadrature->getNumPoints();
    double *c = GET_MEMORY(double, nPoints);

    for (int iq = 0; iq < nPoints; iq++) {
      c[iq] = 0.0;
    }

    int myRank = omp_get_thread_num();
    std::vector<OperatorTerm*>::iterator termIt;
    for (termIt = terms[myRank].begin(); termIt != terms[myRank].end(); ++termIt) {
      (static_cast<ZeroOrderTerm*>((*termIt)))->getC(elInfo, nPoints, c);
    }
      
    if (symmetric) {
      for (int iq = 0; iq < nPoints; iq++) {
	c[iq] *= elInfo->getDet();

	// calculate phi at QPs only once!
	for (int i = 0; i < nCol; i++) {
	  phival[i] = (*(phi->getPhi(i)))(quadrature->getLambda(iq));
	}

	for (int i = 0; i < nRow; i++) {
	  double psival = (*(psi->getPhi(i)))(quadrature->getLambda(iq));
	  (*mat)[i][i] += quadrature->getWeight(iq) * c[iq] * psival * phival[i];
	  for (int j = i + 1; j < nCol; j++) {
	    double val = quadrature->getWeight(iq) * c[iq] * psival * phival[j];
	    (*mat)[i][j] += val;
	    (*mat)[j][i] += val;
	  }
	}
      }
    } else {      //  non symmetric assembling 
      for (int iq = 0; iq < nPoints; iq++) {
	c[iq] *= elInfo->getDet();

	// calculate phi at QPs only once!
	for (int i = 0; i < nCol; i++) {
	  phival[i] = (*(phi->getPhi(i)))(quadrature->getLambda(iq));
	}

	for (int i = 0; i < nRow; i++) {
	  double psival = (*(psi->getPhi(i)))(quadrature->getLambda(iq));
	  for (int j = 0; j < nCol; j++) {
	    (*mat)[i][j] += quadrature->getWeight(iq) * c[iq] * psival * phival[j];
	  }
	}
      }
    }

    FREE_MEMORY(phival, double, nCol);
    FREE_MEMORY(c, double, nPoints);
  }

  void StandardZOA::calculateElementMatrix(const ElInfo *rowElInfo,
					   const ElInfo *colElInfo,
150
151
					   const ElInfo *smallElInfo,
					   const ElInfo *largeElInfo,
152
153
					   ElementMatrix *mat) 
  {
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
    FUNCNAME("StandardZOA::calculateElementMatrix()");

    TEST_EXIT((nRow <= 3) && (nCol <= 3))("not yet!\n");

    const BasisFunction *psi = owner->getRowFESpace()->getBasisFcts();
    const BasisFunction *phi = owner->getColFESpace()->getBasisFcts();
    DimMat<double> *m = smallElInfo->getSubElemCoordsMat();
    //    m->print();
    //    WAIT_REALLY;

    // The basis function on the larger element is defined as a linear
    // combination of basis functions of the smaller element. At the moment,
    // this is supported only for linear lagrange basis functions.
    //
    // If b0 and b1 are the basis functions of the larger element restricted to 
    // the small element, and s0 and s1 are the two basis function of the small 
    // element, b0 and b1 are expressed as follows:
    //
    // b0 = c00 * s0 + c01 * s1;
    // b1 = c10 * s0 + c11 * s1;
    //
    // The constants are defined by the subElement Matrix.

177

Thomas Witkowski's avatar
Thomas Witkowski committed
178
    int myRank = omp_get_thread_num();
179
180
    int nPoints = quadrature->getNumPoints();
    double *c = GET_MEMORY(double, nPoints);
181
182
183
184
    for (int iq = 0; iq < nPoints; iq++) {
      c[iq] = 0.0;
    }

185
186
187
188
    std::vector<OperatorTerm*>::iterator termIt;
    for (termIt = terms[myRank].begin(); termIt != terms[myRank].end(); ++termIt) {
      (static_cast<ZeroOrderTerm*>((*termIt)))->getC(rowElInfo, nPoints, c);
    }
189
    
190
    for (int iq = 0; iq < nPoints; iq++) {
191
      c[iq] *= smallElInfo->getDet();
192

193
194
195
      for (int i = 0; i < nCol; i++) { 
	for (int j = 0; j < nRow; j++) { 
	  double val = quadrature->getWeight(iq) * c[iq] * (*(phi->getPhi(i)))(quadrature->getLambda(iq));
196

197
198
199
	  double tmpval = 0.0;
	  for (int k = 0; k < nCol; k++) {
	    tmpval += (*m)[j][k] * (*(phi->getPhi(k)))(quadrature->getLambda(iq));
200
	  }
201
202
203
	  val *= tmpval;

	  (*mat)[j][i] += val;
204
205
	}
      }
206
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
207
208

    FREE_MEMORY(c, double, nPoints);
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
  }

  void StandardZOA::calculateElementVector(const ElInfo *elInfo, ElementVector *vec)
  {
    int nPoints = quadrature->getNumPoints();

    double *c = GET_MEMORY(double, nPoints);

    for (int iq = 0; iq < nPoints; iq++) {
      c[iq] = 0.0;
    }

    int myRank = omp_get_thread_num();
    std::vector<OperatorTerm*>::iterator termIt;
    for (termIt = terms[myRank].begin(); termIt != terms[myRank].end(); ++termIt) {
      (static_cast<ZeroOrderTerm*>((*termIt)))->getC(elInfo, nPoints, c);
    }

    for (int iq = 0; iq < nPoints; iq++) {
      c[iq] *= elInfo->getDet();

      for (int i = 0; i < nRow; i++) {
	double psi = (*(owner->getRowFESpace()->getBasisFcts()->getPhi(i)))
	  (quadrature->getLambda(iq));
	(*vec)[i] += quadrature->getWeight(iq) * c[iq] * psi;
      }
    }
    
    FREE_MEMORY(c, double, nPoints);
  }

  FastQuadZOA::FastQuadZOA(Operator *op, Assembler *assembler, Quadrature *quad)
    : ZeroOrderAssembler(op, assembler, quad, true)
  {
243
    cPtrs.resize(omp_get_overall_max_threads());
244
245
246
247
  }

  FastQuadZOA::~FastQuadZOA()
  {
248
    for (int i = 0; i < omp_get_overall_max_threads(); i++) {
249
250
251
252
253
254
255
256
257
258
      FREE_MEMORY(cPtrs[i], double, quadrature->getNumPoints());
    }
  }

  void FastQuadZOA::calculateElementMatrix(const ElInfo *elInfo, ElementMatrix *mat)
  {
    int nPoints = quadrature->getNumPoints();
    int myRank = omp_get_thread_num();

    if (firstCall) {
259
260
261
262
263
264
265
266
267
268
269
#ifdef _OPENMP
#pragma omp critical
#endif 
      {
	cPtrs[myRank] = GET_MEMORY(double, nPoints);
	const BasisFunction *basFcts = owner->getRowFESpace()->getBasisFcts();
	psiFast = updateFastQuadrature(psiFast, basFcts, INIT_PHI);
	basFcts = owner->getColFESpace()->getBasisFcts();
	phiFast = updateFastQuadrature(phiFast, basFcts, INIT_PHI);
	firstCall = false;
      }
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
    }

    double *c = cPtrs[myRank];
    for (int iq = 0; iq < nPoints; iq++) {
      c[iq] = 0.0;
    }

    std::vector<OperatorTerm*>::iterator termIt;
    for (termIt = terms[myRank].begin(); termIt != terms[myRank].end(); ++termIt) {
      (static_cast<ZeroOrderTerm*>((*termIt)))->getC(elInfo, nPoints, c);
    }

    if (symmetric) {
      for (int iq = 0; iq < nPoints; iq++) {
	c[iq] *= elInfo->getDet();

	const double *psi = psiFast->getPhi(iq);
	const double *phi = phiFast->getPhi(iq);
	for (int i = 0; i < nRow; i++) {
	  (*mat)[i][i] += quadrature->getWeight(iq) * c[iq] * psi[i] * phi[i];
	  for (int j = i + 1; j < nCol; j++) {
	    double val = quadrature->getWeight(iq) * c[iq] * psi[i] * phi[j];
	    (*mat)[i][j] += val;
	    (*mat)[j][i] += val;
	  }
	}
      }
    } else {      /*  non symmetric assembling   */
      for (int iq = 0; iq < nPoints; iq++) {
	c[iq] *= elInfo->getDet();

	const double *psi = psiFast->getPhi(iq);
	const double *phi = phiFast->getPhi(iq);
	for (int i = 0; i < nRow; i++) {
	  for (int j = 0; j < nCol; j++) {
	    (*mat)[i][j] += quadrature->getWeight(iq) * c[iq] * psi[i] * phi[j];
	  }
	}
      }
    }
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
  void FastQuadZOA::calculateElementMatrix(const ElInfo *rowElInfo,
					   const ElInfo *colElInfo,
					   const ElInfo *smallElInfo,
					   const ElInfo *largeElInfo,
					   ElementMatrix *mat) 
  {
    int nPoints = quadrature->getNumPoints();
    int myRank = omp_get_thread_num();
    DimMat<double> *m = smallElInfo->getSubElemCoordsMat();

    if (firstCall) {
#ifdef _OPENMP
#pragma omp critical
#endif 
      {
	cPtrs[myRank] = GET_MEMORY(double, nPoints);
	const BasisFunction *basFcts = owner->getRowFESpace()->getBasisFcts();
	psiFast = updateFastQuadrature(psiFast, basFcts, INIT_PHI);
	basFcts = owner->getColFESpace()->getBasisFcts();
	phiFast = updateFastQuadrature(phiFast, basFcts, INIT_PHI);
	firstCall = false;
      }
    }

    double *c = cPtrs[myRank];
    for (int iq = 0; iq < nPoints; iq++) {
      c[iq] = 0.0;
    }

    std::vector<OperatorTerm*>::iterator termIt;
    for (termIt = terms[myRank].begin(); termIt != terms[myRank].end(); ++termIt) {
      (static_cast<ZeroOrderTerm*>((*termIt)))->getC(rowElInfo, nPoints, c);
    }

    for (int iq = 0; iq < nPoints; iq++) {
      c[iq] *= smallElInfo->getDet();
      
      const double *psi = psiFast->getPhi(iq);
      const double *phi = phiFast->getPhi(iq);

      //      for (int i = 0; i < nRow; i++) {
      //	for (int j = 0; j < nCol; j++) {
      //	  (*mat)[i][j] += quadrature->getWeight(iq) * c[iq] * psi[i] * phi[j];
      //	}
      //      }

      for (int i = 0; i < nCol; i++) { 
	for (int j = 0; j < nRow; j++) { 
	  double val = quadrature->getWeight(iq) * c[iq] * psi[i];

	  double tmpval = 0.0;
	  for (int k = 0; k < nCol; k++) {
	    tmpval += (*m)[j][k] * phi[k];
	  }
	  val *= tmpval;

	  (*mat)[j][i] += val;
	}
      }

    }    
  }

375
376
377
  void FastQuadZOA::calculateElementVector(const ElInfo *elInfo, ElementVector *vec)
  {
    if (firstCall) {
378
379
380
381
382
383
384
385
386
387
#ifdef _OPENMP
#pragma omp critical
#endif 
      {
	const BasisFunction *basFcts = owner->getRowFESpace()->getBasisFcts();
	psiFast = updateFastQuadrature(psiFast, basFcts, INIT_PHI);
	basFcts = owner->getColFESpace()->getBasisFcts();
	phiFast = updateFastQuadrature(phiFast, basFcts, INIT_PHI);
	firstCall = false;
      }
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
    }

    int nPoints = quadrature->getNumPoints();
    double *c = GET_MEMORY(double, nPoints);

    for (int iq = 0; iq < nPoints; iq++) {
      c[iq] = 0.0;
    }

    int myRank = omp_get_thread_num();
    std::vector<OperatorTerm*>::iterator termIt;
    for (termIt = terms[myRank].begin(); termIt != terms[myRank].end(); ++termIt) {
      (static_cast<ZeroOrderTerm*>((*termIt)))->getC(elInfo, nPoints, c);
    }

    for (int iq = 0; iq < nPoints; iq++) {
      c[iq] *= elInfo->getDet();

      const double *psi = psiFast->getPhi(iq);
      for (int i = 0; i < nRow; i++) {
	(*vec)[i] += quadrature->getWeight(iq) * c[iq] * psi[i];
      }
    }
    FREE_MEMORY(c, double, nPoints);
  }

  PrecalcZOA::PrecalcZOA(Operator *op, Assembler *assembler, Quadrature *quad) 
    : ZeroOrderAssembler(op, assembler, quad, true)
  {
  }

  void PrecalcZOA::calculateElementMatrix(const ElInfo *elInfo, ElementMatrix *mat)
  {
    if (firstCall) {
422
423
424
425
426
427
428
429
430
431
432
#ifdef _OPENMP
#pragma omp critical
#endif 
      {
	q00 = Q00PsiPhi::provideQ00PsiPhi(owner->getRowFESpace()->getBasisFcts(), 
					  owner->getColFESpace()->getBasisFcts(), 
					  quadrature);
	q0 = Q0Psi::provideQ0Psi(owner->getRowFESpace()->getBasisFcts(),
				 quadrature);
	firstCall = false;
      }
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
    }

    double c = 0.0;
    int myRank = omp_get_thread_num();
    int size = static_cast<int>(terms[myRank].size());

    for (int i = 0; i < size; i++) {
      (static_cast<ZeroOrderTerm*>((terms[myRank][i])))->getC(elInfo, 1, &c);
    }

    c *= elInfo->getDet();

    if (symmetric) {
      for (int i = 0; i < nRow; i++) {
	(*mat)[i][i] += c * q00->getValue(i,i);
	for (int j = i + 1; j < nCol; j++) {
	  double val = c * q00->getValue(i, j);
	  (*mat)[i][j] += val;
	  (*mat)[j][i] += val;
	}
      }
    } else {
      for (int i = 0; i < nRow; i++)
	for (int j = 0; j < nCol; j++)
	  (*mat)[i][j] += c * q00->getValue(i, j);
    }
  }

  void PrecalcZOA::calculateElementVector(const ElInfo *elInfo, ElementVector *vec)
  {
    if (firstCall) {
464
465
466
467
468
469
470
471
472
473
474
#ifdef _OPENMP
#pragma omp critical
#endif 
      {
	q00 = Q00PsiPhi::provideQ00PsiPhi(owner->getRowFESpace()->getBasisFcts(), 
					  owner->getColFESpace()->getBasisFcts(), 
					  quadrature);
	q0 = Q0Psi::provideQ0Psi(owner->getRowFESpace()->getBasisFcts(),
				 quadrature);	
	firstCall = false;
      }
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
    }

    std::vector<OperatorTerm*>::iterator termIt;

    int myRank = omp_get_thread_num();
    double c = 0.0;
    for (termIt = terms[myRank].begin(); termIt != terms[myRank].end(); ++termIt) {
      (static_cast<ZeroOrderTerm*>( *termIt))->getC(elInfo, 1, &c);
    }

    c *= elInfo->getDet();

    for (int i = 0; i < nRow; i++)
      (*vec)[i] += c * q0->getValue(i);
  }

}