DOFVector.cc 24.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
    switch (coarsenOperation) {
13
14
15
16
17
18
19
    case NO_OPERATION:
      return;
      break;
    case COARSE_RESTRICT:
      (const_cast<BasisFunction*>(feSpace->getBasisFcts()))->coarseRestr(this, &list, n);
      break;
    case COARSE_INTERPOL:
20
      if (feSpace == NULL || !feSpace)
21
	ERROR_EXIT("ERR1\n");
22
23

      if (feSpace->getBasisFcts() == NULL || !(feSpace->getBasisFcts()))
24
	ERROR_EXIT("ERR2\n");
25

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
      (const_cast<BasisFunction*>(feSpace->getBasisFcts()))->coarseInter(this, &list, n);
      break;
    default:
      ERROR_EXIT("invalid coarsen operation\n");
    }
  }

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

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

45
46
47
48
    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
49
    DegreeOfFreedom dof_new = el->getChild(0)->getDOF(feSpace->getMesh()->getDim(), n0);
50
51
52
53
54
55
56
57
58
59
60
61
62
63
    vec[dof_new] = vec[dof0];
    vec[dof_new] += vec[dof1];
    vec[dof_new] *= 0.5;
  }

  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
64
    if (grad) {
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
      result = grad;
    } else {
      if(result && result->getFESpace() != feSpace) {
	DELETE result;
	result = NEW DOFVector<WorldVector<double> >(feSpace, "gradient");
      }
    }

    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
88

    int numNodes = 0;
    int numDOFs = 0;

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

Thomas Witkowski's avatar
Thomas Witkowski committed
102
103
    //    TEST_EXIT_DBG(numNodes == mesh->getNumberOfNodes())
    //      ("invalid number of nodes\n");
104

105
    TEST_EXIT_DBG(numDOFs == basFcts->getNumber())
106
      ("number of dofs != number of basis functions\n");
107
    
Thomas Witkowski's avatar
Thomas Witkowski committed
108
    for (int i = 0; i < numDOFs; i++) {
109
110
111
112
      bary.push_back(basFcts->getCoords(i));
    }

    // traverse mesh
113
    std::vector<bool> visited(getUsedSize(), false);
114
115
116

    TraverseStack stack;

Thomas Witkowski's avatar
Thomas Witkowski committed
117
    Flag fillFlag = Mesh::CALL_LEAF_EL | Mesh::FILL_GRD_LAMBDA | Mesh::FILL_COORDS;
118
119
120

    ElInfo *elInfo = stack.traverseFirst(mesh, -1, fillFlag);

