ZeroOrderAssembler.cc 9.58 KB
Newer Older
1
#include <vector>
2
#include <boost/numeric/mtl/mtl.hpp>
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "Assembler.h"
#include "ZeroOrderAssembler.h"
#include "Operator.h"
#include "QPsiPhi.h"
#include "FiniteElemSpace.h"
#include "Quadrature.h"
#include "DOFVector.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)
  {}

24

25
26
27
28
29
  ZeroOrderAssembler* ZeroOrderAssembler::getSubAssembler(Operator* op,
							  Assembler *assembler,
							  Quadrature *quad,
							  bool optimized)
  {
30
31
    int myRank = omp_get_thread_num();

32
33
    // check if an assembler is needed at all
    if (op->zeroOrder[myRank].size() == 0)
34
      return NULL;   
35
36
37
38

    ZeroOrderAssembler *newAssembler;

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

41
    std::vector<OperatorTerm*> opTerms = op->zeroOrder[myRank];
42
43
44
45
46
47
48
49
50
51

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

52
	if (opTerms == assTerms && (*subAssemblers)[i]->getQuadrature() == quad)
53
54
55
56
57
58
	  return dynamic_cast<ZeroOrderAssembler*>((*subAssemblers)[i]);
      }
    }
 
    // check if all terms are pw_const
    bool pwConst = true;
59
    for (int i = 0; i < static_cast<int>(op->zeroOrder[myRank].size()); i++) {
60
61
62
63
64
65
66
      if (!op->zeroOrder[myRank][i]->isPWConst()) {
	pwConst = false;
	break;
      }
    }  

    // create new assembler
67
    if (!optimized) {
Thomas Witkowski's avatar
Thomas Witkowski committed
68
      newAssembler = new StandardZOA(op, assembler, quad);
69
    } else {
70
71
72
73
      if (pwConst)
 	newAssembler = new PrecalcZOA(op, assembler, quad);
      else
 	newAssembler = new FastQuadZOA(op, assembler, quad);      
74
    }
75
76
77
78
79

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

80

81
  StandardZOA::StandardZOA(Operator *op, Assembler *assembler, Quadrature *quad)
82
83
84
85
86
    : ZeroOrderAssembler(op, assembler, quad, false)      
  {
    name = "standard zero order assembler";
  }

87

88
  void StandardZOA::calculateElementMatrix(const ElInfo *elInfo, ElementMatrix& mat)
89
  {
90
91
    const BasisFunction *psi = rowFeSpace->getBasisFcts();
    const BasisFunction *phi = colFeSpace->getBasisFcts();
92
93

    int nPoints = quadrature->getNumPoints();
94
95
    std::vector<double> c(nPoints, 0.0);
    std::vector<double> phival(nCol);
96
97
98

    int myRank = omp_get_thread_num();
    std::vector<OperatorTerm*>::iterator termIt;
99
    for (termIt = terms[myRank].begin(); termIt != terms[myRank].end(); ++termIt)
100
101
102
      (static_cast<ZeroOrderTerm*>((*termIt)))->getC(elInfo, nPoints, c);
      
    if (symmetric) {
103
104
      TEST_EXIT_DBG(nCol == nRow)("nCol != nRow, but symmetric assembling!\n");
            
105
106
107
108
      for (int iq = 0; iq < nPoints; iq++) {
	c[iq] *= elInfo->getDet();

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

	for (int i = 0; i < nRow; i++) {
	  double psival = (*(psi->getPhi(i)))(quadrature->getLambda(iq));
114
	  mat[i][i] += quadrature->getWeight(iq) * c[iq] * psival * phival[i];
115
116
	  for (int j = i + 1; j < nCol; j++) {
	    double val = quadrature->getWeight(iq) * c[iq] * psival * phival[j];
117
118
	    mat[i][j] += val;
	    mat[j][i] += val;
119
120
121
122
123
124
125
126
	  }
	}
      }
    } else {      //  non symmetric assembling 
      for (int iq = 0; iq < nPoints; iq++) {
	c[iq] *= elInfo->getDet();

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

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

139

140
  void StandardZOA::calculateElementVector(const ElInfo *elInfo, ElementVector& vec)
141
142
  {
    int nPoints = quadrature->getNumPoints();
143
    std::vector<double> c(nPoints, 0.0);
144
    int myRank = omp_get_thread_num();
145

146
    std::vector<OperatorTerm*>::iterator termIt;
147
    for (termIt = terms[myRank].begin(); termIt != terms[myRank].end(); ++termIt)
148
149
150
151
152
153
      (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++) {
154
	double psi = (*(rowFeSpace->getBasisFcts()->getPhi(i)))
155
	  (quadrature->getLambda(iq));
156
	vec[i] += quadrature->getWeight(iq) * c[iq] * psi;
157
158
159
160
      }
    }
  }

161

162
163
  FastQuadZOA::FastQuadZOA(Operator *op, Assembler *assembler, Quadrature *quad)
    : ZeroOrderAssembler(op, assembler, quad, true)
164
  {
165
    name = "fast quadrature zero order assembler";
166
167
    tmpC.resize(omp_get_overall_max_threads());
  }
168

169

170
  void FastQuadZOA::calculateElementMatrix(const ElInfo *elInfo, ElementMatrix& mat)
171
172
173
174
175
  {
    int nPoints = quadrature->getNumPoints();
    int myRank = omp_get_thread_num();

    if (firstCall) {
176
177
178
179
#ifdef _OPENMP
#pragma omp critical
#endif 
      {
180
	const BasisFunction *basFcts = rowFeSpace->getBasisFcts();
181
	psiFast = updateFastQuadrature(psiFast, basFcts, INIT_PHI);
182
	basFcts = colFeSpace->getBasisFcts();
183
184
185
	phiFast = updateFastQuadrature(phiFast, basFcts, INIT_PHI);
	firstCall = false;
      }
186
187
    }

188
    std::vector<double> &c = tmpC[myRank];
Thomas Witkowski's avatar
Thomas Witkowski committed
189
    if (static_cast<int>(c.size()) < nPoints)
190
      c.resize(nPoints);
Thomas Witkowski's avatar
Thomas Witkowski committed
191
    for (int i = 0; i < nPoints; i++)
192
193
194
195
      c[i] = 0.0;

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

    if (symmetric) {
199
200
      TEST_EXIT_DBG(nCol == nRow)("nCol != nRow, but symmetric assembling!\n");

201
      for (int iq = 0; iq < nPoints; iq++) {
202
	c[iq] *= elInfo->getDet() * quadrature->getWeight(iq);
203
204
	const double *psi = psiFast->getPhi(iq);
	const double *phi = phiFast->getPhi(iq);
205

206
	for (int i = 0; i < nRow; i++) {
207
	  mat[i][i] += c[iq] * psi[i] * phi[i];
208
	  for (int j = i + 1; j < nCol; j++) {
209
	    double val = c[iq] * psi[i] * phi[j];
210
211
	    mat[i][j] += val;
	    mat[j][i] += val;
212
213
214
215
216
	  }
	}
      }
    } else {      /*  non symmetric assembling   */
      for (int iq = 0; iq < nPoints; iq++) {
217
	c[iq] *= elInfo->getDet() * quadrature->getWeight(iq);
218
219
220

	const double *psi = psiFast->getPhi(iq);
	const double *phi = phiFast->getPhi(iq);
221
222
223
	for (int i = 0; i < nRow; i++)
	  for (int j = 0; j < nCol; j++)
	    mat[i][j] += c[iq] * psi[i] * phi[j];
224
225
226
      }
    }
  }
Thomas Witkowski's avatar
Thomas Witkowski committed
227

228

229
  void FastQuadZOA::calculateElementVector(const ElInfo *elInfo, ElementVector& vec)
230
  {
231
232
233
    int myRank = omp_get_thread_num();
    int nPoints = quadrature->getNumPoints();

234
    if (firstCall) {
235
236
237
238
#ifdef _OPENMP
#pragma omp critical
#endif 
      {
239
	const BasisFunction *basFcts = rowFeSpace->getBasisFcts();
240
	psiFast = updateFastQuadrature(psiFast, basFcts, INIT_PHI);
241
	basFcts = colFeSpace->getBasisFcts();
242
243
244
	phiFast = updateFastQuadrature(phiFast, basFcts, INIT_PHI);
	firstCall = false;
      }
245
246
    }

247
    std::vector<double> c(nPoints, 0.0);
248
249
250
251
252
253
254
255
256
257
    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++) {
258
	vec[i] += quadrature->getWeight(iq) * c[iq] * psi[i];
259
260
261
262
      }
    }
  }

