Assembler.cc 47.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;
63
64
    for (int i=0; i < static_cast<int>(terms.size()); i++) {
      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
184
185
186
	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();

    //double* uhLoc = GET_MEMORY(double, basFcts->getNumber());
    //vec->getLocalVector(elInfo->getElement(), uhLoc);

187
188
    if (opt && !quad && sameFESpaces) {
      if (psiFast->getBasisFunctions() == basFcts) {
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
	//psiFast->uhAtQp(uhLoc, values);
	vec->getVecAtQPs(elInfo, NULL, psiFast, values);
      } else if(phiFast->getBasisFunctions() == basFcts) {
	//phiFast->uhAtQp(uhLoc, values);
	vec->getVecAtQPs(elInfo, NULL, phiFast, values);
      } else {
	vec->getVecAtQPs(elInfo, localQuad, NULL, values);
      }
    } else {
      //localQuad->uhAtQp(basFcts, uhLoc, values);
      vec->getVecAtQPs(elInfo, localQuad, NULL, values);
    }

    //FREE_MEMORY(uhLoc, double, basFcts->getNumber());
    
    valuesAtQPs[vec]->valid = true;

    // return values
    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");

220
    if (gradientsAtQPs[vec] && gradientsAtQPs[vec]->valid) 
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
      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);

