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
    
218
    if (ed) {
219
220
221
222
      elementInfo.elementRegion = dynamic_cast<ElementRegion_ED*>(ed)->getRegion();
    } else {
      elementInfo.elementRegion = -1;
    }
223
   
224
225
226
    // read surface regions to element info
    ed = elInfo->getElement()->getElementData(SURFACE_REGION);
    elementInfo.surfaceRegions.set(-1);
227
    while (ed) {
228
229
230
231
232
233
      SurfaceRegion_ED *sr = dynamic_cast<SurfaceRegion_ED*>(ed);
      elementInfo.surfaceRegions[sr->getSide()] = sr->getRegion();
      ed = ed->getDecorated(SURFACE_REGION);
    }

    // for all vertices
234
    for (int i = 0; i < dim + 1; i++) {
235
236
237
238
      // get coords of this vertex
      vertexCoords = elInfo->getCoord(i);
      
      // get dof index of this vertex
239
      vertexDOF = dof[i][nPreDofs];
240
     
241
      // search for coords at this dof
Thomas Witkowski's avatar
Thomas Witkowski committed
242
      std::list<VertexInfo>::iterator it =
243
244
	  find((*vertexInfos)[vertexDOF].begin(),
	       (*vertexInfos)[vertexDOF].end(),
245
246
247
	       vertexCoords);
      
      // coords not yet in list?
248
      if (it == (*vertexInfos)[vertexDOF].end()) {
249
	// create new vertex info and increment number of vertices
250
	VertexInfo newVertexInfo = {vertexCoords, nVertices};
251
252

	// add new vertex info to list
253
	(*vertexInfos)[vertexDOF].push_front(newVertexInfo);
254
255
	
	// set iterator to new vertex info
256
	it = (*vertexInfos)[vertexDOF].begin();	
257

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

    // remember element info
275
    elements.push_back(elementInfo);
276
277
  }

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

    // First, traverse all DOFs at the vertices of the element, determine
285
286
287
    // 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
288

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

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

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

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

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

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

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

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

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

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

420
    return vertexInfos;
421
422
  }

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

428
    return &periodicInfos;
429
430
431
432
  }

  int DataCollector::getNumberVertices()
  {
433
    if (!elementDataCollected)
434
435
      startCollectingElementData();

436
    return nVertices;
437
438
439
440
  }

  int DataCollector::getNumberElements()
  {
441
    if (!elementDataCollected)
442
443
      startCollectingElementData();

444
    return nElements;
445
446
447
448
  }

  int DataCollector::getNumberInterpPoints()
  {
449
    if (!valueDataCollected)
450
451
      startCollectingValueData();

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

460
    return nConnection;
461
462
463
464
  }

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

  Mesh* DataCollector::getMesh()
  {
470
    return mesh;
471
472
473
474
  }

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

478
    return values;
479
480
  }

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

486
    return dofCoord;
487
488
489
490
  }

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

494
    return interpPointInd;
495
496
  }

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

502
    return interpPointCoords;
503
504
  }

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

510
    return &interpPoints;
511
512
  }
}