ZeroOrderAssembler.cc 9.16 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 "ZeroOrderAssembler.h"
#include "Operator.h"
#include "QPsiPhi.h"
#include "FiniteElemSpace.h"
#include "Quadrature.h"
#include "DOFVector.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)
  {}

35

36
37
38
39
40
  ZeroOrderAssembler* ZeroOrderAssembler::getSubAssembler(Operator* op,
							  Assembler *assembler,
							  Quadrature *quad,
							  bool optimized)
  {
41
    // check if an assembler is needed at all
42
    if (op->zeroOrder.size() == 0)
43
      return NULL;   
44
45
46
47

    ZeroOrderAssembler *newAssembler;

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

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

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

    // check if a new assembler is needed
    if (quad) {
Thomas Witkowski's avatar
Thomas Witkowski committed
56
      for (int i = 0; i < static_cast<int>(subAssemblers->size()); i++) {
57
58
59
60
	std::vector<OperatorTerm*> assTerms = *((*subAssemblers)[i]->getTerms());

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

61
	if (opTerms == assTerms && (*subAssemblers)[i]->getQuadrature() == quad)
62
63
64
65
66
67
	  return dynamic_cast<ZeroOrderAssembler*>((*subAssemblers)[i]);
      }
    }
 
    // check if all terms are pw_const
    bool pwConst = true;
68
69
    for (int i = 0; i < static_cast<int>(op->zeroOrder.size()); i++) {
      if (!op->zeroOrder[i]->isPWConst()) {
70
71
72
73
74
75
	pwConst = false;
	break;
      }
    }  

    // create new assembler
76
    if (!optimized) {
77
      newAssembler = new StandardZOA(op, assembler, quad);
78
    } else {
79
      if (pwConst)
80
       	newAssembler = new PrecalcZOA(op, assembler, quad);
81
      else
82
	newAssembler = new FastQuadZOA(op, assembler, quad);      
83
    }
84
85
86
87
88

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

89

90
  StandardZOA::StandardZOA(Operator *op, Assembler *assembler, Quadrature *quad)
91
92
93
94
95
    : ZeroOrderAssembler(op, assembler, quad, false)      
  {
    name = "standard zero order assembler";
  }

96

97
  void StandardZOA::calculateElementMatrix(const ElInfo *elInfo, ElementMatrix& mat)
98
  {
99
100
    const BasisFunction *psi = rowFeSpace->getBasisFcts();
    const BasisFunction *phi = colFeSpace->getBasisFcts();
101
102

    int nPoints = quadrature->getNumPoints();
103
    ElementVector c(nPoints, 0.0);
104
    std::vector<double> phival(nCol);
105

106
107
    for (std::vector<OperatorTerm*>::iterator termIt = terms.begin(); 
	 termIt != terms.end(); ++termIt)
108
      (static_cast<ZeroOrderTerm*>((*termIt)))->getC(elInfo, nPoints, c);
109

110
    if (symmetric) {
111
112
      TEST_EXIT_DBG(nCol == nRow)("nCol != nRow, but symmetric assembling!\n");
            
113
114
115
116
      for (int iq = 0; iq < nPoints; iq++) {
	c[iq] *= elInfo->getDet();

	// calculate phi at QPs only once!
117
	for (int i = 0; i < nCol; i++)
118
119
120
121
	  phival[i] = (*(phi->getPhi(i)))(quadrature->getLambda(iq));

	for (int i = 0; i < nRow; i++) {
	  double psival = (*(psi->getPhi(i)))(quadrature->getLambda(iq));
122
	  mat[i][i] += quadrature->getWeight(iq) * c[iq] * psival * phival[i];
123
124
	  for (int j = i + 1; j < nCol; j++) {
	    double val = quadrature->getWeight(iq) * c[iq] * psival * phival[j];
125
126
	    mat[i][j] += val;
	    mat[j][i] += val;
127
128
129
130
131
132
133
134
	  }
	}
      }
    } else {      //  non symmetric assembling 
      for (int iq = 0; iq < nPoints; iq++) {
	c[iq] *= elInfo->getDet();

	// calculate phi at QPs only once!
135
	for (int i = 0; i < nCol; i++)
136
137
138
139
	  phival[i] = (*(phi->getPhi(i)))(quadrature->getLambda(iq));

	for (int i = 0; i < nRow; i++) {
	  double psival = (*(psi->getPhi(i)))(quadrature->getLambda(iq));
140
	  for (int j = 0; j < nCol; j++)
141
	    mat[i][j] += quadrature->getWeight(iq) * c[iq] * psival * phival[j];	  
142
143
144
145
146
	}
      }
    }
  }

147

148
  void StandardZOA::calculateElementVector(const ElInfo *elInfo, ElementVector& vec)
149
150
  {
    int nPoints = quadrature->getNumPoints();
151
    ElementVector c(nPoints, 0.0);
152

153
    std::vector<OperatorTerm*>::iterator termIt;
154
    for (termIt = terms.begin(); termIt != terms.end(); ++termIt)
155
156
157
158
159
160
      (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++) {
161
	double psi = (*(rowFeSpace->getBasisFcts()->getPhi(i)))
162
	  (quadrature->getLambda(iq));
163
	vec[i] += quadrature->getWeight(iq) * c[iq] * psi;
164
165
166
167
      }
    }
  }

