DOFVector.cc 28.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
//
// Software License for AMDiS
//
// Copyright (c) 2010 Dresden University of Technology 
// All rights reserved.
// Authors: Simon Vey, Thomas Witkowski et al.
//
// This file is part of AMDiS
//
// See also license.opensource.txt in the distribution.


13
#include <boost/numeric/mtl/mtl.hpp>
14
15
16
17
18
19
20
21
22
23
#include "DOFVector.h"
#include "Traverse.h"
#include "DualTraverse.h"
#include "FixVec.h"

namespace AMDiS {

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

26
    switch (coarsenOperation) {
27
28
29
30
31
32
33
    case NO_OPERATION:
      return;
      break;
    case COARSE_RESTRICT:
      (const_cast<BasisFunction*>(feSpace->getBasisFcts()))->coarseRestr(this, &list, n);
      break;
    case COARSE_INTERPOL:
34
35
      TEST_EXIT_DBG(feSpace)("Should not happen!\n");
      TEST_EXIT_DBG(feSpace->getBasisFcts())("Shoud not happen!\n");
36

37
38
39
      (const_cast<BasisFunction*>(feSpace->getBasisFcts()))->coarseInter(this, &list, n);
      break;
    default:
40
41
      WARNING("Invalid coarsen operation \"%d\" in vector \"%s\"\n", 
	      coarsenOperation, this->name.c_str());
42
43
44
    }
  }

45

46
47
48
49
50
51
  template<>
  void DOFVector<double>::refineInterpol(RCNeighbourList& list, int n)
  {
    (const_cast<BasisFunction*>(feSpace->getBasisFcts()))->refineInter(this, &list, n);
  }

52

53
54
55
  template<>
  void DOFVector<WorldVector<double> >::refineInterpol(RCNeighbourList& list, int n)
  {
56
57
58
    if (n < 1) 
      return;

59
    Element *el = list.getElement(0);
60
    int n0 = feSpace->getAdmin()->getNumberOfPreDofs(VERTEX);
61
62
63
    DegreeOfFreedom dof0 = el->getDof(0, n0);
    DegreeOfFreedom dof1 = el->getDof(1, n0);
    DegreeOfFreedom dof_new = el->getChild(0)->getDof(feSpace->getMesh()->getDim(), n0);
64
65
66
67
68
    vec[dof_new] = vec[dof0];
    vec[dof_new] += vec[dof1];
    vec[dof_new] *= 0.5;
  }

69

70
71
72
73
74
75
76
77
78
  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
79
    if (grad) {
80
81
      result = grad;
    } else {
82
      if(result && result->getFeSpace() != feSpace) {
83
84
	delete result;
	result = new DOFVector<WorldVector<double> >(feSpace, "gradient");
85
86
87
88
89
90
91
92
93
      }
    }

    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
94
95
    std::vector<int> nNodeDOFs;
    std::vector<int> nNodePreDofs;
96
    std::vector<DimVec<double>*> bary;
97

98
99
    int nNodes = 0;
    int nDofs = 0;
100

Thomas Witkowski's avatar
Thomas Witkowski committed
101
    for (int i = 0; i < dim + 1; i++) {
102
      GeoIndex geoIndex = INDEX_OF_DIM(i, dim);
103
      int nPositions = mesh->getGeo(geoIndex);
104
      int numPreDofs = admin->getNumberOfPreDofs(i);
105
      for (int j = 0; j < nPositions; j++) {
106
	int dofs = basFcts->getNumberOfDofs(geoIndex);
107
	nNodeDOFs.push_back(dofs);
108
	nDofs += dofs;
109
	nNodePreDofs.push_back(numPreDofs);
110
      }
111
      nNodes += nPositions;
112
113
    }

114
    TEST_EXIT_DBG(nDofs == basFcts->getNumber())
115
      ("number of dofs != number of basis functions\n");
116
    
117
    for (int i = 0; i < nDofs; i++)
118
119
      bary.push_back(basFcts->getCoords(i));    

120
    ElementVector localUh(basFcts->getNumber());
121
122

    // traverse mesh
123
    std::vector<bool> visited(getUsedSize(), false);
124
    TraverseStack stack;
