DOFVector.cc 24.6 KB
Newer Older
1
2
#include <boost/numeric/mtl/mtl.hpp>

3
4
5
6
7
8
9
10
11
12
#include "DOFVector.h"
#include "Traverse.h"
#include "DualTraverse.h"
#include "FixVec.h"

namespace AMDiS {

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

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

27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
      (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)
  {
43
44
45
    if (n < 1) 
      return;

46
47
48
49
    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
50
    DegreeOfFreedom dof_new = el->getChild(0)->getDOF(feSpace->getMesh()->getDim(), n0);
51
52
53
54
55
56
57
58
59
60
61
62
63
64
    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
65
    if (grad) {
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
      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
83
84
85
    std::vector<int> numNodeDOFs;
    std::vector<int> numNodePreDOFs;
    std::vector<DimVec<double>*> bary;
86
87
88
89

    int numNodes = 0;
    int numDOFs = 0;

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

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

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

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

    TraverseStack stack;

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

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

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

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

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
187
    while (elInfo) {
188
189
190
191
      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
192
      basFcts->evalGrdUh(bary, grdLambda, localUh, &grd);
193

Thomas Witkowski's avatar
Thomas Witkowski committed
194
      for (int i = 0; i < dim + 1; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
195
	DegreeOfFreedom dofIndex = dof[i][nPreDOFs];
196
197
198
199
200
201
202
203
204
205
	(*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
206
207
    for (volIt.reset(), grdIt.reset(); !volIt.end(); ++volIt, ++grdIt) {
      if (*volIt != 0.0) {
Thomas Witkowski's avatar
Thomas Witkowski committed
208
	*grdIt *= 1.0 / (*volIt);
209
210
211
212
213
214
215
      }
    }

    return result;
  }

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

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

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

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

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

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

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

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

265
266
	for (int j = 0; j < nBasFcts; j++) {
	  for (int k = 0; k < parts; k++) {
267
268
269
270
	    grd1[k] += quadFast->getGradient(i, j, k) * localVec[j];
	  }
	}

271
272
273
	for (int l=0; l < dow; l++) {
	  result[i][l] = 0.0;
	  for (int k = 0; k < parts; k++) {
274
275
276
277
278
	    result[i][l] += grdLambda[k][l] * grd1[k];
	  }
	}
      }
    } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
279
280
      const BasisFunction *basFcts = feSpace->getBasisFcts();
      DimVec<double>* grdPhi = grdPhis[myRank];
281

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

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

294
295
296
	for (int l = 0; l < dow; l++) {
	  result[i][l] = 0.0;
	  for (int k = 0; k < parts; k++) {
297
298
299
300
301
302
303
304
305
306
	    result[i][l] += grdLambda[k][l] * grd1[k];
	  }
	}
      }
    }
  
    return const_cast<const WorldVector<double>*>(result);
  }

  template<>
Thomas Witkowski's avatar
Thomas Witkowski committed
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
  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");

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

    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];
      for (int i = 0; i < nPoints; i++) {
	grd[i].set(0.0);
      }
      result = grd;
    }
  
    double *localVec = localVectors[myRank];
    getLocalVector(largeElInfo->getElement(), localVec);
346
    const mtl::dense2D<double> m = smallElInfo->getSubElemCoordsMat();
Thomas Witkowski's avatar
Thomas Witkowski committed
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366

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

    
    const BasisFunction *basFcts = feSpace->getBasisFcts();
    DimVec<double>* grdPhi = grdPhis[myRank];
    DimVec<double> tmp(dim, DEFAULT_VALUE, 0.0);
    
    for (int i = 0; i < nPoints; i++) {
      for (int j = 0; j < dim + 1; j++) {
	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);
367
	  tmp *= m[j][k];
Thomas Witkowski's avatar
Thomas Witkowski committed
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
	  *grdPhi += tmp;
	}

	for (int k = 0; k < parts; k++) {
	  grd1[k] += (*grdPhi)[k] * localVec[j];
	}
      }
      
      for (int l = 0; l < dow; l++) {
	result[i][l] = 0.0;
	for (int k = 0; k < parts; k++) {
	  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,
390
391
392
393
394
							       const FastQuadrature *quadFast,
							       WorldMatrix<double>  *d2AtQPs) const
  {
    FUNCNAME("DOFVector<double>::getD2AtQPs()");
  
395
    TEST_EXIT_DBG(quad || quadFast)("neither quad nor quadFast defined\n");
396

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

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

    Element *el = elInfo->getElement(); 

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

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

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

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

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

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

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

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

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

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

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

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

  template<>
  void DOFVector<double>::interpol(DOFVector<double> *source, double factor) 
  {
486
    FUNCNAME("DOFVector<double>::interpol()");
487
488
489
490
491
492
493
494
495
496

    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
497
    DegreeOfFreedom *myLocalIndices = localIndices[omp_get_thread_num()];
498
499
    double *sourceLocalCoeffs = GET_MEMORY(double, nSourceBasisFcts);

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

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

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

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

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
550
551
552
553
554
	      if (coords2[j] < 0) {
		isPositive = false;
		break;
	      }
	    }
	  
555
	    if (isPositive) {
Thomas Witkowski's avatar
Thomas Witkowski committed
556
	      vec[myLocalIndices[i]] = sourceBasisFcts->evalUh(coords2, sourceLocalCoeffs);	    
557
558
559
560
561
562
563
564
565
566
567
568
	    }	
	  }
	}
      
	nextTraverse = dualStack.traverseNext(&elInfo1, &elInfo2,
					      &elInfoSmall, &elInfoLarge);
      }
    }
  
    FREE_MEMORY(sourceLocalCoeffs, double, nSourceBasisFcts);
  }

