SecondOrderAssembler.cc 9.32 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
//
// Software License for AMDiS
//
// Copyright (c) 2010 Dresden University of Technology 
// All rights reserved.
// Authors: Simon Vey, Thomas Witkowski et al.
//
// This file is part of AMDiS
//
// See also license.opensource.txt in the distribution.


13
#include <vector>
14
#include <boost/numeric/mtl/mtl.hpp>
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "Assembler.h"
#include "SecondOrderAssembler.h"
#include "Operator.h"
#include "QPsiPhi.h"
#include "FiniteElemSpace.h"
#include "Quadrature.h"
#include "DOFVector.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)
  {}

35
36
37
38
39

  SecondOrderAssembler* SecondOrderAssembler::getSubAssembler(Operator* op,
							      Assembler *assembler,
							      Quadrature *quad,
							      bool optimized)
40
41
  {
    // check if a assembler is needed at all
42
    if (op->secondOrder.size() == 0)
43
44
45
46
47
      return NULL;

    SecondOrderAssembler *newAssembler;

    std::vector<SubAssembler*> *subAssemblers =
48
49
50
      optimized ?
      &optimizedSubAssemblers :
      &standardSubAssemblers;
51

52
    std::vector<OperatorTerm*> opTerms = op->zeroOrder;
53
54
55
56

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

    // check if a new assembler is needed
57
    for (unsigned int i = 0; i < subAssemblers->size(); i++) {
58
59
60
61
      std::vector<OperatorTerm*> assTerms = *((*subAssemblers)[i]->getTerms());
    
      sort(assTerms.begin(), assTerms.end());

62
      if (opTerms == assTerms && (*subAssemblers)[i]->getQuadrature() == quad)	
63
64
65
66
67
	return dynamic_cast<SecondOrderAssembler*>((*subAssemblers)[i]);
    }

    // check if all terms are pw_const
    bool pwConst = true;
68
69
    for (unsigned int i = 0; i < op->secondOrder.size(); i++) {
      if (!op->secondOrder[i]->isPWConst()) {
70
71
72
73
	pwConst = false;
	break;
      }
    }  
Thomas Witkowski's avatar
Thomas Witkowski committed
74
   
75
    // create new assembler
76
    if (!optimized) {
77
      newAssembler = new Stand2(op, assembler, quad);
78
    } else {
79
      if (pwConst) {
80
    	newAssembler = new Pre2(op, assembler, quad);
81
      } else {
82
    	newAssembler = new Quad2(op, assembler, quad);
83
      }
84
    }
85
86

    subAssemblers->push_back(newAssembler);
Thomas Witkowski's avatar
Thomas Witkowski committed
87

88
89
90
    return newAssembler;
  }

91

92
93
94
  Pre2::Pre2(Operator *op, Assembler *assembler, Quadrature *quad) 
    : SecondOrderAssembler(op, assembler, quad, true)
  {
95
96
    name = "precalculated second order assembler";

97
98
    q11 = Q11PsiPhi::provideQ11PsiPhi(rowFeSpace->getBasisFcts(), 
				      colFeSpace->getBasisFcts(), 
99
100
101
				      quadrature);    
    LALt.resize(1);
    LALt[0].change_dim(dim + 1, dim + 1);
102
103
  }

104

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

111
112
    mtl::dense2D<double> &tmpMat = LALt[0];
    tmpMat = 0.0;
113

114
115
116
117
118
119
120
121
122
    for (unsigned int i = 0; i < terms.size(); i++)
      (static_cast<SecondOrderTerm*>(terms[i]))->getLALt(elInfo, LALt);    

    // Compute: 	LALt[0] *= elInfo->getDet();
    {
      for (int i = 0; i <= dim; i++)
	for (int j = 0; j <= dim; j++)
	  tmpMat[i][j] *= elInfo->getDet();
    }
123
124
125
126

    nEntries = q11->getNumberEntries();

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

129
130
131
132
133
      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;
134
135
	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

	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;
143
144
	  for (int m = 0; m < nEntries[i][j]; m++)
	    val += values[m] * tmpMat[k[m]][l[m]];	  
145
146
	  mat[i][j] += val;
	  mat[j][i] += val;
147
148
149
150
151
152
153
154
155
	}
      }
    } 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;
156
	  for (int m = 0; m < nEntries[i][j]; m++)
157
	    val += values[m] * tmpMat[k[m]][l[m]];
158
	  mat[i][j] += val;
159
160
161
162
163
	}
      }
    }
  }

164

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

171

172
  void Quad2::calculateElementMatrix(const ElInfo *elInfo, ElementMatrix& mat)
