DataCollector.cc 14.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "DataCollector.h"
#include "Traverse.h"
#include "DOFVector.h"
#include "SurfaceRegion_ED.h"
#include "ElementRegion_ED.h"
#include "Projection.h"

namespace AMDiS {
  DataCollector::DataCollector(const FiniteElemSpace *feSpace,
			       DOFVector<double> *values,
			       int level,
			       Flag traverseFlag,
			       bool (*writeElem)(ElInfo*))
    : writeElem_(writeElem)
  {
16
    FUNCNAME("DataCollector::DataCollector()");
17
18
19

    TEST_EXIT(feSpace)("no feSpace\n");
    TEST_EXIT(values)("no value Vector\n");
20
      
21
22
23
24
25
26
27
    // === get mesh  ===    
    mesh_ = feSpace->getMesh();

    // === get admin ===    
    localAdmin_ = const_cast<DOFAdmin*>(feSpace->getAdmin());

    // === create vertex info vector ===
Thomas Witkowski's avatar
Thomas Witkowski committed
28
    vertexInfos_ = NEW DOFVector< std::list<VertexInfo> >(feSpace, "vertex infos");
29
    interpPointInd_ = NEW DOFVector<int>(feSpace, "interpolation point indices");
Thomas Witkowski's avatar
Thomas Witkowski committed
30
31
    interpPointCoords_ = NEW DOFVector< std::list<WorldVector<double> > >(feSpace, "interpolation point coordinates");
    dofCoords_ = NEW DOFVector< std::list<WorldVector<double> > >(feSpace, "dof coords");
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

    dim_ = mesh_->getDim();
    
    nPreDofs_ = localAdmin_->getNumberOfPreDOFs(VERTEX);
    nVertices_ = 0;
    nElements_ = 0;
    nInterpPoints_ = 0;
    nConnections_ = 0;

    feSpace_ = feSpace;
    values_ = values;
    level_ = level;
    traverseFlag_ = traverseFlag;

    elementDataCollected_ = false;
    valueDataCollected_ = false;
    periodicDataCollected_ = false;
Thomas Witkowski's avatar
Thomas Witkowski committed
49
50
51

    vertexCoords = NEW WorldVector<double>;
  } 
52
53
54
55
56

  DataCollector::~DataCollector()
  {
    DELETE vertexInfos_;
    DELETE interpPointInd_;
57
    DELETE interpPointCoords_;
58
    DELETE dofCoords_;
Thomas Witkowski's avatar
Thomas Witkowski committed
59
    DELETE vertexCoords;
60
61
  }

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

Thomas Witkowski's avatar
Thomas Witkowski committed
68
69
70
    if (!periodicDataCollected_) {
      startCollectingPeriodicData();
    }
71

Thomas Witkowski's avatar
Thomas Witkowski committed
72
73
74
75
76
    if (!valueDataCollected_) {
      startCollectingValueData();
    }
  }

