ZeroOrderAssembler.cc 9.59 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
24
25
26
27
28
#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)
  {}

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

31
    // check if a assembler is needed at all
32
    if (op->zeroOrder[myRank].size() == 0) {
33
      return NULL;   
34
    }
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
80
81

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

  StandardZOA::StandardZOA(Operator *op, Assembler *assembler, Quadrature *quad)
    : ZeroOrderAssembler(op, assembler, quad, false)
Thomas Witkowski's avatar
Thomas Witkowski committed
82
  {}
83

84
  void StandardZOA::calculateElementMatrix(const ElInfo *elInfo, ElementMatrix& mat)
85
86
87
88
89
  {
    const BasisFunction *psi = owner->getRowFESpace()->getBasisFcts();
    const BasisFunction *phi = owner->getColFESpace()->getBasisFcts();

    int nPoints = quadrature->getNumPoints();
90
91
    std::vector<double> c(nPoints, 0.0);
    std::vector<double> phival(nCol);
92
93
94

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

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

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

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

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

135
  void StandardZOA::calculateElementVector(const ElInfo *elInfo, ElementVector& vec)
136
137
  {
    int nPoints = quadrature->getNumPoints();
138
    std::vector<double> c(nPoints, 0.0);
139
    int myRank = omp_get_thread_num();
140

141
    std::vector<OperatorTerm*>::iterator termIt;
142
    for (termIt = terms[myRank].begin(); termIt != terms[myRank].end(); ++termIt)
143
144
145
146
147
148
149
150
      (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));
151
	vec[i] += quadrature->getWeight(iq) * c[iq] * psi;
152
153
154
155
156
157
      }
    }
  }

  FastQuadZOA::FastQuadZOA(Operator *op, Assembler *assembler, Quadrature *quad)
    : ZeroOrderAssembler(op, assembler, quad, true)
158
159
160
  {
    tmpC.resize(omp_get_overall_max_threads());
  }
161

162
  void FastQuadZOA::calculateElementMatrix(const ElInfo *elInfo, ElementMatrix& mat)
163
164
165
166
167
  {
    int nPoints = quadrature->getNumPoints();
    int myRank = omp_get_thread_num();

    if (firstCall) {
168
169
170
171
172
173
174
175
176
177
#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;
      }
178
179
    }

180
    std::vector<double> &c = tmpC[myRank];
Thomas Witkowski's avatar
Thomas Witkowski committed
181
    if (static_cast<int>(c.size()) < nPoints)
182
      c.resize(nPoints);
Thomas Witkowski's avatar
Thomas Witkowski committed
183
    for (int i = 0; i < nPoints; i++)
184
185
186
187
      c[i] = 0.0;

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

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

193
      for (int iq = 0; iq < nPoints; iq++) {
194
	c[iq] *= elInfo->getDet() * quadrature->getWeight(iq);
195
196
	const double *psi = psiFast->getPhi(iq);
	const double *phi = phiFast->getPhi(iq);
197

198
	for (int i = 0; i < nRow; i++) {
199
	  mat[i][i] += c[iq] * psi[i] * phi[i];
200
	  for (int j = i + 1; j < nCol; j++) {
201
	    double val = c[iq] * psi[i] * phi[j];
202
203
	    mat[i][j] += val;
	    mat[j][i] += val;
204
205
206
207
208
	  }
	}
      }
    } else {      /*  non symmetric assembling   */
      for (int iq = 0; iq < nPoints; iq++) {
209
	c[iq] *= elInfo->getDet() * quadrature->getWeight(iq);
210
211
212

	const double *psi = psiFast->getPhi(iq);
	const double *phi = phiFast->getPhi(iq);
213
214
215
	for (int i = 0; i < nRow; i++)
	  for (int j = 0; j < nCol; j++)
	    mat[i][j] += c[iq] * psi[i] * phi[j];
216
217
218
      }
    }
  }
Thomas Witkowski's avatar
Thomas Witkowski committed
219

220
  void FastQuadZOA::calculateElementVector(const ElInfo *elInfo, ElementVector& vec)
221
  {
222
223
224
    int myRank = omp_get_thread_num();
    int nPoints = quadrature->getNumPoints();

225
    if (firstCall) {
226
227
228
229
230
231
232
233
234
235
#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;
      }
236
237
    }

238
    std::vector<double> c(nPoints, 0.0);
239
240
241
242
243
244
245
246
247
248
    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++) {
249
	vec[i] += quadrature->getWeight(iq) * c[iq] * psi[i];
250
251
252
253
254
255
256
257
258
      }
    }
  }

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

259
  void PrecalcZOA::calculateElementMatrix(const ElInfo *elInfo, ElementMatrix& mat)
260
261
  {
    if (firstCall) {
262
263
264
265
266
267
268
269
270
271
272
#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;
      }
273
274
    }

275
    std::vector<double> c(1, 0.0);
276
277
278
279
    int myRank = omp_get_thread_num();
    int size = static_cast<int>(terms[myRank].size());

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

283
    c[0] *= elInfo->getDet();
284
    
285
286
    if (symmetric) {
      for (int i = 0; i < nRow; i++) {
287
	mat[i][i] += c[0] * q00->getValue(i,i);
288
	for (int j = i + 1; j < nCol; j++) {
289
	  double val = c[0] * q00->getValue(i, j);
290
291
	  mat[i][j] += val;
	  mat[j][i] += val;
292
293
294
	}
      }
    } else {
295
      for (int i = 0; i < nRow; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
296
	for (int j = 0; j < nCol; j++) {
297
	  mat[i][j] += c[0] * q00->getValue(i, j);
Thomas Witkowski's avatar
Thomas Witkowski committed
298
	}
299
      }
300
301
302
    }
  }

303
  void PrecalcZOA::calculateElementVector(const ElInfo *elInfo, ElementVector& vec)
304
305
  {
    if (firstCall) {
306
307
308
309
310
311
312
313
314
315
316
#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;
      }
317
318
319
320
321
    }

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

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

326
    c[0] *= elInfo->getDet();
327
328

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

}