239
240
    if (opt && !quad && sameFESpaces) {
      if (vec->getFESpace()->getBasisFcts() == psi) {
241
242
243
244
245
246
247
248
249
	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();

250
    if (opt && !quad && sameFESpaces) {
251
252
253
254
255
256
257
258
      if(psiFast->getBasisFunctions() == basFcts) {
	vec->getGrdAtQPs(elInfo, NULL, psiFast, values);
      } else {
	vec->getGrdAtQPs(elInfo, NULL, phiFast, values);
      }
    } else {
      vec->getGrdAtQPs(elInfo, localQuad, NULL, values);
    }
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
285
286
287
288
289
290
291
292
293
    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
294
    if (op->zeroOrder.size() == 0) {
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
      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
310
311
    if (quad) {
      for (int i = 0; i < static_cast<int>( subAssemblers->size()); i++) {
312
313
314
315
	::std::vector<OperatorTerm*> assTerms = *((*subAssemblers)[i]->getTerms());

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

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

    // create new assembler
334
    if (!optimized) {
335
336
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
366
367
      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
368
    if (opTerms.size() == 0) {
369
370
371
372
373
374
375
376
      return NULL;
    }

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

    FirstOrderAssembler *newAssembler;

    // check if a new assembler is needed
377
    for (int i = 0; i < static_cast<int>( subAssemblers->size()); i++) {
378
379
380

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

383
384
      if ((opTerms == assTerms) && 
	  ((*subAssemblers)[i]->getQuadrature() == quad)) {
385

386
387
	return dynamic_cast<FirstOrderAssembler*>((*subAssemblers)[i]);
      }
388
389
390
391
    }

    // check if all terms are pw_const
    bool pwConst = true;
392
393
    for (int i = 0; i < static_cast<int>( opTerms.size()); i++) {
      if (!(opTerms[i])->isPWConst()) {
394
395
396
397
398
399
	pwConst = false;
	break;
      }
    }  

    // create new assembler
400
    if (!optimized) {
401
402
403
404
405
      newAssembler = 
	(type == GRD_PSI) ?
	dynamic_cast<FirstOrderAssembler*>(NEW Stand10(op, assembler, quad)) :
	dynamic_cast<FirstOrderAssembler*>(NEW Stand01(op, assembler, quad));    
    } else {
406
      if (pwConst) {
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
437
438
439
440
441
442
443
444
445
	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
446
    for (int i = 0; i < static_cast<int>( subAssemblers->size()); i++) {
447
448
449
450
      ::std::vector<OperatorTerm*> assTerms = *((*subAssemblers)[i]->getTerms());
    
      sort(assTerms.begin(), assTerms.end());

451
452
453
454
455
      if ((opTerms == assTerms) && 
	  ((*subAssemblers)[i]->getQuadrature() == quad)) {
	
	return dynamic_cast<SecondOrderAssembler*>((*subAssemblers)[i]);
      }
456
457
458
459
    }

    // check if all terms are pw_const
    bool pwConst = true;
460
461
    for (int i = 0; i < static_cast<int>( op->secondOrder.size()); i++) {
      if (!op->secondOrder[i]->isPWConst()) {
462
463
464
465
466
467
	pwConst = false;
	break;
      }
    }  

    // create new assembler
468
    if (!optimized) {
469
470
      newAssembler = NEW Stand2(op, assembler, quad);
    } else {
471
      if (pwConst) {
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
	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);
500
    for (int iq = 0; iq < numPoints; iq++) {
501
502
503
504
      c[iq] = 0.0;
    }

    ::std::vector<OperatorTerm*>::iterator termIt;
505
    for (termIt = terms.begin(); termIt != terms.end(); ++termIt) {
506
507
508
509
      (static_cast<ZeroOrderTerm*>((*termIt)))->getC(elInfo, numPoints, c);
    }
      
    if (symmetric) {
510
      for (int iq = 0; iq < numPoints; iq++) {
511
512
513
	c[iq] *= elInfo->getDet();

	// calculate phi at QPs only once!
514
	for (int i = 0; i < nCol; i++) {
515
516
517
	  phival[i] = (*(phi->getPhi(i)))(quadrature->getLambda(iq));
	}

518
	for (int i = 0; i < nRow; i++) {
519
520
	  psival = (*(psi->getPhi(i)))(quadrature->getLambda(iq));
	  (*mat)[i][i] += quadrature->getWeight(iq)*c[iq]*psival*phival[i];
521
	  for (int j = i + 1; j < nCol; j++) {
522
523
524
525
526
527
528
	    val = quadrature->getWeight(iq)*c[iq]*psival*phival[j];
	    (*mat)[i][j] += val;
	    (*mat)[j][i] += val;
	  }
	}
      }
    } else {      /*  non symmetric assembling   */
529
      for (int iq = 0; iq < numPoints; iq++) {
530
531
532
	c[iq] *= elInfo->getDet();

	// calculate phi at QPs only once!
533
	for (int i = 0; i < nCol; i++) {
534
535
536
	  phival[i] = (*(phi->getPhi(i)))(quadrature->getLambda(iq));
	}

537
	for (int i = 0; i < nRow; i++) {
538
	  psival = (*(psi->getPhi(i)))(quadrature->getLambda(iq));
539
	  for (int j = 0; j < nCol; j++) {
540
541
542
543
544
545
546
547
548
549
550
551
552
553
	    (*mat)[i][j] += quadrature->getWeight(iq)*c[iq]*psival*phival[j];
	  }
	}
      }
    }
    FREE_MEMORY(c, double, numPoints);
    FREE_MEMORY(phival, double, nCol);
  }

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

    double *c = GET_MEMORY(double, numPoints);
554
    for (int iq = 0; iq < numPoints; iq++) {
555
556
557
558
      c[iq] = 0.0;
    }

    ::std::vector<OperatorTerm*>::iterator termIt;
559
    for (termIt = terms.begin(); termIt != terms.end(); ++termIt) {
560
561
562
      (static_cast<ZeroOrderTerm*>((*termIt)))->getC(elInfo, numPoints, c);
    }

563
    for (int iq = 0; iq < numPoints; iq++) {
564
565
      c[iq] *= elInfo->getDet();

566
567
      for (int i = 0; i < nRow; i++) {
	double psi = (*(owner->getRowFESpace()->getBasisFcts()->getPhi(i)))
568
	  (quadrature->getLambda(iq));
569
	(*vec)[i] += quadrature->getWeight(iq) * c[iq] * psi;
570
571
572
573
574
575
576
577
578
579
580
581
582
583
      }
    }
    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;

584
    if (firstCall) {
585
586
587
588
589
590
591
592
593
594
      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);
595
    for (int iq = 0; iq < numPoints; iq++) {
596
597
598
599
      c[iq] = 0.0;
    }

    ::std::vector<OperatorTerm*>::iterator termIt;
600
    for (termIt = terms.begin(); termIt != terms.end(); ++termIt) {
601
602
603
604
      (static_cast<ZeroOrderTerm*>((*termIt)))->getC(elInfo, numPoints, c);
    }

    if (symmetric) {
605
      for (int iq = 0; iq < numPoints; iq++) {
606
607
608
609
	c[iq] *= elInfo->getDet();

	psi = psiFast->getPhi(iq);
	phi = phiFast->getPhi(iq);
610
611
612
613
	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];
614
615
616
617
618
619
	    (*mat)[i][j] += val;
	    (*mat)[j][i] += val;
	  }
	}
      }
    } else {      /*  non symmetric assembling   */
620
      for (int iq = 0; iq < numPoints; iq++) {
621
622
623
624
	c[iq] *= elInfo->getDet();

	psi = psiFast->getPhi(iq);
	phi = phiFast->getPhi(iq);
625
626
627
	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];
628
629
630
631
632
633
634
635
636
	  }
	}
      }
    }
    FREE_MEMORY(c, double, numPoints);
  }

  void Quad0::calculateElementVector(const ElInfo *elInfo, ElementVector *vec)
  {
637
    if (firstCall) {
638
639
640
641
642
643
644
645
646
647
      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);
648
    for (int iq = 0; iq < numPoints; iq++) {
649
650
651
652
      c[iq] = 0.0;
    }

    ::std::vector<OperatorTerm*>::iterator termIt;
653
    for (termIt = terms.begin(); termIt != terms.end(); ++termIt) {
654
655
656
      (static_cast<ZeroOrderTerm*>((*termIt)))->getC(elInfo, numPoints, c);
    }

657
    for (int iq = 0; iq < numPoints; iq++) {
658
659
      c[iq] *= elInfo->getDet();

660
661
662
      const double *psi = psiFast->getPhi(iq);
      for (int i = 0; i < nRow; i++) {
	(*vec)[i] += quadrature->getWeight(iq) * c[iq] * psi[i];
663
664
665
666
667
668
669
670
671
672
673
674
      }
    }
    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)
  {
675
    double *c = GET_MEMORY(double, 1);
676

677
    if (firstCall) {
678
679
680
681
682
683
684
685
686
      q00 = Q00PsiPhi::provideQ00PsiPhi(owner->getRowFESpace()->getBasisFcts(), 
					owner->getColFESpace()->getBasisFcts(), 
					quadrature);
      q0 = Q0Psi::provideQ0Psi(owner->getRowFESpace()->getBasisFcts(),
			       quadrature);
      firstCall = false;
    }

    c[0] = 0.0;
687
    for (int i = 0; i < static_cast<int>( terms.size()); i++) {
688
689
690
691
692
693
      (static_cast<ZeroOrderTerm*>((terms[i])))->getC(elInfo, 1, c);
    }

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

    if (symmetric) {
694
695
696
697
      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);
698
699
700
701
702
	  (*mat)[i][j] += val;
	  (*mat)[j][i] += val;
	}
      }
    } else {
703
704
      for (int i = 0; i < nRow; i++)
	for (int j = 0; j < nCol; j++)
705
706
707
708
709
710
711
712
713
714
	  (*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);;

715
    if (firstCall) {
716
717
718
719
720
721
722
723
724
725
726
      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;
727
    for (termIt = terms.begin(); termIt != terms.end(); ++termIt) {
728
729
730
731
732
      (static_cast<ZeroOrderTerm*>( *termIt))->getC(elInfo, 1, c);
    }

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

733
    for (int i = 0; i < nRow; i++)
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
      (*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);
755
    for (int iq = 0; iq < numPoints; iq++) {
756
757
      Lb[iq].set(0.0);
    }
758
    for (int i = 0; i < static_cast<int>(terms.size()); i++) {
759
760
761
      (static_cast<FirstOrderTerm*>((terms[i])))->getLb(elInfo, numPoints, Lb);
    }
  
762
    for (int iq = 0; iq < numPoints; iq++) {
763
764
      Lb[iq] *= elInfo->getDet();

765
      for (int i = 0; i < nCol; i++) {
766
767
768
	phival[i] = (*(phi->getPhi(i)))(quadrature->getLambda(iq));
      }

769
      for (int i = 0; i < nRow; i++) {
770
	grdPsi = (*(psi->getGrdPhi(i)))(quadrature->getLambda(iq));
771
772
	for (int j = 0; j < nCol; j++) {
	  (*mat)[i][j] += quadrature->getWeight(iq) * (Lb[iq] * grdPsi) * phival[j];
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
	}
      }
    }
    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;

791
    if (firstCall) {
792
793
794
795
796
797
798
799
800
801
      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);
802
    for (int iq = 0; iq < numPoints; iq++) {
803
804
      Lb[iq].set(0.0);
    }
805
806

    for (int i = 0; i < static_cast<int>(terms.size()); i++) {
807
808
809
      (static_cast<FirstOrderTerm*>((terms[i])))->getLb(elInfo, numPoints, Lb);
    }
  
810
    for (int iq = 0; iq < numPoints; iq++) {
811
812
813
814
815
      Lb[iq] *= elInfo->getDet();

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

816
817
818
      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];
819
820
821
822
823
824
825
826
827
828
      }
    }
  }


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