569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591

  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
592
      DegreeOfFreedom *myLocalIndices = localIndices[omp_get_thread_num()];
593
594
595
596
597
598
599
600
601
602
      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
603
	basFcts->getLocalIndices(el, feSpace->getAdmin(), myLocalIndices);
604
605
606
607

	v->getLocalVector(el, vLocalCoeffs);

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

616
      DELETE [] vLocalCoeffs;
617
618
619
620
621
622
    } else {
      ERROR_EXIT("not yet for dual traverse\n");
    }
  }


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

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

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

    // count number of nodes and dofs per node
656
657
658
    std::vector<int> numNodeDOFs;
    std::vector<int> numNodePreDOFs;
    std::vector<DimVec<double>*> bary;
659
660
661
662

    int numNodes = 0;
    int numDOFs = 0;

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

676
677
678
//     TEST_EXIT_DBG(numNodes == mesh->getNumberOfNodes())
//       ("invalid number of nodes\n");

679
    TEST_EXIT_DBG(numDOFs == basFcts->getNumber())
680
      ("number of dofs != number of basis functions\n");
681
    
682
    for (int i = 0; i < numDOFs; i++) {
683
684
685
686
      bary.push_back(basFcts->getCoords(i));
    }

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

693
    while (elInfo) {
694
695
696
697
698
699

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

      int localDOFNr = 0;
700
701
      for (int i = 0; i < numNodes; i++) { // for all nodes
	for (int j = 0; j < numNodeDOFs[i]; j++) { // for all dofs at this node
702
	  DegreeOfFreedom dofIndex = dof[i][numNodePreDOFs[localDOFNr] + j];
703

704
	  if (!visited[dofIndex]) {
705
	    basFcts->evalGrdUh(*(bary[localDOFNr]), grdLambda, localUh, &grd);
706

707
	    for (int k = 0; k < dow; k++) {
708
	      (*(*result)[k])[dofIndex] = grd[k];
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
	    }

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

      elInfo = stack.traverseNext(elInfo);
    }

    return result;
  }


724
725
  DOFVectorDOF::DOFVectorDOF() : 
    DOFVector<DegreeOfFreedom>() 
Thomas Witkowski's avatar
Thomas Witkowski committed
726
  {}
727

728

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

738

739
740
741
  WorldVector<DOFVector<double>*> *transform(DOFVector<WorldVector<double> > *vec,
					     WorldVector<DOFVector<double>*> *res)
  {
742
    FUNCNAME("DOFVector<double>::transform()");
743

744
    TEST_EXIT_DBG(vec)("no vector\n");
745

746
    int dow = Global::getGeo(WORLD);
747
748
    static WorldVector<DOFVector<double>*> *result = NULL;

749
    if (!res && !result) {
750
      result = NEW WorldVector<DOFVector<double>*>;
751
      for (int i = 0; i < dow; i++) {
752
753
754
755
756
757
758
	(*result)[i] = NEW DOFVector<double>(vec->getFESpace(), "transform");
      }
    }

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

    int vecSize = vec->getSize();
759
760
    for (int i = 0; i < vecSize; i++) {
      for (int j = 0; j < dow; j++) {
761
762
763
764
765
766
767
	(*((*r)[j]))[i] = (*vec)[i][j];
      }
    }

    return r;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
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
802
803
804
805
806
  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);

807
    const mtl::dense2D<double> m = smallElInfo->getSubElemCoordsMat();
Thomas Witkowski's avatar
Thomas Witkowski committed
808
809
810
811
812
813

    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++) {
814
	  val += m[j][k] * (*(basFcts->getPhi(k)))(quad->getLambda(i));
Thomas Witkowski's avatar
Thomas Witkowski committed
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
	}
	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;

833
    set_to_zero(this->elementVector);
Thomas Witkowski's avatar
Thomas Witkowski committed
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850

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

851
    addElementVector(factor, this->elementVector, bound, elInfo);
Thomas Witkowski's avatar
Thomas Witkowski committed
852
853
854
855
856
857
858
859
860
861
862
863
864
865
  }

  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;

866
    set_to_zero(this->elementVector);
Thomas Witkowski's avatar
Thomas Witkowski committed
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886

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

887
    addElementVector(factor, this->elementVector, bound, mainElInfo);
Thomas Witkowski's avatar
Thomas Witkowski committed
888
889
890
  }


891
892
}