SecondOrderAssembler.cc 8.88 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
29
30
31
32
#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)
  {}

  SecondOrderAssembler* 
  SecondOrderAssembler::getSubAssembler(Operator* op,
					Assembler *assembler,
					Quadrature *quad,
					bool optimized)
  {
    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
81
82
83
84
85
86

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

  Pre2::Pre2(Operator *op, Assembler *assembler, Quadrature *quad) 
    : SecondOrderAssembler(op, assembler, quad, true)
  {
    q11 = Q11PsiPhi::provideQ11PsiPhi(owner->getRowFESpace()->getBasisFcts(), 
				      owner->getColFESpace()->getBasisFcts(), 
				      quadrature);
87
88
    tmpLALt.resize(omp_get_overall_max_threads());
    for (int i = 0; i < omp_get_overall_max_threads(); i++) {
89
90
      tmpLALt[i] = new DimMat<double>*;
      *(tmpLALt[i]) = new DimMat<double>(dim, NO_INIT);
91
92
93
94
95
96
    }
  }

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

102
  void Pre2::calculateElementMatrix(const ElInfo *elInfo, ElementMatrix& mat)
103
104
105
106
107
108
  {
    const int **nEntries;
    const int *k, *l;
    const double *values;

    int myRank = omp_get_thread_num();
Thomas Witkowski's avatar
Thomas Witkowski committed
109
    DimMat<double> **LALt = tmpLALt[0];
110
111
112
    DimMat<double> &tmpMat = *LALt[0];
    tmpMat.set(0.0);

113
    for (int i = 0; i < static_cast<int>(terms[myRank].size()); i++) {
114
115
116
117
118
119
120
      (static_cast<SecondOrderTerm*>(terms[myRank][i]))->getLALt(elInfo, 1, LALt);
    }

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

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

123
124
125
126
127
128
129
130
      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]];
	}
131
	mat[i][i] += val;
132
133
134
135
136
137
138
139
140

	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]];
	  }
141
142
	  mat[i][j] += val;
	  mat[j][i] += val;
143
144
145
146
147
148
149
150
151
152
153
154
	}
      }
    } 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]];
	  }
155
	  mat[i][j] += val;
156
157
158
159
160
161
162
163
	}
      }
    }
  }

  Quad2::Quad2(Operator *op, Assembler *assembler, Quadrature *quad) 
    : SecondOrderAssembler(op, assembler, quad, true)
  {
164
    tmpVec.resize(omp_get_overall_max_threads());
165
    tmpLALt.resize(omp_get_overall_max_threads());
166
167
168
169
170
171
172
  }

  Quad2::~Quad2()
  {
    if (!firstCall) {
      int nPoints = quadrature->getNumPoints();
      for (int i = 0; i < static_cast<int>(tmpLALt.size()); i++) {
173
	for (int j = 0; j < nPoints; j++)
174
	  delete tmpLALt[i][j];
175
	
176
	delete [] tmpLALt[i];
177
178
179
180
      }
    }
  }

181
  void Quad2::calculateElementMatrix(const ElInfo *elInfo, ElementMatrix& mat)