125
    Flag fillFlag = Mesh::CALL_LEAF_EL | Mesh::FILL_GRD_LAMBDA | Mesh::FILL_COORDS;
126
127
    ElInfo *elInfo = stack.traverseFirst(mesh, -1, fillFlag);

Thomas Witkowski's avatar
Thomas Witkowski committed
128
    while (elInfo) {
129
      const DegreeOfFreedom **dof = elInfo->getElement()->getDof();
130
      const DimVec<WorldVector<double> > &grdLambda = elInfo->getGrdLambda();
131
      getLocalVector(elInfo->getElement(), localUh);
132
133

      int localDOFNr = 0;
134
      for (int i = 0; i < nNodes; i++) { // for all nodes
135
136
	for (int j = 0; j < nNodeDOFs[i]; j++) { // for all dofs at this node
	  DegreeOfFreedom dofIndex = dof[i][nNodePreDofs[i] + j];
Thomas Witkowski's avatar
Thomas Witkowski committed
137
138
139
	  if (!visited[dofIndex]) {	  
	    basFcts->evalGrdUh(*(bary[localDOFNr]), grdLambda, 
			       localUh, &((*result)[dofIndex]));
140
141
142
143
144
145
146
147
148
149
150
151
152

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

      elInfo = stack.traverseNext(elInfo);
    }

    return result;
  }

153

154
155
156
157
158
159
  template<>
  DOFVector<WorldVector<double> >*
  DOFVector<double>::getRecoveryGradient(DOFVector<WorldVector<double> > *grad) const 
  {
    FUNCNAME("DOFVector<double>::getRecoveryGradient()");

160
161
162
163
#ifdef _OPENMP
      ERROR_EXIT("Using static variable while using OpenMP parallelization!\n");
#endif

164
165
166
167
168
    // define result vector
    static DOFVector<WorldVector<double> > *vec = NULL;

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

Thomas Witkowski's avatar
Thomas Witkowski committed
169
    if (!result) {
170
      if (vec && vec->getFeSpace() != feSpace) {
171
	delete vec;
172
173
	vec = NULL;
      }
174
      if (!vec)
175
	vec = new DOFVector<WorldVector<double> >(feSpace, "gradient");
176
      
177
178
179
180
181
182
183
184
185
      result = vec;
    }

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

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

    const BasisFunction *basFcts = feSpace->getBasisFcts();
186
    int nPreDofs = feSpace->getAdmin()->getNumberOfPreDofs(VERTEX);
187

188
    DimVec<double> bary(dim, DEFAULT_VALUE, (1.0 / (dim + 1.0)));
Thomas Witkowski's avatar
Thomas Witkowski committed
189
    WorldVector<double> grd;
190
191

    // traverse mesh
Thomas Witkowski's avatar
Thomas Witkowski committed
192
    Mesh *mesh = feSpace->getMesh();
193
    TraverseStack stack;
Thomas Witkowski's avatar
Thomas Witkowski committed
194
195
    ElInfo *elInfo = stack.traverseFirst(mesh, -1,
					 Mesh::CALL_LEAF_EL | Mesh::FILL_DET | 
196
					 Mesh::FILL_GRD_LAMBDA | Mesh::FILL_COORDS);
197

198
    ElementVector localUh(basFcts->getNumber());
199

Thomas Witkowski's avatar
Thomas Witkowski committed
200
    while (elInfo) {
201
      double det = elInfo->getDet();
202
      const DegreeOfFreedom **dof = elInfo->getElement()->getDof();
203
      const DimVec<WorldVector<double> > &grdLambda = elInfo->getGrdLambda();
204
      getLocalVector(elInfo->getElement(), localUh);
Thomas Witkowski's avatar
Thomas Witkowski committed
205
      basFcts->evalGrdUh(bary, grdLambda, localUh, &grd);
206

Thomas Witkowski's avatar
Thomas Witkowski committed
207
      for (int i = 0; i < dim + 1; i++) {
208
	DegreeOfFreedom dofIndex = dof[i][nPreDofs];
209
210
211
212
213
214
215
216
217
218
	(*result)[dofIndex] += grd * det;
	volume[dofIndex] += det;
      }

      elInfo = stack.traverseNext(elInfo);
    }

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

219
220
    for (volIt.reset(), grdIt.reset(); !volIt.end(); ++volIt, ++grdIt)
      if (*volIt != 0.0)
Thomas Witkowski's avatar
Thomas Witkowski committed
221
	*grdIt *= 1.0 / (*volIt);
222
223
224
225

    return result;
  }

226

227
  template<>
228
229
  const WorldVector<double> *DOFVectorBase<double>::getGrdAtQPs(const ElInfo *elInfo, 
								const Quadrature *quad,
230
								const FastQuadrature *quadFast,
231
								WorldVector<double> *grdAtQPs) const
232
233
234
  {
    FUNCNAME("DOFVector<double>::getGrdAtQPs()");
  
235
    TEST_EXIT_DBG(quad || quadFast)("neither quad nor quadFast defined\n");
236

237
238
239
    if (quad && quadFast) {
      TEST_EXIT_DBG(quad == quadFast->getQuadrature())
      	("quad != quadFast->quadrature\n");
240
241
    }

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

    int dow = Global::getGeo(WORLD);
Thomas Witkowski's avatar
Thomas Witkowski committed
246
247
    int myRank = omp_get_thread_num();
    int nPoints = quadFast ? quadFast->getQuadrature()->getNumPoints() : quad->getNumPoints();
248
249
250
251
252
253
    static WorldVector<double> *grd = NULL;
    WorldVector<double> *result;

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

Thomas Witkowski's avatar
Thomas Witkowski committed
257
      grd = new WorldVector<double>[nPoints];
258
      for (int i = 0; i < nPoints; i++)
259
	grd[i].set(0.0);
260
      
261
262
263
      result = grd;
    }
  
264
265
    const ElementVector& localVec = localVectors[myRank];
    getLocalVector(elInfo->getElement(), const_cast<ElementVector&>(localVec));
266

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

271
    if (quadFast) {
Thomas Witkowski's avatar
Thomas Witkowski committed
272
      for (int i = 0; i < nPoints; i++) {
273
	for (int j = 0; j < dim + 1; j++)
274
275
	  grd1[j] = 0.0;

276
277
	for (int j = 0; j < nBasFcts; j++)
	  for (int k = 0; k < parts; k++)
278
	    grd1[k] += quadFast->getGradient(i, j, k) * localVec[j];
279
	  
280
281
	for (int l=0; l < dow; l++) {
	  result[i][l] = 0.0;
282
	  for (int k = 0; k < parts; k++)
283
284
285
	    result[i][l] += grdLambda[k][l] * grd1[k];
	}
      }
286
287

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

Thomas Witkowski's avatar
Thomas Witkowski committed
291
      for (int i = 0; i < nPoints; i++) {
292
	for (int j = 0; j < dim + 1; j++)
293
	  grd1[j] = 0.0;
294
295

	for (int j = 0; j < nBasFcts; j++) {
296
	  (*(basFcts->getGrdPhi(j)))(quad->getLambda(i), *grdPhi);
297
	  for (int k = 0; k < parts; k++)
298
	    grd1[k] += (*grdPhi)[k] * localVec[j];
299
300
	}

301
302
	for (int l = 0; l < dow; l++) {
	  result[i][l] = 0.0;
303
	  for (int k = 0; k < parts; k++)
304
305
306
307
	    result[i][l] += grdLambda[k][l] * grd1[k];
	}
      }
    }
308

309
310
311
    return const_cast<const WorldVector<double>*>(result);
  }

312

313
  template<>
Thomas Witkowski's avatar
Thomas Witkowski committed
314
315
316
317
318
319
320
321
322
323
  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");

324
    if (quad && quadFast) {
325
      TEST_EXIT_DBG(quad == quadFast->getQuadrature())("quad != quadFast->quadrature\n");
326
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343

    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];
344
      for (int i = 0; i < nPoints; i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
345
	grd[i].set(0.0);
346

Thomas Witkowski's avatar
Thomas Witkowski committed
347
348
349
      result = grd;
    }
  
350
351
    const ElementVector& localVec = localVectors[myRank];
    getLocalVector(largeElInfo->getElement(), const_cast<ElementVector&>(localVec));
352
353

    const BasisFunction *basFcts = feSpace->getBasisFcts();    
354
    mtl::dense2D<double> &m = smallElInfo->getSubElemCoordsMat(basFcts->getDegree());
Thomas Witkowski's avatar
Thomas Witkowski committed
355
356
357
358
359
360
361
362
363

    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++) {
364
      for (int j = 0; j < dim + 1; j++)
Thomas Witkowski's avatar
Thomas Witkowski committed
365
366
367
368
369
370
371
	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);