Thomas Witkowski's avatar
Thomas Witkowski committed
121
    while (elInfo) {
122
123
124
125
126
127

      const DegreeOfFreedom **dof = elInfo->getElement()->getDOF();
      const double *localUh = getLocalVector(elInfo->getElement(), NULL);
      const DimVec<WorldVector<double> > &grdLambda = elInfo->getGrdLambda();

      int localDOFNr = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
128
129
      for (int i = 0; i < numNodes; i++) { // for all nodes
	for (int j = 0; j < numNodeDOFs[i]; j++) { // for all dofs at this node
130
	  DegreeOfFreedom dofIndex = dof[i][numNodePreDOFs[localDOFNr] + j];
Thomas Witkowski's avatar
Thomas Witkowski committed
131
132
133
	  if (!visited[dofIndex]) {	  
	    basFcts->evalGrdUh(*(bary[localDOFNr]), grdLambda, 
			       localUh, &((*result)[dofIndex]));
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157

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

      elInfo = stack.traverseNext(elInfo);
    }

    return result;
  }

  template<>
  DOFVector<WorldVector<double> >*
  DOFVector<double>::getRecoveryGradient(DOFVector<WorldVector<double> > *grad) const 
  {
    FUNCNAME("DOFVector<double>::getRecoveryGradient()");

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
158
159
    if (!result) {
      if (vec && vec->getFESpace() != feSpace) {
160
161
162
	DELETE vec;
	vec = NULL;
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
163
      if (!vec) {
164
165
166
167
168
169
170
171
172
173
174
	vec = NEW DOFVector<WorldVector<double> >(feSpace, "gradient");
      }
      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
175
    int nPreDOFs = feSpace->getAdmin()->getNumberOfPreDOFs(0);
176
    DimVec<double> bary(dim, DEFAULT_VALUE, (1.0 / (dim + 1.0)));
Thomas Witkowski's avatar
Thomas Witkowski committed
177
    WorldVector<double> grd;
178
179

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
193
      for (int i = 0; i < dim + 1; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
194
	DegreeOfFreedom dofIndex = dof[i][nPreDOFs];
195
196
197
198
199
200
201
202
203
204
	(*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);

Thomas Witkowski's avatar
Thomas Witkowski committed
205
206
    for (volIt.reset(), grdIt.reset(); !volIt.end(); ++volIt, ++grdIt) {
      if (*volIt != 0.0) {
Thomas Witkowski's avatar
Thomas Witkowski committed
207
	*grdIt *= 1.0 / (*volIt);
208
209
210
211
212
213
214
      }
    }

    return result;
  }

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

224
225
226
    if (quad && quadFast) {
      TEST_EXIT_DBG(quad == quadFast->getQuadrature())
      	("quad != quadFast->quadrature\n");
227
228
    }

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

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

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

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

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

258
    if (quadFast) {
Thomas Witkowski's avatar
Thomas Witkowski committed
259
      for (int i = 0; i < nPoints; i++) {
260
	for (int j = 0; j < dim + 1; j++) {
261
	  grd1[j] = 0.0;
262
	}
263

264
265
	for (int j = 0; j < nBasFcts; j++) {
	  for (int k = 0; k < parts; k++) {
266
267
268
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;
	  for (int k = 0; k < parts; k++) {
273
274
275
276
277
	    result[i][l] += grdLambda[k][l] * grd1[k];
	  }
	}
      }
    } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
278
279
      const BasisFunction *basFcts = feSpace->getBasisFcts();
      DimVec<double>* grdPhi = grdPhis[myRank];
280

Thomas Witkowski's avatar
Thomas Witkowski committed
281
      for (int i = 0; i < nPoints; i++) {
282
	for (int j = 0; j < dim + 1; j++) {
283
	  grd1[j] = 0.0;
284
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
294
295
	for (int l = 0; l < dow; l++) {
	  result[i][l] = 0.0;
	  for (int k = 0; k < parts; k++) {
296
297
298
299
300
301
302
303
304
305
	    result[i][l] += grdLambda[k][l] * grd1[k];
	  }
	}
      }
    }
  
    return const_cast<const WorldVector<double>*>(result);
  }

  template<>
Thomas Witkowski's avatar
Thomas Witkowski committed
306
307
308
309
310
311
312
313
314
315
  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");

316
317
    if (quad && quadFast)
      TEST_EXIT_DBG(quad == quadFast->getQuadrature())("quad != quadFast->quadrature\n");
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
342
      result = grd;
    }
  
    double *localVec = localVectors[myRank];
    getLocalVector(largeElInfo->getElement(), localVec);
343
344
345
346

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

    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++) {
356
      for (int j = 0; j < dim + 1; j++)
Thomas Witkowski's avatar
Thomas Witkowski committed
357
358
359
360
361
362
363
	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);
364
	  tmp *= m[j][k];
Thomas Witkowski's avatar
Thomas Witkowski committed
365
366
367
	  *grdPhi += tmp;
	}

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

  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
400
401
      ("invalid basis functions");

    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
478
479
480
    return const_cast<const WorldMatrix<double>*>(result);
  }

  template<>
  void DOFVector<double>::interpol(DOFVector<double> *source, double factor) 
  {
481
    FUNCNAME("DOFVector<double>::interpol()");
482
483
484
485
486
487
488
489
490
491

    const FiniteElemSpace *sourceFeSpace = source->getFESpace();
    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
492
    DegreeOfFreedom *myLocalIndices = localIndices[omp_get_thread_num()];
493
494
    double *sourceLocalCoeffs = GET_MEMORY(double, nSourceBasisFcts);

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

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

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

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

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

564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586

  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;

    const FiniteElemSpace *vFESpace = v->getFESpace();

    if (feSpace == vFESpace) {
      WARNING("same FE-spaces\n");
    }

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

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

    if (feSpace->getMesh() == vFESpace->getMesh()) {
Thomas Witkowski's avatar
Thomas Witkowski committed
587
      DegreeOfFreedom *myLocalIndices = localIndices[omp_get_thread_num()];
588
589
590
591
592
593
594
595
596
597
      WorldVector<double> *vLocalCoeffs = NEW WorldVector<double>[vNumBasFcts];
      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
598
	basFcts->getLocalIndices(el, feSpace->getAdmin(), myLocalIndices);
599
600
601
602

	v->getLocalVector(el, vLocalCoeffs);

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

611
      DELETE [] vLocalCoeffs;
612
613
614
615
616
617
    } else {
      ERROR_EXIT("not yet for dual traverse\n");
    }
  }


618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
  template<>
  WorldVector<DOFVector<double>*> *DOFVector<double>::getGradient(WorldVector<DOFVector<double>*> *grad) const
  {
    FUNCNAME("DOFVector<double>::getGradient()");

    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;

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

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

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

    int numNodes = 0;
    int numDOFs = 0;

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

671
672
673
//     TEST_EXIT_DBG(numNodes == mesh->getNumberOfNodes())
//       ("invalid number of nodes\n");

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

    // 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
687
    ElInfo *elInfo = stack.traverseFirst(mesh, -1, fillFlag);
    WorldVector<double> grd;

688
    while (elInfo) {
689
690
691
692
693
694

      const DegreeOfFreedom **dof = elInfo->getElement()->getDOF();
      const double *localUh = getLocalVector(elInfo->getElement(), NULL);
      const DimVec<WorldVector<double> > &grdLambda = elInfo->getGrdLambda();

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

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

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

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

      elInfo = stack.traverseNext(elInfo);
    }

    return result;
  }


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

723

724
  void DOFVectorDOF::freeDOFContent(DegreeOfFreedom dof) {
725
726
    std::vector<DegreeOfFreedom>::iterator it;
    std::vector<DegreeOfFreedom>::iterator end = vec.end();
727
728
729
    DegreeOfFreedom pos = 0;
    for (it = vec.begin(); it != end; ++it, ++pos) {
      if (*it == dof) *it = pos;
730
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
731
  }
732

733

734
735
736
  WorldVector<DOFVector<double>*> *transform(DOFVector<WorldVector<double> > *vec,
					     WorldVector<DOFVector<double>*> *res)
  {
737
    FUNCNAME("DOFVector<double>::transform()");
738

739
    TEST_EXIT_DBG(vec)("no vector\n");
740

741
    int dow = Global::getGeo(WORLD);
742
743
    static WorldVector<DOFVector<double>*> *result = NULL;

744
    if (!res && !result) {
745
      result = NEW WorldVector<DOFVector<double>*>;
746
      for (int i = 0; i < dow; i++) {
747
748
749
750
751
752
753
	(*result)[i] = NEW DOFVector<double>(vec->getFESpace(), "transform");
      }
    }

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

    int vecSize = vec->getSize();
754
755
    for (int i = 0; i < vecSize; i++) {
      for (int j = 0; j < dow; j++) {
756
757
758
759
760
761
762
	(*((*r)[j]))[i] = (*vec)[i][j];
      }
    }

    return r;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
  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");

    if (quad && quadFast) {
      TEST_EXIT_DBG(quad == quadFast->getQuadrature())
      	("quad != quadFast->quadrature\n");
    }

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

    const BasisFunction *basFcts = feSpace->getBasisFcts();
    int nPoints = quadFast ? quadFast->getQuadrature()->getNumPoints() : quad->getNumPoints(); 
    static double *localvec = NULL;
    double *result;

    if (vecAtQPs) {
      result = vecAtQPs;
    } else {
      if (localvec) 
	delete [] localvec;
      localvec = new double[nPoints];
      for (int i = 0; i < nPoints; i++) {
	localvec[i] = 0.0;
      }
      result = localvec;
    }
      
    double *localVec = localVectors[omp_get_thread_num()];
    getLocalVector(largeElInfo->getElement(), localVec);

802
803
    mtl::dense2D<double> m(nBasFcts, nBasFcts);    
    smallElInfo->getSubElemCoordsMat(m, basFcts->getDegree());
Thomas Witkowski's avatar
Thomas Witkowski committed
804
805
806
807
808
809

    for (int i = 0; i < nPoints; i++) {
      result[i] = 0.0;
      for (int j = 0; j < nBasFcts; j++) {
	double val = 0.0;
	for (int k = 0; k < nBasFcts; k++) {
810
	  val += m[j][k] * (*(basFcts->getPhi(k)))(quad->getLambda(i));
Thomas Witkowski's avatar
Thomas Witkowski committed
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
	}
	result[i] += localVec[j] * val;
      }
    }

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

  template<>
  void DOFVectorBase<double>::assemble(double factor, ElInfo *elInfo,
				       const BoundaryType *bound, 
				       Operator *op)
  {
    FUNCNAME("DOFVector::assemble()");

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

829
    set_to_zero(this->elementVector);
Thomas Witkowski's avatar
Thomas Witkowski committed
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846

    if (op) {
      op->getElementVector(elInfo, this->elementVector);
    } else {
      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() == false) {
	  (*it)->getElementVector(elInfo, this->elementVector, 
				  *factorIt ? **factorIt : 1.0);
	}
      }
    }

847
    addElementVector(factor, this->elementVector, bound, elInfo);
Thomas Witkowski's avatar
Thomas Witkowski committed
848
849
850
851
852
853
854
855
856
857
858
859
860
861
  }

  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;

862
    set_to_zero(this->elementVector);
Thomas Witkowski's avatar
Thomas Witkowski committed
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882

    if (op) {
      ERROR_EXIT("TODO");
      //      op->getElementVector(mainElInfo, this->elementVector);
    } else {
      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);
	}
      }
    }
  

883
    addElementVector(factor, this->elementVector, bound, mainElInfo);
Thomas Witkowski's avatar
Thomas Witkowski committed
884
885
886
  }


887
888
}