SecondOrderAssembler.cc 9.07 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 "SecondOrderAssembler.h"
#include "Operator.h"
#include "QPsiPhi.h"
#include "FiniteElemSpace.h"
#include "Quadrature.h"
#include "DOFVector.h"
#include "OpenMP.h"

namespace AMDiS {

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

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

24
25
26
27
28

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

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

    SecondOrderAssembler *newAssembler;

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

43
    std::vector<OperatorTerm*> opTerms = op->zeroOrder[myRank];
44
45
46
47

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

    // check if a new assembler is needed
48
    for (int i = 0; i < static_cast<int>(subAssemblers->size()); i++) {
49
50
51
52
      std::vector<OperatorTerm*> assTerms = *((*subAssemblers)[i]->getTerms());
    
      sort(assTerms.begin(), assTerms.end());

53
      if (opTerms == assTerms && (*subAssemblers)[i]->getQuadrature() == quad)	
54
55
56
57
58
	return dynamic_cast<SecondOrderAssembler*>((*subAssemblers)[i]);
    }

    // check if all terms are pw_const
    bool pwConst = true;
59
    for (int i = 0; i < static_cast<int>(op->secondOrder[myRank].size()); i++) {
60
61
62
63
64
65
66
      if (!op->secondOrder[myRank][i]->isPWConst()) {
	pwConst = false;
	break;
      }
    }  

    // create new assembler
67
    if (!optimized) {
68
      newAssembler = new Stand2(op, assembler, quad);
69
    } else {
70
      if (pwConst) {
71
	newAssembler = new Pre2(op, assembler, quad);
72
      } else {
73
	newAssembler = new Quad2(op, assembler, quad);
74
      }
75
    }
76
77
78
79
80

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

81

82
83
84
  Pre2::Pre2(Operator *op, Assembler *assembler, Quadrature *quad) 
    : SecondOrderAssembler(op, assembler, quad, true)
  {
85
86
    name = "precalculated second order assembler";

87
88
    q11 = Q11PsiPhi::provideQ11PsiPhi(owner->getRowFeSpace()->getBasisFcts(), 
				      owner->getColFeSpace()->getBasisFcts(), 
89
				      quadrature);
90
91
    tmpLALt.resize(omp_get_overall_max_threads());
    for (int i = 0; i < omp_get_overall_max_threads(); i++) {
92
93
      tmpLALt[i] = new DimMat<double>*;
      *(tmpLALt[i]) = new DimMat<double>(dim, NO_INIT);
94
95
96
    }
  }

97

98
99
100
  Pre2::~Pre2()
  {
    for (int i = 0; i < static_cast<int>(tmpLALt.size()); i++) {
101
102
      delete *(tmpLALt[i]);
      delete tmpLALt[i];
103
104
105
    }
  }

106

107
  void Pre2::calculateElementMatrix(const ElInfo *elInfo, ElementMatrix& mat)
108
109
110
111
112
113
  {
    const int **nEntries;
    const int *k, *l;
    const double *values;

    int myRank = omp_get_thread_num();
Thomas Witkowski's avatar
Thomas Witkowski committed
114
    DimMat<double> **LALt = tmpLALt[0];
115
116
117
    DimMat<double> &tmpMat = *LALt[0];
    tmpMat.set(0.0);

118
    for (int i = 0; i < static_cast<int>(terms[myRank].size()); i++) {
119
120
121
122
123
124
125
      (static_cast<SecondOrderTerm*>(terms[myRank][i]))->getLALt(elInfo, 1, LALt);
    }

    tmpMat *= elInfo->getDet();
    nEntries = q11->getNumberEntries();

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

128
129
130
131
132
133
134
135
      for (int i = 0; i < nRow; i++) {
	k = q11->getKVec(i, i);
	l = q11->getLVec(i, i);
	values = q11->getValVec(i, i);
	double val = 0.0;
	for (int m = 0; m < nEntries[i][i]; m++) {
	  val += values[m] * tmpMat[k[m]][l[m]];
	}
136
	mat[i][i] += val;
137
138
139
140
141
142
143
144
145

	for (int j = i + 1; j < nCol; j++) {
	  k = q11->getKVec(i, j);
	  l = q11->getLVec(i, j);
	  values = q11->getValVec(i, j);
	  val = 0.0;
	  for (int m = 0; m < nEntries[i][j]; m++) {
	    val += values[m] * tmpMat[k[m]][l[m]];
	  }
146
147
	  mat[i][j] += val;
	  mat[j][i] += val;
148
149
150
151
152
153
154
155
156
157
158
159
	}
      }
    } else {  /*  A not symmetric or psi != phi        */
      for (int i = 0; i < nRow; i++) {
	for (int j = 0; j < nCol; j++) {
	  k = q11->getKVec(i, j);
	  l = q11->getLVec(i, j);
	  values = q11->getValVec(i, j);
	  double val = 0.0;
	  for (int m = 0; m < nEntries[i][j]; m++) {
	    val += values[m] * tmpMat[k[m]][l[m]];
	  }
160
	  mat[i][j] += val;
161
162
163
164
165
	}
      }
    }
  }