168

169
170
  FastQuadZOA::FastQuadZOA(Operator *op, Assembler *assembler, Quadrature *quad)
    : ZeroOrderAssembler(op, assembler, quad, true)
171
  {
172
    name = "fast quadrature zero order assembler";
173
  }
174

175

176
  void FastQuadZOA::calculateElementMatrix(const ElInfo *elInfo, ElementMatrix& mat)
177
178
179
180
  {
    int nPoints = quadrature->getNumPoints();

    if (firstCall) {
181
182
183
184
185
      const BasisFunction *basFcts = rowFeSpace->getBasisFcts();
      psiFast = updateFastQuadrature(psiFast, basFcts, INIT_PHI);
      basFcts = colFeSpace->getBasisFcts();
      phiFast = updateFastQuadrature(phiFast, basFcts, INIT_PHI);
      firstCall = false;      
186
187
    }

188
    if (num_rows(c) < static_cast<unsigned int>(nPoints))
189
190
      c.change_dim(nPoints);
    c = 0.0;
191

192
193
    for (std::vector<OperatorTerm*>::iterator termIt = terms.begin(); 
	 termIt != terms.end(); ++termIt)
194
195
      (static_cast<ZeroOrderTerm*>((*termIt)))->getC(elInfo, nPoints, c);

196
197
198
    const mtl::dense2D<double>& psi = psiFast->getPhi();
    const mtl::dense2D<double>& phi = phiFast->getPhi();

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

202
      for (int iq = 0; iq < nPoints; iq++) {
203
204
	c[iq] *= elInfo->getDet() * quadrature->getWeight(iq);

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

218
219
220
221
222
223
224
      for (int i = 0; i < nRow; i++)
	for (int j = 0; j < nCol; j++) {
	  double v = 0.0;
	  for (int iq = 0; iq < nPoints; iq++)
	    v += c[iq] * psi[iq][i] * phi[iq][j];
	  mat[i][j] += v;
	}
225
226
    }
  }
Thomas Witkowski's avatar
Thomas Witkowski committed
227

228

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

233
    if (firstCall) {
234
235
236
237
238
      const BasisFunction *basFcts = rowFeSpace->getBasisFcts();
      psiFast = updateFastQuadrature(psiFast, basFcts, INIT_PHI);
      basFcts = colFeSpace->getBasisFcts();
      phiFast = updateFastQuadrature(phiFast, basFcts, INIT_PHI);
      firstCall = false;      
239
240
    }

241
    ElementVector c(nPoints, 0.0);
242
    std::vector<OperatorTerm*>::iterator termIt;
243
    for (termIt = terms.begin(); termIt != terms.end(); ++termIt)
244
245
      (static_cast<ZeroOrderTerm*>((*termIt)))->getC(elInfo, nPoints, c);

246
247
    const mtl::dense2D<double>& psi = psiFast->getPhi();

248
249
250
    for (int iq = 0; iq < nPoints; iq++) {
      c[iq] *= elInfo->getDet();

251
      for (int i = 0; i < nRow; i++)
252
	vec[i] += quadrature->getWeight(iq) * c[iq] * psi[iq][i];
253
254
255
    }
  }

256

257
258
259
  PrecalcZOA::PrecalcZOA(Operator *op, Assembler *assembler, Quadrature *quad) 
    : ZeroOrderAssembler(op, assembler, quad, true)
  {
260
    name = "precalculated zero order assembler";
261
262
  }

263

264
  void PrecalcZOA::calculateElementMatrix(const ElInfo *elInfo, ElementMatrix& mat)
265
266
  {
    if (firstCall) {
267
268
269
270
271
      q00 = Q00PsiPhi::provideQ00PsiPhi(rowFeSpace->getBasisFcts(), 
					colFeSpace->getBasisFcts(), 
					quadrature);
      q0 = Q0Psi::provideQ0Psi(rowFeSpace->getBasisFcts(), quadrature);
      firstCall = false;      
272
273
    }

274
    ElementVector c(1, 0.0);
275
    int size = static_cast<int>(terms.size());
276

277
    for (int i = 0; i < size; i++)
278
      (static_cast<ZeroOrderTerm*>((terms[i])))->getC(elInfo, 1, c);    
279

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

300

301
  void PrecalcZOA::calculateElementVector(const ElInfo *elInfo, ElementVector& vec)
302
303
  {
    if (firstCall) {
304
305
306
307
308
      q00 = Q00PsiPhi::provideQ00PsiPhi(rowFeSpace->getBasisFcts(), 
					colFeSpace->getBasisFcts(), 
					quadrature);
      q0 = Q0Psi::provideQ0Psi(rowFeSpace->getBasisFcts(), quadrature);	
      firstCall = false;      
309
310
311
312
    }

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

313
    ElementVector c(1, 0.0);
314
    for (termIt = terms.begin(); termIt != terms.end(); ++termIt)
Thomas Witkowski's avatar
Thomas Witkowski committed
315
      (static_cast<ZeroOrderTerm*>(*termIt))->getC(elInfo, 1, c);
316

317
    c[0] *= elInfo->getDet();
318
319

    for (int i = 0; i < nRow; i++)
320
      vec[i] += c[0] * q0->getValue(i);
321
322
323
  }

}