77
78
  int DataCollector::startCollectingElementData()
  {
79
    FUNCNAME("DataCollector::startCollectingElementData()");
80
81
82
83
84
85
86
87
88
89
90
91
92
93

    Flag flag = traverseFlag_;
    flag |= 
      Mesh::FILL_NEIGH      | 
      Mesh::FILL_COORDS     |
      Mesh::FILL_OPP_COORDS |
      Mesh::FILL_BOUND;

    elements_.resize(0, dim_);

    TraverseStack stack;

    // Traverse elements to create continuous element indices
    ElInfo *elInfo = stack.traverseFirst(mesh_, level_, flag);
94
95
    while (elInfo) {
      if (!writeElem_ || writeElem_(elInfo))
96
97
98
99
100
101
	outputIndices_[elInfo->getElement()->getIndex()] = nElements_++;
      elInfo = stack.traverseNext(elInfo);
    }

    // Traverse elements to create element information
    elInfo = stack.traverseFirst(mesh_, level_, flag);
102
103
    while (elInfo) {
      if (!writeElem_ || writeElem_(elInfo))
104
105
106
107
108
109
110
111
112
113
114
	addElementData(elInfo);
      elInfo = stack.traverseNext(elInfo);
    }

    elementDataCollected_ = true;

    return(0);
  }

  int DataCollector::startCollectingValueData()
  {
115
    FUNCNAME("DataCollector::startCollectingValueData()");
116
117

    DOFVector<int>::Iterator intPointIt(interpPointInd_, USED_DOFS);
118
    for (intPointIt.reset(); !intPointIt.end(); ++intPointIt) {
119
120
121
122
123
      (*intPointIt) = -1;
    }

    interpPoints_.clear();

Thomas Witkowski's avatar
Thomas Witkowski committed
124
125
    basisFcts_ = const_cast<BasisFunction*>(feSpace_->getBasisFcts());
    nBasisFcts_ = basisFcts_->getNumber();
Thomas Witkowski's avatar
Thomas Witkowski committed
126
    localDOFs_ = GET_MEMORY(DegreeOfFreedom, nBasisFcts_);
Thomas Witkowski's avatar
Thomas Witkowski committed
127
    
128
129
130
131
132
133
134
    TraverseStack stack;

    // Traverse elements to add value information and to mark
    // interpolation points.
    ElInfo *elInfo = stack.traverseFirst(mesh_, 
					 level_, 
					 traverseFlag_ | Mesh::FILL_COORDS);
135
    while (elInfo) {
136
      if (!writeElem_ || writeElem_(elInfo))
Thomas Witkowski's avatar
Thomas Witkowski committed
137
	addValueData(elInfo);
138
139
140
      elInfo = stack.traverseNext(elInfo);
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
    // If there are interpolation points, add them to the corresponding 
    // data array.
    if (nInterpPoints_ > 0) {
      // Remove all interpolation marks and, instead, set to each
      // interpolation point its continous index starting from 0.
      int i = 0;
      for (intPointIt.reset(); !intPointIt.end(); ++intPointIt) {
	if (*intPointIt == -3) {
	  *intPointIt = i++;
	}
      }
      
      // Traverse elements to create interpolation values.
      elInfo = stack.traverseFirst(mesh_, level_, traverseFlag_ | Mesh::FILL_COORDS);
      while (elInfo) {
	if (!writeElem_ || writeElem_(elInfo))
	  addInterpData(elInfo);
	elInfo = stack.traverseNext(elInfo);
      }
160
    }
161
    
Thomas Witkowski's avatar
Thomas Witkowski committed
162
    FREE_MEMORY(localDOFs_, DegreeOfFreedom, nBasisFcts_);
163
    valueDataCollected_ = true;
Thomas Witkowski's avatar
Thomas Witkowski committed
164
    
165
166
167
168
169
    return(0);
  }

  int DataCollector::startCollectingPeriodicData()
  {    
170
    FUNCNAME("DataCollector::startCollectingPeriodicData()");
171
172
173
174
175

    periodicConnections_.clear();
    
    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirst(mesh_, level_, traverseFlag_);
176
177
    while (elInfo) {
      if (!writeElem_ || writeElem_(elInfo)) {
178
179
180
181
182
	LeafDataPeriodic *ldp = dynamic_cast<LeafDataPeriodic*>
	  (elInfo->getElement()->
	   getElementData()->
	   getElementData(PERIODIC));
	
183
184
	if (ldp) {
	  nConnections_ += dynamic_cast<LeafDataPeriodic*>(ldp)->getInfoList().size();
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
	}

	periodicConnections_.push_back(DimVec<bool>(dim_, DEFAULT_VALUE, false));
      }
      elInfo = stack.traverseNext(elInfo);
    }   

    nConnections_ /= 2;

    periodicInfos_.clear();

    Flag flag = traverseFlag_;
    flag |= 
      Mesh::FILL_COORDS    |
      Mesh::FILL_OPP_COORDS|
      Mesh::FILL_NEIGH     | 
      Mesh::FILL_BOUND;

    elInfo = stack.traverseFirst(mesh_, level_, flag);
204
205
    while (elInfo) {
      if (!writeElem_ || writeElem_(elInfo))
206
207
208
209
210
211
212
213
214
215
216
	addPeriodicData(elInfo);
      elInfo = stack.traverseNext(elInfo);
    }   

    periodicDataCollected_ = true;

    return(0);
  }

  int DataCollector::addElementData(ElInfo* elInfo)
  {
217
    FUNCNAME("DataCollector::addElementData()");
218
219
220
221
222
223
224
225
226

    const DegreeOfFreedom **dof = elInfo->getElement()->getDOF();
    DegreeOfFreedom vertexDOF;
    WorldVector<double> vertexCoords;
   
    // create ElementInfo
    ElementInfo elementInfo(dim_);
    
    // read element region
227
    ElementData *ed = elInfo->getElement()->getElementData(ELEMENT_REGION);
228
    
229
    if (ed) {
230
231
232
233
234
235
236
237
238
239
      elementInfo.elementRegion = dynamic_cast<ElementRegion_ED*>(ed)->getRegion();
    } else {
      elementInfo.elementRegion = -1;
    }
    
    // read surface regions to element info
    ed = elInfo->getElement()->getElementData(SURFACE_REGION);
    
    elementInfo.surfaceRegions.set(-1);
    
240
    while (ed) {
241
242
243
244
245
246
      SurfaceRegion_ED *sr = dynamic_cast<SurfaceRegion_ED*>(ed);
      elementInfo.surfaceRegions[sr->getSide()] = sr->getRegion();
      ed = ed->getDecorated(SURFACE_REGION);
    }

    // for all vertices
247
    for (int i = 0; i < dim_ + 1; i++) {
248
249
250
251
252
253
254
      // get coords of this vertex
      vertexCoords = elInfo->getCoord(i);
      
      // get dof index of this vertex
      vertexDOF = dof[i][nPreDofs_];
      
      // search for coords at this dof
Thomas Witkowski's avatar
Thomas Witkowski committed
255
      std::list<VertexInfo>::iterator it =
256
257
258
259
260
	  find((*vertexInfos_)[vertexDOF].begin(),
	       (*vertexInfos_)[vertexDOF].end(),
	       vertexCoords);
      
      // coords not yet in list?
261
      if (it == (*vertexInfos_)[vertexDOF].end()) {
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
	// create new vertex info and increment number of vertices
	VertexInfo newVertexInfo = {vertexCoords, nVertices_};

	// add new vertex info to list
	(*vertexInfos_)[vertexDOF].push_front(newVertexInfo);
	
	// set iterator to new vertex info
	it = (*vertexInfos_)[vertexDOF].begin();	

	nVertices_++;
      }
      
      // 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) ?
	outputIndices_[elInfo->getNeighbour(i)->getIndex()] :
	-1;
    }
    
284
    if (dim_ == 3) {
285
286
287
288
289
290
291
292
293
294
295
      elementInfo.type = (dynamic_cast<ElInfo3d*>(elInfo)->getType());
    }

    // remember element info
    elements_.push_back(elementInfo);

    return(0);
  }

  int DataCollector::addValueData(ElInfo *elInfo)
  {
296
    FUNCNAME("DataCollector::addValueData()");
297

Thomas Witkowski's avatar
Thomas Witkowski committed
298
    basisFcts_->getLocalIndices(elInfo->getElement(), localAdmin_, localDOFs_);
Thomas Witkowski's avatar
Thomas Witkowski committed
299
    //    WorldVector<double> vertexCoords;
300
301

    // First, traverse all DOFs at the vertices of the element, determine
Thomas Witkowski's avatar
Thomas Witkowski committed
302
    // their coordinates and add them to the corresponding entry in dofCoords_.      
303
    for (int i = 0; i < mesh_->getGeo(VERTEX); i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
304
305
306
      DegreeOfFreedom dofi = localDOFs_[i];

      (*interpPointInd_)[dofi] = -2; // mark as vertex
307
308
      
      // get coords of this vertex
Thomas Witkowski's avatar
Thomas Witkowski committed
309
      *vertexCoords = elInfo->getCoord(i);
310
311
      
      // search for coords at this dof
Thomas Witkowski's avatar
Thomas Witkowski committed
312
      std::list<WorldVector<double> >::iterator it =
Thomas Witkowski's avatar
Thomas Witkowski committed
313
314
	  find((*dofCoords_)[dofi].begin(),
	       (*dofCoords_)[dofi].end(),
Thomas Witkowski's avatar
Thomas Witkowski committed
315
	       *vertexCoords);
316
317
      
      // coords not yet in list?
Thomas Witkowski's avatar
Thomas Witkowski committed
318
      if (it == (*dofCoords_)[dofi].end()) {
319
	// add new coords to list
Thomas Witkowski's avatar
Thomas Witkowski committed
320
	(*dofCoords_)[dofi].push_back(*vertexCoords);
321
322
      }
    }
323
   
324

325
326
327
328

    // Then, traverse all interpolation DOFs of the element, determine
    // their coordinates and add them to the corresponding entry in 
    // interpPointCoords_.
Thomas Witkowski's avatar
Thomas Witkowski committed
329
330
    for (int i = mesh_->getGeo(VERTEX); i < nBasisFcts_; i++) {
      DegreeOfFreedom dofi = localDOFs_[i];
331

Thomas Witkowski's avatar
Thomas Witkowski committed
332
      elInfo->coordToWorld(*basisFcts_->getCoords(i), vertexCoords);
333
      
Thomas Witkowski's avatar
Thomas Witkowski committed
334
      if ((*interpPointInd_)[dofi] == -1) {
335
	// mark as interpolation point
Thomas Witkowski's avatar
Thomas Witkowski committed
336
	(*interpPointInd_)[dofi] = -3; 
337
338
339
	
	// 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
340
	std::list<WorldVector<double> >::iterator it =
Thomas Witkowski's avatar
Thomas Witkowski committed
341
342
		find((*interpPointCoords_)[dofi].begin(),
		     (*interpPointCoords_)[dofi].end(),
Thomas Witkowski's avatar
Thomas Witkowski committed
343
		     *vertexCoords);
344
	
Thomas Witkowski's avatar
Thomas Witkowski committed
345
	if (it == (*interpPointCoords_)[dofi].end()) {
Thomas Witkowski's avatar
Thomas Witkowski committed
346
	  (*interpPointCoords_)[dofi].push_back(*vertexCoords); 
347
	  nInterpPoints_++;
348
	}
349
      }      
350
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
351
    
352
353
354
355
356
    return(0);
  }

  int DataCollector::addInterpData(ElInfo *elInfo)
  {
357
    FUNCNAME("DataCollector::addInterpData()");
358
    
Thomas Witkowski's avatar
Thomas Witkowski committed
359
    basisFcts_->getLocalIndices(elInfo->getElement(), localAdmin_, localDOFs_);
360

361
    std::vector<int> elemInterpPoints(0);
Thomas Witkowski's avatar
Thomas Witkowski committed
362
    for (int i = mesh_->getGeo(VERTEX); i < nBasisFcts_; i++) {
363
      elemInterpPoints.push_back((*interpPointInd_)[localDOFs_[i]]);
364
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
365
    
366
    interpPoints_.push_back(elemInterpPoints);
367
  
368
369
370
371
372
373
374
375
376
377
378
    return(0);
  }

  int DataCollector::addPeriodicData(ElInfo *elInfo) {
    FUNCNAME("DataCollector::addPeriodicData");

    LeafDataPeriodic *ldp = dynamic_cast<LeafDataPeriodic*>
      (elInfo->getElement()->
       getElementData()->
       getElementData(PERIODIC));
    
379
    if (ldp) {
Thomas Witkowski's avatar
Thomas Witkowski committed
380
      std::list<LeafDataPeriodic::PeriodicInfo>::iterator it;
381
      
382
383
384
385
386
387
388
389
390
391
392
      for (it = dynamic_cast<LeafDataPeriodic*>(ldp)->getInfoList().begin();
	   it != dynamic_cast<LeafDataPeriodic*>(ldp)->getInfoList().end();
	   ++it) {

	int outputIndex = outputIndices_[elInfo->getElement()->getIndex()];
	int neighIndex  = outputIndices_[elInfo->
					 getNeighbour(it->elementSide)->
					 getIndex()];
	
	if (!periodicConnections_[outputIndex][it->elementSide]) {
	  PeriodicInfo periodicInfo;
393
	  
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
	  periodicInfo.mode = it->periodicMode;
	  periodicInfo.type = it->type;
	  periodicInfo.outputIndex = outputIndex;
	  periodicInfo.neighIndex = neighIndex;
	  periodicInfo.vertexMap.clear();
	  
	  periodicConnections_[outputIndex][it->elementSide] = true;
	  periodicConnections_
	    [neighIndex][elInfo->getOppVertex(it->elementSide)] = true;
	    
  
	  for (int i = 0; i < dim_; i++) {
	    int index1 = elInfo->getElement()->getVertexOfPosition(INDEX_OF_DIM(dim_ - 1, dim_),
								   it->elementSide,
								   i);
	    int dof1 = elInfo->getElement()->getDOF(index1, nPreDofs_);
	    
	    for (int j = 0; j < dim_; j++) {
	      int index2 = elInfo->getElement()->getVertexOfPosition(INDEX_OF_DIM(dim_ - 1, dim_),
								     elInfo->getOppVertex(it->elementSide),
								     j);
	      int dof2 = elInfo->getNeighbour(it->elementSide)->getDOF(index2, nPreDofs_);
	      
	      if ((dof1 == dof2) || (mesh_->associated(dof1, dof2))) {
		periodicInfo.vertexMap[index1] = index2;
		break;
	      } 
421
422
	    }
	  }
423
424
	  
	  periodicInfos_.push_back(periodicInfo);
425
	}
426
      }
427
428
429
430
431
    }
      
    return(0);
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
432
  std::list<ElementInfo>* DataCollector::getElementInfos()
433
434
435
436
437
438
439
440
  {        
    if (!elementDataCollected_) {
      startCollectingElementData();
    }
      
    return &elements_;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
441
  DOFVector< std::list<VertexInfo> >* DataCollector::getVertexInfos()
442
443
444
445
446
447
448
449
  {
    if (!elementDataCollected_) {
      startCollectingElementData();
    }

    return vertexInfos_;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
450
  std::list<PeriodicInfo>* DataCollector::getPeriodicInfos()
451
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
452
    if (!periodicDataCollected_) {
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
      startCollectingPeriodicData();
    }

    return &periodicInfos_;
  }

  int DataCollector::getNumberVertices()
  {
    if (!elementDataCollected_) {
      startCollectingElementData();
    }

    return nVertices_;
  }

  int DataCollector::getNumberElements()
  {
    if (!elementDataCollected_) {
      startCollectingElementData();
    }

    return nElements_;
  }

  int DataCollector::getNumberInterpPoints()
  {
    if (!valueDataCollected_) {
      startCollectingValueData();
    }

    return nInterpPoints_;
  }
  
  int DataCollector::getNumberConnections()
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
488
    if (!periodicDataCollected_) {
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
      startCollectingPeriodicData();
    }

    return nConnections_;
  }

  const FiniteElemSpace* DataCollector::getFeSpace()
  {
    return feSpace_;
  }

  Mesh* DataCollector::getMesh()
  {
    return mesh_;
  }

  DOFVector<double>* DataCollector::getValues()
  {
    if (!valueDataCollected_) {
      startCollectingValueData();
    }

    return values_;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
514
  DOFVector< std::list<WorldVector<double> > >* DataCollector::getDofCoords()
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
  {
    if (!valueDataCollected_) {
      startCollectingValueData();
    }

    return dofCoords_;
  }

  DOFVector<int>* DataCollector::getInterpPointInd()
  {
    if (!valueDataCollected_) {
      startCollectingValueData();
    }

    return interpPointInd_;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
532
  DOFVector< std::list<WorldVector<double> > >* DataCollector::getInterpPointCoords()
533
534
535
536
537
538
539
540
  {
    if (!valueDataCollected_) {
      startCollectingValueData();
    }

    return interpPointCoords_;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
541
  std::vector< std::vector<int> >* DataCollector::getInterpPoints()
542
543
544
545
546
547
548
549
  {
    if (!valueDataCollected_) {
      startCollectingValueData();
    }

    return &interpPoints_;
  }
}