166

167
168
169
  Quad2::Quad2(Operator *op, Assembler *assembler, Quadrature *quad) 
    : SecondOrderAssembler(op, assembler, quad, true)
  {
170
171
    name = "fast quadrature second order assembler";

172
    tmpVec.resize(omp_get_overall_max_threads());
173
    tmpLALt.resize(omp_get_overall_max_threads());
174
175
  }

176

177
178
179
180
181
  Quad2::~Quad2()
  {
    if (!firstCall) {
      int nPoints = quadrature->getNumPoints();
      for (int i = 0; i < static_cast<int>(tmpLALt.size()); i++) {
182
	for (int j = 0; j < nPoints; j++)
183
	  delete tmpLALt[i][j];
184
	
185
	delete [] tmpLALt[i];
186
187
188
189
      }
    }
  }

190

191
  void Quad2::calculateElementMatrix(const ElInfo *elInfo, ElementMatrix& mat)
192
193
194
195
196
  {
    int nPoints = quadrature->getNumPoints();
    int myRank = omp_get_thread_num();

    if (firstCall) {
197
      tmpVec[myRank] = new DimVec<double>(dim, NO_INIT);
198
      tmpLALt[myRank] = new DimMat<double>*[nPoints];
199
200
      for (int j = 0; j < nPoints; j++)
	tmpLALt[myRank][j] = new DimMat<double>(dim, NO_INIT);      
201
202

#ifdef _OPENMP
203
#pragma omp critical
204
205
#endif
      {   
206
        psiFast = updateFastQuadrature(psiFast, owner->getRowFeSpace()->getBasisFcts(), 
207
  				       INIT_GRD_PHI);
208
        phiFast = updateFastQuadrature(phiFast, owner->getRowFeSpace()->getBasisFcts(), 
209
				       INIT_GRD_PHI);
210
      }
211
212
213
214
      firstCall = false;
    }

    DimMat<double> **LALt = tmpLALt[myRank];
215
    DimVec<double> &dimVec = *(tmpVec[myRank]);
216
    
217
    for (int i = 0; i < nPoints; i++)
218
      LALt[i]->set(0.0);
219
220

    for (int i = 0; i < static_cast<int>(terms[myRank].size()); i++)
221
      (static_cast<SecondOrderTerm*>(terms[myRank][i]))->getLALt(elInfo, nPoints, LALt);
222

223
224
    VectorOfFixVecs< DimVec<double> > *grdPsi, *grdPhi;

225
    if (symmetric) {
226
      // === Symmetric assembling. ===
227
228
      TEST_EXIT_DBG(nCol == nRow)("nCol != nRow, but symmetric assembling!\n");

229
230
      for (int iq = 0; iq < nPoints; iq++) {
	(*LALt[iq]) *= elInfo->getDet();
231
232
	grdPsi = psiFast->getGradient(iq);
	grdPhi = phiFast->getGradient(iq);
233

234
	for (int i = 0; i < nCol; i++) {	  
235
	  amv(quadrature->getWeight(iq), (*LALt[iq]), (*grdPhi)[i], dimVec);
236

237
	  mat[i][i] += (*grdPsi)[i] * dimVec;
238
	  for (int j = i + 1; j < nRow; j++) {
239
	    double tmp = (*grdPhi)[j] * dimVec;
240
241
	    mat[i][j] += tmp;
	    mat[j][i] += tmp;
242
243
244
	  }
	}
      }
245
246
    } else {      
      // === Non symmetric assembling. ===
247
248
249
250
251
      for (int iq = 0; iq < nPoints; iq++) {
	(*LALt[iq]) *= elInfo->getDet();
	grdPsi = psiFast->getGradient(iq);
	grdPhi = phiFast->getGradient(iq);

252
253
	for (int i = 0; i < nRow; i++)
	  for (int j = 0; j < nCol; j++)
254
	    mat[i][j] += quadrature->getWeight(iq) * 
255
256
257
258
259
	      xAy((*grdPsi)[i], (*LALt[iq]), (*grdPhi)[j]);
      }
    }
  }

