Assembler.cc 45.8 KB
Newer Older
1
2
3
4
5
6
7
#include "Assembler.h"
#include "Operator.h"
#include "Element.h"
#include "QPsiPhi.h"
#include "ElementMatrix.h"
#include "ElementVector.h"
#include "DOFVector.h"
8
#include "OpenMP.h"
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 <vector>
#include <algorithm>

namespace AMDiS {

  ::std::vector<SubAssembler*> ZeroOrderAssembler::optimizedSubAssemblers;
  ::std::vector<SubAssembler*> FirstOrderAssembler::optimizedSubAssemblersGrdPhi;
  ::std::vector<SubAssembler*> FirstOrderAssembler::optimizedSubAssemblersGrdPsi;
  ::std::vector<SubAssembler*> SecondOrderAssembler::optimizedSubAssemblers;
  
  ::std::vector<SubAssembler*> ZeroOrderAssembler::standardSubAssemblers;
  ::std::vector<SubAssembler*> FirstOrderAssembler::standardSubAssemblersGrdPhi;
  ::std::vector<SubAssembler*> FirstOrderAssembler::standardSubAssemblersGrdPsi;
  ::std::vector<SubAssembler*> SecondOrderAssembler::standardSubAssemblers;

  SubAssembler::SubAssembler(Operator *op,
			     Assembler *assembler,
			     Quadrature *quadrat,
			     int order, 
			     bool optimized,
			     FirstOrderType type) 
    : nRow(0),
      nCol(0),
      coordsAtQPs(NULL),
Thomas Witkowski's avatar
Thomas Witkowski committed
33
      coordsNumAllocated(0),
34
35
36
37
38
39
40
41
42
43
44
45
46
47
      quadrature(quadrat),
      psiFast(NULL),
      phiFast(NULL),
      owner(assembler),
      symmetric(true),
      opt(optimized),
      firstCall(true)
  {
    const BasisFunction *psi = assembler->rowFESpace->getBasisFcts();
    const BasisFunction *phi = assembler->colFESpace->getBasisFcts();

    nRow = psi->getNumber();
    nCol = phi->getNumber();

48
49
50
    int maxThreads = omp_get_max_threads();
    terms.resize(maxThreads);

51
    switch (order) {
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
    case 0:
      terms = op->zeroOrder;
      break;
    case 1:
      if(type == GRD_PHI)
	terms = op->firstOrderGrdPhi;
      else 
	terms = op->firstOrderGrdPsi;
      break;
    case 2:
      terms = op->secondOrder;
      break;
    }

    // check if all terms are symmetric
    symmetric = true;
68
69
    for (int i = 0; i < static_cast<int>(terms[0].size()); i++) {
      if (!(terms[0][i])->isSymmetric()) {
70
71
72
73
74
75
76
77
78
79
80
81
	symmetric = false;
	break;
      }
    }  

    dim = assembler->rowFESpace->getMesh()->getDim();
  }

  FastQuadrature *SubAssembler::updateFastQuadrature(FastQuadrature *quadFast,
						     const BasisFunction *psi,
						     Flag updateFlag)
  {
82
83
84
85
    if (!quadFast) {
      quadFast = FastQuadrature::provideFastQuadrature(psi,
						       *quadrature,
						       updateFlag);
86
    } else {
87
      if (!quadFast->initialized(updateFlag))
88
89
90
91
92
93
94
95
96
97
98
99
100
101
	quadFast->init(updateFlag);
    }

    return quadFast;
  }

  void SubAssembler::initElement(const ElInfo* elInfo,
				 Quadrature *quad)
  {
    // set corrdsAtQPs invalid
    coordsValid = false;

    // set values at QPs invalid
    ::std::map<const DOFVectorBase<double>*, ValuesAtQPs*>::iterator it1;
102
    for (it1 = valuesAtQPs.begin(); it1 != valuesAtQPs.end(); ++it1) {
103
104
105
106
107
      ((*it1).second)->valid = false;
    }

    // set gradients at QPs invalid
    ::std::map<const DOFVectorBase<double>*, GradientsAtQPs*>::iterator it2;
108
    for (it2 = gradientsAtQPs.begin(); it2 != gradientsAtQPs.end(); ++it2) {
109
110
111
      ((*it2).second)->valid = false;
    }

112
    int myRank = omp_get_thread_num();
113
114
    // calls initElement of each term
    ::std::vector<OperatorTerm*>::iterator it;
115
    for (it = terms[myRank].begin(); it != terms[myRank].end(); ++it) {
116
117
118
119
120
121
122
123
124
125
126
127
      (*it)->initElement(elInfo, this, quad);
    }
  }

  WorldVector<double>* SubAssembler::getCoordsAtQPs(const ElInfo* elInfo,
						    Quadrature *quad)
  {
    Quadrature *localQuad = quad ? quad : quadrature;
  
    const int numPoints = localQuad->getNumPoints();

    // already calculated for this element ?
128
    if (coordsValid) {
129
130
131
      return coordsAtQPs;
    }
   
Thomas Witkowski's avatar
Thomas Witkowski committed
132
133
134
135
136
137
138
139
140
141
    if (coordsAtQPs)  {
      if (coordsNumAllocated != numPoints) {
	DELETE [] coordsAtQPs;
        coordsAtQPs = NEW WorldVector<double>[numPoints];
	coordsNumAllocated = numPoints;
      }
    } else {
      coordsAtQPs = NEW WorldVector<double>[numPoints];
      coordsNumAllocated = numPoints;
    }
142
143

    // set new values
Thomas Witkowski's avatar
Thomas Witkowski committed
144
145
    WorldVector<double>* k = &(coordsAtQPs[0]);
    for (int l = 0; k < &(coordsAtQPs[numPoints]); ++k, ++l) {
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
      elInfo->coordToWorld(localQuad->getLambda(l), k);
    }

    // mark values as valid
    coordsValid = true;

    return coordsAtQPs;
  }