372
	  tmp *= m[j][k];
Thomas Witkowski's avatar
Thomas Witkowski committed
373
374
375
	  *grdPhi += tmp;
	}

376
	for (int k = 0; k < parts; k++)
Thomas Witkowski's avatar
Thomas Witkowski committed
377
378
379
380
381
	  grd1[k] += (*grdPhi)[k] * localVec[j];
      }
      
      for (int l = 0; l < dow; l++) {
	result[i][l] = 0.0;
382
	for (int k = 0; k < parts; k++)
Thomas Witkowski's avatar
Thomas Witkowski committed
383
384
385
386
387
388
389
	  result[i][l] += grdLambda[k][l] * grd1[k];
      }
    }    
  
    return const_cast<const WorldVector<double>*>(result);
  }

390

Thomas Witkowski's avatar
Thomas Witkowski committed
391
392
393
  template<>
  const WorldMatrix<double> *DOFVectorBase<double>::getD2AtQPs(const ElInfo *elInfo, 
							       const Quadrature *quad,
394
395
396
397
398
							       const FastQuadrature *quadFast,
							       WorldMatrix<double>  *d2AtQPs) const
  {
    FUNCNAME("DOFVector<double>::getD2AtQPs()");
  
399
    TEST_EXIT_DBG(quad || quadFast)("neither quad nor quadFast defined\n");
400

401
402
403
    if (quad && quadFast) {
      TEST_EXIT_DBG(quad == quadFast->getQuadrature())
      	("quad != quadFast->quadrature\n");
404
405
    }

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

409
410
    Element *el = elInfo->getElement(); 

Thomas Witkowski's avatar
Thomas Witkowski committed
411
    int myRank = omp_get_thread_num();
412
    int dow = Global::getGeo(WORLD);
Thomas Witkowski's avatar
Thomas Witkowski committed
413
    int nPoints = quadFast ? quadFast->getQuadrature()->getNumPoints() : quad->getNumPoints();
414
415
416
417
418
419
420
421

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

    if (d2AtQPs) {
      result = d2AtQPs;
    } else {
      if(vec) delete [] vec;
Thomas Witkowski's avatar
Thomas Witkowski committed
422
423
      vec = new WorldMatrix<double>[nPoints];
      for (int i = 0; i < nPoints; i++) {
424
425
426
427
428
	vec[i].set(0.0);
      }
      result = vec;
    }
  
429
430
    const ElementVector& localVec = localVectors[myRank];
    getLocalVector(el, const_cast<ElementVector&>(localVec));
431
432
433
434
435

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

436
    if (quadFast) {
Thomas Witkowski's avatar
Thomas Witkowski committed
437
438
439
      for (int iq = 0; iq < nPoints; iq++) {
	for (int k = 0; k < parts; k++)
	  for (int l = 0; l < parts; l++)
440
441
	    D2Tmp[k][l] = 0.0;

Thomas Witkowski's avatar
Thomas Witkowski committed
442
443
444
445
	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);
446
447
	}

Thomas Witkowski's avatar
Thomas Witkowski committed
448
449
	for (int i = 0; i < dow; i++)
	  for (int j = 0; j < dow; j++) {
450
	    result[iq][i][j] = 0.0;
Thomas Witkowski's avatar
Thomas Witkowski committed
451
452
	    for (int k = 0; k < parts; k++)
	      for (int l = 0; l < parts; l++)
453
454
455
456
		result[iq][i][j] += grdLambda[k][i]*grdLambda[l][j]*D2Tmp[k][l];
	  }
      }
    } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