829

830
831
832
833
834
835
836
  void Pre10::calculateElementMatrix(const ElInfo *elInfo, ElementMatrix *mat)
  {
    VectorOfFixVecs<DimVec<double> > Lb(dim,1,NO_INIT);
    const int *k;
    const double *values;
    double val;

837
    if (firstCall) {
838
839
840
841
842
843
844
845
846
847
848
      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);
849
    for (int i = 0; i < static_cast<int>( terms.size()); i++) {
850
851
852
853
854
      (static_cast<FirstOrderTerm*>((terms[i])))->getLb(elInfo, 1, Lb);
    }

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

855
856
857
    for (int i = 0; i < nRow; i++) {
      for (int j = 0; j < nCol; j++) {
	k = q10->getKVec(i, j);
858
	values = q10->getValVec(i, j);
859
	int m;
860
861
862
863
864
865
866
867
868
869
870
871
	for (val = m = 0; m < nEntries[i][j]; m++)
	  val += values[m]*Lb[0][k[m]];
	(*mat)[i][j] += val;
      }
    }
  }


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

872

873
874
875
876
877
878
879
880
881
882
883
884
885
  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);

886
    for (int iq = 0; iq < numPoints; iq++) {
887
888
      Lb[iq].set(0.0);
    }
889
    for (int i = 0; i < static_cast<int>(terms.size()); i++) {
890
891
892
      (static_cast<FirstOrderTerm*>((terms[i])))->getLb(elInfo, numPoints, Lb);
    }
  