182
183
184
185
186
  {
    int nPoints = quadrature->getNumPoints();
    int myRank = omp_get_thread_num();

    if (firstCall) {
187
      tmpVec[myRank] = new DimVec<double>(dim, NO_INIT);
188
      tmpLALt[myRank] = new DimMat<double>*[nPoints];
189
190
      for (int j = 0; j < nPoints; j++)
	tmpLALt[myRank][j] = new DimMat<double>(dim, NO_INIT);      
191
192

#ifdef _OPENMP
193
#pragma omp critical
194
195
#endif
      {   
196
197
198
199
        psiFast = updateFastQuadrature(psiFast, owner->getRowFESpace()->getBasisFcts(), 
  				       INIT_GRD_PHI);
        phiFast = updateFastQuadrature(phiFast, owner->getRowFESpace()->getBasisFcts(), 
				       INIT_GRD_PHI);
200
      }
201
202
203
204
      firstCall = false;
    }

    DimMat<double> **LALt = tmpLALt[myRank];
205
    DimVec<double> &dimVec = *(tmpVec[myRank]);
206
    
207
    for (int i = 0; i < nPoints; i++)
208
      LALt[i]->set(0.0);
209
210

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

213
    if (symmetric) {
214
215
      // === Symmetric assembling. ===

216
217
      TEST_EXIT_DBG(nCol == nRow)("nCol != nRow, but symmetric assembling!\n");

218
219
      for (int iq = 0; iq < nPoints; iq++) {
	(*LALt[iq]) *= elInfo->getDet();
220

221
222
223
	for (int i = 0; i < nCol; i++) {	  
	  amv(quadrature->getWeight(iq), 
	      (*LALt[iq]), phiFast->getGradient(iq, i), dimVec);
224

225
226
227
228
229
	  mat[i][i] += (psiFast->getGradient(iq, i) * dimVec);
	  for (int j = i + 1; j < nRow; j++) {
	    double tmp = (phiFast->getGradient(iq, j) * dimVec);
	    mat[i][j] += tmp;
	    mat[j][i] += tmp;
230
231
232
	  }
	}
      }
233
    } else {      
234
235
      VectorOfFixVecs< DimVec<double> > *grdPsi, *grdPhi;

236
      // === Non symmetric assembling. ===
237
238
239
240
241
242
      for (int iq = 0; iq < nPoints; iq++) {
	(*LALt[iq]) *= elInfo->getDet();

	grdPsi = psiFast->getGradient(iq);
	grdPhi = phiFast->getGradient(iq);

243
244
	for (int i = 0; i < nRow; i++)
	  for (int j = 0; j < nCol; j++)
245
	    mat[i][j] += quadrature->getWeight(iq) * 
246
247
248
249
250
251
252
253
254
	      xAy((*grdPsi)[i], (*LALt[iq]), (*grdPhi)[j]);
      }
    }
  }

  Stand2::Stand2(Operator *op, Assembler *assembler, Quadrature *quad) 
    : SecondOrderAssembler(op, assembler, quad, false)
  {}

255
  void Stand2::calculateElementMatrix(const ElInfo *elInfo, ElementMatrix& mat)
256
257
258
259
260
261
262
263
264
  {
    DimVec<double> grdPsi(dim, NO_INIT);
    VectorOfFixVecs<DimVec<double> > grdPhi(dim, nCol, NO_INIT);

    const BasisFunction *psi = owner->getRowFESpace()->getBasisFcts();
    const BasisFunction *phi = owner->getColFESpace()->getBasisFcts();

    int nPoints = quadrature->getNumPoints();

265
    DimMat<double> **LALt = new DimMat<double>*[nPoints];
266
    for (int iq = 0; iq < nPoints; iq++) {
267
      LALt[iq] = new DimMat<double>(dim, NO_INIT);
268
269
270
271
272
      LALt[iq]->set(0.0);
    }

    int myRank = omp_get_thread_num();

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

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

279
280
281
      for (int iq = 0; iq < nPoints; iq++) {
	(*LALt[iq]) *= elInfo->getDet();

282
	for (int i = 0; i < nCol; i++)
283
284
285
286
	  (*(phi->getGrdPhi(i)))(quadrature->getLambda(iq), grdPhi[i]);

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

288
	  mat[i][i] += quadrature->getWeight(iq) * 
289
290
291
	    (grdPsi * ((*LALt[iq]) * grdPhi[i]));

	  for (int j = i + 1; j < nCol; j++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
292
293
	    double val = quadrature->getWeight(iq) * 
	      (grdPsi * ((*LALt[iq]) * grdPhi[j]));
294
295
	    mat[i][j] += val;
	    mat[j][i] += val;
296
297
298
299
300
301
302
	  }
	}
      }
    } else {      /*  non symmetric assembling   */
      for (int iq = 0; iq < nPoints; iq++) {
	(*LALt[iq]) *= elInfo->getDet();

303
	for (int i = 0; i < nCol; i++)
304
305
306
307
308
	  (*(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++) {
309
	    mat[i][j] += quadrature->getWeight(iq) *
310
311
312
313
314
315
316
	      (grdPsi * ((*LALt[iq]) * grdPhi[j]));
	  }
	}
      }
    }

    for (int iq = 0; iq < nPoints; iq++) 
317
      delete LALt[iq];
318

319
    delete [] LALt;
320
321
322
  }

}