Assembler.cc 44.2 KB
Newer Older
1
2
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
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "Assembler.h"
#include "Operator.h"
#include "Element.h"
#include "QPsiPhi.h"
#include "ElementMatrix.h"
#include "ElementVector.h"
#include "DOFVector.h"
#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),
      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();

46
    switch (order) {
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
    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;
Thomas Witkowski's avatar
Thomas Witkowski committed
63
    for (int i = 0; i < static_cast<int>(terms.size()); i++) {
64
      if (!(terms[i])->isSymmetric()) {
65
66
67
68
69
70
71
72
73
74
75
76
	symmetric = false;
	break;
      }
    }  

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

  FastQuadrature *SubAssembler::updateFastQuadrature(FastQuadrature *quadFast,
						     const BasisFunction *psi,
						     Flag updateFlag)
  {
77
78
79
80
    if (!quadFast) {
      quadFast = FastQuadrature::provideFastQuadrature(psi,
						       *quadrature,
						       updateFlag);
81
    } else {
82
      if (!quadFast->initialized(updateFlag))
83
84
85
86
87
88
89
90
91
92
93
94
95
96
	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;
97
    for (it1 = valuesAtQPs.begin(); it1 != valuesAtQPs.end(); ++it1) {
98
99
100
101
102
      ((*it1).second)->valid = false;
    }

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

    // calls initElement of each term
    ::std::vector<OperatorTerm*>::iterator it;
109
    for (it = terms.begin(); it != terms.end(); ++it) {
110
111
112
113
114
115
116
117
118
119
120
121
      (*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 ?
122
    if (coordsValid) {
123
124
125
126
      return coordsAtQPs;
    }
   
    // not yet calcualted !
127
128
    if (coordsAtQPs) 
      DELETE [] coordsAtQPs;
129
130
131
132
133
134
    // allocate memory
    coordsAtQPs = NEW WorldVector<double>[numPoints];

    // set new values
    WorldVector<double>* k = NULL;
    int l;
135
    for (k = &(coordsAtQPs[0]), l = 0; k < &(coordsAtQPs[numPoints]); ++k, ++l) {
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
      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();

    TEST_EXIT(vec)("no dof vector!\n");

155
    if (valuesAtQPs[vec] && valuesAtQPs[vec]->valid) 
156
157
158
159
      return valuesAtQPs[vec]->values.getValArray();

    Quadrature *localQuad = quad ? quad : quadrature;

160
    if (!valuesAtQPs[vec]) {
161
162
163
164
165
166
167
168
169
170
      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);

171
    if (opt && !quad && sameFESpaces) {
172
173
      const BasisFunction *psi = owner->rowFESpace->getBasisFcts();
      const BasisFunction *phi = owner->colFESpace->getBasisFcts();
174
      if (vec->getFESpace()->getBasisFcts() == psi) {
175
176
177
178
179
180
181
182
183
	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();

184
185
    if (opt && !quad && sameFESpaces) {
      if (psiFast->getBasisFunctions() == basFcts) {
186
187
188
189
190
191
192
193
194
	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
195
  
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
    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();

    TEST_EXIT(vec)("no dof vector!\n");

211
    if (gradientsAtQPs[vec] && gradientsAtQPs[vec]->valid) 
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
      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);

230
231
    if (opt && !quad && sameFESpaces) {
      if (vec->getFESpace()->getBasisFcts() == psi) {
232
233
234
235
236
237
238
239
240
	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();

241
    if (opt && !quad && sameFESpaces) {
242
243
244
245
246
247
248
249
      if(psiFast->getBasisFunctions() == basFcts) {
	vec->getGrdAtQPs(elInfo, NULL, psiFast, values);
      } else {
	vec->getGrdAtQPs(elInfo, NULL, phiFast, values);
      }
    } else {
      vec->getGrdAtQPs(elInfo, localQuad, NULL, values);
    }
250
   
251
252
253
254
255
256
257
258
259
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
    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
285
    if (op->zeroOrder.size() == 0) {
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
      return NULL;
    }

    ZeroOrderAssembler *newAssembler;

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

    ::std::vector<OperatorTerm*> opTerms  = op->zeroOrder;

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

    // check if a new assembler is needed
301
302
    if (quad) {
      for (int i = 0; i < static_cast<int>( subAssemblers->size()); i++) {
303
304
305
306
	::std::vector<OperatorTerm*> assTerms = *((*subAssemblers)[i]->getTerms());

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

307
308
309
310
311
	if ((opTerms == assTerms) && 
	    ((*subAssemblers)[i]->getQuadrature() == quad)) {
	
	  return dynamic_cast<ZeroOrderAssembler*>((*subAssemblers)[i]);
	}
312
313
314
315
316
      }
    }
  
    // check if all terms are pw_const
    bool pwConst = true;
317
318
    for (int i = 0; i < static_cast<int>( op->zeroOrder.size()); i++) {
      if (!op->zeroOrder[i]->isPWConst()) {
319
320
321
322
323
324
	pwConst = false;
	break;
      }
    }  

    // create new assembler
325
    if (!optimized) {
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
      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);

    ::std::vector<OperatorTerm*> opTerms 
	= (type == GRD_PSI) ? op->firstOrderGrdPsi : op->firstOrderGrdPhi;

    // check if a assembler is needed at all
359
    if (opTerms.size() == 0) {
360
361
362
363
364
365
366
367
      return NULL;
    }

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

    FirstOrderAssembler *newAssembler;

    // check if a new assembler is needed
368
    for (int i = 0; i < static_cast<int>( subAssemblers->size()); i++) {
369
370
371

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

374
375
      if ((opTerms == assTerms) && 
	  ((*subAssemblers)[i]->getQuadrature() == quad)) {
376

377
378
	return dynamic_cast<FirstOrderAssembler*>((*subAssemblers)[i]);
      }
379
380
381
382
    }

    // check if all terms are pw_const
    bool pwConst = true;
383
384
    for (int i = 0; i < static_cast<int>( opTerms.size()); i++) {
      if (!(opTerms[i])->isPWConst()) {
385
386
387
388
389
390
	pwConst = false;
	break;
      }
    }  

    // create new assembler
391
    if (!optimized) {
392
393
394
395
396
      newAssembler = 
	(type == GRD_PSI) ?
	dynamic_cast<FirstOrderAssembler*>(NEW Stand10(op, assembler, quad)) :
	dynamic_cast<FirstOrderAssembler*>(NEW Stand01(op, assembler, quad));    
    } else {
397
      if (pwConst) {
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
	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)
  {
    // check if a assembler is needed at all
    if(op->secondOrder.size() == 0) {
      return NULL;
    }

    SecondOrderAssembler *newAssembler;

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

    ::std::vector<OperatorTerm*> opTerms  = op->zeroOrder;

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

    // check if a new assembler is needed
437
    for (int i = 0; i < static_cast<int>( subAssemblers->size()); i++) {
438
439
440
441
      ::std::vector<OperatorTerm*> assTerms = *((*subAssemblers)[i]->getTerms());
    
      sort(assTerms.begin(), assTerms.end());

442
443
444
445
446
      if ((opTerms == assTerms) && 
	  ((*subAssemblers)[i]->getQuadrature() == quad)) {
	
	return dynamic_cast<SecondOrderAssembler*>((*subAssemblers)[i]);
      }
447
448
449
450
    }

    // check if all terms are pw_const
    bool pwConst = true;
451
452
    for (int i = 0; i < static_cast<int>( op->secondOrder.size()); i++) {
      if (!op->secondOrder[i]->isPWConst()) {
453
454
455
456
457
458
	pwConst = false;
	break;
      }
    }  

    // create new assembler
459
    if (!optimized) {
460
461
      newAssembler = NEW Stand2(op, assembler, quad);
    } else {
462
      if (pwConst) {
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
	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
491

492
    for (int iq = 0; iq < numPoints; iq++) {
493
494
495
496
      c[iq] = 0.0;
    }

    ::std::vector<OperatorTerm*>::iterator termIt;
497
    for (termIt = terms.begin(); termIt != terms.end(); ++termIt) {
498
499
500
501
      (static_cast<ZeroOrderTerm*>((*termIt)))->getC(elInfo, numPoints, c);
    }
      
    if (symmetric) {
502
      for (int iq = 0; iq < numPoints; iq++) {
503
504
505
	c[iq] *= elInfo->getDet();

	// calculate phi at QPs only once!
506
	for (int i = 0; i < nCol; i++) {
507
508
509
	  phival[i] = (*(phi->getPhi(i)))(quadrature->getLambda(iq));
	}

510
	for (int i = 0; i < nRow; i++) {
511
	  psival = (*(psi->getPhi(i)))(quadrature->getLambda(iq));
Thomas Witkowski's avatar
Thomas Witkowski committed
512
	  (*mat)[i][i] += quadrature->getWeight(iq) * c[iq] * psival * phival[i];
513
	  for (int j = i + 1; j < nCol; j++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
514
	    val = quadrature->getWeight(iq) * c[iq] * psival * phival[j];
515
516
517
518
519
	    (*mat)[i][j] += val;
	    (*mat)[j][i] += val;
	  }
	}
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
520
    } else {      //  non symmetric assembling 
521
      for (int iq = 0; iq < numPoints; iq++) {
522
523
524
	c[iq] *= elInfo->getDet();

	// calculate phi at QPs only once!
525
	for (int i = 0; i < nCol; i++) {
526
527
528
	  phival[i] = (*(phi->getPhi(i)))(quadrature->getLambda(iq));
	}

529
	for (int i = 0; i < nRow; i++) {
530
	  psival = (*(psi->getPhi(i)))(quadrature->getLambda(iq));
531
	  for (int j = 0; j < nCol; j++) {
532
533
534
535
536
	    (*mat)[i][j] += quadrature->getWeight(iq)*c[iq]*psival*phival[j];
	  }
	}
      }
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
537

538
    FREE_MEMORY(phival, double, nCol);
Thomas Witkowski's avatar
Thomas Witkowski committed
539
    FREE_MEMORY(c, double, numPoints);
540
541
542
543
544
545
546
  }

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

    double *c = GET_MEMORY(double, numPoints);
547
    for (int iq = 0; iq < numPoints; iq++) {
548
549
550
551
      c[iq] = 0.0;
    }

    ::std::vector<OperatorTerm*>::iterator termIt;
552
    for (termIt = terms.begin(); termIt != terms.end(); ++termIt) {
553
554
555
      (static_cast<ZeroOrderTerm*>((*termIt)))->getC(elInfo, numPoints, c);
    }

556
    for (int iq = 0; iq < numPoints; iq++) {
557
558
      c[iq] *= elInfo->getDet();

559
560
      for (int i = 0; i < nRow; i++) {
	double psi = (*(owner->getRowFESpace()->getBasisFcts()->getPhi(i)))
561
	  (quadrature->getLambda(iq));
562
	(*vec)[i] += quadrature->getWeight(iq) * c[iq] * psi;
563
564
565
566
567
568
569
570
571
572
573
574
575
576
      }
    }
    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;

577
    if (firstCall) {
578
579
580
581
582
583
584
585
586
587
      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);
588
    for (int iq = 0; iq < numPoints; iq++) {
589
590
591
592
      c[iq] = 0.0;
    }

    ::std::vector<OperatorTerm*>::iterator termIt;
593
    for (termIt = terms.begin(); termIt != terms.end(); ++termIt) {
594
595
596
597
      (static_cast<ZeroOrderTerm*>((*termIt)))->getC(elInfo, numPoints, c);
    }

    if (symmetric) {
598
      for (int iq = 0; iq < numPoints; iq++) {
599
600
601
602
	c[iq] *= elInfo->getDet();

	psi = psiFast->getPhi(iq);
	phi = phiFast->getPhi(iq);
603
604
605
606
	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];
607
608
609
610
611
612
	    (*mat)[i][j] += val;
	    (*mat)[j][i] += val;
	  }
	}
      }
    } else {      /*  non symmetric assembling   */
613
      for (int iq = 0; iq < numPoints; iq++) {
614
615
616
617
	c[iq] *= elInfo->getDet();

	psi = psiFast->getPhi(iq);
	phi = phiFast->getPhi(iq);
618
619
620
	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];
621
622
623
624
625
626
627
628
629
	  }
	}
      }
    }
    FREE_MEMORY(c, double, numPoints);
  }

  void Quad0::calculateElementVector(const ElInfo *elInfo, ElementVector *vec)
  {
630
    if (firstCall) {
631
632
633
634
635
636
637
638
639
640
      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);
641
    for (int iq = 0; iq < numPoints; iq++) {
642
643
644
645
      c[iq] = 0.0;
    }

    ::std::vector<OperatorTerm*>::iterator termIt;
646
    for (termIt = terms.begin(); termIt != terms.end(); ++termIt) {
647
648
649
      (static_cast<ZeroOrderTerm*>((*termIt)))->getC(elInfo, numPoints, c);
    }

650
    for (int iq = 0; iq < numPoints; iq++) {
651
652
      c[iq] *= elInfo->getDet();

653
654
655
      const double *psi = psiFast->getPhi(iq);
      for (int i = 0; i < nRow; i++) {
	(*vec)[i] += quadrature->getWeight(iq) * c[iq] * psi[i];
656
657
658
659
660
661
662
663
664
665
666
667
      }
    }
    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)
  {
668
    double *c = GET_MEMORY(double, 1);
669

670
    if (firstCall) {
671
672
673
674
675
676
677
678
679
      q00 = Q00PsiPhi::provideQ00PsiPhi(owner->getRowFESpace()->getBasisFcts(), 
					owner->getColFESpace()->getBasisFcts(), 
					quadrature);
      q0 = Q0Psi::provideQ0Psi(owner->getRowFESpace()->getBasisFcts(),
			       quadrature);
      firstCall = false;
    }

    c[0] = 0.0;
680
    for (int i = 0; i < static_cast<int>( terms.size()); i++) {
681
682
683
684
685
686
      (static_cast<ZeroOrderTerm*>((terms[i])))->getC(elInfo, 1, c);
    }

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

    if (symmetric) {
687
688
689
690
      for (int i = 0; i < nRow; i++) {
	(*mat)[i][i] += c[0] * q00->getValue(i,i);
	for (int j = i + 1; j < nCol; j++) {
	  double val = c[0] * q00->getValue(i, j);
691
692
693
694
695
	  (*mat)[i][j] += val;
	  (*mat)[j][i] += val;
	}
      }
    } else {
696
697
      for (int i = 0; i < nRow; i++)
	for (int j = 0; j < nCol; j++)
698
699
700
701
702
703
704
705
706
707
	  (*mat)[i][j] += c[0]*q00->getValue(i,j);
    }

    FREE_MEMORY(c, double, 1);
  }

  void Pre0::calculateElementVector(const ElInfo *elInfo, ElementVector *vec)
  {
    double *c = GET_MEMORY(double, 1);;

708
    if (firstCall) {
709
710
711
712
713
714
715
716
717
718
719
      q00 = Q00PsiPhi::provideQ00PsiPhi(owner->getRowFESpace()->getBasisFcts(), 
					owner->getColFESpace()->getBasisFcts(), 
					quadrature);
      q0 = Q0Psi::provideQ0Psi(owner->getRowFESpace()->getBasisFcts(),
			       quadrature);
      firstCall = false;
    }

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

    c[0] = 0.0;
720
    for (termIt = terms.begin(); termIt != terms.end(); ++termIt) {
721
722
723
724
725
      (static_cast<ZeroOrderTerm*>( *termIt))->getC(elInfo, 1, c);
    }

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

726
    for (int i = 0; i < nRow; i++)
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
      (*vec)[i] += c[0] * q0->getValue(i);

    FREE_MEMORY(c, double, 1);
  }

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


  void Stand10::calculateElementMatrix(const ElInfo *elInfo, ElementMatrix *mat)
  {
    DimVec<double> grdPsi(dim, NO_INIT);
    double *phival = GET_MEMORY(double, nCol);

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

    int numPoints = quadrature->getNumPoints();

    VectorOfFixVecs<DimVec<double> > Lb(dim,numPoints,NO_INIT);
748
    for (int iq = 0; iq < numPoints; iq++) {
749
750
      Lb[iq].set(0.0);
    }
751
    for (int i = 0; i < static_cast<int>(terms.size()); i++) {
752
753
754
      (static_cast<FirstOrderTerm*>((terms[i])))->getLb(elInfo, numPoints, Lb);
    }
  
755
    for (int iq = 0; iq < numPoints; iq++) {
756
757
      Lb[iq] *= elInfo->getDet();

758
      for (int i = 0; i < nCol; i++) {
759
760
761
	phival[i] = (*(phi->getPhi(i)))(quadrature->getLambda(iq));
      }

762
      for (int i = 0; i < nRow; i++) {
763
	grdPsi = (*(psi->getGrdPhi(i)))(quadrature->getLambda(iq));
764
765
	for (int j = 0; j < nCol; j++) {
	  (*mat)[i][j] += quadrature->getWeight(iq) * (Lb[iq] * grdPsi) * phival[j];
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
	}
      }
    }
    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;

784
    if (firstCall) {
785
786
787
788
789
790
791
792
793
794
      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);
795
    for (int iq = 0; iq < numPoints; iq++) {
796
797
      Lb[iq].set(0.0);
    }
798
799

    for (int i = 0; i < static_cast<int>(terms.size()); i++) {
800
801
802
      (static_cast<FirstOrderTerm*>((terms[i])))->getLb(elInfo, numPoints, Lb);
    }
  
803
    for (int iq = 0; iq < numPoints; iq++) {
804
805
806
807
808
      Lb[iq] *= elInfo->getDet();

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

809
810
811
      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];
812
813
814
815
816
817
818
819
820
821
      }
    }
  }


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

822

823
824
825
826
827
828
829
  void Pre10::calculateElementMatrix(const ElInfo *elInfo, ElementMatrix *mat)
  {
    VectorOfFixVecs<DimVec<double> > Lb(dim,1,NO_INIT);
    const int *k;
    const double *values;
    double val;

830
    if (firstCall) {
831
832
833
834
835
836
837
838
839
840
841
      q10 = Q10PsiPhi::provideQ10PsiPhi(owner->getRowFESpace()->getBasisFcts(), 
					owner->getColFESpace()->getBasisFcts(), 
					quadrature);
      q1 = Q1Psi::provideQ1Psi(owner->getRowFESpace()->getBasisFcts(),
			       quadrature);
      firstCall = false;
    }

    const int **nEntries = q10->getNumberEntries();

    Lb[0].set(0.0);
842
    for (int i = 0; i < static_cast<int>( terms.size()); i++) {
843
844
845
846
847
      (static_cast<FirstOrderTerm*>((terms[i])))->getLb(elInfo, 1, Lb);
    }

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

848
849
850
    for (int i = 0; i < nRow; i++) {
      for (int j = 0; j < nCol; j++) {
	k = q10->getKVec(i, j);
851
	values = q10->getValVec(i, j);
Thomas Witkowski's avatar
Thomas Witkowski committed
852
853
854
855
	val = 0.0;
	for (int m = 0; m < nEntries[i][j]; m++) {
	  val += values[m] * Lb[0][k[m]];
	}
856
857
858
859
860
861
862
863
864
865
	(*mat)[i][j] += val;
      }
    }
  }


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

866

867
868
869
870
871
872
873
874
875
876
877
878
879
  void Stand01::calculateElementMatrix(const ElInfo *elInfo, ElementMatrix *mat)
  {
    VectorOfFixVecs<DimVec<double> > grdPhi(dim, nCol, NO_INIT);
    double psival;

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

    int numPoints = quadrature->getNumPoints();

    VectorOfFixVecs<DimVec<double> > Lb(dim,numPoints,NO_INIT);
    //  DimVec<double> *Lb = NEW DimVec<double>[numPoints](dim, NO_INIT);

880
    for (int iq = 0; iq < numPoints; iq++) {
881
882
      Lb[iq].set(0.0);
    }
883
    for (int i = 0; i < static_cast<int>(terms.size()); i++) {
884
885
886
      (static_cast<FirstOrderTerm*>((terms[i])))->getLb(elInfo, numPoints, Lb);
    }
  
887
    for (int iq = 0; iq < numPoints; iq++) {
888
889
      Lb[iq] *= elInfo->getDet();

890
      for (int i = 0; i < nCol; i++) {
891
892
893
	grdPhi[i] = (*(phi->getGrdPhi(i)))(quadrature->getLambda(iq));
      }

894
      for (int i = 0; i < nRow; i++) {
895
	psival = (*(psi->getPhi(i)))(quadrature->getLambda(iq));
896
897
	for (int j = 0; j < nCol; j++)
	  (*mat)[i][j] += quadrature->getWeight(iq) * ((Lb[iq] * psival) * grdPhi[j]);
898
      }
899
    } 
900
901
902
903
904
905
906
907
908
909
910
911
  }

  void Stand10::calculateElementVector(const ElInfo *elInfo, ElementVector *vec)
  {
    DimVec<double> grdPsi(dim, NO_INIT);

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

    int numPoints = quadrature->getNumPoints();

    VectorOfFixVecs<DimVec<double> > Lb(dim,numPoints,NO_INIT);

912
    for (int iq = 0; iq < numPoints; iq++) {
913
914
      Lb[iq].set(0.0);
    }
915
    for (int i = 0; i < static_cast<int>(terms.size()); i++) {
916
917
918
      (static_cast<FirstOrderTerm*>((terms[i])))->getLb(elInfo, numPoints, Lb);
    }
  
919
    for (int iq = 0; iq < numPoints; iq++) {
920
921
      Lb[iq] *= elInfo->getDet();

922
      for (int i = 0; i < nRow; i++) {
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
	grdPsi = (*(psi->getGrdPhi(i)))(quadrature->getLambda(iq));
	(*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;

938
    if (firstCall) {
939
940
941
942
943
944
945
946
947
948
949
      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);

950
    for (int iq = 0; iq < numPoints; iq++) {
951
952
      Lb[iq].set(0.0);
    }
953
    for (int i = 0; i < static_cast<int>(terms.size()); i++) {
954
955
956
      (static_cast<FirstOrderTerm*>((terms[i])))->getLb(elInfo, numPoints, Lb);
    }
  
957
    for (int iq = 0; iq < numPoints; iq++) {
958
959
      Lb[iq] *= elInfo->getDet();

960
      const double *psi = psiFast->getPhi(iq);
961
962
      grdPhi = phiFast->getGradient(iq);

963
964
965
      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];
966
967
968
969
970
971
972
973
      }
    }
  }

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

974
    if (firstCall) {
975
976
977
978
979
980
981
982
983
984
985
      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);

986
    for (int iq = 0; iq < numPoints; iq++) {
987
988
      Lb[iq].set(0.0);
    }
989
    for (int i = 0; i < static_cast<int>(terms.size()); i++) {
990
991
992
      (static_cast<FirstOrderTerm*>((terms[i])))->getLb(elInfo, numPoints, Lb);
    }
  
993
    for (int iq = 0; iq < numPoints; iq++) {
994
995
996
997
998

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

      grdPsi = psiFast->getGradient(iq);

999
      for (int i = 0; i < nRow; i++) {
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
	(*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;
    double val;

1018
    if (firstCall) {
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
      q01 = Q01PsiPhi::provideQ01PsiPhi(owner->getRowFESpace()->getBasisFcts(), 
					owner->getColFESpace()->getBasisFcts(), 
					quadrature);
      q1 = Q1Psi::provideQ1Psi(owner->getRowFESpace()->getBasisFcts(),
			       quadrature);
      firstCall = false;
    }

    const int **nEntries = q01->getNumberEntries();

    Lb[0].set(0.0);
1030
    for (int i = 0; i < static_cast<int>( terms.size()); i++) {
1031
1032
1033
1034
1035
      (static_cast<FirstOrderTerm*>((terms[i])))->getLb(elInfo, 1, Lb);
    }

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

1036
1037
1038
    for (int i = 0; i < nRow; i++) {
      for (int j = 0; j < nCol; j++) {
	l = q01->getLVec(i, j);
1039
	values = q01->getValVec(i, j);
Thomas Witkowski's avatar
Thomas Witkowski committed
1040
1041
1042
1043
	val = 0.0;
	for (int m = 0; m < nEntries[i][j]; m++) {
	  val += values[m] * Lb[0][l[m]];
	}
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
	(*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;
    double val;

Thomas Witkowski's avatar
Thomas Witkowski committed
1057
    if (firstCall) {
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
      q10 = Q10PsiPhi::provideQ10PsiPhi(owner->getRowFESpace()->getBasisFcts(), 
					owner->getColFESpace()->getBasisFcts(), 
					quadrature);
      q1 = Q1Psi::provideQ1Psi(owner->getRowFESpace()->getBasisFcts(),
			       quadrature);
      firstCall = false;
    }

    const int *nEntries = q1->getNumberEntries();

    Lb[0].set(0.0);
Thomas Witkowski's avatar
Thomas Witkowski committed
1069
    for (int i = 0; i < static_cast<int>(terms.size()); i++) {
1070
1071
1072
1073
1074
      (static_cast<FirstOrderTerm*>(terms[i]))->getLb(elInfo, 1, Lb);
    }

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

Thomas Witkowski's avatar
Thomas Witkowski committed
1075
1076
    for (int i = 0; i < nRow; i++) {
      k = q1->getKVec(i);
1077
      values = q1->getValVec(i);
Thomas Witkowski's avatar
Thomas Witkowski committed
1078
1079
1080
1081
      val = 0.0;
      for (int m = 0; m < nEntries[i]; m++) {
	val += values[m] * Lb[0][k[m]];
      }
1082
1083
1084
1085
1086
1087
      (*vec)[i] += val;
    }
  }

  Pre2::Pre2(Operator *op, Assembler *assembler, Quadrature *quad) 
    : SecondOrderAssembler(op, assembler, quad, true)
Thomas Witkowski's avatar
Thomas Witkowski committed
1088
  {}
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098

  void Pre2::calculateElementMatrix(const ElInfo *elInfo, ElementMatrix *mat)
  {
    DimMat<double> **LALt = NEW DimMat<double>*;
    *LALt=NEW DimMat<double>(dim, NO_INIT);
    const int **nEntries;
    const int *k, *l;
    const double *values;
    double val;

Thomas Witkowski's avatar
Thomas Witkowski committed
1099
    if (firstCall) {
1100
1101
1102
1103
1104
1105
1106
1107
      q11 = Q11PsiPhi::provideQ11PsiPhi(owner->getRowFESpace()->getBasisFcts(), 
					owner->getColFESpace()->getBasisFcts(), 
					quadrature);
      firstCall = false;
    }

    LALt[0]->set(0.0);

Thomas Witkowski's avatar
Thomas Witkowski committed
1108
    for (int i = 0; i < static_cast<int>( terms.size()); i++) {
1109
1110
1111
1112
1113
1114
1115
1116
      (static_cast<SecondOrderTerm*>(terms[i]))->getLALt(elInfo, 1, LALt);
    }

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

    nEntries = q11->getNumberEntries();

    if (symmetric) {
Thomas Witkowski's avatar
Thomas Witkowski committed
1117
1118
1119
      for (int i = 0; i < nRow; i++) {
	k = q11->getKVec(i, i);
	l = q11->getLVec(i, i);
1120
	values = q11->getValVec(i, i);
Thomas Witkowski's avatar
Thomas Witkowski committed
1121
1122
1123
1124
	val = 0.0;
	for (int m = 0; m < nEntries[i][i]; m++) {
	  val += values[m] * (*LALt[0])[k[m]][l[m]];
	}
1125
	(*mat)[i][i] += val;
Thomas Witkowski's avatar
Thomas Witkowski committed
1126
1127
1128
1129

	for (int j = i + 1; j < nCol; j++) {
	  k = q11->getKVec(i, j);
	  l = q11->getLVec(i, j);
1130
	  values = q11->getValVec(i, j);
Thomas Witkowski's avatar
Thomas Witkowski committed
1131
1132
1133
1134
	  val = 0.0;
	  for (int m = 0; m < nEntries[i][j]; m++) {
	    val += values[m] * (*LALt[0])[k[m]][l[m]];
	  }
1135
1136
1137
1138
	  (*mat)[i][j] += val;
	  (*mat)[j][i] += val;
	}
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
1139
1140
1141
1142
1143
    } 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);
1144
	  values = q11->getValVec(i, j);
Thomas Witkowski's avatar
Thomas Witkowski committed
1145
1146
1147
1148
	  val = 0.0;
	  for (int m = 0; m < nEntries[i][j]; m++) {
	    val += values[m] * (*LALt[0])[k[m]][l[m]];
	  }
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
	  (*mat)[i][j] += val;
	}
      }
    }

    DELETE *LALt;
    DELETE LALt;
  }

  Quad2::Quad2(Operator *op, Assembler *assembler, Quadrature *quad) 
    : SecondOrderAssembler(op, assembler, quad, true)
Thomas Witkowski's avatar
Thomas Witkowski committed
1160
  {}
1161
1162
1163
1164
1165
1166

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

Thomas Witkowski's avatar
Thomas Witkowski committed
1167
    if (firstCall) {
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
      const BasisFunction *basFcts = owner->getRowFESpace()->getBasisFcts();
      psiFast = updateFastQuadrature(psiFast, basFcts, INIT_GRD_PHI);
      basFcts = owner->getColFESpace()->getBasisFcts();
      phiFast = updateFastQuadrature(phiFast, basFcts, INIT_GRD_PHI);
      firstCall = false;
    }

    int nPoints = quadrature->getNumPoints();

    DimMat<double> **LALt = NEW DimMat<double>*[nPoints];
Thomas Witkowski's avatar
Thomas Witkowski committed
1178
1179
1180
1181
    for (int i = 0; i < nPoints;i++) {
      LALt[i] = NEW DimMat<double>(dim, NO_INIT);
    }
    for (int iq = 0; iq < nPoints; iq++) {
1182
1183
      LALt[iq]->set(0.0);
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
1184
    for (int i = 0; i < static_cast<int>(terms.size()); i++) {
1185
1186
1187
1188
      (static_cast<SecondOrderTerm*>(terms[i]))->getLALt(elInfo, nPoints, LALt);
    }

    if (symmetric) {
Thomas Witkowski's avatar
Thomas Witkowski committed
1189
      for (int iq = 0; iq < nPoints; iq++) {
1190
1191
1192
1193
1194
	(*LALt[iq]) *= elInfo->getDet();

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

Thomas Witkowski's avatar
Thomas Witkowski committed
1195
	for (int i = 0; i < nRow; i++) {
1196
1197
1198
	  (*mat)[i][i] += quadrature->getWeight(iq) * 
	    ((*grdPsi)[i] * ((*LALt[iq]) * (*grdPhi)[i]));

Thomas Witkowski's avatar
Thomas Witkowski committed
1199
	  for (int j = i + 1; j < nCol; j++) {
1200
1201
1202
1203
1204
1205
1206
1207
	    val = quadrature->getWeight(iq) * ((*grdPsi)[i] * ((*LALt[iq]) * (*grdPhi)[j]));
	    (*mat)[i][j] += val;
	    (*mat)[j][i] += val;
	  }
	}
      }
    }
    else {      /*  non symmetric assembling   */
Thomas Witkowski's avatar
Thomas Witkowski committed
1208
      for (int iq = 0; iq < nPoints; iq++) {
1209
1210
1211
1212
1213
	(*LALt[iq]) *= elInfo->getDet();

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

Thomas Witkowski's avatar
Thomas Witkowski committed
1214
1215
	for (int i = 0; i < nRow; i++) {
	  for (int j = 0; j < nCol; j++) {
1216
1217
1218
1219
1220
1221
1222
	    (*mat)[i][j] += quadrature->getWeight(iq) *
	      ((*grdPsi)[i] * ((*LALt[iq]) * (*grdPhi)[j]));
	  }
	}
      }
    }
  
Thomas Witkowski's avatar
Thomas Witkowski committed
1223
1224
1225
    for (int i = 0;i < nPoints; i++) 
      DELETE LALt[i];

1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
    DELETE [] LALt; 
  }

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

  void Stand2::calculateElementMatrix(const ElInfo *elInfo, ElementMatrix *mat)
  {
    double val;
    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();

    DimMat<double> **LALt = NEW DimMat<double>*[nPoints];
Thomas Witkowski's avatar
Thomas Witkowski committed
1245
1246
    for (int iq = 0; iq < nPoints; iq++) {
      LALt[iq] = NEW DimMat<double>(dim, NO_INIT);
1247
1248
      LALt[iq]->set(0.0);
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
1249
1250

    for (int i = 0; i < static_cast<int>(terms.size()); i++) {
1251
1252
1253
1254
      (static_cast<SecondOrderTerm*>(terms[i]))->getLALt(elInfo, nPoints, LALt);
    }

    if (symmetric) {
Thomas Witkowski's avatar
Thomas Witkowski committed
1255
      for (int iq = 0; iq < nPoints; iq++) {
1256
1257
	(*LALt[iq]) *= elInfo->getDet();

Thomas Witkowski's avatar
Thomas Witkowski committed
1258
	for (int i = 0; i < nCol; i++) {
1259
1260
1261
	  grdPhi[i] = (*(phi->getGrdPhi(i)))(quadrature->getLambda(iq));
	}

Thomas Witkowski's avatar
Thomas Witkowski committed
1262
	for (int i = 0; i < nRow; i++) {
1263
1264
1265
1266
	  grdPsi = (*(psi->getGrdPhi(i)))(quadrature->getLambda(iq));
	  (*mat)[i][i] += quadrature->getWeight(iq) * 
	    (grdPsi * ((*LALt[iq]) * grdPhi[i]));

Thomas Witkowski's avatar
Thomas Witkowski committed
1267
	  for (int j = i + 1; j < nCol; j++) {
1268
1269
1270
1271
1272
1273
	    val = quadrature->getWeight(iq) * (grdPsi * ((*LALt[iq]) * grdPhi[j]));
	    (*mat)[i][j] += val;
	    (*mat)[j][i] += val;
	  }
	}
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
1274
1275
    } else {      /*  non symmetric assembling   */
      for (int iq = 0; iq < nPoints; iq++) {
1276
1277
	(*LALt[iq]) *= elInfo->getDet();

Thomas Witkowski's avatar
Thomas Witkowski committed
1278
	for (int i = 0; i < nCol; i++) {
1279
1280
1281
	  grdPhi[i] = (*(phi->getGrdPhi(i)))(quadrature->getLambda(iq));
	}

Thomas Witkowski's avatar
Thomas Witkowski committed
1282
	for (int i = 0; i < nRow; i++) {
1283
	  grdPsi = (*(psi->getGrdPhi(i)))(quadrature->getLambda(iq));
Thomas Witkowski's avatar
Thomas Witkowski committed
1284
	  for (int j = 0; j < nCol; j++) {
1285
1286
1287
1288
1289
1290
1291
	    (*mat)[i][j] += quadrature->getWeight(iq) *
	      (grdPsi * ((*LALt[iq]) * grdPhi[j]));
	  }
	}
      }
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
1292
1293
1294
    for (int iq = 0; iq < nPoints; iq++) 
      DELETE LALt[iq];

1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
    DELETE [] LALt;
  }

  Assembler::Assembler(Operator  *op,
		       const FiniteElemSpace *rowFESpace_,
		       const FiniteElemSpace *colFESpace_) 
    : operat(op),
      rowFESpace(rowFESpace_),
      colFESpace(colFESpace_ ? colFESpace_ : rowFESpace_),
      nRow(rowFESpace->getBasisFcts()->getNumber()),
      nCol(colFESpace->getBasisFcts()->getNumber()),
      remember(true),
      rememberElMat(false),
      rememberElVec(false),
      elementMatrix(NULL),
      elementVector(NULL),
      lastMatEl(NULL),
      lastVecEl(NULL),
      lastTraverseId(-1)
  
Thomas Witkowski's avatar
Thomas Witkowski committed
1315
  {}
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331

  void Assembler::calculateElementMatrix(const ElInfo *elInfo, 
					 ElementMatrix *userMat,
					 double factor)
  {
    FUNCNAME("Assembler::calculateElementMatrix()");

    if (remember && ((factor != 1.0) || (operat->uhOld))) {
      rememberElMat = true;
    }
  
    if (rememberElMat && !elementMatrix)
      elementMatrix = NEW ElementMatrix(nRow, nCol);

    Element *el = elInfo->getElement();