893
    for (int iq = 0; iq < numPoints; iq++) {
894
895
      Lb[iq] *= elInfo->getDet();

896
      for (int i = 0; i < nCol; i++) {
897
898
899
	grdPhi[i] = (*(phi->getGrdPhi(i)))(quadrature->getLambda(iq));
      }

900
      for (int i = 0; i < nRow; i++) {
901
	psival = (*(psi->getPhi(i)))(quadrature->getLambda(iq));
902
903
	for (int j = 0; j < nCol; j++)
	  (*mat)[i][j] += quadrature->getWeight(iq) * ((Lb[iq] * psival) * grdPhi[j]);
904
      }
905
    } 
906
907
908
909
910
911
912
913
914
915
916
917
  }

  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);

918
    for (int iq = 0; iq < numPoints; iq++) {
919
920
      Lb[iq].set(0.0);
    }
921
    for (int i = 0; i < static_cast<int>(terms.size()); i++) {
922
923
924
      (static_cast<FirstOrderTerm*>((terms[i])))->getLb(elInfo, numPoints, Lb);
    }
  
925
    for (int iq = 0; iq < numPoints; iq++) {
926
927
      Lb[iq] *= elInfo->getDet();

928
      for (int i = 0; i < nRow; i++) {
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
	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;

944
    if (firstCall) {
945
946
947
948
949
950
951
952
953
954
955
      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);

956
    for (int iq = 0; iq < numPoints; iq++) {
957
958
      Lb[iq].set(0.0);
    }
959
    for (int i = 0; i < static_cast<int>(terms.size()); i++) {
960
961
962
      (static_cast<FirstOrderTerm*>((terms[i])))->getLb(elInfo, numPoints, Lb);
    }
  
963
    for (int iq = 0; iq < numPoints; iq++) {
964
965
      Lb[iq] *= elInfo->getDet();

966
      const double *psi = psiFast->getPhi(iq);
967
968
      grdPhi = phiFast->getGradient(iq);

969
970
971
      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];
972
973
974
975
976
977
978
979
      }
    }
  }

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