263

264
265
266
  PrecalcZOA::PrecalcZOA(Operator *op, Assembler *assembler, Quadrature *quad) 
    : ZeroOrderAssembler(op, assembler, quad, true)
  {
267
    name = "precalculated zero order assembler";
268
269
  }

270

271
  void PrecalcZOA::calculateElementMatrix(const ElInfo *elInfo, ElementMatrix& mat)
272
273
  {
    if (firstCall) {
274
275
276
277
#ifdef _OPENMP
#pragma omp critical
#endif 
      {
278
279
	q00 = Q00PsiPhi::provideQ00PsiPhi(rowFeSpace->getBasisFcts(), 
					  colFeSpace->getBasisFcts(), 
280
					  quadrature);
281
	q0 = Q0Psi::provideQ0Psi(rowFeSpace->getBasisFcts(), quadrature);
282
283
	firstCall = false;
      }
284
285
    }

286
    std::vector<double> c(1, 0.0);
287
288
289
    int myRank = omp_get_thread_num();
    int size = static_cast<int>(terms[myRank].size());

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

293
    c[0] *= elInfo->getDet();
294
    
295
296
    if (symmetric) {
      for (int i = 0; i < nRow; i++) {
297
	mat[i][i] += c[0] * q00->getValue(i,i);
298
	for (int j = i + 1; j < nCol; j++) {
299
	  double val = c[0] * q00->getValue(i, j);
300
301
	  mat[i][j] += val;
	  mat[j][i] += val;
302
303
304
	}
      }
    } else {
305
      for (int i = 0; i < nRow; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
306
	for (int j = 0; j < nCol; j++) {
307
	  mat[i][j] += c[0] * q00->getValue(i, j);
Thomas Witkowski's avatar
Thomas Witkowski committed
308
	}
309
      }
310
311
312
    }
  }

313

314
  void PrecalcZOA::calculateElementVector(const ElInfo *elInfo, ElementVector& vec)
315
316
  {
    if (firstCall) {
317
318
319
320
#ifdef _OPENMP
#pragma omp critical
#endif 
      {
321
322
	q00 = Q00PsiPhi::provideQ00PsiPhi(rowFeSpace->getBasisFcts(), 
					  colFeSpace->getBasisFcts(), 
323
					  quadrature);
324
	q0 = Q0Psi::provideQ0Psi(rowFeSpace->getBasisFcts(), quadrature);	
325
326
	firstCall = false;
      }
327
328
329
330
331
    }

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

    int myRank = omp_get_thread_num();
332
    std::vector<double> c(1, 0.0);
333
    for (termIt = terms[myRank].begin(); termIt != terms[myRank].end(); ++termIt)
334
      (static_cast<ZeroOrderTerm*>( *termIt))->getC(elInfo, 1, c);
335

336
    c[0] *= elInfo->getDet();
337
338

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

}