173
  {
174
    const int nPoints = quadrature->getNumPoints();
175
176

    if (firstCall) {
177
178
      dimVec.change_dim(dim + 1);
      LALt.resize(nPoints);
179
      for (int j = 0; j < nPoints; j++)
180
181
182
183
184
185
186
	LALt[j].change_dim(dim + 1, dim + 1);

      psiFast = 
	updateFastQuadrature(psiFast, rowFeSpace->getBasisFcts(), INIT_GRD_PHI);
      phiFast = 
	updateFastQuadrature(phiFast, rowFeSpace->getBasisFcts(), INIT_GRD_PHI);
      
187
188
189
      firstCall = false;
    }

190
    for (int i = 0; i < nPoints; i++)
191
      LALt[i] = 0.0;
192

193
194
    for (unsigned int i = 0; i < terms.size(); i++)
      (static_cast<SecondOrderTerm*>(terms[i]))->getLALt(elInfo, LALt);
195

196
    if (symmetric) {
197
      // === Symmetric assembling. ===
198
199
      TEST_EXIT_DBG(nCol == nRow)("nCol != nRow, but symmetric assembling!\n");

200
      for (int iq = 0; iq < nPoints; iq++) {
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
	// Compute: 	LALt[iq] *= elInfo->getDet();	
	for (int i = 0; i <= dim; i++)
	  for (int j = 0; j <= dim; j++)
	    LALt[iq][i][j] *= elInfo->getDet();	

  	const vector<mtl::dense_vector<double> >& grdPsi = psiFast->getGradient(iq);
  	const vector<mtl::dense_vector<double> >& grdPhi = phiFast->getGradient(iq);
	double weight = quadrature->getWeight(iq);

	for (int i = 0; i < nCol; i++) {
	  // Compute: dimVec = quadrate->getWeight(iq) * (LALt[iq] * grdPhi[i])
	  // 	  dimVec = LALt[iq] * grdPhi[i];
	  // 	  dimVec *= quadrature->getWeight(iq);	  
	  double v = 0.0;
	  for (int j = 0; j <= dim; j++) {
	    v = 0.0;
	    for (int k = 0; k <= dim; k++)
	      v += LALt[iq][j][k] * grdPhi[i][k];
	    dimVec[j] = weight * v;
	  }	  

 	  mat[i][i] += dot(dimVec, grdPsi[i]);
 	  for (int j = i + 1; j < nRow; j++) {
 	    double tmp = dot(dimVec, grdPhi[j]);
 	    mat[i][j] += tmp;
 	    mat[j][i] += tmp;
 	  }
228
229
	}
      }
230
231
    } else {      
      // === Non symmetric assembling. ===
232

233
      for (int iq = 0; iq < nPoints; iq++) {
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
	// Compute: 	LALt[iq] *= elInfo->getDet();
	{
	  for (int i = 0; i <= dim; i++)
	    for (int j = 0; j <= dim; j++)
	      LALt[iq][i][j] *= elInfo->getDet();
	}

	const vector<mtl::dense_vector<double> >& grdPsi = psiFast->getGradient(iq);
	const vector<mtl::dense_vector<double> >& grdPhi = phiFast->getGradient(iq);

	for (int i = 0; i < nRow; i++) {
	  const mtl::dense_vector<double>& grdPsi_i = grdPsi[i];

	  for (int j = 0; j < nCol; j++) {
	    const mtl::dense_vector<double>& grdPhi_j = grdPhi[j];

	    // Compute: mat[i][j] += quadrature->getWeight(iq) * (grdPsi[i] * (LALt[iq] * grdPhi[j]))
	    //	    dimVec = LALt[iq] * grdPhi[j];
	    //	    mat[i][j] += quadrature->getWeight(iq) * dot(grdPsi[i], dimVec);
	    {
	      double v = 0.0;
	      for (int k = 0; k <= dim; k++) {
		double w = 0.0;
		for (int l = 0; l <= dim; l++)
		  w += LALt[iq][k][l] * grdPhi_j[l];
		v += grdPsi_i[k] * w;
	      }
	      mat[i][j] += quadrature->getWeight(iq) * v;
	    }
	  }
	}
265
266
267
268
      }
    }
  }

269

270
271
  Stand2::Stand2(Operator *op, Assembler *assembler, Quadrature *quad) 
    : SecondOrderAssembler(op, assembler, quad, false)
272
273
274
275
  {
    name = "standard second order assembler";
  }

276

277
  void Stand2::calculateElementMatrix(const ElInfo *elInfo, ElementMatrix& mat)
278
  {
279
280
281
282
    mtl::dense_vector<double> grdPsi(dim + 1);
    vector<mtl::dense_vector<double> > grdPhi(nCol);
    for (int i = 0; i < nCol; i++)
      grdPhi[i].change_dim(dim + 1);
283

284
285
    const BasisFunction *psi = rowFeSpace->getBasisFcts();
    const BasisFunction *phi = colFeSpace->getBasisFcts();
286
287
288

    int nPoints = quadrature->getNumPoints();

289
    std::vector<mtl::dense2D<double> > LALt(nPoints);
290
    for (int iq = 0; iq < nPoints; iq++) {
291
292
      LALt[iq].change_dim(dim + 1, dim + 1);
      LALt[iq] = 0.0;
293
294
    }

295
    mtl::dense_vector<double> tmpVec;
296

297
298
    for (unsigned int i = 0; i < terms.size(); i++)
      (static_cast<SecondOrderTerm*>(terms[i]))->getLALt(elInfo, LALt);
299
300

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

303
      for (int iq = 0; iq < nPoints; iq++) {
304
	LALt[iq] *= elInfo->getDet();
305

306
	for (int i = 0; i < nCol; i++)
307
308
309
310
	  (*(phi->getGrdPhi(i)))(quadrature->getLambda(iq), grdPhi[i]);

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

312
313
	  tmpVec = LALt[iq] * grdPhi[i];
	  mat[i][i] += quadrature->getWeight(iq) * dot(grdPsi, tmpVec);
314
315

	  for (int j = i + 1; j < nCol; j++) {
316
317
	    tmpVec = (LALt[iq] * grdPhi[j]);
	    double val = quadrature->getWeight(iq) * dot(grdPsi, tmpVec);
318
319
	    mat[i][j] += val;
	    mat[j][i] += val;
320
321
322
323
324
	  }
	}
      }
    } else {      /*  non symmetric assembling   */
      for (int iq = 0; iq < nPoints; iq++) {
325
	LALt[iq] *= elInfo->getDet();
326

327
	for (int i = 0; i < nCol; i++)
328
329
330
331
332
	  (*(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++) {
333
	    tmpVec = (LALt[iq] * grdPhi[j]);	 
334
	    mat[i][j] += quadrature->getWeight(iq) * dot(grdPsi, tmpVec);
335
336
337
338
339
340
341
	  }
	}
      }
    }
  }

}