980
    if (firstCall) {
981
982
983
984
985
986
987
988
989
990
991
      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);

992
    for (int iq = 0; iq < numPoints; iq++) {
993
994
      Lb[iq].set(0.0);
    }
995
    for (int i = 0; i < static_cast<int>(terms.size()); i++) {
996
997
998
      (static_cast<FirstOrderTerm*>((terms[i])))->getLb(elInfo, numPoints, Lb);
    }
  
999
    for (int iq = 0; iq < numPoints; iq++) {
1000
1001
1002
1003
1004

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

      grdPsi = psiFast->getGradient(iq);

1005
      for (int i = 0; i < nRow; i++) {
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
	(*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;

1024
    if (firstCall) {
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
      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);
1036
    for (int i = 0; i < static_cast<int>( terms.size()); i++) {
1037
1038
1039
1040
1041
      (static_cast<FirstOrderTerm*>((terms[i])))->getLb(elInfo, 1, Lb);
    }

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

1042
1043
1044
    for (int i = 0; i < nRow; i++) {
      for (int j = 0; j < nCol; j++) {
	l = q01->getLVec(i, j);
1045
	values = q01->getValVec(i, j);
1046
	int m;
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
	for (val = m = 0; m < nEntries[i][j]; m++)
	  val += values[m]*Lb[0][l[m]];
	(*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;
    int i, m;
    double val;

    if(firstCall) {
      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);
    for(i=0; i < static_cast<int>(terms.size()); i++) {
      (static_cast<FirstOrderTerm*>(terms[i]))->getLb(elInfo, 1, Lb);
    }

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

    for (i = 0; i < nRow; i++) {
      k      = q1->getKVec(i);
      values = q1->getValVec(i);
      for (val = m = 0; m < nEntries[i]; m++)
	val += values[m]*Lb[0][k[m]];
      (*vec)[i] += val;
    }

    //DELETE [] Lb;
  }

  Pre2::Pre2(Operator *op, Assembler *assembler, Quadrature *quad) 
    : SecondOrderAssembler(op, assembler, quad, true)
  {
    //   q11 = Q11PsiPhi::provideQ11PsiPhi(assembler->getRowFESpace()->getBasisFcts(), 
    // 				    assembler->getColFESpace()->getBasisFcts(), 
    // 				    quadrature);
  }

  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;
    int          i, j, m;
    double val;

    if(firstCall) {
      q11 = Q11PsiPhi::provideQ11PsiPhi(owner->getRowFESpace()->getBasisFcts(), 
					owner->getColFESpace()->getBasisFcts(), 
					quadrature);
      firstCall = false;
    }

    LALt[0]->set(0.0);

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

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

    nEntries = q11->getNumberEntries();

    if (symmetric) {
      for (i = 0; i < nRow; i++) {
	k      = q11->getKVec(i, i);
	l      = q11->getLVec(i, i);
	values = q11->getValVec(i, i);
	for (val = m = 0; m < nEntries[i][i]; m++)
	  val += values[m]*(*LALt[0])[k[m]][l[m]];
	(*mat)[i][i] += val;
	for (j = i+1; j < nCol; j++) {
	  k      = q11->getKVec(i, j);
	  l      = q11->getLVec(i, j);
	  values = q11->getValVec(i, j);
	  for (val = m = 0; m < nEntries[i][j]; m++)
	    val += values[m]*(*LALt[0])[k[m]][l[m]];
	  (*mat)[i][j] += val;
	  (*mat)[j][i] += val;
	}
      }
    }
    else {  /*  A not symmetric or psi != phi        */
      for (i = 0; i < nRow; i++) {
	for (j = 0; j < nCol; j++) {
	  k      = q11->getKVec(i, j);
	  l      = q11->getLVec(i, j);
	  values = q11->getValVec(i, j);
	  for (val = m = 0; m < nEntries[i][j]; m++)
	    val += values[m]*(*LALt[0])[k[m]][l[m]];
	  (*mat)[i][j] += val;
	}
      }
    }

    DELETE *LALt;
    DELETE LALt;
  }

  // void Pre2::calculateElementVector(const ElInfo *elInfo, double *vec)
  // {
  //   FUNCNAME("Pre2::calculateElementVector");
  //   ERROR_EXIT("should not be called\n");
  // }

  // void Pre2::calculateElementVector(const ElInfo *elInfo, double *vec)
  // {  
  //   DimMat<double>  LALt(dim, NO_INIT);
  //   const int *nEntries;
  //   const int *k, *l;
  //   const double *values;
  //   int          i, m;
  //   double val;

  //   LALt.set(0.0);

  //   for(i=0; i < static_cast<int>( terms->size()); i++) {
  //     (static_cast<SecondOrderTerm*>((*terms)[i])->eval(elInfo, 0, &LALt);
  //   }

  //   nEntries = q2->getNumberEntries();

  //   for (i = 0; i < nRow; i++) {
  //     k      = q2->getKVec(i);
  //     l      = q2->getLVec(i);
  //     values = q2->getValVec(i);
  //     for (val = m = 0; m < nEntries[i]; m++)
  //       val += values[m]*LALt[k[m]][l[m]];
  //     vec[i] += val;
  //   }
  // }

  Quad2::Quad2(Operator *op, Assembler *assembler, Quadrature *quad) 
    : SecondOrderAssembler(op, assembler, quad, true)
  {
    //   if(!psiFast) {
    //     psiFast = FastQuadrature::provideFastQuadrature(
    //       assembler->getRowFESpace()->getBasisFcts(), 
    //       *quadrature,
    //       INIT_GRD_PHI);
    //   } else {
    //     psiFast->init(INIT_GRD_PHI);
    //   }
    //   if(!phiFast) {
    //     phiFast = FastQuadrature::provideFastQuadrature(
    //       assembler->getColFESpace()->getBasisFcts(), 
    //       *quadrature,
    //       INIT_PHI);
    //   } else {
    //     phiFast->init(INIT_GRD_PHI);
    //   }
  }

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

    if(firstCall) {
      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];
    for(i=0;i<nPoints;i++) LALt[i]=NEW DimMat<double>(dim, NO_INIT);
    for (iq = 0; iq < nPoints; iq++) {
      LALt[iq]->set(0.0);
    }
    for(i=0; i < static_cast<int>(terms.size()); i++) {
      (static_cast<SecondOrderTerm*>(terms[i]))->getLALt(elInfo, nPoints, LALt);
    }

    if (symmetric) {
      for (iq = 0; iq < nPoints; iq++) {
	(*LALt[iq]) *= elInfo->getDet();

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

	for (i = 0; i < nRow; i++) {
	  (*mat)[i][i] += quadrature->getWeight(iq) * 
	    ((*grdPsi)[i] * ((*LALt[iq]) * (*grdPhi)[i]));

	  for (j = i+1; j < nCol; j++) {
	    val = quadrature->getWeight(iq) * ((*grdPsi)[i] * ((*LALt[iq]) * (*grdPhi)[j]));
	    (*mat)[i][j] += val;
	    (*mat)[j][i] += val;
	  }
	}
      }
    }
    else {      /*  non symmetric assembling   */
      for (iq = 0; iq < nPoints; iq++) {
	(*LALt[iq]) *= elInfo->getDet();

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

	for (i = 0; i < nRow; i++) {
	  for (j = 0; j < nCol; j++) {
	    (*mat)[i][j] += quadrature->getWeight(iq) *
	      ((*grdPsi)[i] * ((*LALt[iq]) * (*grdPhi)[j]));
	  }
	}
      }
    }
  
    for(i=0;i<nPoints;i++) DELETE LALt[i];
    DELETE [] LALt; 
  }

  // void Quad2::calculateElementVector(const ElInfo *elInfo, double *vec)
  // {
  //   FUNCNAME("Quad2::calculateElementVector");
  //   ERROR_EXIT("should not be called\n");
  // }

  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);
    int iq, i, j;

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

    int nPoints = quadrature->getNumPoints();

    DimMat<double> **LALt = NEW DimMat<double>*[nPoints];
    for (iq = 0; iq < nPoints; iq++) {
      LALt[iq]=NEW DimMat<double>(dim,NO_INIT);
      LALt[iq]->set(0.0);
    }
    for(i=0; i < static_cast<int>(terms.size()); i++) {
      (static_cast<SecondOrderTerm*>(terms[i]))->getLALt(elInfo, nPoints, LALt);
    }

    if (symmetric) {
      for (iq = 0; iq < nPoints; iq++) {
	(*LALt[iq]) *= elInfo->getDet();

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

	for (i = 0; i < nRow; i++) {
	  grdPsi = (*(psi->getGrdPhi(i)))(quadrature->getLambda(iq));
	  (*mat)[i][i] += quadrature->getWeight(iq) * 
	    (grdPsi * ((*LALt[iq]) * grdPhi[i]));

	  for (j = i+1; j < nCol; j++) {
	    val = quadrature->getWeight(iq) * (grdPsi * ((*LALt[iq]) * grdPhi[j]));
	    (*mat)[i][j] += val;
	    (*mat)[j][i] += val;
	  }
	}
      }
    }
    else {      /*  non symmetric assembling   */
      for (iq = 0; iq < nPoints; iq++) {
	(*LALt[iq]) *= elInfo->getDet();

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

	for (i = 0; i < nRow; i++) {
	  grdPsi = (*(psi->getGrdPhi(i)))(quadrature->getLambda(iq));
	  for (j = 0; j < nCol; j++) {
	    (*mat)[i][j] += quadrature->getWeight(iq) *
	      (grdPsi * ((*LALt[iq]) * grdPhi[j]));
	  }
	}
      }
    }

    for(iq=0;iq<nPoints;iq++) DELETE LALt[iq];
    DELETE [] LALt;
  }

  // void Stand2::calculateElementVector(const ElInfo *elInfo, double *vec)
  // {
  //   FUNCNAME("Stand2::calculateElementVector");
  //   ERROR_EXIT("should not be called\n");
  // }

  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)
  
  {
    //if(op->uhOld) rememberElMat = true;
  }

  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();

    checkForNewTraverse();

    checkQuadratures();

    if ((el != lastMatEl && el != lastVecEl) || !operat->isOptimized()) {
      initElement(elInfo);
    }

    if (el != lastMatEl || !operat->isOptimized()) {
      if (rememberElMat) {
	elementMatrix->set(0.0);
      }
      lastMatEl = el;
    } else {
      if (rememberElMat) {
	axpy(factor, *elementMatrix, *userMat);
	//*userMat += *elementMatrix * factor;
	//operat->addElementMatrix(elementMatrix, userMat, factor);
	return;
      }
    }
  
    ElementMatrix *mat = rememberElMat ? elementMatrix : userMat;

    if (secondOrderAssembler)
      secondOrderAssembler->calculateElementMatrix(elInfo, mat);
    if (firstOrderAssemblerGrdPsi)
      firstOrderAssemblerGrdPsi->calculateElementMatrix(elInfo, mat);
    if (firstOrderAssemblerGrdPhi)
      firstOrderAssemblerGrdPhi->calculateElementMatrix(elInfo, mat);
    if (zeroOrderAssembler)
      zeroOrderAssembler->calculateElementMatrix(elInfo, mat);

    if(rememberElMat && userMat) {
      axpy(factor, *elementMatrix, *userMat);
      //*userMat += *elementMatrix * factor;
      //operat->addElementMatrix(elementMatrix, userMat, factor);
    }
  }

  void Assembler::calculateElementVector(const ElInfo *elInfo, 
					 ElementVector *userVec,
					 double factor)
  {
    FUNCNAME("Assembler::calculateElementVector()");

    if(remember && factor != 1.0) {
      rememberElVec = true;
    }

    if(rememberElVec && !elementVector)
      elementVector = NEW ElementVector(nRow);

    Element *el = elInfo->getElement();

    checkForNewTraverse();

    checkQuadratures();

    if((el != lastMatEl && el != lastVecEl) || !operat->isOptimized()) {
      initElement(elInfo);
    }

    if(el != lastVecEl || !operat->isOptimized()) {
      if(rememberElVec) {
	elementVector->set(0.0);
      }
      lastVecEl = el;
    } else {
      if(rememberElVec) {
	axpy(factor, *elementVector, *userVec);
	//*userVec += *elementVector * factor;
	//operat->addElementVector(elementVector, userVec, factor);
	return;
      }
    }

    ElementVector *vec = rememberElVec ? elementVector : userVec;

    if(operat->uhOld && remember)