260

261
262
  Stand2::Stand2(Operator *op, Assembler *assembler, Quadrature *quad) 
    : SecondOrderAssembler(op, assembler, quad, false)
263
264
265
266
  {
    name = "standard second order assembler";
  }

267

268
  void Stand2::calculateElementMatrix(const ElInfo *elInfo, ElementMatrix& mat)
269
270
271
272
  {
    DimVec<double> grdPsi(dim, NO_INIT);
    VectorOfFixVecs<DimVec<double> > grdPhi(dim, nCol, NO_INIT);

273
274
    const BasisFunction *psi = owner->getRowFeSpace()->getBasisFcts();
    const BasisFunction *phi = owner->getColFeSpace()->getBasisFcts();
275
276
277

    int nPoints = quadrature->getNumPoints();

278
    DimMat<double> **LALt = new DimMat<double>*[nPoints];
279
    for (int iq = 0; iq < nPoints; iq++) {
280
      LALt[iq] = new DimMat<double>(dim, NO_INIT);
281
282
283
284
285
      LALt[iq]->set(0.0);
    }

    int myRank = omp_get_thread_num();

286
287
    for (int i = 0; i < static_cast<int>(terms[myRank].size()); i++)
      (static_cast<SecondOrderTerm*>(terms[myRank][i]))->getLALt(elInfo, nPoints, LALt);    
288
289

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

292
293
294
      for (int iq = 0; iq < nPoints; iq++) {
	(*LALt[iq]) *= elInfo->getDet();

295
	for (int i = 0; i < nCol; i++)
296
297
298
299
	  (*(phi->getGrdPhi(i)))(quadrature->getLambda(iq), grdPhi[i]);

	for (int i = 0; i < nRow; i++) {
	  (*(psi->getGrdPhi(i)))(quadrature->getLambda(iq), grdPsi);
300

301
	  mat[i][i] += quadrature->getWeight(iq) * 
302
303
304
	    (grdPsi * ((*LALt[iq]) * grdPhi[i]));

	  for (int j = i + 1; j < nCol; j++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
305
306
	    double val = quadrature->getWeight(iq) * 
	      (grdPsi * ((*LALt[iq]) * grdPhi[j]));
307
308
	    mat[i][j] += val;
	    mat[j][i] += val;
309
310
311
312
313
314
315
	  }
	}
      }
    } else {      /*  non symmetric assembling   */
      for (int iq = 0; iq < nPoints; iq++) {
	(*LALt[iq]) *= elInfo->getDet();

316
	for (int i = 0; i < nCol; i++)
317
318
319
320
321
	  (*(phi->getGrdPhi(i)))(quadrature->getLambda(iq), grdPhi[i]);

	for (int i = 0; i < nRow; i++) {
	  (*(psi->getGrdPhi(i)))(quadrature->getLambda(iq), grdPsi);
	  for (int j = 0; j < nCol; j++) {
322
	    mat[i][j] += quadrature->getWeight(iq) *
323
324
325
326
327
328
329
	      (grdPsi * ((*LALt[iq]) * grdPhi[j]));
	  }
	}
      }
    }

    for (int iq = 0; iq < nPoints; iq++) 
330
      delete LALt[iq];
331

332
    delete [] LALt;
333
334
335
  }

}