DOFVector.cc 29.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
//
// Software License for AMDiS
//
// Copyright (c) 2010 Dresden University of Technology 
// All rights reserved.
// Authors: Simon Vey, Thomas Witkowski et al.
//
// This file is part of AMDiS
//
// See also license.opensource.txt in the distribution.


13
#include <boost/numeric/mtl/mtl.hpp>
14
15
16
17
18
19
20
21
22
23
#include "DOFVector.h"
#include "Traverse.h"
#include "DualTraverse.h"
#include "FixVec.h"

namespace AMDiS {

  template<>
  void DOFVector<double>::coarseRestrict(RCNeighbourList& list, int n)
  {
24
25
    FUNCNAME("DOFVector<double>::coarseRestrict()");

26
    switch (coarsenOperation) {
27
28
29
30
31
32
33
    case NO_OPERATION:
      return;
      break;
    case COARSE_RESTRICT:
      (const_cast<BasisFunction*>(feSpace->getBasisFcts()))->coarseRestr(this, &list, n);
      break;
    case COARSE_INTERPOL:
34
35
      TEST_EXIT_DBG(feSpace)("Should not happen!\n");
      TEST_EXIT_DBG(feSpace->getBasisFcts())("Shoud not happen!\n");
36

37
38
39
      (const_cast<BasisFunction*>(feSpace->getBasisFcts()))->coarseInter(this, &list, n);
      break;
    default:
40
41
      WARNING("Invalid coarsen operation \"%d\" in vector \"%s\"\n", 
	      coarsenOperation, this->name.c_str());
42
43
44
    }
  }

45

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

52

53
54
55
  template<>
  void DOFVector<WorldVector<double> >::refineInterpol(RCNeighbourList& list, int n)
  {
56
57
58
    if (n < 1) 
      return;

59
    Element *el = list.getElement(0);
60
    int n0 = feSpace->getAdmin()->getNumberOfPreDofs(VERTEX);
61
62
63
    DegreeOfFreedom dof0 = el->getDof(0, n0);
    DegreeOfFreedom dof1 = el->getDof(1, n0);
    DegreeOfFreedom dof_new = el->getChild(0)->getDof(feSpace->getMesh()->getDim(), n0);
64
65
66
67
68
    vec[dof_new] = vec[dof0];
    vec[dof_new] += vec[dof1];
    vec[dof_new] *= 0.5;
  }

69

70
71
72
73
74
75
76
77
78
  template<>
  DOFVector<WorldVector<double> >*
  DOFVector<double>::getGradient(DOFVector<WorldVector<double> > *grad) const 
  {
    FUNCNAME("DOFVector<double>::getGradient()");

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

Thomas Witkowski's avatar
Thomas Witkowski committed
79
    if (grad) {
80
81
      result = grad;
    } else {
82
      if(result && result->getFeSpace() != feSpace) {
83
84
	delete result;
	result = new DOFVector<WorldVector<double> >(feSpace, "gradient");
85
86
87
88
89
90
91
92
93
      }
    }

    Mesh *mesh = feSpace->getMesh();
    int dim = mesh->getDim();
    const BasisFunction *basFcts = feSpace->getBasisFcts();
    DOFAdmin *admin = feSpace->getAdmin();

    // count number of nodes and dofs per node
94
95
    std::vector<int> nNodeDOFs;
    std::vector<int> nNodePreDofs;
96
    std::vector<DimVec<double>*> bary;
97

98
99
    int nNodes = 0;
    int nDofs = 0;
100

Thomas Witkowski's avatar
Thomas Witkowski committed
101
    for (int i = 0; i < dim + 1; i++) {
102
      GeoIndex geoIndex = INDEX_OF_DIM(i, dim);
103
      int nPositions = mesh->getGeo(geoIndex);
104
      int numPreDofs = admin->getNumberOfPreDofs(i);
105
      for (int j = 0; j < nPositions; j++) {
106
	int dofs = basFcts->getNumberOfDofs(geoIndex);
107
	nNodeDOFs.push_back(dofs);
108
	nDofs += dofs;
109
	nNodePreDofs.push_back(numPreDofs);
110
      }
111
      nNodes += nPositions;
112
113
    }

114
    TEST_EXIT_DBG(nDofs == basFcts->getNumber())
115
      ("number of dofs != number of basis functions\n");
116
    
117
    for (int i = 0; i < nDofs; i++)
118
119
      bary.push_back(basFcts->getCoords(i));    

120
    ElementVector localUh(basFcts->getNumber());
121
122

    // traverse mesh
123
    std::vector<bool> visited(getUsedSize(), false);
124
    TraverseStack stack;
125
    Flag fillFlag = Mesh::CALL_LEAF_EL | Mesh::FILL_GRD_LAMBDA | Mesh::FILL_COORDS;
126
127
    ElInfo *elInfo = stack.traverseFirst(mesh, -1, fillFlag);

Thomas Witkowski's avatar
Thomas Witkowski committed
128
    while (elInfo) {
129
      const DegreeOfFreedom **dof = elInfo->getElement()->getDof();
130
      const DimVec<WorldVector<double> > &grdLambda = elInfo->getGrdLambda();
131
      getLocalVector(elInfo->getElement(), localUh);
132
133

      int localDOFNr = 0;
134
      for (int i = 0; i < nNodes; i++) { // for all nodes
135
136
	for (int j = 0; j < nNodeDOFs[i]; j++) { // for all dofs at this node
	  DegreeOfFreedom dofIndex = dof[i][nNodePreDofs[i] + j];
Thomas Witkowski's avatar
Thomas Witkowski committed
137
138
139
	  if (!visited[dofIndex]) {	  
	    basFcts->evalGrdUh(*(bary[localDOFNr]), grdLambda, 
			       localUh, &((*result)[dofIndex]));
140
141
142
143
144
145
146
147
148
149
150
151
152

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

      elInfo = stack.traverseNext(elInfo);
    }

    return result;
  }

153

154
155
156
157
158
159
160
161
162
163
164
  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
165
    if (!result) {
166
      if (vec && vec->getFeSpace() != feSpace) {
167
	delete vec;
168
169
	vec = NULL;
      }
170
      if (!vec)
171
	vec = new DOFVector<WorldVector<double> >(feSpace, "gradient");
172
      
173
174
175
176
177
178
179
180
181
      result = vec;
    }

    result->set(WorldVector<double>(DEFAULT_VALUE, 0.0));

    DOFVector<double> volume(feSpace, "volume");
    volume.set(0.0);

    const BasisFunction *basFcts = feSpace->getBasisFcts();
182
    int nPreDofs = feSpace->getAdmin()->getNumberOfPreDofs(VERTEX);
183

184
    DimVec<double> bary(dim, DEFAULT_VALUE, (1.0 / (dim + 1.0)));
Thomas Witkowski's avatar
Thomas Witkowski committed
185
    WorldVector<double> grd;
186
187

    // traverse mesh
Thomas Witkowski's avatar
Thomas Witkowski committed
188
    Mesh *mesh = feSpace->getMesh();
189
    TraverseStack stack;
Thomas Witkowski's avatar
Thomas Witkowski committed
190
191
    ElInfo *elInfo = stack.traverseFirst(mesh, -1,
					 Mesh::CALL_LEAF_EL | Mesh::FILL_DET | 
192
					 Mesh::FILL_GRD_LAMBDA | Mesh::FILL_COORDS);
193

194
    ElementVector localUh(basFcts->getNumber());
195

Thomas Witkowski's avatar
Thomas Witkowski committed
196
    while (elInfo) {
197
      double det = elInfo->getDet();
198
      const DegreeOfFreedom **dof = elInfo->getElement()->getDof();
199
      const DimVec<WorldVector<double> > &grdLambda = elInfo->getGrdLambda();
200
      getLocalVector(elInfo->getElement(), localUh);
Thomas Witkowski's avatar
Thomas Witkowski committed
201
      basFcts->evalGrdUh(bary, grdLambda, localUh, &grd);
202

Thomas Witkowski's avatar
Thomas Witkowski committed
203
      for (int i = 0; i < dim + 1; i++) {
204
	DegreeOfFreedom dofIndex = dof[i][nPreDofs];
205
206
207
208
209
210
211
212
213
214
	(*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);

215
216
    for (volIt.reset(), grdIt.reset(); !volIt.end(); ++volIt, ++grdIt)
      if (*volIt != 0.0)
Thomas Witkowski's avatar
Thomas Witkowski committed
217
	*grdIt *= 1.0 / (*volIt);
218
219
220
221

    return result;
  }

222

223
  template<>
224
225
  const WorldVector<double> *DOFVectorBase<double>::getGrdAtQPs(const ElInfo *elInfo, 
								const Quadrature *quad,
226
								const FastQuadrature *quadFast,
227
								WorldVector<double> *grdAtQPs) const
228
229
230
  {
    FUNCNAME("DOFVector<double>::getGrdAtQPs()");
  
231
    TEST_EXIT_DBG(quad || quadFast)("neither quad nor quadFast defined\n");
232

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

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

    int dow = Global::getGeo(WORLD);
Thomas Witkowski's avatar
Thomas Witkowski committed
242
    int nPoints = quadFast ? quadFast->getQuadrature()->getNumPoints() : quad->getNumPoints();
243
244
245
246
247
248
    static WorldVector<double> *grd = NULL;
    WorldVector<double> *result;

    if (grdAtQPs) {
      result = grdAtQPs;
    } else {
249
250
251
      if (grd) 
	delete [] grd;

Thomas Witkowski's avatar
Thomas Witkowski committed
252
      grd = new WorldVector<double>[nPoints];
253
      for (int i = 0; i < nPoints; i++)
254
	grd[i].set(0.0);
255
      
256
257
258
      result = grd;
    }
  
259
260
    ElementVector localVec(nBasFcts);
    getLocalVector(elInfo->getElement(), localVec);
261

262
    mtl::dense_vector<double> grd1(dim + 1);
263
264
265
    int parts = Global::getGeo(PARTS, dim);
    const DimVec<WorldVector<double> > &grdLambda = elInfo->getGrdLambda();

266
    if (quadFast) {
Thomas Witkowski's avatar
Thomas Witkowski committed
267
      for (int i = 0; i < nPoints; i++) {
268
	grd1 = 0.0;
269

270
271
	for (int j = 0; j < nBasFcts; j++)
	  for (int k = 0; k < parts; k++)
272
	    grd1[k] += quadFast->getGradient(i, j, k) * localVec[j];
273
	  
274
275
	for (int l=0; l < dow; l++) {
	  result[i][l] = 0.0;
276
	  for (int k = 0; k < parts; k++)
277
278
279
	    result[i][l] += grdLambda[k][l] * grd1[k];
	}
      }
280
281

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

Thomas Witkowski's avatar
Thomas Witkowski committed
285
      for (int i = 0; i < nPoints; i++) {
286
	grd1 = 0.0;
287
288

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

294
295
	for (int l = 0; l < dow; l++) {
	  result[i][l] = 0.0;
296
	  for (int k = 0; k < parts; k++)
297
298
299
300
	    result[i][l] += grdLambda[k][l] * grd1[k];
	}
      }
    }
301

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

305

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

317
    if (quad && quadFast) {
318
      TEST_EXIT_DBG(quad == quadFast->getQuadrature())("quad != quadFast->quadrature\n");
319
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335

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

    int dow = Global::getGeo(WORLD);
    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];
336
      for (int i = 0; i < nPoints; i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
337
	grd[i].set(0.0);
338

Thomas Witkowski's avatar
Thomas Witkowski committed
339
340
341
      result = grd;
    }
  
342
343
    ElementVector localVec(nBasFcts);
    getLocalVector(largeElInfo->getElement(), localVec);
344
345

    const BasisFunction *basFcts = feSpace->getBasisFcts();    
346
    mtl::dense2D<double> &m = smallElInfo->getSubElemCoordsMat(basFcts->getDegree());
Thomas Witkowski's avatar
Thomas Witkowski committed
347
348
349
350

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

351
352
353
    mtl::dense_vector<double> grd1(dim + 1);
    mtl::dense_vector<double> grdPhi(dim + 1);
    mtl::dense_vector<double> tmp(dim + 1);
Thomas Witkowski's avatar
Thomas Witkowski committed
354
355
    
    for (int i = 0; i < nPoints; i++) {
356
      grd1 = 0.0;
Thomas Witkowski's avatar
Thomas Witkowski committed
357
358
      
      for (int j = 0; j < nBasFcts; j++) {
359
	grdPhi = 0.0;
Thomas Witkowski's avatar
Thomas Witkowski committed
360
361
362

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

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

381

Thomas Witkowski's avatar
Thomas Witkowski committed
382
383
384
  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
      ("invalid basis functions");

400
401
    Element *el = elInfo->getElement(); 

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

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

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

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

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

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

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

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

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

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

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

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

477

478
479
480
  template<>
  void DOFVector<double>::interpol(DOFVector<double> *source, double factor) 
  {
481
    FUNCNAME("DOFVector<double>::interpol()");
482

483
    const FiniteElemSpace *sourceFeSpace = source->getFeSpace();
484
485
486
487
488
489
490
491
    const BasisFunction *basisFcts = feSpace->getBasisFcts();
    const BasisFunction *sourceBasisFcts = sourceFeSpace->getBasisFcts();

    int nBasisFcts = basisFcts->getNumber();
    int nSourceBasisFcts = sourceBasisFcts->getNumber();

    this->set(0.0);

492
    std::vector<DegreeOfFreedom> myLocalIndices(nBasisFcts);
493
    ElementVector sourceLocalCoeffs(nSourceBasisFcts);
494

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
	basisFcts->getLocalIndices(elInfo1->getElement(), feSpace->getAdmin(), 
Thomas Witkowski's avatar
Thomas Witkowski committed
532
				   myLocalIndices);
533
	source->getLocalVector(elInfo2->getElement(), sourceLocalCoeffs);
534

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

560
561

  template<>
562
563
  void DOFVector<WorldVector<double> >::interpol(DOFVector<WorldVector<double> > *v, 
						 double factor) 
564
565
566
567
568
569
  {
    WorldVector<double> nul(DEFAULT_VALUE,0.0);

    this->set(nul);

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

572
    if (feSpace == vFeSpace)
573
574
575
      WARNING("same FE-spaces\n");

    const BasisFunction *basFcts = feSpace->getBasisFcts();
576
    const BasisFunction *vBasFcts = vFeSpace->getBasisFcts();
577

578
    int nBasFcts = basFcts->getNumber();
579
580
    int vNumBasFcts = vBasFcts->getNumber();

581
582
    if (feSpace->getMesh() == vFeSpace->getMesh()) {      
      std::vector<DegreeOfFreedom> myLocalIndices(nBasFcts);
583
      mtl::dense_vector<WorldVector<double> > vLocalCoeffs(vNumBasFcts);
584
585
      Mesh *mesh = feSpace->getMesh();
      TraverseStack stack;
586
587
      ElInfo *elInfo = 
	stack.traverseFirst(mesh, -1, Mesh::CALL_LEAF_EL | Mesh::FILL_COORDS);
588
589
590

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

594
	for (int i = 0; i < nBasFcts; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
595
	  if (vec[myLocalIndices[i]] == nul) {
596
	    coords = basFcts->getCoords(i);
597
	    vec[myLocalIndices[i]] += vBasFcts->evalUh(*coords, vLocalCoeffs, NULL) * factor;
598
599
600
601
602
603
604
605
606
607
	  }
	}
	elInfo = stack.traverseNext(elInfo);
      }
    } else {
      ERROR_EXIT("not yet for dual traverse\n");
    }
  }


608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
  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;

624
    if (grad) {
625
626
      result = grad;
    } else {
627
      if (!result) {
628
	result = new WorldVector<DOFVector<double>*>;
629

630
631
	result->set(NULL);
      }
632
      for (int i = 0; i < dow; i++) {
633
	if ((*result)[i] && (*result)[i]->getFeSpace() != feSpace) {
634
635
	  delete (*result)[i];
	  (*result)[i] = new DOFVector<double>(feSpace, "gradient");
636
637
638
639
640
	}
      }
    }

    // count number of nodes and dofs per node
641
642
    std::vector<int> nNodeDOFs;
    std::vector<int> nNodePreDofs;
643
    std::vector<DimVec<double>*> bary;
644

645
646
    int nNodes = 0;
    int nDofs = 0;
647

648
    for (int i = 0; i < dim + 1; i++) {
649
650
      GeoIndex geoIndex = INDEX_OF_DIM(i, dim);
      int numPositionNodes = mesh->getGeo(geoIndex);
651
      int numPreDofs = admin->getNumberOfPreDofs(i);
652
      for (int j = 0; j < numPositionNodes; j++) {
653
	int dofs = basFcts->getNumberOfDofs(geoIndex);
654
	nNodeDOFs.push_back(dofs);
655
	nDofs += dofs;
656
	nNodePreDofs.push_back(numPreDofs);
657
      }
658
      nNodes += numPositionNodes;
659
660
    }

661
    TEST_EXIT_DBG(nDofs == basFcts->getNumber())
662
      ("number of dofs != number of basis functions\n");
663
    
664
    for (int i = 0; i < nDofs; i++)
665
      bary.push_back(basFcts->getCoords(i));    
666
667

    // traverse mesh
668
    std::vector<bool> visited(getUsedSize(), false);
669
    TraverseStack stack;
670
    Flag fillFlag = Mesh::CALL_LEAF_EL | Mesh::FILL_GRD_LAMBDA | Mesh::FILL_COORDS;
671
672
    ElInfo *elInfo = stack.traverseFirst(mesh, -1, fillFlag);
    WorldVector<double> grd;
673
    ElementVector localUh(basFcts->getNumber());
674

675
    while (elInfo) {
676
      const DegreeOfFreedom **dof = elInfo->getElement()->getDof();
677
      getLocalVector(elInfo->getElement(), localUh);
678
679
680
      const DimVec<WorldVector<double> > &grdLambda = elInfo->getGrdLambda();

      int localDOFNr = 0;
681
      for (int i = 0; i < nNodes; i++) { // for all nodes
682
683
	for (int j = 0; j < nNodeDOFs[i]; j++) { // for all dofs at this node
	  DegreeOfFreedom dofIndex = dof[i][nNodePreDofs[i] + j];
684

685
	  if (!visited[dofIndex]) {
686
	    basFcts->evalGrdUh(*(bary[localDOFNr]), grdLambda, localUh, &grd);
687

688
689
	    for (int k = 0; k < dow; k++)
	      (*(*result)[k])[dofIndex] = grd[k];	    
690
691
692
693
694
695
696
697
698
699
700
701
702
703

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

      elInfo = stack.traverseNext(elInfo);
    }

    return result;
  }


704
705
  DOFVectorDOF::DOFVectorDOF() : 
    DOFVector<DegreeOfFreedom>() 
Thomas Witkowski's avatar
Thomas Witkowski committed
706
  {}
707

708

709
  void DOFVectorDOF::freeDOFContent(DegreeOfFreedom dof) 
710
711
  {}

712
713
714
715

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

718
    TEST_EXIT_DBG(vec)("no vector\n");
719

720
    int dow = Global::getGeo(WORLD);
721
722
    static WorldVector<DOFVector<double>*> *result = NULL;

723
    if (!res && !result) {
724
725
      result = new WorldVector<DOFVector<double>*>;
      for (int i = 0; i < dow; i++)
726
	(*result)[i] = new DOFVector<double>(vec->getFeSpace(), "transform");
727
728
729
730
731
    }

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

    int vecSize = vec->getSize();
732
733
    for (int i = 0; i < vecSize; i++)
      for (int j = 0; j < dow; j++)
734
735
736
737
738
	(*((*r)[j]))[i] = (*vec)[i][j];

    return r;
  }

739

Thomas Witkowski's avatar
Thomas Witkowski committed
740
  template<>
741
742
743
744
745
  void DOFVectorBase<double>::getVecAtQPs(const ElInfo *smallElInfo, 
					  const ElInfo *largeElInfo,
					  const Quadrature *quad,
					  const FastQuadrature *quadFast,
					  mtl::dense_vector<double>& vecAtQPs) const
Thomas Witkowski's avatar
Thomas Witkowski committed
746
747
  {
    FUNCNAME("DOFVector<double>::getVecAtQPs()");
748
 
Thomas Witkowski's avatar
Thomas Witkowski committed
749
750
    TEST_EXIT_DBG(quad || quadFast)("neither quad nor quadFast defined\n");

751
    if (quad && quadFast) {
Thomas Witkowski's avatar
Thomas Witkowski committed
752
753
      TEST_EXIT_DBG(quad == quadFast->getQuadrature())
      	("quad != quadFast->quadrature\n");
754
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
755
756
757
758

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

759
    if (smallElInfo->getMesh() == feSpace->getMesh())
760
      return getVecAtQPs(smallElInfo, quad, quadFast, vecAtQPs);    
761

Thomas Witkowski's avatar
Thomas Witkowski committed
762
    const BasisFunction *basFcts = feSpace->getBasisFcts();
763
764
    int nPoints = 
      quadFast ? quadFast->getQuadrature()->getNumPoints() : quad->getNumPoints(); 
Thomas Witkowski's avatar
Thomas Witkowski committed
765

766
767
    vecAtQPs.change_dim(nPoints);
    
768
769
    ElementVector localVec(nBasFcts);
    getLocalVector(largeElInfo->getElement(), localVec);
770
    mtl::dense2D<double> &m = smallElInfo->getSubElemCoordsMat(basFcts->getDegree());
Thomas Witkowski's avatar
Thomas Witkowski committed
771
772

    for (int i = 0; i < nPoints; i++) {
773
      vecAtQPs[i] = 0.0;
Thomas Witkowski's avatar
Thomas Witkowski committed
774
775
      for (int j = 0; j < nBasFcts; j++) {
	double val = 0.0;
776
	for (int k = 0; k < nBasFcts; k++)
777
	  val += m[j][k] * (*(basFcts->getPhi(k)))(quad->getLambda(i));
778
779

	vecAtQPs[i] += localVec[j] * val;
Thomas Witkowski's avatar
Thomas Witkowski committed
780
781
782
783
      }
    }
  }

784

Thomas Witkowski's avatar
Thomas Witkowski committed
785
786
787
788
789
790
791
792
793
794
  template<>
  void DOFVectorBase<double>::assemble(double factor, ElInfo *elInfo,
				       const BoundaryType *bound, 
				       Operator *op)
  {
    FUNCNAME("DOFVector::assemble()");

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

795
    set_to_zero(this->elementVector);
796
    bool addVector = false;
Thomas Witkowski's avatar
Thomas Witkowski committed
797
798
799

    if (op) {
      op->getElementVector(elInfo, this->elementVector);
800
      addVector = true;
Thomas Witkowski's avatar
Thomas Witkowski committed
801
802
803
804
805
806
    } else {
      std::vector<Operator*>::iterator it;
      std::vector<double*>::iterator factorIt;

      for (it = this->operators.begin(), factorIt = this->operatorFactor.begin();	
	   it != this->operators.end(); 
807
	   ++it, ++factorIt)
808
	if ((*it)->getNeedDualTraverse() == false) {
Thomas Witkowski's avatar
Thomas Witkowski committed
809
	  (*it)->getElementVector(elInfo, this->elementVector, 
810
				  *factorIt ? **factorIt : 1.0);      
811
812
	  addVector = true;
	}
Thomas Witkowski's avatar
Thomas Witkowski committed
813
814
    }

815
816
    if (addVector)
      addElementVector(factor, this->elementVector, bound, elInfo);
Thomas Witkowski's avatar
Thomas Witkowski committed
817
818
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
819

Thomas Witkowski's avatar
Thomas Witkowski committed
820
821
822
823
824
825
826
827
828
829
830
831
  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;

832
    set_to_zero(this->elementVector);
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
    bool addVector = false;

    TEST_EXIT(!op)("Not yet implemented!\n");

    std::vector<Operator*>::iterator it;
    std::vector<double*>::iterator factorIt;
    for (it = this->operators.begin(), factorIt = this->operatorFactor.begin();	
	 it != this->operators.end(); 
	 ++it, ++factorIt)
      if ((*it)->getNeedDualTraverse()) {
	(*it)->getElementVector(mainElInfo, auxElInfo,
				smallElInfo, largeElInfo,
				this->elementVector, 
				*factorIt ? **factorIt : 1.0);
	addVector = true;
Thomas Witkowski's avatar
Thomas Witkowski committed
848
849
      }
  
850
851
    if (addVector)
      addElementVector(factor, this->elementVector, bound, mainElInfo);
Thomas Witkowski's avatar
Thomas Witkowski committed
852
853
854
  }


855
856
857
858
  double integrate(const DOFVector<double> &vec1,
		   const DOFVector<double> &vec2,
		   BinaryAbstractFunction<double, double, double> *fct)
  {
859
    if (vec1.getFeSpace()->getMesh() == vec2.getFeSpace()->getMesh())
860
861
862
863
864
865
866
867
868
869
870
871
872
873
      return intSingleMesh(vec1, vec2, fct);
    else
      return intDualMesh(vec1, vec2, fct);
  }


  double intSingleMesh(const DOFVector<double> &vec1,
		       const DOFVector<double> &vec2,
		       BinaryAbstractFunction<double, double, double> *fct)
  {
    FUNCNAME("intDualmesh()");
    
    TEST_EXIT(fct)("No function defined!\n");

874
875
    int deg = std::max(fct->getDegree(), 
		       2 * vec1.getFeSpace()->getBasisFcts()->getDegree());
876
    Quadrature* quad = 
877
      Quadrature::provideQuadrature(vec1.getFeSpace()->getMesh()->getDim(), deg);
878
    FastQuadrature *fastQuad =
879
      FastQuadrature::provideFastQuadrature(vec1.getFeSpace()->getBasisFcts(), *quad, INIT_PHI);
880

881
882
    mtl::dense_vector<double> qp1(fastQuad->getNumPoints());
    mtl::dense_vector<double> qp2(fastQuad->getNumPoints());
883
884
885
886

    double value = 0.0;
    Flag traverseFlag = Mesh::CALL_LEAF_EL | Mesh::FILL_COORDS | Mesh::FILL_DET;      
    TraverseStack stack;    
887
    ElInfo *elInfo = stack.traverseFirst(vec1.getFeSpace()->getMesh(), -1, traverseFlag);
888
    while (elInfo) {
889
890
891
892
      vec1.getVecAtQPs(elInfo, quad, fastQuad, qp1);
      vec2.getVecAtQPs(elInfo, quad, fastQuad, qp2);

      double tmp = 0.0;
893
      for (int iq = 0; iq < fastQuad->getNumPoints(); iq++)
894
895
 	tmp += fastQuad->getWeight(iq) * (*fct)(qp1[iq], qp2[iq]);     
      value += tmp * elInfo->getDet();
896
897
898
899
      
      elInfo = stack.traverseNext(elInfo);
    }

900
901
902
903
904
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
    double localValue = value;
    MPI::COMM_WORLD.Allreduce(&localValue, &value, 1, MPI_DOUBLE, MPI_SUM);
#endif
    
905
906
907
908
909
910
911
912
913
914
915
916
    return value;
  }


  double intDualMesh(const DOFVector<double> &vec1,
		     const DOFVector<double> &vec2,
		     BinaryAbstractFunction<double, double, double> *fct)
  {
    FUNCNAME("intDualmesh()");
    
    TEST_EXIT(fct)("No function defined!\n");

917
918
    int deg = std::max(fct->getDegree(), 
		       2 * vec1.getFeSpace()->getBasisFcts()->getDegree());
919
    Quadrature* quad = 
920
      Quadrature::provideQuadrature(vec1.getFeSpace()->getMesh()->getDim(), deg);
921
    FastQuadrature *fastQuad =
922
      FastQuadrature::provideFastQuadrature(vec1.getFeSpace()->getBasisFcts(), *quad, INIT_PHI);
923

924
925
    mtl::dense_vector<double> qp1(fastQuad->getNumPoints());
    mtl::dense_vector<double> qp2(fastQuad->getNumPoints());
926
927
928
929
930

    double value = 0.0;
    Flag traverseFlag = Mesh::CALL_LEAF_EL | Mesh::FILL_COORDS | Mesh::FILL_DET;      
    DualTraverse dualTraverse;    
    DualElInfo dualElInfo;
931
932
    bool cont = dualTraverse.traverseFirst(vec1.getFeSpace()->getMesh(),
					   vec2.getFeSpace()->getMesh(),
933
934
935
					   -1, -1, traverseFlag, traverseFlag,
					   dualElInfo);
    while (cont) {
936
937
938
939
      vec1.getVecAtQPs(dualElInfo.smallElInfo, dualElInfo.largeElInfo, quad, NULL, qp1);
      vec2.getVecAtQPs(dualElInfo.smallElInfo, dualElInfo.largeElInfo, quad, NULL, qp2);

      double tmp = 0.0;