DOFVector.cc 29.8 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

267
    mtl::dense_vector<double> grd1(dim + 1);
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
	grd1 = 0.0;
274

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

    } else {      
Thomas Witkowski's avatar
Thomas Witkowski committed
287
      const BasisFunction *basFcts = feSpace->getBasisFcts();
288
      mtl::dense_vector<double> grdPhi(dim + 1);
289

Thomas Witkowski's avatar
Thomas Witkowski committed
290
      for (int i = 0; i < nPoints; i++) {
291
	grd1 = 0.0;
292
293

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

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

307
308
309
    return const_cast<const WorldVector<double>*>(result);
  }

310

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

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

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

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

    const BasisFunction *basFcts = feSpace->getBasisFcts();    
352
    mtl::dense2D<double> &m = smallElInfo->getSubElemCoordsMat(basFcts->getDegree());
Thomas Witkowski's avatar
Thomas Witkowski committed
353
354
355
356

    int parts = Global::getGeo(PARTS, dim);
    const DimVec<WorldVector<double> > &grdLambda = largeElInfo->getGrdLambda();

357
358
359
    mtl::dense_vector<double> grd1(dim + 1);
    mtl::dense_vector<double> grdPhi(dim + 1);
    mtl::dense_vector<double> tmp(dim + 1);
Thomas Witkowski's avatar
Thomas Witkowski committed
360
361
    
    for (int i = 0; i < nPoints; i++) {
362
      grd1 = 0.0;
Thomas Witkowski's avatar
Thomas Witkowski committed
363
364
      
      for (int j = 0; j < nBasFcts; j++) {
365
	grdPhi = 0.0;
Thomas Witkowski's avatar
Thomas Witkowski committed
366
367
368

	for (int k = 0; k < nBasFcts; k++) {
	  (*(basFcts->getGrdPhi(k)))(quad->getLambda(i), tmp);
369
	  tmp *= m[j][k];
370
	  grdPhi += tmp;
Thomas Witkowski's avatar
Thomas Witkowski committed
371
372
	}

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

387

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

398
399
400
    if (quad && quadFast) {
      TEST_EXIT_DBG(quad == quadFast->getQuadrature())
      	("quad != quadFast->quadrature\n");
401
402
    }

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

406
407
    Element *el = elInfo->getElement(); 

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

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

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

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
439
440
441
442
	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);
443
444
	}

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

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

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

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

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

481
482
483
    return const_cast<const WorldMatrix<double>*>(result);
  }

484

485
486
487
  template<>
  void DOFVector<double>::interpol(DOFVector<double> *source, double factor) 
  {
488
    FUNCNAME("DOFVector<double>::interpol()");
489

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

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
512
	basisFcts->getLocalIndices(el, feSpace->getAdmin(), myLocalIndices);
513
514
	source->getLocalVector(el, sourceLocalCoeffs);

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

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

567
568

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

    this->set(nul);

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

579
    if (feSpace == vFeSpace)
580
581
582
      WARNING("same FE-spaces\n");

    const BasisFunction *basFcts = feSpace->getBasisFcts();
583
    const BasisFunction *vBasFcts = vFeSpace->getBasisFcts();
584
585
586
587

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

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

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

	for (int i = 0; i < numBasFcts; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
602
	  if (vec[myLocalIndices[i]] == nul) {
603
	    coords = basFcts->getCoords(i);
604
	    vec[myLocalIndices[i]] += vBasFcts->evalUh(*coords, vLocalCoeffs, NULL) * factor;
605
606
607
608
609
610
611
612
613
614
	  }
	}
	elInfo = stack.traverseNext(elInfo);
      }
    } 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
    std::vector<int> nNodeDOFs;
    std::vector<int> nNodePreDofs;
654
    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
      GeoIndex geoIndex = INDEX_OF_DIM(i, dim);
      int numPositionNodes = mesh->getGeo(geoIndex);
662
      int numPreDofs = admin->getNumberOfPreDofs(i);
663
      for (int j = 0; j < numPositionNodes; j++) {
664
	int dofs = basFcts->getNumberOfDofs(geoIndex);
665
	nNodeDOFs.push_back(dofs);
666
	nDofs += dofs;
667
	nNodePreDofs.push_back(numPreDofs);
668
      }
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
    ElInfo *elInfo = stack.traverseFirst(mesh, -1, fillFlag);
    WorldVector<double> grd;
684
    ElementVector localUh(basFcts->getNumber());
685

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

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

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

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

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

      elInfo = stack.traverseNext(elInfo);
    }

    return result;
  }


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

719

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

723
724
725
726

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

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

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

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

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

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

    return r;
  }

750

Thomas Witkowski's avatar
Thomas Witkowski committed
751
  template<>
752
753
754
755
756
  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
757
758
  {
    FUNCNAME("DOFVector<double>::getVecAtQPs()");
759
 
Thomas Witkowski's avatar
Thomas Witkowski committed
760
761
    TEST_EXIT_DBG(quad || quadFast)("neither quad nor quadFast defined\n");

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

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

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

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

777
778
779
780
    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
781

782
    mtl::dense2D<double> &m = smallElInfo->getSubElemCoordsMat(basFcts->getDegree());
Thomas Witkowski's avatar
Thomas Witkowski committed
783
784

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

	vecAtQPs[i] += localVec[j] * val;
Thomas Witkowski's avatar
Thomas Witkowski committed
792
793
794
795
      }
    }
  }

796

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

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

807
    set_to_zero(this->elementVector);
808
    bool addVector = false;
Thomas Witkowski's avatar
Thomas Witkowski committed
809
810
811

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

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

827
828
    if (addVector)
      addElementVector(factor, this->elementVector, bound, elInfo);
Thomas Witkowski's avatar
Thomas Witkowski committed
829
830
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
831

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

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


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

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

893
894
    mtl::dense_vector<double> qp1(fastQuad->getNumPoints());
    mtl::dense_vector<double> qp2(fastQuad->getNumPoints());
895
896
897
898

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

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

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

931
932
    mtl::dense_vector<double> qp1(fastQuad->getNumPoints());
    mtl::dense_vector<double> qp2(fastQuad->getNumPoints());
933
934
935
936
937

    double value = 0.0;
    Flag traverseFlag = Mesh::CALL_LEAF_EL | Mesh::FILL_COORDS | Mesh::FILL_DET;      
    DualTraverse dualTraverse;    
    DualElInfo dualElInfo;
938
939
    bool cont = dualTraverse.traverseFirst(vec1.getFeSpace()->getMesh(),
					   vec2.getFeSpace()->getMesh(),
940
941
942
					   -1, -1, traverseFlag, traverseFlag,
					   dualElInfo);
    while (cont) {
Thomas Witkowski's avatar