457
      const BasisFunction *basFcts = feSpace->getBasisFcts();
458
459
      DimMat<double>* D2Phi = D2Phis[omp_get_thread_num()];

Thomas Witkowski's avatar
Thomas Witkowski committed
460
461
462
      for (int iq = 0; iq < nPoints; iq++) {
	for (int k = 0; k < parts; k++)
	  for (int l = 0; l < parts; l++)
463
464
	    D2Tmp[k][l] = 0.0;

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

Thomas Witkowski's avatar
Thomas Witkowski committed
469
470
	  for (int k = 0; k < parts; k++)
	    for (int l = 0; l < parts; l++)
471
	      D2Tmp[k][l] += localVec[i] * (*D2Phi)[k][l];
472
473
	}

Thomas Witkowski's avatar
Thomas Witkowski committed
474
475
	for (int i = 0; i < dow; i++)
	  for (int j = 0; j < dow; j++) {
476
	    result[iq][i][j] = 0.0;
Thomas Witkowski's avatar
Thomas Witkowski committed
477
478
	    for (int k = 0; k < parts; k++)
	      for (int l = 0; l < parts; l++)
479
		result[iq][i][j] += grdLambda[k][i] * grdLambda[l][j] * D2Tmp[k][l];
480
481
482
	  }
      }
    }
