DOFVector.cc 19.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "DOFVector.h"
#include "Traverse.h"
#include "DualTraverse.h"
#include "FixVec.h"

namespace AMDiS {

  template<>
  void DOFVector<double>::coarseRestrict(RCNeighbourList& list, int n)
  {
    switch(coarsenOperation) {
    case NO_OPERATION:
      return;
      break;
    case COARSE_RESTRICT:
      (const_cast<BasisFunction*>(feSpace->getBasisFcts()))->coarseRestr(this, &list, n);
      break;
    case COARSE_INTERPOL:
      (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)
  {
    if(n < 1) return;
    int dim = feSpace->getMesh()->getDim();
    Element *el = list.getElement(0);
    int n0 = feSpace->getAdmin()->getNumberOfPreDOFs(VERTEX);
    DegreeOfFreedom dof0 = el->getDOF(0, n0);
    DegreeOfFreedom dof1 = el->getDOF(1, n0);
    DegreeOfFreedom dof_new = el->getChild(0)->getDOF(dim, n0);
    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;

    if(grad) {
      result = grad;
    } else {
      if(result && result->getFESpace() != feSpace) {
	DELETE result;
	result = NEW DOFVector<WorldVector<double> >(feSpace, "gradient");
      }
    }

    int i, j;

    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
    ::std::vector<int> numNodeDOFs;
    ::std::vector<int> numNodePreDOFs;
    ::std::vector<DimVec<double>*> bary;

    int numNodes = 0;
    int numDOFs = 0;

    for(i = 0; i < dim + 1; i++) {
      GeoIndex geoIndex = INDEX_OF_DIM(i, dim);
      int numPositionNodes = mesh->getGeo(geoIndex);
      int numPreDOFs = admin->getNumberOfPreDOFs(i);
      for(j = 0; j < numPositionNodes; j++) {
	int dofs = basFcts->getNumberOfDOFs(geoIndex);
	numNodeDOFs.push_back(dofs);
	numDOFs += dofs;
	numNodePreDOFs.push_back(numPreDOFs);
      }
      numNodes += numPositionNodes;
    }

96
    TEST_EXIT_DBG(numNodes == mesh->getNumberOfNodes())
97
98
      ("invalid number of nodes\n");

99
    TEST_EXIT_DBG(numDOFs == basFcts->getNumber())
100
      ("number of dofs != number of basis functions\n");
101
    
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
    for(i = 0; i < numDOFs; i++) {
      bary.push_back(basFcts->getCoords(i));
    }

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

    TraverseStack stack;

    Flag fillFlag = Mesh::CALL_LEAF_EL | Mesh::FILL_GRD_LAMBDA;

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

    while(elInfo) {

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

      int localDOFNr = 0;
      for(i = 0; i < numNodes; i++) { // for all nodes
	for(j = 0; j < numNodeDOFs[i]; j++) { // for all dofs at this node
	  DegreeOfFreedom dofIndex = dof[i][numNodePreDOFs[localDOFNr] + j];
	  if(!visited[dofIndex]) {
	  
	    result[dofIndex] = basFcts->evalGrdUh(*(bary[localDOFNr]),
						  grdLambda, 
						  localUh, 
						  NULL);

	    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;

    if(!result) {
      if(vec && vec->getFESpace() != feSpace) {
	DELETE vec;
	vec = NULL;
      }
      if(!vec) {
	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);

    int i;

    Mesh *mesh = feSpace->getMesh();

    int dim = mesh->getDim();

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

    DOFAdmin *admin = feSpace->getAdmin();

    int numPreDOFs = admin->getNumberOfPreDOFs(0);

    DimVec<double> bary(dim, DEFAULT_VALUE, (1.0 / (dim + 1.0)));

    // traverse mesh
    TraverseStack stack;

    Flag fillFlag = 
      Mesh::CALL_LEAF_EL | Mesh::FILL_DET | Mesh::FILL_GRD_LAMBDA | Mesh::FILL_COORDS;

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

    while(elInfo) {
      double det = elInfo->getDet();
      const DegreeOfFreedom **dof = elInfo->getElement()->getDOF();
      const double *localUh = getLocalVector(elInfo->getElement(), NULL);
      const DimVec<WorldVector<double> > &grdLambda = elInfo->getGrdLambda();
      const WorldVector<double> &grd = basFcts->evalGrdUh(bary,
							  grdLambda, 
							  localUh, 
							  NULL);

      for(i = 0; i < dim + 1; i++) {
	DegreeOfFreedom dofIndex = dof[i][numPreDOFs];
	(*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);

    for(volIt.reset(), grdIt.reset(); !volIt.end(); ++volIt, ++grdIt) {
      if(*volIt != 0.0) {
	*grdIt *= 1.0/(*volIt);
      }
    }

    return result;
  }

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

234
235
236
    if (quad && quadFast) {
      TEST_EXIT_DBG(quad == quadFast->getQuadrature())
      	("quad != quadFast->quadrature\n");
237
238
    }

239
    TEST_EXIT_DBG(!quadFast || quadFast->getBasisFunctions() == feSpace->getBasisFcts())
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
      ("invalid basis functions");

    Element *el = elInfo->getElement(); 

    int dim = elInfo->getMesh()->getDim();
    int dow = Global::getGeo(WORLD);

    const Quadrature *quadrature = quadFast ? quadFast->getQuadrature() : quad;
    const BasisFunction *basFcts = feSpace->getBasisFcts();

    int numPoints = quadrature->getNumPoints();
    int nBasFcts  = basFcts->getNumber();

    static WorldVector<double> *grd = NULL;

    WorldVector<double> *result;

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

263
      grd = new WorldVector<double>[numPoints];
264
      for (int i = 0; i < numPoints; i++) {
265
266
267
268
269
	grd[i].set(0.0);
      }
      result = grd;
    }
  
270
271
    double *localVec = new double[nBasFcts];
    getLocalVector(el, localVec);
272
273
274
275
276

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

277
278
279
    if (quadFast) {
      for (int i = 0; i < numPoints; i++) {
	for (int j = 0; j < dim + 1; j++) {
280
	  grd1[j] = 0.0;
281
	}
282

283
284
	for (int j = 0; j < nBasFcts; j++) {
	  for (int k = 0; k < parts; k++) {
285
286
287
288
	    grd1[k] += quadFast->getGradient(i, j, k) * localVec[j];
	  }
	}

289
290
291
	for (int l=0; l < dow; l++) {
	  result[i][l] = 0.0;
	  for (int k = 0; k < parts; k++) {
292
293
294
295
296
	    result[i][l] += grdLambda[k][l] * grd1[k];
	  }
	}
      }
    } else {
297
298
299
300
      DimVec<double> grdPhi(dim, DEFAULT_VALUE, 0.0);

      for (int i = 0; i < numPoints; i++) {
	for (int j = 0; j < dim + 1; j++) {
301
	  grd1[j] = 0.0;
302
303
304
305
306
	}

	for (int j = 0; j < nBasFcts; j++) {
	  (*(basFcts->getGrdPhi(j)))(quad->getLambda(i), grdPhi);
	  for (int k = 0; k < parts; k++) {
307
308
309
310
	    grd1[k] += grdPhi[k] * localVec[j];
	  }
	}

311
312
313
	for (int l = 0; l < dow; l++) {
	  result[i][l] = 0.0;
	  for (int k = 0; k < parts; k++) {
314
315
316
317
318
319
	    result[i][l] += grdLambda[k][l] * grd1[k];
	  }
	}
      }
    }
  
320
    delete [] localVec;
321
322
323
324
325
326
327
328
329
330
331
332

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

  template<>
  const WorldMatrix<double> *DOFVectorBase<double>::getD2AtQPs(const ElInfo         *elInfo, 
							       const Quadrature     *quad,
							       const FastQuadrature *quadFast,
							       WorldMatrix<double>  *d2AtQPs) const
  {
    FUNCNAME("DOFVector<double>::getD2AtQPs()");
  
333
    TEST_EXIT_DBG(quad || quadFast)("neither quad nor quadFast defined\n");
334

335
336
337
    if (quad && quadFast) {
      TEST_EXIT_DBG(quad == quadFast->getQuadrature())
      	("quad != quadFast->quadrature\n");
338
339
    }

340
    TEST_EXIT_DBG(!quadFast || quadFast->getBasisFunctions() == feSpace->getBasisFcts())
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
      ("invalid basis functions");

    Element *el = elInfo->getElement(); 

    int dim = elInfo->getMesh()->getDim();
    int dow = Global::getGeo(WORLD);

    const Quadrature *quadrature = quadFast ? quadFast->getQuadrature() : quad;
    const BasisFunction *basFcts = feSpace->getBasisFcts();

    int numPoints = quadrature->getNumPoints();
    int nBasFcts  = basFcts->getNumber();
    int i, j, k, l, iq;

    static WorldMatrix<double> *vec = NULL;

    WorldMatrix<double> *result;

    if (d2AtQPs) {
      result = d2AtQPs;
    } else {
      if(vec) delete [] vec;
      vec = new WorldMatrix<double>[numPoints];
      for(i = 0; i < numPoints; i++) {
	vec[i].set(0.0);
      }
      result = vec;
    }
  
370
371
    double *localVec = new double[nBasFcts];
    getLocalVector(el, localVec);
372
373
374
375
376

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

377
    if (quadFast) {
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
      for (iq = 0; iq < numPoints; iq++) {
	for (k = 0; k < parts; k++)
	  for (l = 0; l < parts; l++)
	    D2Tmp[k][l] = 0.0;

	for (i = 0; i < nBasFcts; i++) {
	  for (k = 0; k < parts; k++)
	    for (l = 0; l < parts; l++)
	      D2Tmp[k][l] += localVec[i]* quadFast->getSecDer(iq, i, k, l);
	}

	for (i = 0; i < dow; i++)
	  for (j = 0; j < dow; j++) {
	    result[iq][i][j] = 0.0;
	    for (k = 0; k < parts; k++)
	      for (l = 0; l < parts; l++)
		result[iq][i][j] += grdLambda[k][i]*grdLambda[l][j]*D2Tmp[k][l];
	  }
      }
    } else {
      DimMat<double> D2Phi(dim, NO_INIT);
      for (iq = 0; iq < numPoints; iq++) {
	for (k = 0; k < parts; k++)
	  for (l = 0; l < parts; l++)
	    D2Tmp[k][l] = 0.0;

	for (i = 0; i < nBasFcts; i++) {
	  WARNING("not tested after index correction\n");
406
407
408
	  //(*(basFcts->getD2Phi(j)))(quad->getLambda(i), D2Phi);
	  (*(basFcts->getD2Phi(i)))(quad->getLambda(iq), D2Phi);

409
410
411
412
413
414
415
416
417
418
	  for (k = 0; k < parts; k++)
	    for (l = 0; l < parts; l++)
	      D2Tmp[k][l] += localVec[i] * D2Phi[k][l];
	}

	for (i = 0; i < dow; i++)
	  for (j = 0; j < dow; j++) {
	    result[iq][i][j] = 0.0;
	    for (k = 0; k < parts; k++)
	      for (l = 0; l < parts; l++)
419
		result[iq][i][j] += grdLambda[k][i] * grdLambda[l][j] * D2Tmp[k][l];
420
421
422
	  }
      }
    }
423
424

    delete [] localVec;
425
426
427
428
429
430
431
432
  

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

  template<>
  void DOFVector<double>::interpol(DOFVector<double> *source, double factor) 
  {
433
    FUNCNAME("DOFVector<double>::interpol()");
434
435
436
437
438
439
440
441
442
443
444
445
446

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

    DegreeOfFreedom *localIndices = GET_MEMORY(DegreeOfFreedom, nBasisFcts);
    double *sourceLocalCoeffs = GET_MEMORY(double, nSourceBasisFcts);

447
    if (feSpace->getMesh() == sourceFeSpace->getMesh()) {
448
449
450
451
452
453
      DimVec<double> *coords = NULL;
      TraverseStack stack;
      ElInfo *elInfo = stack.traverseFirst(feSpace->getMesh(), -1,
					   Mesh::CALL_LEAF_EL | 
					   Mesh::FILL_COORDS);

454
      while (elInfo) {
455
456
457
458
459
	Element *el = elInfo->getElement();

	basisFcts->getLocalIndices(el, feSpace->getAdmin(), localIndices);
	source->getLocalVector(el, sourceLocalCoeffs);

460
461
	for (int i = 0; i < nBasisFcts; i++) {
	  if (vec[localIndices[i]] == 0.0) {
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
	    coords = basisFcts->getCoords(i);
	    vec[localIndices[i]] = sourceBasisFcts->evalUh(*coords, sourceLocalCoeffs) * factor;
	  }
	}
	elInfo = stack.traverseNext(elInfo);
      }
    } else {
      DimVec<double> *coords1 = NULL;
      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);
483
      while (nextTraverse) {     
484
485
486
487
488
489
	basisFcts->getLocalIndices(elInfo1->getElement(), 
				   feSpace->getAdmin(), 
				   localIndices);
	source->getLocalVector(elInfo2->getElement(), 
			       sourceLocalCoeffs);

490
491
	for (int i = 0; i < nBasisFcts; i++) {
	  if (vec[localIndices[i]] == 0.0) {
492
493
494
495
496
	    coords1 = basisFcts->getCoords(i);
	    elInfo1->coordToWorld(*coords1, &worldVec);
	    elInfo2->worldToCoord(worldVec, &coords2);
	  
	    bool isPositive = true;
497
	    for (int j = 0; j < coords2.size(); j++) {
498
499
500
501
502
503
	      if (coords2[j] < 0) {
		isPositive = false;
		break;
	      }
	    }
	  
504
	    if (isPositive) {
505
506
507
508
509
510
511
512
513
514
515
516
517
518
	      vec[localIndices[i]] = sourceBasisFcts->evalUh(coords2, sourceLocalCoeffs);	    
	    }	
	  }
	}
      
	nextTraverse = dualStack.traverseNext(&elInfo1, &elInfo2,
					      &elInfoSmall, &elInfoLarge);
      }
    }
  
    FREE_MEMORY(localIndices, DegreeOfFreedom, nBasisFcts);
    FREE_MEMORY(sourceLocalCoeffs, double, nSourceBasisFcts);
  }

519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566

  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()) {
      DegreeOfFreedom *localIndices = GET_MEMORY(DegreeOfFreedom, numBasFcts);
      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();

	basFcts->getLocalIndices(el, feSpace->getAdmin(), localIndices);

	v->getLocalVector(el, vLocalCoeffs);

	for (int i = 0; i < numBasFcts; i++) {
	  if (vec[localIndices[i]] == nul) {
	    coords = basFcts->getCoords(i);
	    vec[localIndices[i]] += vBasFcts->evalUh(*coords, vLocalCoeffs,NULL) * factor;
	  }
	}
	elInfo = stack.traverseNext(elInfo);
      }

      FREE_MEMORY(localIndices, DegreeOfFreedom, numBasFcts);
567
      DELETE [] vLocalCoeffs;
568
569
570
571
572
573
    } else {
      ERROR_EXIT("not yet for dual traverse\n");
    }
  }


574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
  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;

590
    if (grad) {
591
592
      result = grad;
    } else {
593
      if (!result) {
594
595
596
	result = NEW WorldVector<DOFVector<double>*>;
	result->set(NULL);
      }
597
      for (int i = 0; i < dow; i++) {
598
	if ((*result)[i] && (*result)[i]->getFESpace() != feSpace) {
599
600
601
602
603
604
605
606
607
608
609
610
611
612
	  DELETE (*result)[i];
	  (*result)[i] = NEW DOFVector<double>(feSpace, "gradient");
	}
      }
    }

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

    int numNodes = 0;
    int numDOFs = 0;

613
    for (int i = 0; i < dim + 1; i++) {
614
615
616
      GeoIndex geoIndex = INDEX_OF_DIM(i, dim);
      int numPositionNodes = mesh->getGeo(geoIndex);
      int numPreDOFs = admin->getNumberOfPreDOFs(i);
617
      for (int j = 0; j < numPositionNodes; j++) {
618
619
620
621
622
623
624
625
	int dofs = basFcts->getNumberOfDOFs(geoIndex);
	numNodeDOFs.push_back(dofs);
	numDOFs += dofs;
	numNodePreDOFs.push_back(numPreDOFs);
      }
      numNodes += numPositionNodes;
    }

626
    TEST_EXIT_DBG(numNodes == mesh->getNumberOfNodes())
627
      ("invalid number of nodes\n");
628
    TEST_EXIT_DBG(numDOFs == basFcts->getNumber())
629
      ("number of dofs != number of basis functions\n");
630
    
631
    for (int i = 0; i < numDOFs; i++) {
632
633
634
635
636
637
638
639
640
641
      bary.push_back(basFcts->getCoords(i));
    }

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

642
    while (elInfo) {
643
644
645
646
647
648

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

      int localDOFNr = 0;
649
650
      for (int i = 0; i < numNodes; i++) { // for all nodes
	for (int j = 0; j < numNodeDOFs[i]; j++) { // for all dofs at this node
651
	  DegreeOfFreedom dofIndex = dof[i][numNodePreDOFs[localDOFNr] + j];
652

653
	  if (!visited[dofIndex]) {
654
655
656
657
658
	    grd = basFcts->evalGrdUh(*(bary[localDOFNr]),
				     grdLambda, 
				     localUh, 
				     NULL);

659
	    for (int k = 0; k < dow; k++) {
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
	      (*result)[k][dofIndex] = grd[k];
	    }

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

      elInfo = stack.traverseNext(elInfo);
    }

    return result;
  }


  DOFVectorDOF::DOFVectorDOF() : DOFVector<DegreeOfFreedom>() {};

678

679
680
681
  void DOFVectorDOF::freeDOFContent(DegreeOfFreedom dof) {
    ::std::vector<DegreeOfFreedom>::iterator it;
    ::std::vector<DegreeOfFreedom>::iterator end = vec.end();
682
683
684
    DegreeOfFreedom pos = 0;
    for (it = vec.begin(); it != end; ++it, ++pos) {
      if (*it == dof) *it = pos;
685
686
687
    }
  };

688

689
690
691
  WorldVector<DOFVector<double>*> *transform(DOFVector<WorldVector<double> > *vec,
					     WorldVector<DOFVector<double>*> *res)
  {
692
    FUNCNAME("DOFVector<double>::transform()");
693

694
    TEST_EXIT_DBG(vec)("no vector\n");
695

696
    int dow = Global::getGeo(WORLD);
697
698
    static WorldVector<DOFVector<double>*> *result = NULL;

699
    if (!res && !result) {
700
      result = NEW WorldVector<DOFVector<double>*>;
701
      for (int i = 0; i < dow; i++) {
702
703
704
705
706
707
708
	(*result)[i] = NEW DOFVector<double>(vec->getFESpace(), "transform");
      }
    }

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

    int vecSize = vec->getSize();
709
710
    for (int i = 0; i < vecSize; i++) {
      for (int j = 0; j < dow; j++) {
711
712
713
714
715
716
717
718
719
	(*((*r)[j]))[i] = (*vec)[i][j];
      }
    }

    return r;
  }

}