DataCollector.cc 13.5 KB
Newer Older
1
2
3
4
5
6
7
8
#include "DataCollector.h"
#include "Traverse.h"
#include "DOFVector.h"
#include "SurfaceRegion_ED.h"
#include "ElementRegion_ED.h"
#include "Projection.h"

namespace AMDiS {
9

10
11
12
13
14
15
16
17
18
19
  DataCollector::DataCollector(const FiniteElemSpace *fe,
			       DOFVector<double> *val,
			       int l,
			       Flag flag,
			       bool (*writeElemFct)(ElInfo*))
    : values(val),
      level(l),
      traverseFlag(flag),
      feSpace(fe),
      writeElem(writeElemFct)
20
  {
21
    FUNCNAME("DataCollector::DataCollector()");
22
23
24

    TEST_EXIT(feSpace)("no feSpace\n");
    TEST_EXIT(values)("no value Vector\n");
25
   
26
    // === get mesh  ===    
27
    mesh = feSpace->getMesh();
28
29

    // === get admin ===    
30
    localAdmin = const_cast<DOFAdmin*>(feSpace->getAdmin());
31
    // === create vertex info vector ===
32
    vertexInfos = new DOFVector< std::list<VertexInfo> >(feSpace, "vertex infos");
33

34
35
36
    interpPointInd = new DOFVector<int>(feSpace, "interpolation point indices");
    interpPointCoords = new DOFVector< std::list<WorldVector<double> > >(feSpace, "interpolation point coordinates");
    dofCoord = new DOFVector< std::list<WorldVector<double> > >(feSpace, "dof coords");
37

38
    dim = mesh->getDim();
39
    
40
41
42
43
44
    nPreDofs = localAdmin->getNumberOfPreDOFs(VERTEX);
    nVertices = 0;
    nElements = 0;
    nInterpPoints = 0;
    nConnection = 0;
45

46
47
48
    elementDataCollected = false;
    valueDataCollected = false;
    periodicDataCollected = false;
Thomas Witkowski's avatar
Thomas Witkowski committed
49

Thomas Witkowski's avatar
Thomas Witkowski committed
50
    vertexCoords = new WorldVector<double>;
Thomas Witkowski's avatar
Thomas Witkowski committed
51
  } 
52
53
54