483

484
485
486
    return const_cast<const WorldMatrix<double>*>(result);
  }

487

488
489
490
  template<>
  void DOFVector<double>::interpol(DOFVector<double> *source, double factor) 
  {
491
    FUNCNAME("DOFVector<double>::interpol()");
492

493
    const FiniteElemSpace *sourceFeSpace = source->getFeSpace();
494
495
496
497
498
499
500
501
    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
502
    DegreeOfFreedom *myLocalIndices = localIndices[omp_get_thread_num()];
503
    ElementVector sourceLocalCoeffs(nSourceBasisFcts);
504

505
    if (feSpace->getMesh() == sourceFeSpace->getMesh()) {
506
507
508
509
510
511
      DimVec<double> *coords = NULL;
      TraverseStack stack;
      ElInfo *elInfo = stack.traverseFirst(feSpace->getMesh(), -1,
					   Mesh::CALL_LEAF_EL | 
					   Mesh::FILL_COORDS);

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

Thomas Witkowski's avatar
Thomas Witkowski committed
515
	basisFcts->getLocalIndices(el, feSpace->getAdmin(), myLocalIndices);
516
517
	source->getLocalVector(el, sourceLocalCoeffs);

518
	for (int i = 0; i < nBasisFcts; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
519
	  if (vec[myLocalIndices[i]] == 0.0) {
520
	    coords = basisFcts->getCoords(i);
Thomas Witkowski's avatar
Thomas Witkowski committed
521
	    vec[myLocalIndices[i]] = sourceBasisFcts->evalUh(*coords, sourceLocalCoeffs) * factor;
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
	  }
	}
	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);
540
      while (nextTraverse) {     
541
	basisFcts->getLocalIndices(elInfo1->getElement(), feSpace->getAdmin(), 
Thomas Witkowski's avatar
Thomas Witkowski committed
542
				   myLocalIndices);
543
	source->getLocalVector(elInfo2->getElement(), sourceLocalCoeffs);
544

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

570
571

  template<>
572
573
  void DOFVector<WorldVector<double> >::interpol(DOFVector<WorldVector<double> > *v, 
						 double factor) 
574
575
576
577
578
579
  {
    WorldVector<double> nul(DEFAULT_VALUE,0.0);

    this->set(nul);

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

582
    if (feSpace == vFeSpace)
583
584
585
      WARNING("same FE-spaces\n");

    const BasisFunction *basFcts = feSpace->getBasisFcts();
586
    const BasisFunction *vBasFcts = vFeSpace->getBasisFcts();
587
588
589
590

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

591
    if (feSpace->getMesh() == vFeSpace->getMesh()) {
Thomas Witkowski's avatar
Thomas Witkowski committed
592
      DegreeOfFreedom *myLocalIndices = localIndices[omp_get_thread_num()];
593
      mtl::dense_vector<WorldVector<double> > vLocalCoeffs(vNumBasFcts);
594
595
      Mesh *mesh = feSpace->getMesh();
      TraverseStack stack;
596
597
      ElInfo *elInfo = 
	stack.traverseFirst(mesh, -1, Mesh::CALL_LEAF_EL | Mesh::FILL_COORDS);
598
599
600

      while (elInfo) {
	Element *el = elInfo->getElement();
Thomas Witkowski's avatar
Thomas Witkowski committed
601
	basFcts->getLocalIndices(el, feSpace->getAdmin(), myLocalIndices);
602
603
604
	v->getLocalVector(el, vLocalCoeffs);

	for (int i = 0; i < numBasFcts; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
605
	  if (vec[myLocalIndices[i]] == nul) {
606
	    coords = basFcts->getCoords(i);
607
	    vec[myLocalIndices[i]] += vBasFcts->evalUh(*coords, vLocalCoeffs, NULL) * factor;
608
609
610
611
612
613
614
615
616
617
	  }
	}
	elInfo = stack.traverseNext(elInfo);
      }
    } else {
      ERROR_EXIT("not yet for dual traverse\n");
    }
  }


618
619
620
621
622
  template<>
  WorldVector<DOFVector<double>*> *DOFVector<double>::getGradient(WorldVector<DOFVector<double>*> *grad) const
  {
    FUNCNAME("DOFVector<double>::getGradient()");

623
624
625
626
#ifdef _OPENMP
      ERROR_EXIT("Using static variable while using OpenMP parallelization!\n");
#endif

627
628
629
630
631
632
633
634
635
636
637
    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;

638
    if (grad) {
639
640
      result = grad;
    } else {
641
      if (!result) {
642
	result = new WorldVector<DOFVector<double>*>;
643

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

    // count number of nodes and dofs per node
655
656
    std::vector<int> nNodeDOFs;
    std::vector<int> nNodePreDofs;
657
    std::vector<DimVec<double>*> bary;
658

659
660
    int nNodes = 0;
    int nDofs = 0;
661

662
    for (int i = 0; i < dim + 1; i++) {
663
664
      GeoIndex geoIndex = INDEX_OF_DIM(i, dim);
      int numPositionNodes = mesh->getGeo(geoIndex);
665
      int numPreDofs = admin->getNumberOfPreDofs(i);
666
      for (int j = 0; j < numPositionNodes; j++) {
667
	int dofs = basFcts->getNumberOfDofs(geoIndex);
668
	nNodeDOFs.push_back(dofs);
669
	nDofs += dofs;
670
	nNodePreDofs.push_back(numPreDofs);
671
      }
672
      nNodes += numPositionNodes;
673
674
    }

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

    // traverse mesh
682
    std::vector<bool> visited(getUsedSize(), false);
683
    TraverseStack stack;
684
    Flag fillFlag = Mesh::CALL_LEAF_EL | Mesh::FILL_GRD_LAMBDA | Mesh::FILL_COORDS;
685
686
    ElInfo *elInfo = stack.traverseFirst(mesh, -1, fillFlag);
    WorldVector<double> grd;
687
    ElementVector localUh(basFcts->getNumber());
688

689
    while (elInfo) {
690
      const DegreeOfFreedom **dof = elInfo->getElement()->getDof();
691
      getLocalVector(elInfo->getElement(), localUh);
692
693
694
      const DimVec<WorldVector<double> > &grdLambda = elInfo->getGrdLambda();

      int localDOFNr = 0;
695
      for (int i = 0; i < nNodes; i++) { // for all nodes
696
697
	for (int j = 0; j < nNodeDOFs[i]; j++) { // for all dofs at this node
	  DegreeOfFreedom dofIndex = dof[i][nNodePreDofs[i] + j];
698

699
	  if (!visited[dofIndex]) {
700
	    basFcts->evalGrdUh(*(bary[localDOFNr]), grdLambda, localUh, &grd);
701

702
703
	    for (int k = 0; k < dow; k++)
	      (*(*result)[k])[dofIndex] = grd[k];	    
704
705
706
707
708
709
710
711
712
713
714
715
716
717

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

      elInfo = stack.traverseNext(elInfo);
    }

    return result;
  }


718
719
  DOFVectorDOF::DOFVectorDOF() : 
    DOFVector<DegreeOfFreedom>() 
Thomas Witkowski's avatar
Thomas Witkowski committed
720
  {}
721

722

723
  void DOFVectorDOF::freeDOFContent(DegreeOfFreedom dof) 
724
725
  {}

726
727
728
729

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

732
    TEST_EXIT_DBG(vec)("no vector\n");
733

734
    int dow = Global::getGeo(WORLD);
735
736
    static WorldVector<DOFVector<double>*> *result = NULL;

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

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

    int vecSize = vec->getSize();
746
747
    for (int i = 0; i < vecSize; i++)
      for (int j = 0; j < dow; j++)
748
749
750
751
752
	(*((*r)[j]))[i] = (*vec)[i][j];

    return r;
  }

753

Thomas Witkowski's avatar
Thomas Witkowski committed
754
  template<>
755
756
757
758
759
  void DOFVectorBase<double>::getVecAtQPs(const ElInfo *smallElInfo, 
					  const ElInfo *largeElInfo,
					  const Quadrature *quad,
					  const FastQuadrature *quadFast,
					  mtl::dense_vector<double>& vecAtQPs) const
Thomas Witkowski's avatar
Thomas Witkowski committed
760
761
  {
    FUNCNAME("DOFVector<double>::getVecAtQPs()");
762
 
Thomas Witkowski's avatar
Thomas Witkowski committed
763
764
    TEST_EXIT_DBG(quad || quadFast)("neither quad nor quadFast defined\n");

765
    if (quad && quadFast) {
Thomas Witkowski's avatar
Thomas Witkowski committed
766
767
      TEST_EXIT_DBG(quad == quadFast->getQuadrature())
      	("quad != quadFast->quadrature\n");
768
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
769
770
771
772

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

773
    if (smallElInfo->getMesh() == feSpace->getMesh())
774
      return getVecAtQPs(smallElInfo, quad, quadFast, vecAtQPs);    
775

Thomas Witkowski's avatar
Thomas Witkowski committed
776
    const BasisFunction *basFcts = feSpace->getBasisFcts();
777
778
    int nPoints = 
      quadFast ? quadFast->getQuadrature()->getNumPoints() : quad->getNumPoints(); 
Thomas Witkowski's avatar
Thomas Witkowski committed
779

780
781
782
783
    vecAtQPs.change_dim(nPoints);
    
    const ElementVector &localVec = localVectors[omp_get_thread_num()];
    getLocalVector(largeElInfo->getElement(), const_cast<ElementVector&>(localVec));
Thomas Witkowski's avatar
Thomas Witkowski committed
784

785
    mtl::dense2D<double> &m = smallElInfo->getSubElemCoordsMat(basFcts->getDegree());
Thomas Witkowski's avatar
Thomas Witkowski committed
786
787

    for (int i = 0; i < nPoints; i++) {
788
      vecAtQPs[i] = 0.0;
Thomas Witkowski's avatar
Thomas Witkowski committed
789
790
      for (int j = 0; j < nBasFcts; j++) {
	double val = 0.0;
791
	for (int k = 0; k < nBasFcts; k++)
792
	  val += m[j][k] * (*(basFcts->getPhi(k)))(quad->getLambda(i));
793
794

	vecAtQPs[i] += localVec[j] * val;
Thomas Witkowski's avatar
Thomas Witkowski committed
795
796
797
798
      }
    }
  }

799

Thomas Witkowski's avatar
Thomas Witkowski committed
800
801
802
803
804
805
806
807
808
809
  template<>
  void DOFVectorBase<double>::assemble(double factor, ElInfo *elInfo,
				       const BoundaryType *bound, 
				       Operator *op)
  {
    FUNCNAME("DOFVector::assemble()");

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

810
    set_to_zero(this->elementVector);
811
    bool addVector = false;
Thomas Witkowski's avatar
Thomas Witkowski committed
812
813
814

    if (op) {
      op->getElementVector(elInfo, this->elementVector);
815
      addVector = true;
Thomas Witkowski's avatar
Thomas Witkowski committed
816
817
818
819
820
821
    } else {
      std::vector<Operator*>::iterator it;
      std::vector<double*>::iterator factorIt;

      for (it = this->operators.begin(), factorIt = this->operatorFactor.begin();	
	   it != this->operators.end(); 
822
	   ++it, ++factorIt)
823
	if ((*it)->getNeedDualTraverse() == false) {
Thomas Witkowski's avatar
Thomas Witkowski committed
824
	  (*it)->getElementVector(elInfo, this->elementVector, 
825
				  *factorIt ? **factorIt : 1.0);      
826
827
	  addVector = true;
	}
Thomas Witkowski's avatar
Thomas Witkowski committed
828
829
    }

830
831
    if (addVector)
      addElementVector(factor, this->elementVector, bound, elInfo);
Thomas Witkowski's avatar
Thomas Witkowski committed
832
833
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
834

Thomas Witkowski's avatar
Thomas Witkowski committed
835
836
837
838
839
840
841
842
843
844
845
846
  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;

847
    set_to_zero(this->elementVector);
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
    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
863
864
      }
  
865
866
    if (addVector)
      addElementVector(factor, this->elementVector, bound, mainElInfo);
Thomas Witkowski's avatar
Thomas Witkowski committed
867
868
869
  }


870
871
872
873
  double integrate(const DOFVector<double> &vec1,
		   const DOFVector<double> &vec2,
		   BinaryAbstractFunction<double, double, double> *fct)
  {
874
    if (vec1.getFeSpace()->getMesh() == vec2.getFeSpace()->getMesh())
875
876
877
878
879
880
881
882
883
884
885
886
887
888
      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");

889
890
    int deg = std::max(fct->getDegree(), 
		       2 * vec1.getFeSpace()->getBasisFcts()->getDegree());
891
    Quadrature* quad = 
892
      Quadrature::provideQuadrature(vec1.getFeSpace()->getMesh()->getDim(), deg);
893
    FastQuadrature *fastQuad =
894
      FastQuadrature::provideFastQuadrature(vec1.getFeSpace()->getBasisFcts(), *quad, INIT_PHI);
895

896
897
    mtl::dense_vector<double> qp1(fastQuad->getNumPoints());
    mtl::dense_vector<double> qp2(fastQuad->getNumPoints());
898
899
900
901

    double value = 0.0;
    Flag traverseFlag = Mesh::CALL_LEAF_EL | Mesh::FILL_COORDS | Mesh::FILL_DET;      
    TraverseStack stack;    
902
    ElInfo *elInfo = stack.traverseFirst(vec1.getFeSpace()->getMesh(), -1, traverseFlag);
903
    while (elInfo) {
904
905
906
907
      vec1.getVecAtQPs(elInfo, quad, fastQuad, qp1);
      vec2.getVecAtQPs(elInfo, quad, fastQuad, qp2);

      double tmp = 0.0;
908
      for (int iq = 0; iq < fastQuad->getNumPoints(); iq++)
909
910
 	tmp += fastQuad->getWeight(iq) * (*fct)(qp1[iq], qp2[iq]);     
      value += tmp * elInfo->getDet();
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
      
      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");

927
928
    int deg = std::max(fct->getDegree(), 
		       2 * vec1.getFeSpace()->getBasisFcts()->getDegree());
929
    Quadrature* quad = 
930
      Quadrature::provideQuadrature(vec1.getFeSpace()->getMesh()->getDim(), deg);
931
    FastQuadrature *fastQuad =
932
      FastQuadrature::provideFastQuadrature(vec1.getFeSpace()->getBasisFcts(), *quad, INIT_PHI);
933

934
935
    mtl::dense_vector<double> qp1(fastQuad->getNumPoints());
    mtl::dense_vector<double> qp2(fastQuad->getNumPoints());
936
937
938
939
940

    double value = 0.0;
    Flag traverseFlag = Mesh::CALL_LEAF_EL | Mesh::FILL_COORDS | Mesh::FILL_DET;      
    DualTraverse dualTraverse;    
    DualElInfo dualElInfo;
941
942
    bool cont = dualTraverse.traverseFirst(vec1.getFeSpace()->getMesh(),
					   vec2.getFeSpace()->getMesh(),
943
944
945
					   -1, -1, traverseFlag, traverseFlag,
					   dualElInfo);
    while (cont) {
946
947
948
949
      vec1.getVecAtQPs(dualElInfo.smallElInfo, dualElInfo.largeElInfo, quad, NULL, qp1);
      vec2.getVecAtQPs(dualElInfo.smallElInfo, dualElInfo.largeElInfo, quad, NULL, qp2);

      double tmp = 0.0;
950
      for (int iq = 0; iq < quad->getNumPoints(); iq++)
951
952
 	tmp += quad->