DOFVector.cc 24.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
#include "DOFVector.h"
#include "Traverse.h"
#include "DualTraverse.h"
#include "FixVec.h"

namespace AMDiS {

  template<>
  void DOFVector<double>::coarseRestrict(RCNeighbourList& list, int n)
  {
11
    switch (coarsenOperation) {
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
    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)
  {
35
36
37
    if (n < 1) 
      return;

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

    int numNodes = 0;
    int numDOFs = 0;

Thomas Witkowski's avatar
Thomas Witkowski committed
82
    for (int i = 0; i < dim + 1; i++) {
83
84
85
      GeoIndex geoIndex = INDEX_OF_DIM(i, dim);
      int numPositionNodes = mesh->getGeo(geoIndex);
      int numPreDOFs = admin->getNumberOfPreDOFs(i);
Thomas Witkowski's avatar
Thomas Witkowski committed
86
      for (int j = 0; j < numPositionNodes; j++) {
87
88
89
90
91
92
93
94
	int dofs = basFcts->getNumberOfDOFs(geoIndex);
	numNodeDOFs.push_back(dofs);
	numDOFs += dofs;
	numNodePreDOFs.push_back(numPreDOFs);
      }
      numNodes += numPositionNodes;
    }

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

98
    TEST_EXIT_DBG(numDOFs == basFcts->getNumber())
99
      ("number of dofs != number of basis functions\n");
100
    
Thomas Witkowski's avatar
Thomas Witkowski committed
101
    for (int i = 0; i < numDOFs; i++) {
102
103
104
105
      bary.push_back(basFcts->getCoords(i));
    }

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

    TraverseStack stack;

Thomas Witkowski's avatar
Thomas Witkowski committed
110
    Flag fillFlag = Mesh::CALL_LEAF_EL | Mesh::FILL_GRD_LAMBDA | Mesh::FILL_COORDS;
111
112
113

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

Thomas Witkowski's avatar
Thomas Witkowski committed
114
    while (elInfo) {
115
116
117
118
119
120

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

	    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
151
152
    if (!result) {
      if (vec && vec->getFESpace() != feSpace) {
153
154
155
	DELETE vec;
	vec = NULL;
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
156
      if (!vec) {
157
158
159
160
161
162
163
164
165
166
167
	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
168
    int nPreDOFs = feSpace->getAdmin()->getNumberOfPreDOFs(0);
169
    DimVec<double> bary(dim, DEFAULT_VALUE, (1.0 / (dim + 1.0)));
Thomas Witkowski's avatar
Thomas Witkowski committed
170
    WorldVector<double> grd;
171
172

    // traverse mesh
Thomas Witkowski's avatar
Thomas Witkowski committed
173
    Mesh *mesh = feSpace->getMesh();
174
    TraverseStack stack;
Thomas Witkowski's avatar
Thomas Witkowski committed
175
176
177
    ElInfo *elInfo = stack.traverseFirst(mesh, -1,
					 Mesh::CALL_LEAF_EL | Mesh::FILL_DET | 
					 Mesh::FILL_GRD_LAMBDA | Mesh::FILL_COORDS);
178

Thomas Witkowski's avatar
Thomas Witkowski committed
179
    while (elInfo) {
180
181
182
183
      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
184
      basFcts->evalGrdUh(bary, grdLambda, localUh, &grd);
185

Thomas Witkowski's avatar
Thomas Witkowski committed
186
      for (int i = 0; i < dim + 1; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
187
	DegreeOfFreedom dofIndex = dof[i][nPreDOFs];
188
189
190
191
192
193
194
195
196
197
	(*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
198
199
    for (volIt.reset(), grdIt.reset(); !volIt.end(); ++volIt, ++grdIt) {
      if (*volIt != 0.0) {
Thomas Witkowski's avatar
Thomas Witkowski committed
200
	*grdIt *= 1.0 / (*volIt);
201
202
203
204
205
206
207
      }
    }

    return result;
  }

  template<>
208
209
  const WorldVector<double> *DOFVectorBase<double>::getGrdAtQPs(const ElInfo *elInfo, 
								const Quadrature *quad,
210
								const FastQuadrature *quadFast,
211
								WorldVector<double> *grdAtQPs) const
212
213
214
  {
    FUNCNAME("DOFVector<double>::getGrdAtQPs()");
  
215
    TEST_EXIT_DBG(quad || quadFast)("neither quad nor quadFast defined\n");
216

217
218
219
    if (quad && quadFast) {
      TEST_EXIT_DBG(quad == quadFast->getQuadrature())
      	("quad != quadFast->quadrature\n");
220
221
    }

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

    int dow = Global::getGeo(WORLD);
Thomas Witkowski's avatar
Thomas Witkowski committed
226
227
    int myRank = omp_get_thread_num();
    int nPoints = quadFast ? quadFast->getQuadrature()->getNumPoints() : quad->getNumPoints();
228
229
230
231
232
233
    static WorldVector<double> *grd = NULL;
    WorldVector<double> *result;

    if (grdAtQPs) {
      result = grdAtQPs;
    } else {
234
235
236
      if (grd) 
	delete [] grd;

Thomas Witkowski's avatar
Thomas Witkowski committed
237
238
      grd = new WorldVector<double>[nPoints];
      for (int i = 0; i < nPoints; i++) {
239
240
241
242
243
	grd[i].set(0.0);
      }
      result = grd;
    }
  
Thomas Witkowski's avatar
Thomas Witkowski committed
244
245
    double *localVec = localVectors[myRank];
    getLocalVector(elInfo->getElement(), localVec);
246

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

251
    if (quadFast) {
Thomas Witkowski's avatar
Thomas Witkowski committed
252
      for (int i = 0; i < nPoints; i++) {
253
	for (int j = 0; j < dim + 1; j++) {
254
	  grd1[j] = 0.0;
255
	}
256

257
258
	for (int j = 0; j < nBasFcts; j++) {
	  for (int k = 0; k < parts; k++) {
259
260
261
262
	    grd1[k] += quadFast->getGradient(i, j, k) * localVec[j];
	  }
	}

263
264
265
	for (int l=0; l < dow; l++) {
	  result[i][l] = 0.0;
	  for (int k = 0; k < parts; k++) {
266
267
268
269
270
	    result[i][l] += grdLambda[k][l] * grd1[k];
	  }
	}
      }
    } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
271
272
      const BasisFunction *basFcts = feSpace->getBasisFcts();
      DimVec<double>* grdPhi = grdPhis[myRank];
273

Thomas Witkowski's avatar
Thomas Witkowski committed
274
      for (int i = 0; i < nPoints; i++) {
275
	for (int j = 0; j < dim + 1; j++) {
276
	  grd1[j] = 0.0;
277
278
279
	}

	for (int j = 0; j < nBasFcts; j++) {
280
	  (*(basFcts->getGrdPhi(j)))(quad->getLambda(i), *grdPhi);
281
	  for (int k = 0; k < parts; k++) {
282
	    grd1[k] += (*grdPhi)[k] * localVec[j];
283
284
285
	  }
	}

286
287
288
	for (int l = 0; l < dow; l++) {
	  result[i][l] = 0.0;
	  for (int k = 0; k < parts; k++) {
289
290
291
292
293
294
295
296
297
298
	    result[i][l] += grdLambda[k][l] * grd1[k];
	  }
	}
      }
    }
  
    return const_cast<const WorldVector<double>*>(result);
  }

  template<>
Thomas Witkowski's avatar
Thomas Witkowski committed
299
300
301
302
303
304
305
306
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
  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);
    DimMat<double> *m = smallElInfo->getSubElemCoordsMat();

    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);
	  tmp *= (*m)[j][k];
	  *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,
382
383
384
385
386
							       const FastQuadrature *quadFast,
							       WorldMatrix<double>  *d2AtQPs) const
  {
    FUNCNAME("DOFVector<double>::getD2AtQPs()");
  
387
    TEST_EXIT_DBG(quad || quadFast)("neither quad nor quadFast defined\n");
388

389
390
391
    if (quad && quadFast) {
      TEST_EXIT_DBG(quad == quadFast->getQuadrature())
      	("quad != quadFast->quadrature\n");
392
393
    }

394
    TEST_EXIT_DBG(!quadFast || quadFast->getBasisFunctions() == feSpace->getBasisFcts())
395
396
397
398
      ("invalid basis functions");

    Element *el = elInfo->getElement(); 

Thomas Witkowski's avatar
Thomas Witkowski committed
399
    int myRank = omp_get_thread_num();
400
    int dow = Global::getGeo(WORLD);
Thomas Witkowski's avatar
Thomas Witkowski committed
401
    int nPoints = quadFast ? quadFast->getQuadrature()->getNumPoints() : quad->getNumPoints();
402
403
404
405
406
407
408
409

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

    if (d2AtQPs) {
      result = d2AtQPs;
    } else {
      if(vec) delete [] vec;
Thomas Witkowski's avatar
Thomas Witkowski committed
410
411
      vec = new WorldMatrix<double>[nPoints];
      for (int i = 0; i < nPoints; i++) {
412
413
414
415
416
	vec[i].set(0.0);
      }
      result = vec;
    }
  
Thomas Witkowski's avatar
Thomas Witkowski committed
417
    double *localVec = localVectors[myRank];
418
    getLocalVector(el, localVec);
419
420
421
422
423

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

424
    if (quadFast) {
Thomas Witkowski's avatar
Thomas Witkowski committed
425
426
427
      for (int iq = 0; iq < nPoints; iq++) {
	for (int k = 0; k < parts; k++)
	  for (int l = 0; l < parts; l++)
428
429
	    D2Tmp[k][l] = 0.0;

Thomas Witkowski's avatar
Thomas Witkowski committed
430
431
432
433
	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);
434
435
	}

Thomas Witkowski's avatar
Thomas Witkowski committed
436
437
	for (int i = 0; i < dow; i++)
	  for (int j = 0; j < dow; j++) {
438
	    result[iq][i][j] = 0.0;
Thomas Witkowski's avatar
Thomas Witkowski committed
439
440
	    for (int k = 0; k < parts; k++)
	      for (int l = 0; l < parts; l++)
441
442
443
444
		result[iq][i][j] += grdLambda[k][i]*grdLambda[l][j]*D2Tmp[k][l];
	  }
      }
    } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
445
      const BasisFunction *basFcts = feSpace->getBasisFcts();
446
447
      DimMat<double>* D2Phi = D2Phis[omp_get_thread_num()];

Thomas Witkowski's avatar
Thomas Witkowski committed
448
449
450
      for (int iq = 0; iq < nPoints; iq++) {
	for (int k = 0; k < parts; k++)
	  for (int l = 0; l < parts; l++)
451
452
	    D2Tmp[k][l] = 0.0;

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

Thomas Witkowski's avatar
Thomas Witkowski committed
457
458
	  for (int k = 0; k < parts; k++)
	    for (int l = 0; l < parts; l++)
459
	      D2Tmp[k][l] += localVec[i] * (*D2Phi)[k][l];
460
461
	}

Thomas Witkowski's avatar
Thomas Witkowski committed
462
463
	for (int i = 0; i < dow; i++)
	  for (int j = 0; j < dow; j++) {
464
	    result[iq][i][j] = 0.0;
Thomas Witkowski's avatar
Thomas Witkowski committed
465
466
	    for (int k = 0; k < parts; k++)
	      for (int l = 0; l < parts; l++)
467
		result[iq][i][j] += grdLambda[k][i] * grdLambda[l][j] * D2Tmp[k][l];
468
469
470
	  }
      }
    }
471

472
473
474
475
476
477
    return const_cast<const WorldMatrix<double>*>(result);
  }

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

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

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

499
      while (elInfo) {
500
501
	Element *el = elInfo->getElement();

Thomas Witkowski's avatar
Thomas Witkowski committed
502
	basisFcts->getLocalIndices(el, feSpace->getAdmin(), myLocalIndices);
503
504
	source->getLocalVector(el, sourceLocalCoeffs);

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

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

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

  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
586
      DegreeOfFreedom *myLocalIndices = localIndices[omp_get_thread_num()];
587
588
589
590
591
592
593
594
595
596
      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
597
	basFcts->getLocalIndices(el, feSpace->getAdmin(), myLocalIndices);
598
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);
Thomas Witkowski's avatar
Thomas Witkowski committed
604
	    vec[myLocalIndices[i]] += vBasFcts->evalUh(*coords, vLocalCoeffs,NULL) * factor;
605
606
607
608
609
	  }
	}
	elInfo = stack.traverseNext(elInfo);
      }

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


617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
  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;

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

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

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

    int numNodes = 0;
    int numDOFs = 0;

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

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

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

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

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

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

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

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

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

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

      elInfo = stack.traverseNext(elInfo);
    }

    return result;
  }


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

722

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

732

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

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

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

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

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

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

    return r;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
762
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
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
  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);

    DimMat<double> *m = smallElInfo->getSubElemCoordsMat();

    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++) {
	  val += (*m)[j][k] * (*(basFcts->getPhi(k)))(quad->getLambda(i));
	}
	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;

    Operator *operat = op ? op : this->operators[0];

    this->elementVector = 
      operat->getAssembler(omp_get_thread_num())->initElementVector(this->elementVector, elInfo);

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

    addElementVector(factor, *this->elementVector, bound);
  }

  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;

    Operator *operat = op ? op : this->operators[0];

    this->elementVector = 
      operat->getAssembler(omp_get_thread_num())->
      initElementVector(this->elementVector, mainElInfo);

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

    addElementVector(factor, *this->elementVector, bound);
  }


892
893
}