  DataCollector::~DataCollector()
  {
55
56
57
58
    delete vertexInfos;
    delete interpPointInd;
    delete interpPointCoords;
    delete dofCoord;
Thomas Witkowski's avatar
Thomas Witkowski committed
59
    delete vertexCoords;
60
61
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
62
63
  void DataCollector::fillAllData()
  {
64
    if (!elementDataCollected)
Thomas Witkowski's avatar
Thomas Witkowski committed
65
      startCollectingElementData();
66

67
    if (!periodicDataCollected)
Thomas Witkowski's avatar
Thomas Witkowski committed
68
      startCollectingPeriodicData();
69

70
    if (!valueDataCollected)
Thomas Witkowski's avatar
Thomas Witkowski committed
71
72
73
      startCollectingValueData();
  }

74
  void DataCollector::startCollectingElementData()
75
  {
76
    FUNCNAME("DataCollector::startCollectingElementData()");
77

78
    Flag flag = traverseFlag;
79
80
81
82
83
84
    flag |= 
      Mesh::FILL_NEIGH      | 
      Mesh::FILL_COORDS     |
      Mesh::FILL_OPP_COORDS |
      Mesh::FILL_BOUND;

85
    elements.resize(0, dim);
86
87
88
89

    TraverseStack stack;

    // Traverse elements to create continuous element indices
90
    ElInfo *elInfo = stack.traverseFirst(mesh, level, flag);
91
    while (elInfo) {
92
93
      if (!writeElem || writeElem(elInfo))
	outputIndices[elInfo->getElement()->getIndex()] = nElements++;
94
95
96
97
      elInfo = stack.traverseNext(elInfo);
    }

    // Traverse elements to create element information
98
    elInfo = stack.traverseFirst(mesh, level, flag);
99

100
    while (elInfo) {
101
      if (!writeElem || writeElem(elInfo))
102
	addElementData(elInfo);
103

104
105
106
      elInfo = stack.traverseNext(elInfo);
    }

107
    elementDataCollected = true;
108
109
  }

110
  void DataCollector::startCollectingValueData()
111
  {
112
    FUNCNAME("DataCollector::startCollectingValueData()");
113

114
    DOFVector<int>::Iterator intPointIt(interpPointInd, USED_DOFS);
115
    for (intPointIt.reset(); !intPointIt.end(); ++intPointIt)
116
117
      (*intPointIt) = -1;

118
    interpPoints.clear();
119

120
121
122
    basisFcts = const_cast<BasisFunction*>(feSpace->getBasisFcts());
    nBasisFcts = basisFcts->getNumber();
    localDOFs = new DegreeOfFreedom[nBasisFcts];
123
  
124
125
126
127
    TraverseStack stack;

    // Traverse elements to add value information and to mark
    // interpolation points.
128
129
    ElInfo *elInfo = stack.traverseFirst(mesh, level, 
					 traverseFlag | Mesh::FILL_COORDS);
130
    while (elInfo) {
131
      if (!writeElem || writeElem(elInfo))
Thomas Witkowski's avatar
Thomas Witkowski committed
132
	addValueData(elInfo);
133
134
135
      elInfo = stack.traverseNext(elInfo);
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
136
137
    // If there are interpolation points, add them to the corresponding 
    // data array.
138
    if (nInterpPoints > 0) {
Thomas Witkowski's avatar
Thomas Witkowski committed
139
140
141
      // Remove all interpolation marks and, instead, set to each
      // interpolation point its continous index starting from 0.
      int i = 0;
142
143
      for (intPointIt.reset(); !intPointIt.end(); ++intPointIt)
	if (*intPointIt == -3)
Thomas Witkowski's avatar
Thomas Witkowski committed
144
145
146
	  *intPointIt = i++;
      
      // Traverse elements to create interpolation values.
147
      elInfo = stack.traverseFirst(mesh, level, traverseFlag | Mesh::FILL_COORDS);
Thomas Witkowski's avatar
Thomas Witkowski committed
148
      while (elInfo) {
149
	if (!writeElem || writeElem(elInfo))
Thomas Witkowski's avatar
Thomas Witkowski committed
150
151
152
	  addInterpData(elInfo);
	elInfo = stack.traverseNext(elInfo);
      }
153
    }
154
   
155
156
    delete [] localDOFs;
    valueDataCollected = true;
157
158
  }

159
  void DataCollector::startCollectingPeriodicData()
160
  {    
161
    FUNCNAME("DataCollector::startCollectingPeriodicData()");
162

163
    periodicConnections.clear();
164
165
    
    TraverseStack stack;
166
    ElInfo *elInfo = stack.traverseFirst(mesh, level, traverseFlag);
167
    while (elInfo) {
168
      if (!writeElem || writeElem(elInfo)) {
169
170
171
172
173
	LeafDataPeriodic *ldp = dynamic_cast<LeafDataPeriodic*>
	  (elInfo->getElement()->
	   getElementData()->
	   getElementData(PERIODIC));
	
174
	if (ldp) {
175
	  nConnection += dynamic_cast<LeafDataPeriodic*>(ldp)->getInfoList().size();
176
177
	}

178
	periodicConnections.push_back(DimVec<bool>(dim, DEFAULT_VALUE, false));
179
180
181
182
      }
      elInfo = stack.traverseNext(elInfo);
    }   

183
    nConnection /= 2;
184

185
    periodicInfos.clear();
186

187
    Flag flag = traverseFlag;
188
189
190
191
192
193
    flag |= 
      Mesh::FILL_COORDS    |
      Mesh::FILL_OPP_COORDS|
      Mesh::FILL_NEIGH     | 
      Mesh::FILL_BOUND;

194
    elInfo = stack.traverseFirst(mesh, level, flag);
195
    while (elInfo) {
196
      if (!writeElem || writeElem(elInfo))
197
198
199
200
	addPeriodicData(elInfo);
      elInfo = stack.traverseNext(elInfo);
    }   

201
    periodicDataCollected = true;
202
203
  }

204
  void DataCollector::addElementData(ElInfo* elInfo)
205
  {
206
    FUNCNAME("DataCollector::addElementData()");
207
208
209
210

    const DegreeOfFreedom **dof = elInfo->getElement()->getDOF();
    DegreeOfFreedom vertexDOF;
    WorldVector<double> vertexCoords;
211

212
    // create ElementInfo
213
    ElementInfo elementInfo(dim);
214
215
    
    // read element region
216
    ElementData *ed = elInfo->getElement()->getElementData(ELEMENT_REGION);
217
    
Thomas Witkowski's avatar
Thomas Witkowski committed
218
    if (ed)
219
      elementInfo.elementRegion = dynamic_cast<ElementRegion_ED*>(ed)->getRegion();
Thomas Witkowski's avatar
Thomas Witkowski committed
220
221
    else 
      elementInfo.elementRegion = -1;    
222
   
223
224
225
    // read surface regions to element info
    ed = elInfo->getElement()->getElementData(SURFACE_REGION);
    elementInfo.surfaceRegions.set(-1);
226
    while (ed) {
227
228
229
230
231
232
      SurfaceRegion_ED *sr = dynamic_cast<SurfaceRegion_ED*>(ed);
      elementInfo.surfaceRegions[sr->getSide()] = sr->getRegion();
      ed = ed->getDecorated(SURFACE_REGION);
    }

    // for all vertices
233
    for (int i = 0; i < dim + 1; i++) {
234
235
236
237
      // get coords of this vertex
      vertexCoords = elInfo->getCoord(i);
      
      // get dof index of this vertex
238
      vertexDOF = dof[i][nPreDofs];
239

240
      // search for coords at this dof
Thomas Witkowski's avatar
Thomas Witkowski committed
241
      std::list<VertexInfo>::iterator it =
242
	  find((*vertexInfos)[vertexDOF].begin(), (*vertexInfos)[vertexDOF].end(),
243
244
245
	       vertexCoords);
      
      // coords not yet in list?
246
      if (it == (*vertexInfos)[vertexDOF].end()) {
247
	// create new vertex info and increment number of vertices
248
	VertexInfo newVertexInfo = {vertexCoords, nVertices};
249
250

	// add new vertex info to list
251
	(*vertexInfos)[vertexDOF].push_front(newVertexInfo);
252

253
	// set iterator to new vertex info
254
	it = (*vertexInfos)[vertexDOF].begin();	
255

256
	nVertices++;
257
258
259
260
261
262
263
264
      }
      
      // fill element info
      elementInfo.vertexInfo[i] = it;
      elementInfo.boundary[i] = elInfo->getBoundary(i);
      elementInfo.projection[i] = elInfo->getProjection(i);
      elementInfo.neighbour[i] = 
	elInfo->getNeighbour(i) ?
265
	outputIndices[elInfo->getNeighbour(i)->getIndex()] :
266
267
268
	-1;
    }
    
269
    if (dim == 3)
270
271
272
      elementInfo.type = (dynamic_cast<ElInfo3d*>(elInfo)->getType());

    // remember element info
273
    elements.push_back(elementInfo);
274
275
  }

276
  void DataCollector::addValueData(ElInfo *elInfo)
277
  {
278
    FUNCNAME("DataCollector::addValueData()");
279
    
280
    basisFcts->getLocalIndices(elInfo->getElement(), localAdmin, localDOFs);
281
282

    // First, traverse all DOFs at the vertices of the element, determine
283
284
285
    // their coordinates and add them to the corresponding entry in dofCoords.
    for (int i = 0; i < mesh->getGeo(VERTEX); i++) {
      DegreeOfFreedom dofi = localDOFs[i];
Thomas Witkowski's avatar
Thomas Witkowski committed
286

287
      (*interpPointInd)[dofi] = -2; // mark as vertex
288
289
      
      // get coords of this vertex
Thomas Witkowski's avatar
Thomas Witkowski committed
290
      *vertexCoords = elInfo->getCoord(i);
291

292
      // search for coords at this dof
Thomas Witkowski's avatar
Thomas Witkowski committed
293
      std::list<WorldVector<double> >::iterator it =
294
295
	  find((*dofCoord)[dofi].begin(),
	       (*dofCoord)[dofi].end(),
Thomas Witkowski's avatar
Thomas Witkowski committed
296
	       *vertexCoords);
297
298
      
      // coords not yet in list?
299
      if (it == (*dofCoord)[dofi].end()) {
300
	// add new coords to list
301
	(*dofCoord)[dofi].push_back(*vertexCoords);
302
303
      }
    }
304
   
305
306
    // Then, traverse all interpolation DOFs of the element, determine
    // their coordinates and add them to the corresponding entry in 
307
308
309
    // interpPointCoords.
    for (int i = mesh->getGeo(VERTEX); i < nBasisFcts; i++) {
      DegreeOfFreedom dofi = localDOFs[i];
310

311
      elInfo->coordToWorld(*basisFcts->getCoords(i), *vertexCoords);
312
      
313
      if ((*interpPointInd)[dofi] == -1) {
314
	// mark as interpolation point
315
	(*interpPointInd)[dofi] = -3; 
316
317
318
	
	// search for interpolation point coordinates, and insert them to the 
	// dof-entry, if not contained in the list
Thomas Witkowski's avatar
Thomas Witkowski committed
319
	std::list<WorldVector<double> >::iterator it =
320
		find((*interpPointCoords)[dofi].begin(), 
321
		     (*interpPointCoords)[dofi].end(),
Thomas Witkowski's avatar
Thomas Witkowski committed
322
		     *vertexCoords);
323
	
324
325
326
	if (it == (*interpPointCoords)[dofi].end()) {
	  (*interpPointCoords)[dofi].push_back(*vertexCoords); 
	  nInterpPoints++;
327
	}
328
      }      
329
    }  
330
331
  }

332
  void DataCollector::addInterpData(ElInfo *elInfo)
333
  {
334
    FUNCNAME("DataCollector::addInterpData()");
335
    
336
    basisFcts->getLocalIndices(elInfo->getElement(), localAdmin, localDOFs);
337

338
    std::vector<int> elemInterpPoints(0);
339
340
    for (int i = mesh->getGeo(VERTEX); i < nBasisFcts; i++)
      elemInterpPoints.push_back((*interpPointInd)[localDOFs[i]]);
Thomas Witkowski's avatar
Thomas Witkowski committed
341
    
342
    interpPoints.push_back(elemInterpPoints); 
343
344
  }

345
346
  void DataCollector::addPeriodicData(ElInfo *elInfo) 
  {
347
348
349
350
351
352
353
    FUNCNAME("DataCollector::addPeriodicData");

    LeafDataPeriodic *ldp = dynamic_cast<LeafDataPeriodic*>
      (elInfo->getElement()->
       getElementData()->
       getElementData(PERIODIC));
    
354
    if (ldp) {
Thomas Witkowski's avatar
Thomas Witkowski committed
355
      std::list<LeafDataPeriodic::PeriodicInfo>::iterator it;
356
      
357
358
359
360
      for (it = dynamic_cast<LeafDataPeriodic*>(ldp)->getInfoList().begin();
	   it != dynamic_cast<LeafDataPeriodic*>(ldp)->getInfoList().end();
	   ++it) {

361
362
	int outputIndex = outputIndices[elInfo->getElement()->getIndex()];
	int neighIndex  = outputIndices[elInfo->
363
364
365
					 getNeighbour(it->elementSide)->
					 getIndex()];
	
366
	if (!periodicConnections[outputIndex][it->elementSide]) {
367
	  PeriodicInfo periodicInfo;
368
	  
369
370
371
372
373
374
	  periodicInfo.mode = it->periodicMode;
	  periodicInfo.type = it->type;
	  periodicInfo.outputIndex = outputIndex;
	  periodicInfo.neighIndex = neighIndex;
	  periodicInfo.vertexMap.clear();
	  
375
376
	  periodicConnections[outputIndex][it->elementSide] = true;
	  periodicConnections
377
378
379
	    [neighIndex][elInfo->getOppVertex(it->elementSide)] = true;
	    
  
380
381
	  for (int i = 0; i < dim; i++) {
	    int index1 = elInfo->getElement()->getVertexOfPosition(INDEX_OF_DIM(dim - 1, dim),
382
383
								   it->elementSide,
								   i);
384
	    int dof1 = elInfo->getElement()->getDOF(index1, nPreDofs);
385
	    
386
387
	    for (int j = 0; j < dim; j++) {
	      int index2 = elInfo->getElement()->getVertexOfPosition(INDEX_OF_DIM(dim - 1, dim),
388
389
								     elInfo->getOppVertex(it->elementSide),
								     j);
390
	      int dof2 = elInfo->getNeighbour(it->elementSide)->getDOF(index2, nPreDofs);
391
	      
392
	      if ((dof1 == dof2) || (mesh->associated(dof1, dof2))) {
393
394
395
		periodicInfo.vertexMap[index1] = index2;
		break;
	      } 
396
397
	    }
	  }
398
	  
399
	  periodicInfos.push_back(periodicInfo);
400
	}
401
      }
402
403
404
    }
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
405
  std::list<ElementInfo>* DataCollector::getElementInfos()
406
  {        
407
    if (!elementDataCollected)
408
409
      startCollectingElementData();
      
410
    return &elements;
411
412
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
413
  DOFVector< std::list<VertexInfo> >* DataCollector::getVertexInfos()
414
  {
415
    if (!elementDataCollected)
416
417
      startCollectingElementData();

418
    return vertexInfos;
419
420
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
421
  std::list<PeriodicInfo>* DataCollector::getPeriodicInfos()
422
  {
423
    if (!periodicDataCollected)
424
425
      startCollectingPeriodicData();

426
    return &periodicInfos;
427
428
429
430
  }

  int DataCollector::getNumberVertices()
  {
431
    if (!elementDataCollected)
432
433
      startCollectingElementData();

434
    return nVertices;
435
436
437
438
  }

  int DataCollector::getNumberElements()
  {
439
    if (!elementDataCollected)
440
441
      startCollectingElementData();

442
    return nElements;
443
444
445
446
  }

  int DataCollector::getNumberInterpPoints()
  {
447
    if (!valueDataCollected)
448
449
      startCollectingValueData();

450
    return nInterpPoints;
451
452
453
454
  }
  
  int DataCollector::getNumberConnections()
  {
455
    if (!periodicDataCollected)
456
457
      startCollectingPeriodicData();

458
    return nConnection;
459
460
461
462
  }

  const FiniteElemSpace* DataCollector::getFeSpace()
  {
463
    return feSpace;
464
465
466
467
  }

  Mesh* DataCollector::getMesh()
  {
468
    return mesh;
469
470
471
472
  }

  DOFVector<double>* DataCollector::getValues()
  {
473
    if (!valueDataCollected)
474
475
      startCollectingValueData();

476
    return values;
477
478
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
479
  DOFVector< std::list<WorldVector<double> > >* DataCollector::getDofCoords()
480
  {
481
    if (!valueDataCollected)
482
483
      startCollectingValueData();

484
    return dofCoord;
485
486
487
488
  }

  DOFVector<int>* DataCollector::getInterpPointInd()
  {
489
    if (!valueDataCollected)
490
491
      startCollectingValueData();

492
    return interpPointInd;
493
494
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
495
  DOFVector< std::list<WorldVector<double> > >* DataCollector::getInterpPointCoords()
496
  {
497
    if (!valueDataCollected)
498
499
      startCollectingValueData();

500
    return interpPointCoords;
501
502
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
503
  std::vector< std::vector<int> >* DataCollector::getInterpPoints()
504
  {
505
    if (!valueDataCollected)
506
507
      startCollectingValueData();

508
    return &interpPoints;
509
510
  }
}