DOFVector.cc 27.7 KB
Newer Older
1
#include <boost/numeric/mtl/mtl.hpp>
2
3
4
5
6
7
8
9
10
11
#include "DOFVector.h"
#include "Traverse.h"
#include "DualTraverse.h"
#include "FixVec.h"

namespace AMDiS {

  template<>
  void DOFVector<double>::coarseRestrict(RCNeighbourList& list, int n)
  {
12
13
    FUNCNAME("DOFVector<double>::coarseRestrict()");

14
    switch (coarsenOperation) {
15
16
17
18
19
20
21
    case NO_OPERATION:
      return;
      break;
    case COARSE_RESTRICT:
      (const_cast<BasisFunction*>(feSpace->getBasisFcts()))->coarseRestr(this, &list, n);
      break;
    case COARSE_INTERPOL:
22
23
      TEST_EXIT_DBG(feSpace)("Should not happen!\n");
      TEST_EXIT_DBG(feSpace->getBasisFcts())("Shoud not happen!\n");
24

25
26
27
      (const_cast<BasisFunction*>(feSpace->getBasisFcts()))->coarseInter(this, &list, n);
      break;
    default:
28
29
      WARNING("Invalid coarsen operation \"%d\" in vector \"%s\"\n", 
	      coarsenOperation, this->name.c_str());
30
31
32
    }
  }

33

34
35
36
37
38
39
  template<>
  void DOFVector<double>::refineInterpol(RCNeighbourList& list, int n)
  {
    (const_cast<BasisFunction*>(feSpace->getBasisFcts()))->refineInter(this, &list, n);
  }

40

41
42
43
  template<>
  void DOFVector<WorldVector<double> >::refineInterpol(RCNeighbourList& list, int n)
  {
44
45
46
    if (n < 1) 
      return;

47
48
49
50
    Element *el = list.getElement(0);
    int n0 = feSpace->getAdmin()->getNumberOfPreDOFs(VERTEX);
    DegreeOfFreedom dof0 = el->getDOF(0, n0);
    DegreeOfFreedom dof1 = el->getDOF(1, n0);
Thomas Witkowski's avatar
Thomas Witkowski committed
51
    DegreeOfFreedom dof_new = el->getChild(0)->getDOF(feSpace->getMesh()->getDim(), n0);
52
53
54
55
56
    vec[dof_new] = vec[dof0];
    vec[dof_new] += vec[dof1];
    vec[dof_new] *= 0.5;
  }

57

58
59
60
61
62
63
64
65
66
  template<>
  DOFVector<WorldVector<double> >*
  DOFVector<double>::getGradient(DOFVector<WorldVector<double> > *grad) const 
  {
    FUNCNAME("DOFVector<double>::getGradient()");

    // define result vector
    static DOFVector<WorldVector<double> > *result = NULL;

Thomas Witkowski's avatar
Thomas Witkowski committed
67
    if (grad) {
68
69
      result = grad;
    } else {
70
      if(result && result->getFeSpace() != feSpace) {
71
72
	delete result;
	result = new DOFVector<WorldVector<double> >(feSpace, "gradient");
73
74
75
76
77
78
79
80
81
      }
    }

    Mesh *mesh = feSpace->getMesh();
    int dim = mesh->getDim();
    const BasisFunction *basFcts = feSpace->getBasisFcts();
    DOFAdmin *admin = feSpace->getAdmin();

    // count number of nodes and dofs per node
82
83
84
    std::vector<int> numNodeDOFs;
    std::vector<int> numNodePreDOFs;
    std::vector<DimVec<double>*> bary;
85

86
87
    int nNodes = 0;
    int nDofs = 0;
88

Thomas Witkowski's avatar
Thomas Witkowski committed
89
    for (int i = 0; i < dim + 1; i++) {
90
      GeoIndex geoIndex = INDEX_OF_DIM(i, dim);
91
      int nPositions = mesh->getGeo(geoIndex);
92
      int numPreDOFs = admin->getNumberOfPreDOFs(i);
93
      for (int j = 0; j < nPositions; j++) {
94
95
	int dofs = basFcts->getNumberOfDOFs(geoIndex);
	numNodeDOFs.push_back(dofs);
96
	nDofs += dofs;
97
98
	numNodePreDOFs.push_back(numPreDOFs);
      }
99
      nNodes += nPositions;
100
101
    }

102
    TEST_EXIT_DBG(nDofs == basFcts->getNumber())
103
      ("number of dofs != number of basis functions\n");
104
    
105
    for (int i = 0; i < nDofs; i++)
106
107
108
      bary.push_back(basFcts->getCoords(i));    

    double *localUh = new double[basFcts->getNumber()];
109
110

    // traverse mesh
111
    std::vector<bool> visited(getUsedSize(), false);
112
    TraverseStack stack;
113
    Flag fillFlag = Mesh::CALL_LEAF_EL | Mesh::FILL_GRD_LAMBDA | Mesh::FILL_COORDS;
114
115
    ElInfo *elInfo = stack.traverseFirst(mesh, -1, fillFlag);

Thomas Witkowski's avatar
Thomas Witkowski committed
116
    while (elInfo) {
117
118
      const DegreeOfFreedom **dof = elInfo->getElement()->getDOF();
      const DimVec<WorldVector<double> > &grdLambda = elInfo->getGrdLambda();
119
      getLocalVector(elInfo->getElement(), localUh);
120
121

      int localDOFNr = 0;
122
      for (int i = 0; i < nNodes; i++) { // for all nodes
Thomas Witkowski's avatar
Thomas Witkowski committed
123
	for (int j = 0; j < numNodeDOFs[i]; j++) { // for all dofs at this node
124
	  DegreeOfFreedom dofIndex = dof[i][numNodePreDOFs[i] + j];
Thomas Witkowski's avatar
Thomas Witkowski committed
125
126
127
	  if (!visited[dofIndex]) {	  
	    basFcts->evalGrdUh(*(bary[localDOFNr]), grdLambda, 
			       localUh, &((*result)[dofIndex]));
128
129
130
131
132
133
134
135
136
137

	    visited[dofIndex] = true;
	  }
	  localDOFNr++;
	}
      }

      elInfo = stack.traverseNext(elInfo);
    }

138
139
    delete [] localUh;

140
141
142
    return result;
  }

143

144
145
146
147
148
149
  template<>
  DOFVector<WorldVector<double> >*
  DOFVector<double>::getRecoveryGradient(DOFVector<WorldVector<double> > *grad) const 
  {
    FUNCNAME("DOFVector<double>::getRecoveryGradient()");

150
151
152
153
#ifdef _OPENMP
      ERROR_EXIT("Using static variable while using OpenMP parallelization!\n");
#endif

154
155
156
157
158
    // define result vector
    static DOFVector<WorldVector<double> > *vec = NULL;

    DOFVector<WorldVector<double> > *result = grad;

Thomas Witkowski's avatar
Thomas Witkowski committed
159
    if (!result) {
160
      if (vec && vec->getFeSpace() != feSpace) {
161
	delete vec;
162
163
	vec = NULL;
      }
164
      if (!vec)
165
	vec = new DOFVector<WorldVector<double> >(feSpace, "gradient");
166
      
167
168
169
170
171
172
173
174
175
      result = vec;
    }

    result->set(WorldVector<double>(DEFAULT_VALUE, 0.0));

    DOFVector<double> volume(feSpace, "volume");
    volume.set(0.0);

    const BasisFunction *basFcts = feSpace->getBasisFcts();
Thomas Witkowski's avatar
Thomas Witkowski committed
176
    int nPreDOFs = feSpace->getAdmin()->getNumberOfPreDOFs(0);
177
    DimVec<double> bary(dim, DEFAULT_VALUE, (1.0 / (dim + 1.0)));
Thomas Witkowski's avatar
Thomas Witkowski committed
178
    WorldVector<double> grd;
179
180

    // traverse mesh
Thomas Witkowski's avatar
Thomas Witkowski committed
181
    Mesh *mesh = feSpace->getMesh();
182
    TraverseStack stack;
Thomas Witkowski's avatar
Thomas Witkowski committed
183
184
    ElInfo *elInfo = stack.traverseFirst(mesh, -1,
					 Mesh::CALL_LEAF_EL | Mesh::FILL_DET | 
185
					 Mesh::FILL_GRD_LAMBDA | Mesh::FILL_COORDS);
186

187
188
    double *localUh = new double[basFcts->getNumber()];

Thomas Witkowski's avatar
Thomas Witkowski committed
189
    while (elInfo) {
190
191
192
      double det = elInfo->getDet();
      const DegreeOfFreedom **dof = elInfo->getElement()->getDOF();
      const DimVec<WorldVector<double> > &grdLambda = elInfo->getGrdLambda();
193
      getLocalVector(elInfo->getElement(), localUh);
Thomas Witkowski's avatar
Thomas Witkowski committed
194
      basFcts->evalGrdUh(bary, grdLambda, localUh, &grd);
195

Thomas Witkowski's avatar
Thomas Witkowski committed
196
      for (int i = 0; i < dim + 1; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
197
	DegreeOfFreedom dofIndex = dof[i][nPreDOFs];
198
199
200
201
202
203
204
	(*result)[dofIndex] += grd * det;
	volume[dofIndex] += det;
      }

      elInfo = stack.traverseNext(elInfo);
    }

205
206
    delete [] localUh;

207
208
209
    DOFVector<double>::Iterator volIt(&volume, USED_DOFS);
    DOFVector<WorldVector<double> >::Iterator grdIt(result, USED_DOFS);

210
211
    for (volIt.reset(), grdIt.reset(); !volIt.end(); ++volIt, ++grdIt)
      if (*volIt != 0.0)
Thomas Witkowski's avatar
Thomas Witkowski committed
212
	*grdIt *= 1.0 / (*volIt);
213
214
215
216

    return result;
  }

217

218
  template<>
219
220
  const WorldVector<double> *DOFVectorBase<double>::getGrdAtQPs(const ElInfo *elInfo, 
								const Quadrature *quad,
221
								const FastQuadrature *quadFast,
222
								WorldVector<double> *grdAtQPs) const
223
224
225
  {
    FUNCNAME("DOFVector<double>::getGrdAtQPs()");
  
226
    TEST_EXIT_DBG(quad || quadFast)("neither quad nor quadFast defined\n");
227

228
229
230
    if (quad && quadFast) {
      TEST_EXIT_DBG(quad == quadFast->getQuadrature())
      	("quad != quadFast->quadrature\n");
231
232
    }

233
    TEST_EXIT_DBG(!quadFast || quadFast->getBasisFunctions() == feSpace->getBasisFcts())
234
235
236
      ("invalid basis functions");

    int dow = Global::getGeo(WORLD);
Thomas Witkowski's avatar
Thomas Witkowski committed
237
238
    int myRank = omp_get_thread_num();
    int nPoints = quadFast ? quadFast->getQuadrature()->getNumPoints() : quad->getNumPoints();
239
240
241
242
243
244
    static WorldVector<double> *grd = NULL;
    WorldVector<double> *result;

    if (grdAtQPs) {
      result = grdAtQPs;
    } else {
245
246
247
      if (grd) 
	delete [] grd;

Thomas Witkowski's avatar
Thomas Witkowski committed
248
      grd = new WorldVector<double>[nPoints];
249
      for (int i = 0; i < nPoints; i++)
250
	grd[i].set(0.0);
251
      
252
253
254
      result = grd;
    }
  
Thomas Witkowski's avatar
Thomas Witkowski committed
255
    double *localVec = localVectors[myRank];
256
    getLocalVector(elInfo->getElement(), localVec);
257

Thomas Witkowski's avatar
Thomas Witkowski committed
258
    DimVec<double> &grd1 = *grdTmp[myRank];
259
260
261
    int parts = Global::getGeo(PARTS, dim);
    const DimVec<WorldVector<double> > &grdLambda = elInfo->getGrdLambda();

262
    if (quadFast) {
Thomas Witkowski's avatar
Thomas Witkowski committed
263
      for (int i = 0; i < nPoints; i++) {
264
	for (int j = 0; j < dim + 1; j++)
265
266
	  grd1[j] = 0.0;

267
268
	for (int j = 0; j < nBasFcts; j++)
	  for (int k = 0; k < parts; k++)
269
	    grd1[k] += quadFast->getGradient(i, j, k) * localVec[j];
270
	  
271
272
	for (int l=0; l < dow; l++) {
	  result[i][l] = 0.0;
273
	  for (int k = 0; k < parts; k++)
274
275
276
	    result[i][l] += grdLambda[k][l] * grd1[k];
	}
      }
277
278

    } else {      
Thomas Witkowski's avatar
Thomas Witkowski committed
279
280
      const BasisFunction *basFcts = feSpace->getBasisFcts();
      DimVec<double>* grdPhi = grdPhis[myRank];
281

Thomas Witkowski's avatar
Thomas Witkowski committed
282
      for (int i = 0; i < nPoints; i++) {
283
	for (int j = 0; j < dim + 1; j++)
284
	  grd1[j] = 0.0;
285
286

	for (int j = 0; j < nBasFcts; j++) {
287
	  (*(basFcts->getGrdPhi(j)))(quad->getLambda(i), *grdPhi);
288
	  for (int k = 0; k < parts; k++)
289
	    grd1[k] += (*grdPhi)[k] * localVec[j];
290
291
	}

292
293
	for (int l = 0; l < dow; l++) {
	  result[i][l] = 0.0;
294
	  for (int k = 0; k < parts; k++)
295
296
297
298
	    result[i][l] += grdLambda[k][l] * grd1[k];
	}
      }
    }
299

300
301
302
    return const_cast<const WorldVector<double>*>(result);
  }

303

304
  template<>
Thomas Witkowski's avatar
Thomas Witkowski committed
305
306
307
308
309
310
311
312
313
314
  const WorldVector<double> *DOFVectorBase<double>::getGrdAtQPs(const ElInfo *smallElInfo,
								const ElInfo *largeElInfo,
								const Quadrature *quad,
								const FastQuadrature *quadFast,
								WorldVector<double> *grdAtQPs) const
  {
    FUNCNAME("DOFVector<double>::getGrdAtQPs()");
  
    TEST_EXIT_DBG(quad || quadFast)("neither quad nor quadFast defined\n");

315
    if (quad && quadFast) {
316
      TEST_EXIT_DBG(quad == quadFast->getQuadrature())("quad != quadFast->quadrature\n");
317
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334

    TEST_EXIT_DBG(!quadFast || quadFast->getBasisFunctions() == feSpace->getBasisFcts())
      ("invalid basis functions");

    int dow = Global::getGeo(WORLD);
    int myRank = omp_get_thread_num();
    int nPoints = quadFast ? quadFast->getQuadrature()->getNumPoints() : quad->getNumPoints();
    static WorldVector<double> *grd = NULL;
    WorldVector<double> *result;

    if (grdAtQPs) {
      result = grdAtQPs;
    } else {
      if (grd) 
	delete [] grd;

      grd = new WorldVector<double>[nPoints];
335
      for (int i = 0; i < nPoints; i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
336
	grd[i].set(0.0);
337

Thomas Witkowski's avatar
Thomas Witkowski committed
338
339
340
341
      result = grd;
    }
  
    double *localVec = localVectors[myRank];
342
    getLocalVector(largeElInfo->getElement(), localVec);
343
344

    const BasisFunction *basFcts = feSpace->getBasisFcts();    
345
    mtl::dense2D<double> &m = smallElInfo->getSubElemCoordsMat(basFcts->getDegree());
Thomas Witkowski's avatar
Thomas Witkowski committed
346
347
348
349
350
351
352
353
354

    DimVec<double> &grd1 = *grdTmp[myRank];
    int parts = Global::getGeo(PARTS, dim);
    const DimVec<WorldVector<double> > &grdLambda = largeElInfo->getGrdLambda();

    DimVec<double>* grdPhi = grdPhis[myRank];
    DimVec<double> tmp(dim, DEFAULT_VALUE, 0.0);
    
    for (int i = 0; i < nPoints; i++) {
355
      for (int j = 0; j < dim + 1; j++)
Thomas Witkowski's avatar
Thomas Witkowski committed
356
357
358
359
360
361
362
	grd1[j] = 0.0;
      
      for (int j = 0; j < nBasFcts; j++) {

	grdPhi->set(0.0);
	for (int k = 0; k < nBasFcts; k++) {
	  (*(basFcts->getGrdPhi(k)))(quad->getLambda(i), tmp);
363
	  tmp *= m[j][k];
Thomas Witkowski's avatar
Thomas Witkowski committed
364
365
366
	  *grdPhi += tmp;
	}

367
	for (int k = 0; k < parts; k++)
Thomas Witkowski's avatar
Thomas Witkowski committed
368
369
370
371
372
	  grd1[k] += (*grdPhi)[k] * localVec[j];
      }
      
      for (int l = 0; l < dow; l++) {
	result[i][l] = 0.0;
373
	for (int k = 0; k < parts; k++)
Thomas Witkowski's avatar
Thomas Witkowski committed
374
375
376
377
378
379
380
	  result[i][l] += grdLambda[k][l] * grd1[k];
      }
    }    
  
    return const_cast<const WorldVector<double>*>(result);
  }

381

Thomas Witkowski's avatar
Thomas Witkowski committed
382
383
384
  template<>
  const WorldMatrix<double> *DOFVectorBase<double>::getD2AtQPs(const ElInfo *elInfo, 
							       const Quadrature *quad,
385
386
387
388
389
							       const FastQuadrature *quadFast,
							       WorldMatrix<double>  *d2AtQPs) const
  {
    FUNCNAME("DOFVector<double>::getD2AtQPs()");
  
390
    TEST_EXIT_DBG(quad || quadFast)("neither quad nor quadFast defined\n");
391

392
393
394
    if (quad && quadFast) {
      TEST_EXIT_DBG(quad == quadFast->getQuadrature())
      	("quad != quadFast->quadrature\n");
395
396
    }

397
    TEST_EXIT_DBG(!quadFast || quadFast->getBasisFunctions() == feSpace->getBasisFcts())
398
399
      ("invalid basis functions");

400
401
    Element *el = elInfo->getElement(); 

Thomas Witkowski's avatar
Thomas Witkowski committed
402
    int myRank = omp_get_thread_num();
403
    int dow = Global::getGeo(WORLD);
Thomas Witkowski's avatar
Thomas Witkowski committed
404
    int nPoints = quadFast ? quadFast->getQuadrature()->getNumPoints() : quad->getNumPoints();
405
406
407
408
409
410
411
412

    static WorldMatrix<double> *vec = NULL;
    WorldMatrix<double> *result;

    if (d2AtQPs) {
      result = d2AtQPs;
    } else {
      if(vec) delete [] vec;
Thomas Witkowski's avatar
Thomas Witkowski committed
413
414
      vec = new WorldMatrix<double>[nPoints];
      for (int i = 0; i < nPoints; i++) {
415
416
417
418
419
	vec[i].set(0.0);
      }
      result = vec;
    }
  
Thomas Witkowski's avatar
Thomas Witkowski committed
420
    double *localVec = localVectors[myRank];
421
    getLocalVector(el, localVec);
422
423
424
425
426

    DimMat<double> D2Tmp(dim, DEFAULT_VALUE, 0.0);
    int parts = Global::getGeo(PARTS, dim);
    const DimVec<WorldVector<double> > &grdLambda = elInfo->getGrdLambda();

427
    if (quadFast) {
Thomas Witkowski's avatar
Thomas Witkowski committed
428
429
430
      for (int iq = 0; iq < nPoints; iq++) {
	for (int k = 0; k < parts; k++)
	  for (int l = 0; l < parts; l++)
431
432
	    D2Tmp[k][l] = 0.0;

Thomas Witkowski's avatar
Thomas Witkowski committed
433
434
435
436
	for (int i = 0; i < nBasFcts; i++) {
	  for (int k = 0; k < parts; k++)
	    for (int l = 0; l < parts; l++)
	      D2Tmp[k][l] += localVec[i] * quadFast->getSecDer(iq, i, k, l);
437
438
	}

Thomas Witkowski's avatar
Thomas Witkowski committed
439
440
	for (int i = 0; i < dow; i++)
	  for (int j = 0; j < dow; j++) {
441
	    result[iq][i][j] = 0.0;
Thomas Witkowski's avatar
Thomas Witkowski committed
442
443
	    for (int k = 0; k < parts; k++)
	      for (int l = 0; l < parts; l++)
444
445
446
447
		result[iq][i][j] += grdLambda[k][i]*grdLambda[l][j]*D2Tmp[k][l];
	  }
      }
    } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
448
      const BasisFunction *basFcts = feSpace->getBasisFcts();
449
450
      DimMat<double>* D2Phi = D2Phis[omp_get_thread_num()];

Thomas Witkowski's avatar
Thomas Witkowski committed
451
452
453
      for (int iq = 0; iq < nPoints; iq++) {
	for (int k = 0; k < parts; k++)
	  for (int l = 0; l < parts; l++)
454
455
	    D2Tmp[k][l] = 0.0;

Thomas Witkowski's avatar
Thomas Witkowski committed
456
	for (int i = 0; i < nBasFcts; i++) {
457
	  WARNING("not tested after index correction\n");
458
	  (*(basFcts->getD2Phi(i)))(quad->getLambda(iq), *D2Phi);
459

Thomas Witkowski's avatar
Thomas Witkowski committed
460
461
	  for (int k = 0; k < parts; k++)
	    for (int l = 0; l < parts; l++)
462
	      D2Tmp[k][l] += localVec[i] * (*D2Phi)[k][l];
463
464
	}

Thomas Witkowski's avatar
Thomas Witkowski committed
465
466
	for (int i = 0; i < dow; i++)
	  for (int j = 0; j < dow; j++) {
467
	    result[iq][i][j] = 0.0;
Thomas Witkowski's avatar
Thomas Witkowski committed
468
469
	    for (int k = 0; k < parts; k++)
	      for (int l = 0; l < parts; l++)
470
		result[iq][i][j] += grdLambda[k][i] * grdLambda[l][j] * D2Tmp[k][l];
471
472
473
	  }
      }
    }
474

475
476
477
    return const_cast<const WorldMatrix<double>*>(result);
  }

478

479
480
481
  template<>
  void DOFVector<double>::interpol(DOFVector<double> *source, double factor) 
  {
482
    FUNCNAME("DOFVector<double>::interpol()");
483

484
    const FiniteElemSpace *sourceFeSpace = source->getFeSpace();
485
486
487
488
489
490
491
492
    const BasisFunction *basisFcts = feSpace->getBasisFcts();
    const BasisFunction *sourceBasisFcts = sourceFeSpace->getBasisFcts();

    int nBasisFcts = basisFcts->getNumber();
    int nSourceBasisFcts = sourceBasisFcts->getNumber();

    this->set(0.0);

Thomas Witkowski's avatar
Thomas Witkowski committed
493
    DegreeOfFreedom *myLocalIndices = localIndices[omp_get_thread_num()];
494
    double *sourceLocalCoeffs = new double[nSourceBasisFcts];
495

496
    if (feSpace->getMesh() == sourceFeSpace->getMesh()) {
497
498
499
500
501
502
      DimVec<double> *coords = NULL;
      TraverseStack stack;
      ElInfo *elInfo = stack.traverseFirst(feSpace->getMesh(), -1,
					   Mesh::CALL_LEAF_EL | 
					   Mesh::FILL_COORDS);

503
      while (elInfo) {
504
505
	Element *el = elInfo->getElement();

Thomas Witkowski's avatar
Thomas Witkowski committed
506
	basisFcts->getLocalIndices(el, feSpace->getAdmin(), myLocalIndices);
507
508
	source->getLocalVector(el, sourceLocalCoeffs);

509
	for (int i = 0; i < nBasisFcts; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
510
	  if (vec[myLocalIndices[i]] == 0.0) {
511
	    coords = basisFcts->getCoords(i);
Thomas Witkowski's avatar
Thomas Witkowski committed
512
	    vec[myLocalIndices[i]] = sourceBasisFcts->evalUh(*coords, sourceLocalCoeffs) * factor;
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
	  }
	}
	elInfo = stack.traverseNext(elInfo);
      }
    } else {
      DimVec<double> coords2(feSpace->getMesh()->getDim(), NO_INIT);
      DualTraverse dualStack;
      ElInfo *elInfo1, *elInfo2;
      ElInfo *elInfoSmall, *elInfoLarge;
      WorldVector<double> worldVec;

      bool nextTraverse = dualStack.traverseFirst(feSpace->getMesh(),
						  sourceFeSpace->getMesh(),
						  -1, -1,
						  Mesh::CALL_LEAF_EL | Mesh::FILL_COORDS,
						  Mesh::CALL_LEAF_EL | Mesh::FILL_COORDS,
						  &elInfo1, &elInfo2,
						  &elInfoSmall, &elInfoLarge);
531
      while (nextTraverse) {     
532
	basisFcts->getLocalIndices(elInfo1->getElement(), feSpace->getAdmin(), 
Thomas Witkowski's avatar
Thomas Witkowski committed
533
				   myLocalIndices);
534
	source->getLocalVector(elInfo2->getElement(), sourceLocalCoeffs);
535

536
	for (int i = 0; i < nBasisFcts; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
537
	  if (vec[myLocalIndices[i]] == 0.0) {
538
            elInfo1->coordToWorld(*(basisFcts->getCoords(i)), worldVec);
539
540
541
	    elInfo2->worldToCoord(worldVec, &coords2);
	  
	    bool isPositive = true;
542
	    for (int j = 0; j < coords2.size(); j++) {
543
	      if (coords2[j] < -0.00001) {
544
545
546
547
548
		isPositive = false;
		break;
	      }
	    }
	  
549
550
551
	    if (isPositive)
	      vec[myLocalIndices[i]] = 
		sourceBasisFcts->evalUh(coords2, sourceLocalCoeffs);	   
552
553
554
	  }
	}
      
555
556
	nextTraverse = 
	  dualStack.traverseNext(&elInfo1, &elInfo2, &elInfoSmall, &elInfoLarge);
557
558
559
      }
    }
  
560
    delete [] sourceLocalCoeffs;
561
562
  }

563
564
565
566
567
568
569
570
571

  template<>
  void DOFVector<WorldVector<double> >::interpol(DOFVector<WorldVector<double> > *v, double factor) 
  {
    WorldVector<double> nul(DEFAULT_VALUE,0.0);

    this->set(nul);

    DimVec<double> *coords = NULL;
572
    const FiniteElemSpace *vFeSpace = v->getFeSpace();
573

574
    if (feSpace == vFeSpace)
575
576
577
      WARNING("same FE-spaces\n");

    const BasisFunction *basFcts = feSpace->getBasisFcts();
578
    const BasisFunction *vBasFcts = vFeSpace->getBasisFcts();
579
580
581
582

    int numBasFcts = basFcts->getNumber();
    int vNumBasFcts = vBasFcts->getNumber();

583
    if (feSpace->getMesh() == vFeSpace->getMesh()) {
Thomas Witkowski's avatar
Thomas Witkowski committed
584
      DegreeOfFreedom *myLocalIndices = localIndices[omp_get_thread_num()];
585
      WorldVector<double> *vLocalCoeffs = new WorldVector<double>[vNumBasFcts];
586
587
588
589
590
591
592
593
594
      Mesh *mesh = feSpace->getMesh();
      TraverseStack stack;
      ElInfo *elInfo = stack.traverseFirst(mesh, -1,
					   Mesh::CALL_LEAF_EL | 
					   Mesh::FILL_COORDS);

      while (elInfo) {
	Element *el = elInfo->getElement();

Thomas Witkowski's avatar
Thomas Witkowski committed
595
	basFcts->getLocalIndices(el, feSpace->getAdmin(), myLocalIndices);
596
597
598
599

	v->getLocalVector(el, vLocalCoeffs);

	for (int i = 0; i < numBasFcts; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
600
	  if (vec[myLocalIndices[i]] == nul) {
601
	    coords = basFcts->getCoords(i);
602
	    vec[myLocalIndices[i]] += vBasFcts->evalUh(*coords, vLocalCoeffs, NULL) * factor;
603
604
605
606
607
	  }
	}
	elInfo = stack.traverseNext(elInfo);
      }

608
      delete [] vLocalCoeffs;
609
610
611
612
613
614
    } else {
      ERROR_EXIT("not yet for dual traverse\n");
    }
  }


615
616
617
618
619
  template<>
  WorldVector<DOFVector<double>*> *DOFVector<double>::getGradient(WorldVector<DOFVector<double>*> *grad) const
  {
    FUNCNAME("DOFVector<double>::getGradient()");

620
621
622
623
#ifdef _OPENMP
      ERROR_EXIT("Using static variable while using OpenMP parallelization!\n");
#endif

624
625
626
627
628
629
630
631
632
633
634
    Mesh *mesh = feSpace->getMesh();
    int dim = mesh->getDim();
    int dow = Global::getGeo(WORLD);

    const BasisFunction *basFcts = feSpace->getBasisFcts();

    DOFAdmin *admin = feSpace->getAdmin();

    // define result vector
    static WorldVector<DOFVector<double>*> *result = NULL;

635
    if (grad) {
636
637
      result = grad;
    } else {
638
      if (!result) {
639
	result = new WorldVector<DOFVector<double>*>;
640

641
642
	result->set(NULL);
      }
643
      for (int i = 0; i < dow; i++) {
644
	if ((*result)[i] && (*result)[i]->getFeSpace() != feSpace) {
645
646
	  delete (*result)[i];
	  (*result)[i] = new DOFVector<double>(feSpace, "gradient");
647
648
649
650
651
	}
      }
    }

    // count number of nodes and dofs per node
652
653
654
    std::vector<int> numNodeDOFs;
    std::vector<int> numNodePreDOFs;
    std::vector<DimVec<double>*> bary;
655

656
657
    int nNodes = 0;
    int nDofs = 0;
658

659
    for (int i = 0; i < dim + 1; i++) {
660
661
662
      GeoIndex geoIndex = INDEX_OF_DIM(i, dim);
      int numPositionNodes = mesh->getGeo(geoIndex);
      int numPreDOFs = admin->getNumberOfPreDOFs(i);
663
      for (int j = 0; j < numPositionNodes; j++) {
664
665
	int dofs = basFcts->getNumberOfDOFs(geoIndex);
	numNodeDOFs.push_back(dofs);
666
	nDofs += dofs;
667
668
	numNodePreDOFs.push_back(numPreDOFs);
      }
669
      nNodes += numPositionNodes;
670
671
    }

672
    TEST_EXIT_DBG(nDofs == basFcts->getNumber())
673
      ("number of dofs != number of basis functions\n");
674
    
675
    for (int i = 0; i < nDofs; i++)
676
      bary.push_back(basFcts->getCoords(i));    
677
678

    // traverse mesh
679
    std::vector<bool> visited(getUsedSize(), false);
680
    TraverseStack stack;
681
    Flag fillFlag = Mesh::CALL_LEAF_EL | Mesh::FILL_GRD_LAMBDA | Mesh::FILL_COORDS;
682
683
684
    ElInfo *elInfo = stack.traverseFirst(mesh, -1, fillFlag);
    WorldVector<double> grd;

685
    while (elInfo) {
686
687
688
689
690
      const DegreeOfFreedom **dof = elInfo->getElement()->getDOF();
      const double *localUh = getLocalVector(elInfo->getElement(), NULL);
      const DimVec<WorldVector<double> > &grdLambda = elInfo->getGrdLambda();

      int localDOFNr = 0;
691
      for (int i = 0; i < nNodes; i++) { // for all nodes
692
	for (int j = 0; j < numNodeDOFs[i]; j++) { // for all dofs at this node
693
	  DegreeOfFreedom dofIndex = dof[i][numNodePreDOFs[i] + j];
694

695
	  if (!visited[dofIndex]) {
696
	    basFcts->evalGrdUh(*(bary[localDOFNr]), grdLambda, localUh, &grd);
697

698
699
	    for (int k = 0; k < dow; k++)
	      (*(*result)[k])[dofIndex] = grd[k];	    
700
701
702
703
704
705
706
707
708
709
710
711
712
713

	    visited[dofIndex] = true;
	  }
	  localDOFNr++;
	}
      }

      elInfo = stack.traverseNext(elInfo);
    }

    return result;
  }


714
715
  DOFVectorDOF::DOFVectorDOF() : 
    DOFVector<DegreeOfFreedom>() 
Thomas Witkowski's avatar
Thomas Witkowski committed
716
  {}
717

718

719
  void DOFVectorDOF::freeDOFContent(DegreeOfFreedom dof) 
720
721
  {}

722
723
724
725

  WorldVector<DOFVector<double>*> *transform(DOFVector<WorldVector<double> > *vec,
					     WorldVector<DOFVector<double>*> *res)
  {
726
    FUNCNAME("DOFVector<double>::transform()");
727

728
    TEST_EXIT_DBG(vec)("no vector\n");
729

730
    int dow = Global::getGeo(WORLD);
731
732
    static WorldVector<DOFVector<double>*> *result = NULL;

733
    if (!res && !result) {
734
735
      result = new WorldVector<DOFVector<double>*>;
      for (int i = 0; i < dow; i++)
736
	(*result)[i] = new DOFVector<double>(vec->getFeSpace(), "transform");
737
738
739
740
741
    }

    WorldVector<DOFVector<double>*> *r = res ? res : result;

    int vecSize = vec->getSize();
742
743
    for (int i = 0; i < vecSize; i++)
      for (int j = 0; j < dow; j++)
744
745
746
747
748
	(*((*r)[j]))[i] = (*vec)[i][j];

    return r;
  }

749

Thomas Witkowski's avatar
Thomas Witkowski committed
750
751
752
753
754
755
756
757
758
759
760
  template<>
  const double *DOFVectorBase<double>::getVecAtQPs(const ElInfo *smallElInfo, 
						   const ElInfo *largeElInfo,
						   const Quadrature *quad,
						   const FastQuadrature *quadFast,
						   double *vecAtQPs) const
  {
    FUNCNAME("DOFVector<double>::getVecAtQPs()");
  
    TEST_EXIT_DBG(quad || quadFast)("neither quad nor quadFast defined\n");

761
    if (quad && quadFast)
Thomas Witkowski's avatar
Thomas Witkowski committed
762
763
764
765
766
767
      TEST_EXIT_DBG(quad == quadFast->getQuadrature())
      	("quad != quadFast->quadrature\n");

    TEST_EXIT_DBG(!quadFast || quadFast->getBasisFunctions() == feSpace->getBasisFcts())
      ("invalid basis functions");

768
769
770
    if (smallElInfo->getMesh() == feSpace->getMesh())
      return getVecAtQPs(smallElInfo, quad, quadFast, vecAtQPs);

Thomas Witkowski's avatar
Thomas Witkowski committed
771
    const BasisFunction *basFcts = feSpace->getBasisFcts();
772
773
    int nPoints = 
      quadFast ? quadFast->getQuadrature()->getNumPoints() : quad->getNumPoints(); 
Thomas Witkowski's avatar
Thomas Witkowski committed
774
775
776
777
778
779
780
781
782
    static double *localvec = NULL;
    double *result;

    if (vecAtQPs) {
      result = vecAtQPs;
    } else {
      if (localvec) 
	delete [] localvec;
      localvec = new double[nPoints];
783
784
      for (int i = 0; i < nPoints; i++)
	localvec[i] = 0.0;      
Thomas Witkowski's avatar
Thomas Witkowski committed
785
786
787
788
      result = localvec;
    }
      
    double *localVec = localVectors[omp_get_thread_num()];
789
    getLocalVector(largeElInfo->getElement(), localVec);
Thomas Witkowski's avatar
Thomas Witkowski committed
790

791
    mtl::dense2D<double> &m = smallElInfo->getSubElemCoordsMat(basFcts->getDegree());
Thomas Witkowski's avatar
Thomas Witkowski committed
792
793
794
795
796

    for (int i = 0; i < nPoints; i++) {
      result[i] = 0.0;
      for (int j = 0; j < nBasFcts; j++) {
	double val = 0.0;
797
	for (int k = 0; k < nBasFcts; k++)
798
	  val += m[j][k] * (*(basFcts->getPhi(k)))(quad->getLambda(i));
799
	
Thomas Witkowski's avatar
Thomas Witkowski committed
800
801
802
803
804
805
806
	result[i] += localVec[j] * val;
      }
    }

    return const_cast<const double*>(result);
  }

807

Thomas Witkowski's avatar
Thomas Witkowski committed
808
809
810
811
812
813
814
815
816
817
  template<>
  void DOFVectorBase<double>::assemble(double factor, ElInfo *elInfo,
				       const BoundaryType *bound, 
				       Operator *op)
  {
    FUNCNAME("DOFVector::assemble()");

    if (!(op || this->operators.size())) 
      return;

818
    set_to_zero(this->elementVector);
819
    bool addVector = false;
Thomas Witkowski's avatar
Thomas Witkowski committed
820
821
822

    if (op) {
      op->getElementVector(elInfo, this->elementVector);
823
      addVector = true;
Thomas Witkowski's avatar
Thomas Witkowski committed
824
825
826
827
828
829
    } else {
      std::vector<Operator*>::iterator it;
      std::vector<double*>::iterator factorIt;

      for (it = this->operators.begin(), factorIt = this->operatorFactor.begin();	
	   it != this->operators.end(); 
830
	   ++it, ++factorIt)
831
	if ((*it)->getNeedDualTraverse() == false) {
Thomas Witkowski's avatar
Thomas Witkowski committed
832
	  (*it)->getElementVector(elInfo, this->elementVector, 
833
				  *factorIt ? **factorIt : 1.0);      
834
835
	  addVector = true;
	}
Thomas Witkowski's avatar
Thomas Witkowski committed
836
837
    }

838
839
    if (addVector)
      addElementVector(factor, this->elementVector, bound, elInfo);
Thomas Witkowski's avatar
Thomas Witkowski committed
840
841
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
842

Thomas Witkowski's avatar
Thomas Witkowski committed
843
844
845
846
847
848
849
850
851
852
853
854
  template<>
  void DOFVectorBase<double>::assemble2(double factor, 
					ElInfo *mainElInfo, ElInfo *auxElInfo,
					ElInfo *smallElInfo, ElInfo *largeElInfo,
					const BoundaryType *bound, 
					Operator *op)
  {
    FUNCNAME("DOFVector::assemble2()");

    if (!(op || this->operators.size())) 
      return;

855
    set_to_zero(this->elementVector);
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
    bool addVector = false;

    TEST_EXIT(!op)("Not yet implemented!\n");

    std::vector<Operator*>::iterator it;
    std::vector<double*>::iterator factorIt;
    for (it = this->operators.begin(), factorIt = this->operatorFactor.begin();	
	 it != this->operators.end(); 
	 ++it, ++factorIt)
      if ((*it)->getNeedDualTraverse()) {
	(*it)->getElementVector(mainElInfo, auxElInfo,
				smallElInfo, largeElInfo,
				this->elementVector, 
				*factorIt ? **factorIt : 1.0);
	addVector = true;
Thomas Witkowski's avatar
Thomas Witkowski committed
871
872
      }
  
873
874
    if (addVector)
      addElementVector(factor, this->elementVector, bound, mainElInfo);
Thomas Witkowski's avatar
Thomas Witkowski committed
875
876
877
  }


878
879
880
881
  double integrate(const DOFVector<double> &vec1,
		   const DOFVector<double> &vec2,
		   BinaryAbstractFunction<double, double, double> *fct)
  {
882
    if (vec1.getFeSpace()->getMesh() == vec2.getFeSpace()->getMesh())
883
884
885
886
887
888
889
890
891
892
893
894
895
896
      return intSingleMesh(vec1, vec2, fct);
    else
      return intDualMesh(vec1, vec2, fct);
  }


  double intSingleMesh(const DOFVector<double> &vec1,
		       const DOFVector<double> &vec2,
		       BinaryAbstractFunction<double, double, double> *fct)
  {
    FUNCNAME("intDualmesh()");
    
    TEST_EXIT(fct)("No function defined!\n");

897
    int deg = std::max(fct->getDegree(), 2 * vec1.getFeSpace()->getBasisFcts()->getDegree());
898
    Quadrature* quad = 
899
      Quadrature::provideQuadrature(vec1.getFeSpace()->getMesh()->getDim(), deg);
900
    FastQuadrature *fastQuad =
901
      FastQuadrature::provideFastQuadrature(vec1.getFeSpace()->getBasisFcts(), *quad, INIT_PHI);
902
903
904
905
906
907
908

    std::vector<double> qp1(fastQuad->getNumPoints());
    std::vector<double> qp2(fastQuad->getNumPoints());

    double value = 0.0;
    Flag traverseFlag = Mesh::CALL_LEAF_EL | Mesh::FILL_COORDS | Mesh::FILL_DET;      
    TraverseStack stack;    
909
    ElInfo *elInfo = stack.traverseFirst(vec1.getFeSpace()->getMesh(), -1, traverseFlag);
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
    while (elInfo) {
      double det = elInfo->getDet();
      double tmp = 0.0;      
      vec1.getVecAtQPs(elInfo, quad, fastQuad, &(qp1[0]));
      vec2.getVecAtQPs(elInfo, quad, fastQuad, &(qp2[0]));      
      for (int iq = 0; iq < fastQuad->getNumPoints(); iq++)
 	tmp += fastQuad->getWeight(iq) * (*fct)(qp1[iq], qp2[iq]);
      
      value += tmp * det;
      
      elInfo = stack.traverseNext(elInfo);
    }

    return value;
  }


  double intDualMesh(const DOFVector<double> &vec1,
		     const DOFVector<double> &vec2,
		     BinaryAbstractFunction<double, double, double> *fct)
  {
    FUNCNAME("intDualmesh()");
    
    TEST_EXIT(fct)("No function defined!\n");

935
    int deg = std::max(fct->getDegree(), 2 * vec1.getFeSpace()->getBasisFcts()->getDegree());
936
    Quadrature* quad = 
937
      Quadrature::provideQuadrature(vec1.getFeSpace()->getMesh()->getDim(), deg);
938
    FastQuadrature *fastQuad =
939
      FastQuadrature::provideFastQuadrature(vec1.getFeSpace()->getBasisFcts(), *quad, INIT_PHI);
940
941
942
943
944
945
946
947

    std::vector<double> qp1(fastQuad->getNumPoints());
    std::vector<double> qp2(fastQuad->getNumPoints());

    double value = 0.0;
    Flag traverseFlag = Mesh::CALL_LEAF_EL | Mesh::FILL_COORDS | Mesh::FILL_DET;      
    DualTraverse dualTraverse;    
    DualElInfo dualElInfo;
948
949
    bool cont = dualTraverse.traverseFirst(vec1.getFeSpace()->getMesh(),
					   vec2.getFeSpace()->getMesh(),
950
951
952
953
954
955
					   -1, -1, traverseFlag, traverseFlag,
					   dualElInfo);
    while (cont) {
      double det = dualElInfo.smallElInfo->getDet();
      double tmp = 0.0;      
      vec1.getVecAtQPs(dualElInfo.smallElInfo, dualElInfo.largeElInfo,
956
		       quad, NULL, &(qp1[0]));
957
      vec2.getVecAtQPs(dualElInfo.smallElInfo, dualElInfo.largeElInfo,
958
959
960
		       quad, NULL, &(qp2[0]));      
      for (int iq = 0; iq < quad->getNumPoints(); iq++)
 	tmp += quad->getWeight(iq) * (*fct)(qp1[iq], qp2[iq]);
961
962
963
964
965
966
967
968
969
      
      value += tmp * det;
      
      cont = dualTraverse.traverseNext(dualElInfo);
    }

    return value;
  }

970
971
}