  double* SubAssembler::getVectorAtQPs(DOFVectorBase<double>* dv, 
				       const ElInfo* elInfo,
				       Quadrature *quad)
  {
    FUNCNAME("SubAssembler::getVectorAtQPs()");

    const DOFVectorBase<double>* vec = dv ? dv : owner->operat->getUhOld();

163
    TEST_EXIT_DBG(vec)("no dof vector!\n");
164

165
    if (valuesAtQPs[vec] && valuesAtQPs[vec]->valid) 
166
167
168
169
      return valuesAtQPs[vec]->values.getValArray();

    Quadrature *localQuad = quad ? quad : quadrature;

170
    if (!valuesAtQPs[vec]) {
171
172
173
174
175
176
177
178
179
180
      valuesAtQPs[vec] = new ValuesAtQPs;
    }
    valuesAtQPs[vec]->values.resize(localQuad->getNumPoints());

    double *values = valuesAtQPs[vec]->values.getValArray();

    bool sameFESpaces = 
      (vec->getFESpace() == owner->rowFESpace) || 
      (vec->getFESpace() == owner->colFESpace);

181
    if (opt && !quad && sameFESpaces) {
182
183
      const BasisFunction *psi = owner->rowFESpace->getBasisFcts();
      const BasisFunction *phi = owner->colFESpace->getBasisFcts();
184
      if (vec->getFESpace()->getBasisFcts() == psi) {
185
186
187
188
189
190
191
192
193
	psiFast = updateFastQuadrature(psiFast, psi, INIT_PHI);
      } else if(vec->getFESpace()->getBasisFcts() == phi) {
	phiFast = updateFastQuadrature(phiFast, phi, INIT_PHI);
      }
    }

    // calculate new values
    const BasisFunction *basFcts = vec->getFESpace()->getBasisFcts();

194
195
    if (opt && !quad && sameFESpaces) {
      if (psiFast->getBasisFunctions() == basFcts) {
196
197
198
199
200
201
202
203
204
	vec->getVecAtQPs(elInfo, NULL, psiFast, values);
      } else if(phiFast->getBasisFunctions() == basFcts) {
	vec->getVecAtQPs(elInfo, NULL, phiFast, values);
      } else {
	vec->getVecAtQPs(elInfo, localQuad, NULL, values);
      }
    } else {
      vec->getVecAtQPs(elInfo, localQuad, NULL, values);
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
205
  
206
207
208
209
210
211
212
213
214
215
216
217
218
    valuesAtQPs[vec]->valid = true;

    return values;
  }

  WorldVector<double>* SubAssembler::getGradientsAtQPs(DOFVectorBase<double>* dv, 
						       const ElInfo* elInfo,
						       Quadrature *quad)
  {
    FUNCNAME("SubAssembler::getGradientsAtQPs()");

    const DOFVectorBase<double>* vec = dv ? dv : owner->operat->getUhOld();

219
    TEST_EXIT_DBG(vec)("no dof vector!\n");
220

221
    if (gradientsAtQPs[vec] && gradientsAtQPs[vec]->valid) 
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
      return gradientsAtQPs[vec]->values.getValArray();

    Quadrature *localQuad = quad ? quad : quadrature;

    if(!gradientsAtQPs[vec]) {
      gradientsAtQPs[vec] = new GradientsAtQPs;
    } 
    gradientsAtQPs[vec]->values.resize(localQuad->getNumPoints());

    WorldVector<double> *values = gradientsAtQPs[vec]->values.getValArray();

    const BasisFunction *psi = owner->rowFESpace->getBasisFcts();
    const BasisFunction *phi = owner->colFESpace->getBasisFcts();

    bool sameFESpaces = 
      (vec->getFESpace() == owner->rowFESpace) || 
      (vec->getFESpace() == owner->colFESpace);

240
241
    if (opt && !quad && sameFESpaces) {
      if (vec->getFESpace()->getBasisFcts() == psi) {
242
243
244
245
246
247
248
249
250
	psiFast = updateFastQuadrature(psiFast, psi, INIT_GRD_PHI);
      } else if(vec->getFESpace()->getBasisFcts() == phi) {
	phiFast = updateFastQuadrature(phiFast, phi, INIT_GRD_PHI);
      }
    }
  
    // calculate new values
    const BasisFunction *basFcts = vec->getFESpace()->getBasisFcts();

251
    if (opt && !quad && sameFESpaces) {
252
253
254
255
256
257
258
259
      if(psiFast->getBasisFunctions() == basFcts) {
	vec->getGrdAtQPs(elInfo, NULL, psiFast, values);
      } else {
	vec->getGrdAtQPs(elInfo, NULL, phiFast, values);
      }
    } else {
      vec->getGrdAtQPs(elInfo, localQuad, NULL, values);
    }
260
   
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
    gradientsAtQPs[vec]->valid = true;

    return values;
  }

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

  FirstOrderAssembler::FirstOrderAssembler(Operator *op,
					   Assembler *assembler,
					   Quadrature *quad,
					   bool optimized,
					   FirstOrderType type)
    : SubAssembler(op, assembler, quad, 1, optimized, type)
  {}

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

  ZeroOrderAssembler* 
  ZeroOrderAssembler::getSubAssembler(Operator* op,
				      Assembler *assembler,
				      Quadrature *quad,
				      bool optimized)
  {
    // check if a assembler is needed at all
295
    if (op->zeroOrder.size() == 0) {
296
297
298
299
300
301
302
303
304
305
      return NULL;
    }

    ZeroOrderAssembler *newAssembler;

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

306
307
    int myRank = omp_get_thread_num();
    ::std::vector<OperatorTerm*> opTerms  = op->zeroOrder[myRank];
308
309
310
311

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

    // check if a new assembler is needed
312
313
    if (quad) {
      for (int i = 0; i < static_cast<int>( subAssemblers->size()); i++) {
314
315
316
317
	::std::vector<OperatorTerm*> assTerms = *((*subAssemblers)[i]->getTerms());

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

318
319
320
321
322
	if ((opTerms == assTerms) && 
	    ((*subAssemblers)[i]->getQuadrature() == quad)) {
	
	  return dynamic_cast<ZeroOrderAssembler*>((*subAssemblers)[i]);
	}
323
324
      }
    }
325
 
326
327
    // check if all terms are pw_const
    bool pwConst = true;
328
329
    for (int i = 0; i < static_cast<int>( op->zeroOrder[myRank].size()); i++) {
      if (!op->zeroOrder[myRank][i]->isPWConst()) {
330
331
332
333
334
335
	pwConst = false;
	break;
      }
    }  

    // create new assembler
336
    if (!optimized) {
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
      newAssembler = NEW Stand0(op, assembler, quad);
    } else {
      if(pwConst) {
	newAssembler = NEW Pre0(op, assembler, quad);
      } else {
	newAssembler = NEW Quad0(op, assembler, quad);
      }
    }

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

  FirstOrderAssembler* 
  FirstOrderAssembler::getSubAssembler(Operator* op,
				       Assembler *assembler,
				       Quadrature *quad,
				       FirstOrderType type,
				       bool optimized)
  {
    ::std::vector<SubAssembler*> *subAssemblers =
	optimized ?
	(type == GRD_PSI ? 
	 &optimizedSubAssemblersGrdPsi : 
	 &optimizedSubAssemblersGrdPhi) :
    (type == GRD_PSI ? 
     &standardSubAssemblersGrdPsi :
     &standardSubAssemblersGrdPhi);

366
    int myRank = omp_get_thread_num();
367
    ::std::vector<OperatorTerm*> opTerms 
368
369
370
	= (type == GRD_PSI) ? 
	    op->firstOrderGrdPsi[myRank] : 
            op->firstOrderGrdPhi[myRank];
371
372

    // check if a assembler is needed at all
373
    if (opTerms.size() == 0) {
374
375
376
377
378
379
380
381
      return NULL;
    }

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

    FirstOrderAssembler *newAssembler;

    // check if a new assembler is needed
382
    for (int i = 0; i < static_cast<int>( subAssemblers->size()); i++) {
383
384
385

      ::std::vector<OperatorTerm*> assTerms = *((*subAssemblers)[i]->getTerms());
    
386
      sort(assTerms.begin(), assTerms.end());   
387

388
389
      if ((opTerms == assTerms) && 
	  ((*subAssemblers)[i]->getQuadrature() == quad)) {
390

391
392
	return dynamic_cast<FirstOrderAssembler*>((*subAssemblers)[i]);
      }
393
394
395
396
    }

    // check if all terms are pw_const
    bool pwConst = true;
397
398
    for (int i = 0; i < static_cast<int>( opTerms.size()); i++) {
      if (!(opTerms[i])->isPWConst()) {
399
400
401
402
403
404
	pwConst = false;
	break;
      }
    }  

    // create new assembler
405
    if (!optimized) {
406
407
408
409
410
      newAssembler = 
	(type == GRD_PSI) ?
	dynamic_cast<FirstOrderAssembler*>(NEW Stand10(op, assembler, quad)) :
	dynamic_cast<FirstOrderAssembler*>(NEW Stand01(op, assembler, quad));    
    } else {
411
      if (pwConst) {
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
	newAssembler = 
	  (type == GRD_PSI) ?
	  dynamic_cast<FirstOrderAssembler*>(NEW Pre10(op, assembler, quad)) :
	  dynamic_cast<FirstOrderAssembler*>(NEW Pre01(op, assembler, quad));
      } else {
	newAssembler = 
	  (type == GRD_PSI) ?
	  dynamic_cast<FirstOrderAssembler*>( NEW Quad10(op, assembler, quad)) :
	  dynamic_cast<FirstOrderAssembler*>( NEW Quad01(op, assembler, quad));
      }
    }

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

  SecondOrderAssembler* 
  SecondOrderAssembler::getSubAssembler(Operator* op,
					Assembler *assembler,
					Quadrature *quad,
					bool optimized)
  {
434
435
    int myRank = omp_get_thread_num();

436
    // check if a assembler is needed at all
437
    if (op->secondOrder[myRank].size() == 0) {
438
439
440
441
442
443
444
445
446
447
      return NULL;
    }

    SecondOrderAssembler *newAssembler;

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

448
    ::std::vector<OperatorTerm*> opTerms  = op->zeroOrder[myRank];
449
450
451
452

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

    // check if a new assembler is needed
453
    for (int i = 0; i < static_cast<int>( subAssemblers->size()); i++) {
454
455
456
457
      ::std::vector<OperatorTerm*> assTerms = *((*subAssemblers)[i]->getTerms());
    
      sort(assTerms.begin(), assTerms.end());

458
459
460
461
462
      if ((opTerms == assTerms) && 
	  ((*subAssemblers)[i]->getQuadrature() == quad)) {
	
	return dynamic_cast<SecondOrderAssembler*>((*subAssemblers)[i]);
      }
463
464
465
466
    }

    // check if all terms are pw_const
    bool pwConst = true;
467
468
    for (int i = 0; i < static_cast<int>( op->secondOrder[myRank].size()); i++) {
      if (!op->secondOrder[myRank][i]->isPWConst()) {
469
470
471
472
473
474
	pwConst = false;
	break;
      }
    }  

    // create new assembler
475
    if (!optimized) {
476
477
      newAssembler = NEW Stand2(op, assembler, quad);
    } else {
478
      if (pwConst) {
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
	newAssembler = NEW Pre2(op, assembler, quad);
      } else {
	newAssembler = NEW Quad2(op, assembler, quad);
      }
    }

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

  Stand0::Stand0(Operator *op, Assembler *assembler, Quadrature *quad)
    : ZeroOrderAssembler(op, assembler, quad, false)
  {
  }

  void Stand0::calculateElementMatrix(const ElInfo *elInfo, ElementMatrix *mat)
  {
    double val;

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

    double  psival;
    double *phival = GET_MEMORY(double, nCol);
    int numPoints = quadrature->getNumPoints();
    double *c = GET_MEMORY(double, numPoints);
Thomas Witkowski's avatar
Thomas Witkowski committed
505

506
    for (int iq = 0; iq < numPoints; iq++) {
507
508
509
      c[iq] = 0.0;
    }

510
    int myRank = omp_get_thread_num();
511
    ::std::vector<OperatorTerm*>::iterator termIt;
512
    for (termIt = terms[myRank].begin(); termIt != terms[myRank].end(); ++termIt) {
513
514
515
516
      (static_cast<ZeroOrderTerm*>((*termIt)))->getC(elInfo, numPoints, c);
    }
      
    if (symmetric) {
517
      for (int iq = 0; iq < numPoints; iq++) {
518
519
520
	c[iq] *= elInfo->getDet();

	// calculate phi at QPs only once!
521
	for (int i = 0; i < nCol; i++) {
522
523
524
	  phival[i] = (*(phi->getPhi(i)))(quadrature->getLambda(iq));
	}

525
	for (int i = 0; i < nRow; i++) {
526
	  psival = (*(psi->getPhi(i)))(quadrature->getLambda(iq));
Thomas Witkowski's avatar
Thomas Witkowski committed
527
	  (*mat)[i][i] += quadrature->getWeight(iq) * c[iq] * psival * phival[i];
528
	  for (int j = i + 1; j < nCol; j++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
529
	    val = quadrature->getWeight(iq) * c[iq] * psival * phival[j];
530
531
532
533
534
	    (*mat)[i][j] += val;
	    (*mat)[j][i] += val;
	  }
	}
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
535
    } else {      //  non symmetric assembling 
536
      for (int iq = 0; iq < numPoints; iq++) {
537
538
539
	c[iq] *= elInfo->getDet();

	// calculate phi at QPs only once!
540
	for (int i = 0; i < nCol; i++) {
541
542
543
	  phival[i] = (*(phi->getPhi(i)))(quadrature->getLambda(iq));
	}

544
	for (int i = 0; i < nRow; i++) {
545
	  psival = (*(psi->getPhi(i)))(quadrature->getLambda(iq));
546
	  for (int j = 0; j < nCol; j++) {
547
548
549
550
551
	    (*mat)[i][j] += quadrature->getWeight(iq)*c[iq]*psival*phival[j];
	  }
	}
      }
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
552

553
    FREE_MEMORY(phival, double, nCol);
Thomas Witkowski's avatar
Thomas Witkowski committed
554
    FREE_MEMORY(c, double, numPoints);
555
556
557
558
559
560
561
  }

  void Stand0::calculateElementVector(const ElInfo *elInfo, ElementVector *vec)
  {
    int numPoints = quadrature->getNumPoints();

    double *c = GET_MEMORY(double, numPoints);
562

563
    for (int iq = 0; iq < numPoints; iq++) {
564
565
566
      c[iq] = 0.0;
    }

567
    int myRank = omp_get_thread_num();
568
    ::std::vector<OperatorTerm*>::iterator termIt;
569
    for (termIt = terms[myRank].begin(); termIt != terms[myRank].end(); ++termIt) {
570
571
572
      (static_cast<ZeroOrderTerm*>((*termIt)))->getC(elInfo, numPoints, c);
    }

573
    for (int iq = 0; iq < numPoints; iq++) {
574
575
      c[iq] *= elInfo->getDet();

576
577
      for (int i = 0; i < nRow; i++) {
	double psi = (*(owner->getRowFESpace()->getBasisFcts()->getPhi(i)))
578
	  (quadrature->getLambda(iq));
579
	(*vec)[i] += quadrature->getWeight(iq) * c[iq] * psi;
580
581
      }
    }
582
    
583
584
585
586
587
588
589
590
591
592
593
594
    FREE_MEMORY(c, double, numPoints);
  }

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

  void Quad0::calculateElementMatrix(const ElInfo *elInfo, ElementMatrix *mat)
  {
    const double *psi, *phi;

595
    if (firstCall) {
596
597
598
599
600
601
602
603
604
      const BasisFunction *basFcts = owner->getRowFESpace()->getBasisFcts();
      psiFast = updateFastQuadrature(psiFast, basFcts, INIT_PHI);
      basFcts = owner->getColFESpace()->getBasisFcts();
      phiFast = updateFastQuadrature(phiFast, basFcts, INIT_PHI);
      firstCall = false;
    }

    int numPoints = quadrature->getNumPoints();
    double *c = GET_MEMORY(double, numPoints);
605

606
    for (int iq = 0; iq < numPoints; iq++) {
607
608
609
      c[iq] = 0.0;
    }

610
    int myRank = omp_get_thread_num();
611
    ::std::vector<OperatorTerm*>::iterator termIt;
612
    for (termIt = terms[myRank].begin(); termIt != terms[myRank].end(); ++termIt) {
613
614
615
616
      (static_cast<ZeroOrderTerm*>((*termIt)))->getC(elInfo, numPoints, c);
    }

    if (symmetric) {
617
      for (int iq = 0; iq < numPoints; iq++) {
618
619
620
621
	c[iq] *= elInfo->getDet();

	psi = psiFast->getPhi(iq);
	phi = phiFast->getPhi(iq);
622
623
624
625
	for (int i = 0; i < nRow; i++) {
	  (*mat)[i][i] += quadrature->getWeight(iq) * c[iq] * psi[i] * phi[i];
	  for (int j = i + 1; j < nCol; j++) {
	    double val = quadrature->getWeight(iq) * c[iq] * psi[i] * phi[j];
626
627
628
629
630
631
	    (*mat)[i][j] += val;
	    (*mat)[j][i] += val;
	  }
	}
      }
    } else {      /*  non symmetric assembling   */
632
      for (int iq = 0; iq < numPoints; iq++) {
633
634
635
636
	c[iq] *= elInfo->getDet();

	psi = psiFast->getPhi(iq);
	phi = phiFast->getPhi(iq);
637
638
639
	for (int i = 0; i < nRow; i++) {
	  for (int j = 0; j < nCol; j++) {
	    (*mat)[i][j] += quadrature->getWeight(iq) * c[iq] * psi[i] * phi[j];
640
641
642
643
644
645
646
647
648
	  }
	}
      }
    }
    FREE_MEMORY(c, double, numPoints);
  }

  void Quad0::calculateElementVector(const ElInfo *elInfo, ElementVector *vec)
  {
649
    if (firstCall) {
650
651
652
653
654
655
656
657
658
      const BasisFunction *basFcts = owner->getRowFESpace()->getBasisFcts();
      psiFast = updateFastQuadrature(psiFast, basFcts, INIT_PHI);
      basFcts = owner->getColFESpace()->getBasisFcts();
      phiFast = updateFastQuadrature(phiFast, basFcts, INIT_PHI);
      firstCall = false;
    }

    int numPoints = quadrature->getNumPoints();
    double *c = GET_MEMORY(double, numPoints);
659

660
    for (int iq = 0; iq < numPoints; iq++) {
661
662
663
      c[iq] = 0.0;
    }

664
    int myRank = omp_get_thread_num();
665
    ::std::vector<OperatorTerm*>::iterator termIt;
666
    for (termIt = terms[myRank].begin(); termIt != terms[myRank].end(); ++termIt) {
667
668
669
      (static_cast<ZeroOrderTerm*>((*termIt)))->getC(elInfo, numPoints, c);
    }

670
    for (int iq = 0; iq < numPoints; iq++) {
671
672
      c[iq] *= elInfo->getDet();

673
674
675
      const double *psi = psiFast->getPhi(iq);
      for (int i = 0; i < nRow; i++) {
	(*vec)[i] += quadrature->getWeight(iq) * c[iq] * psi[i];
676
677
678
679
680
681
682
683
684
685
686
687
      }
    }
    FREE_MEMORY(c, double, numPoints);
  }

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

  void Pre0::calculateElementMatrix(const ElInfo *elInfo, ElementMatrix *mat)
  {
688
    if (firstCall) {
689
690
691
692
693
694
695
696
      q00 = Q00PsiPhi::provideQ00PsiPhi(owner->getRowFESpace()->getBasisFcts(), 
					owner->getColFESpace()->getBasisFcts(), 
					quadrature);
      q0 = Q0Psi::provideQ0Psi(owner->getRowFESpace()->getBasisFcts(),
			       quadrature);
      firstCall = false;
    }

697
698
    //    c[0] = 0.0;
    double c = 0.0;
699
    int myRank = omp_get_thread_num();
700
    int size = static_cast<int>(terms[myRank].size());
701

702
703
    for (int i = 0; i < size; i++) {
      (static_cast<ZeroOrderTerm*>((terms[myRank][i])))->getC(elInfo, 1, &c);
704
705
    }

706
    c *= elInfo->getDet();
707
708

    if (symmetric) {
709
      for (int i = 0; i < nRow; i++) {
710
	(*mat)[i][i] += c * q00->getValue(i,i);
711
	for (int j = i + 1; j < nCol; j++) {
712
	  double val = c * q00->getValue(i, j);
713
714
715
716
717
	  (*mat)[i][j] += val;
	  (*mat)[j][i] += val;
	}
      }
    } else {
718
719
      for (int i = 0; i < nRow; i++)
	for (int j = 0; j < nCol; j++)
720
	  (*mat)[i][j] += c * q00->getValue(i, j);
721
722
723
724
725
    }
  }

  void Pre0::calculateElementVector(const ElInfo *elInfo, ElementVector *vec)
  {
726
    if (firstCall) {
727
728
729
730
731
732
733
734
735
736
      q00 = Q00PsiPhi::provideQ00PsiPhi(owner->getRowFESpace()->getBasisFcts(), 
					owner->getColFESpace()->getBasisFcts(), 
					quadrature);
      q0 = Q0Psi::provideQ0Psi(owner->getRowFESpace()->getBasisFcts(),
			       quadrature);
      firstCall = false;
    }

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

737
    int myRank = omp_get_thread_num();
738
    double c = 0.0;
739
    for (termIt = terms[myRank].begin(); termIt != terms[myRank].end(); ++termIt) {
740
      (static_cast<ZeroOrderTerm*>( *termIt))->getC(elInfo, 1, &c);
741
742
    }

743
    c *= elInfo->getDet();
744

745
    for (int i = 0; i < nRow; i++)
746
      (*vec)[i] += c * q0->getValue(i);
747
748
749
750
751
752
753
754
755
  }

  Stand10::Stand10(Operator *op, Assembler *assembler, Quadrature *quad) 
    : FirstOrderAssembler(op, assembler, quad, false, GRD_PSI)
  {}


  void Stand10::calculateElementMatrix(const ElInfo *elInfo, ElementMatrix *mat)
  {
756
    DimVec<double> grdPsi(dim, DEFAULT_VALUE, 0.0);
757
758
759
760
761
762
763
    double *phival = GET_MEMORY(double, nCol);

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

    int numPoints = quadrature->getNumPoints();

764
    VectorOfFixVecs<DimVec<double> > Lb(dim, numPoints, NO_INIT);
765
    for (int iq = 0; iq < numPoints; iq++) {
766
767
      Lb[iq].set(0.0);
    }
768
769
770
771

    int myRank = omp_get_thread_num();
    for (int i = 0; i < static_cast<int>(terms[myRank].size()); i++) {
      (static_cast<FirstOrderTerm*>((terms[myRank][i])))->getLb(elInfo, numPoints, Lb);
772
773
    }
  
774
    for (int iq = 0; iq < numPoints; iq++) {
775
776
      Lb[iq] *= elInfo->getDet();

777
      for (int i = 0; i < nCol; i++) {
778
779
780
	phival[i] = (*(phi->getPhi(i)))(quadrature->getLambda(iq));
      }

781
      for (int i = 0; i < nRow; i++) {
782
	(*(psi->getGrdPhi(i)))(quadrature->getLambda(iq), grdPsi);
783
784
	for (int j = 0; j < nCol; j++) {
	  (*mat)[i][j] += quadrature->getWeight(iq) * (Lb[iq] * grdPsi) * phival[j];
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
	}
      }
    }
    FREE_MEMORY(phival, double, nCol);
  }


  Quad10::Quad10(Operator *op, Assembler *assembler, Quadrature *quad) 
    : FirstOrderAssembler(op, assembler, quad, true, GRD_PSI)
  {
  }


  void Quad10::calculateElementMatrix(const ElInfo *elInfo, ElementMatrix *mat)
  {
    VectorOfFixVecs<DimVec<double> > *grdPsi;
    const double *phi;

803
    if (firstCall) {
804
805
806
807
808
809
810
811
812
813
      const BasisFunction *basFcts = owner->getRowFESpace()->getBasisFcts();
      psiFast = updateFastQuadrature(psiFast, basFcts, INIT_GRD_PHI);
      basFcts = owner->getColFESpace()->getBasisFcts();
      phiFast = updateFastQuadrature(phiFast, basFcts, INIT_PHI);
      firstCall = false;
    }

    int numPoints = quadrature->getNumPoints();

    VectorOfFixVecs<DimVec<double> > Lb(dim,numPoints,NO_INIT);
814
    for (int iq = 0; iq < numPoints; iq++) {
815
816
      Lb[iq].set(0.0);
    }
817

818
819
820
    int myRank = omp_get_thread_num();
    for (int i = 0; i < static_cast<int>(terms[myRank].size()); i++) {
      (static_cast<FirstOrderTerm*>((terms[myRank][i])))->getLb(elInfo, numPoints, Lb);
821
822
    }
  
823
    for (int iq = 0; iq < numPoints; iq++) {
824
825
826
827
828
      Lb[iq] *= elInfo->getDet();

      phi    = phiFast->getPhi(iq);
      grdPsi = psiFast->getGradient(iq);

829
830
831
      for (int i = 0; i < nRow; i++) {
	for (int j = 0; j < nCol; j++)
	  (*mat)[i][j] += quadrature->getWeight(iq) * (Lb[iq] * (*grdPsi)[i]) * phi[j];
832
833
834
835
836
837
838
839
840
841
      }
    }
  }


  Pre10::Pre10(Operator *op, Assembler *assembler, Quadrature *quad) 
    : FirstOrderAssembler(op, assembler, quad, true, GRD_PSI)
  {
  }

842

843
844
  void Pre10::calculateElementMatrix(const ElInfo *elInfo, ElementMatrix *mat)
  {
845
    VectorOfFixVecs<DimVec<double> > Lb(dim, 1, NO_INIT);
846
847
848
    const int *k;
    const double *values;

849
    if (firstCall) {
850
851
852
853
854
855
856
857
858
      q10 = Q10PsiPhi::provideQ10PsiPhi(owner->getRowFESpace()->getBasisFcts(), 
					owner->getColFESpace()->getBasisFcts(), 
					quadrature);
      q1 = Q1Psi::provideQ1Psi(owner->getRowFESpace()->getBasisFcts(),
			       quadrature);
      firstCall = false;
    }

    const int **nEntries = q10->getNumberEntries();
859
    int myRank = omp_get_thread_num();
860
    Lb[0].set(0.0);
861
862
863

    for (int i = 0; i < static_cast<int>( terms[myRank].size()); i++) {
      (static_cast<FirstOrderTerm*>((terms[myRank][i])))->getLb(elInfo, 1, Lb);
864
865
866
867
    }

    Lb[0] *= elInfo->getDet();

868
869
870
    for (int i = 0; i < nRow; i++) {
      for (int j = 0; j < nCol; j++) {
	k = q10->getKVec(i, j);
871
	values = q10->getValVec(i, j);
872
	double val = 0.0;
Thomas Witkowski's avatar
Thomas Witkowski committed
873
874
875
	for (int m = 0; m < nEntries[i][j]; m++) {
	  val += values[m] * Lb[0][k[m]];
	}
876
877
878
879
880
881
882
883
884
885
	(*mat)[i][j] += val;
      }
    }
  }


  Stand01::Stand01(Operator *op, Assembler *assembler, Quadrature *quad) 
    : FirstOrderAssembler(op, assembler, quad, false, GRD_PHI)
  {}

886

887
888
889
890
891
892
893
894
  void Stand01::calculateElementMatrix(const ElInfo *elInfo, ElementMatrix *mat)
  {
    VectorOfFixVecs<DimVec<double> > grdPhi(dim, nCol, NO_INIT);

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

    int numPoints = quadrature->getNumPoints();
895
896
    VectorOfFixVecs<DimVec<double> > Lb(dim, numPoints, NO_INIT);
    int myRank = omp_get_thread_num();
897

898
    for (int iq = 0; iq < numPoints; iq++) {
899
900
      Lb[iq].set(0.0);
    }
901
902
    for (int i = 0; i < static_cast<int>(terms[myRank].size()); i++) {
      (static_cast<FirstOrderTerm*>((terms[myRank][i])))->getLb(elInfo, numPoints, Lb);
903
904
    }
  
905
    for (int iq = 0; iq < numPoints; iq++) {
906
907
      Lb[iq] *= elInfo->getDet();

908
      for (int i = 0; i < nCol; i++) {
909
	(*(phi->getGrdPhi(i)))(quadrature->getLambda(iq), grdPhi[i]);
910
911
      }

912
      for (int i = 0; i < nRow; i++) {
913
	double psival = (*(psi->getPhi(i)))(quadrature->getLambda(iq));
914
915
	for (int j = 0; j < nCol; j++)
	  (*mat)[i][j] += quadrature->getWeight(iq) * ((Lb[iq] * psival) * grdPhi[j]);
916
      }
917
    } 
918
919
920
921
  }

  void Stand10::calculateElementVector(const ElInfo *elInfo, ElementVector *vec)
  {
922
    DimVec<double> grdPsi(dim, DEFAULT_VALUE, 0.0);
923
924
925
    const BasisFunction *psi = owner->getRowFESpace()->getBasisFcts();
    int numPoints = quadrature->getNumPoints();
    VectorOfFixVecs<DimVec<double> > Lb(dim,numPoints,NO_INIT);
926
    int myRank = omp_get_thread_num();
927

928
    for (int iq = 0; iq < numPoints; iq++) {
929
930
      Lb[iq].set(0.0);
    }
931
932
    for (int i = 0; i < static_cast<int>(terms[myRank].size()); i++) {
      (static_cast<FirstOrderTerm*>((terms[myRank][i])))->getLb(elInfo, numPoints, Lb);
933
934
    }
  
935
    for (int iq = 0; iq < numPoints; iq++) {
936
937
      Lb[iq] *= elInfo->getDet();

938
      for (int i = 0; i < nRow; i++) {
939
	(*(psi->getGrdPhi(i)))(quadrature->getLambda(iq), grdPsi);
940
941
942
943
944
945
946
947
948
949
950
951
952
953
	(*vec)[i] += quadrature->getWeight(iq) * (Lb[iq] * grdPsi);
      }
    }
  }

  Quad01::Quad01(Operator *op, Assembler *assembler, Quadrature *quad) 
    : FirstOrderAssembler(op, assembler, quad, true, GRD_PHI)
  {
  }

  void Quad01::calculateElementMatrix(const ElInfo *elInfo, ElementMatrix *mat)
  {
    VectorOfFixVecs<DimVec<double> > *grdPhi;

954
    if (firstCall) {
955
956
957
958
959
960
961
962
963
      const BasisFunction *basFcts = owner->getRowFESpace()->getBasisFcts();
      psiFast = updateFastQuadrature(psiFast, basFcts, INIT_PHI);
      basFcts = owner->getColFESpace()->getBasisFcts();
      phiFast = updateFastQuadrature(phiFast, basFcts, INIT_GRD_PHI);
      firstCall = false;
    }

    int numPoints = quadrature->getNumPoints();
    VectorOfFixVecs<DimVec<double> > Lb(dim,numPoints,NO_INIT);
964
    int myRank = omp_get_thread_num();
965

966
    for (int iq = 0; iq < numPoints; iq++) {
967
968
      Lb[iq].set(0.0);
    }
969
970
    for (int i = 0; i < static_cast<int>(terms[myRank].size()); i++) {
      (static_cast<FirstOrderTerm*>((terms[myRank][i])))->getLb(elInfo, numPoints, Lb);
971
972
    }
  
973
    for (int iq = 0; iq < numPoints; iq++) {
974
975
      Lb[iq] *= elInfo->getDet();

976
      const double *psi = psiFast->getPhi(iq);
977
978
      grdPhi = phiFast->getGradient(iq);

979
980
981
      for (int i = 0; i < nRow; i++) {
	for (int j = 0; j < nCol; j++)
	  (*mat)[i][j] += quadrature->getWeight(iq) * (Lb[iq] * (*grdPhi)[j]) * psi[i];
982
983
984
985
986
987
988
989
      }
    }
  }

  void Quad10::calculateElementVector(const ElInfo *elInfo, ElementVector *vec)
  {
    VectorOfFixVecs<DimVec<double> > *grdPsi;

990
    if (firstCall) {
991
992
993
994
995
996
997
998
999
      const BasisFunction *basFcts = owner->getRowFESpace()->getBasisFcts();
      psiFast = updateFastQuadrature(psiFast, basFcts, INIT_GRD_PHI);
      basFcts = owner->getColFESpace()->getBasisFcts();
      phiFast = updateFastQuadrature(phiFast, basFcts, INIT_PHI);
      firstCall = false;
    }

    int numPoints = quadrature->getNumPoints();
    VectorOfFixVecs<DimVec<double> > Lb(dim,numPoints,NO_INIT);
1000
    int myRank = omp_get_thread_num();
1001

1002
    for (int iq = 0; iq < numPoints; iq++) {
1003
1004
      Lb[iq].set(0.0);
    }
1005
1006
    for (int i = 0; i < static_cast<int>(terms[myRank].size()); i++) {
      (static_cast<FirstOrderTerm*>((terms[myRank][i])))->getLb(elInfo, numPoints, Lb);
1007
1008
    }
  
1009
    for (int iq = 0; iq < numPoints; iq++) {
1010
1011
1012
1013

      Lb[iq] *= elInfo->getDet();
      grdPsi = psiFast->getGradient(iq);

1014
      for (int i = 0; i < nRow; i++) {
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
	(*vec)[i] += quadrature->getWeight(iq) * (Lb[iq] * (*grdPsi)[i]);
      }
    }
  }

  Pre01::Pre01(Operator *op, Assembler *assembler, Quadrature *quad) 
    : FirstOrderAssembler(op, assembler, quad, true, GRD_PHI)
  {
  }

  void Pre01::calculateElementMatrix(const ElInfo *elInfo, ElementMatrix *mat)
  {
    VectorOfFixVecs<DimVec<double> > Lb(dim,1,NO_INIT);

    const int *l;
    const double *values;

1032
    if (firstCall) {
1033
1034
1035
1036
1037
1038
1039
1040
1041
      q01 = Q01PsiPhi::provideQ01PsiPhi(owner->getRowFESpace()->getBasisFcts(), 
					owner->getColFESpace()->getBasisFcts(), 
					quadrature);
      q1 = Q1Psi::provideQ1Psi(owner->getRowFESpace()->getBasisFcts(),
			       quadrature);
      firstCall = false;
    }

    const int **nEntries = q01->getNumberEntries();
1042
    int myRank = omp_get_thread_num();
1043
    Lb[0].set(0.0);
1044
1045
1046

    for (int i = 0; i < static_cast<int>( terms[myRank].size()); i++) {
      (static_cast<FirstOrderTerm*>((terms[myRank][i])))->getLb(elInfo, 1, Lb);
1047
1048
1049
1050
    }

    Lb[0] *= elInfo->getDet();

1051
1052
1053
    for (int i = 0; i < nRow; i++) {
      for (int j = 0; j < nCol; j++) {
	l = q01->getLVec(i, j);
1054
	values = q01->getValVec(i, j);
1055
	double val = 0.0;
Thomas Witkowski's avatar
Thomas Witkowski committed
1056
1057
1058
	for (int m = 0; m < nEntries[i][j]; m++) {
	  val += values[m] * Lb[0][l[m]];
	}
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
	(*mat)[i][j] += val;
      }
    }
  }

  void Pre10::calculateElementVector(const ElInfo *elInfo, ElementVector *vec)
  {
    VectorOfFixVecs<DimVec<double> > Lb(dim,1,NO_INIT);

    const int *k;
    const double *values;

Thomas Witkowski's avatar
Thomas Witkowski committed
1071
    if (firstCall) {
1072
1073
1074
1075
1076
1077
1078
1079
1080
      q10 = Q10PsiPhi::provideQ10PsiPhi(owner->getRowFESpace()->getBasisFcts(), 
					owner->getColFESpace()->getBasisFcts(), 
					quadrature);
      q1 = Q1Psi::provideQ1Psi(owner->getRowFESpace()->getBasisFcts(),
			       quadrature);
      firstCall = false;
    }

    const int *nEntries = q1->getNumberEntries();
1081
    int myRank = omp_get_thread_num();
1082
    Lb[0].set(0.0);
1083
1084
1085

    for (int i = 0; i < static_cast<int>(terms[myRank].size()); i++) {
      (static_cast<FirstOrderTerm*>(terms[myRank][i]))->getLb(elInfo, 1, Lb);
1086
1087
1088
1089
    }

    Lb[0] *= elInfo->getDet();

Thomas Witkowski's avatar
Thomas Witkowski committed
1090
1091
    for (int i = 0; i < nRow; i++) {
      k = q1->getKVec(i);
1092
      values = q1->getValVec(i);
1093
      double val = 0.0;
Thomas Witkowski's avatar
Thomas Witkowski committed
1094
1095
1096
      for (int m = 0; m < nEntries[i]; m++) {
	val += values[m] * Lb[0][k[m]];
      }
1097
1098
1099
1100
1101
1102
      (*vec)[i] += val;
    }
  }

  Pre2::Pre2(Operator *op, Assembler *assembler, Quadrature *quad) 
    : SecondOrderAssembler(op, assembler, quad, true)
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
  {
    q11 = Q11PsiPhi::provideQ11PsiPhi(owner->getRowFESpace()->getBasisFcts(), 
				      owner->getColFESpace()->getBasisFcts(), 
				      quadrature);
    tmpLALt.resize(omp_get_max_threads());
    for (int i = 0; i < omp_get_max_threads(); i++) {
      tmpLALt[i] = NEW DimMat<double>*;
      *(tmpLALt[i]) = NEW DimMat<double>(dim, NO_INIT);
    }
  }

  Pre2::~Pre2()
  {
    for (int i = 0; i < static_cast<int>(tmpLALt.size()); i++) {
      DELETE *(tmpLALt[i]);
      DELETE tmpLALt[i];
    }
  }
1121
1122
1123
1124
1125
1126
1127

  void Pre2::calculateElementMatrix(const ElInfo *elInfo, ElementMatrix *mat)
  {
    const int **nEntries;
    const int *k, *l;
    const double *values;

1128
    int myRank = omp_get_thread_num();