ZeroOrderAssembler.cc 9.48 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

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

    // check if a new assembler is needed
    if (quad) {
Thomas Witkowski's avatar
Thomas Witkowski committed
47
      for (int i = 0; i < static_cast<int>(subAssemblers->size()); i++) {
48
49
50
51
	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
      if (pwConst)
71
     	newAssembler = new PrecalcZOA(op, assembler, quad);
72
      else
73
     	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
    ElementVector c(nPoints, 0.0);
95
    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
      (static_cast<ZeroOrderTerm*>((*termIt)))->getC(elInfo, nPoints, c);
101

102
    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
    ElementVector 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
189
190
191
    ElementVector &c = tmpC[myRank];
    if (num_rows(c) < nPoints)
      c.change_dim(nPoints);
    c = 0.0;
192
193
194

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

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

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

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

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

227

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

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

246
    ElementVector c(nPoints, 0.0);
247
    std::vector<OperatorTerm*>::iterator termIt;
248
    for (termIt = terms[myRank].begin(); termIt != terms[myRank].end(); ++termIt)
249
250
251
252
253
254
      (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);
255
256
      for (int i = 0; i < nRow; i++)
	vec[i] += quadrature->getWeight(iq) * c[iq] * psi[i];      
257
258
259
    }
  }

260

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

267

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

283
    ElementVector c(1, 0.0);
284
285
286
    int myRank = omp_get_thread_num();
    int size = static_cast<int>(terms[myRank].size());

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

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

310

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

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

    int myRank = omp_get_thread_num();
329
    ElementVector c(1, 0.0);
330
    for (termIt = terms[myRank].begin(); termIt != terms[myRank].end(); ++termIt)
Thomas Witkowski's avatar
Thomas Witkowski committed
331
      (static_cast<ZeroOrderTerm*>(*termIt))->getC(elInfo, 1, c);
332

333
    c[0] *= elInfo->getDet();
334
335

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

}