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
	periodicConnections.push_back(DimVec<bool>(dim, DEFAULT_VALUE, false));
178
179
180
181
      }
      elInfo = stack.traverseNext(elInfo);
    }   

182
    nConnection /= 2;
183

184
    periodicInfos.clear();
185

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

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

200
    periodicDataCollected = true;
201
202
  }

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

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

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

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

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

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

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

255
	nVertices++;
256
257
258
259
260
261
262
263
      }
      
      // 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) ?
264
	outputIndices[elInfo->getNeighbour(i)->getIndex()] :
265
266
267
	-1;
    }
    
268
    if (dim == 3)
269
      elementInfo.type = elInfo->getType();
270
271

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

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

    // First, traverse all DOFs at the vertices of the element, determine
282
283
284
    // 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
285

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

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

310
      elInfo->coordToWorld(*basisFcts->getCoords(i), *vertexCoords);
311
      
312
      if ((*interpPointInd)[dofi] == -1) {
313
	// mark as interpolation point
314
	(*interpPointInd)[dofi] = -3; 
315
316
317
	
	// 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
318
	std::list<WorldVector<double> >::iterator it =
319
		find((*interpPointCoords)[dofi].begin(), 
320
		     (*interpPointCoords)[dofi].end(),
Thomas Witkowski's avatar
Thomas Witkowski committed
321
		     *vertexCoords);
322
	
323
324
325
	if (it == (*interpPointCoords)[dofi].end()) {
	  (*interpPointCoords)[dofi].push_back(*vertexCoords); 
	  nInterpPoints++;
326
	}
327
      }      
328
    }  
329
330
  }

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

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

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

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

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

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

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

417
    return vertexInfos;
418
419
  }

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

425
    return &periodicInfos;
426
427
428
429
  }

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

433
    return nVertices;
434
435
436
437
  }

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

441
    return nElements;
442
443
444
445
  }

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

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

457
    return nConnection;
458
459
460
461
  }

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

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

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

475
    return values;
476
477
  }

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

483
    return dofCoord;
484
485
486
487
  }

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

491
    return interpPointInd;
492
493
  }

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

499
    return interpPointCoords;
500
501
  }

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

507
    return &interpPoints;
508
509
  }
}