LeafData.cc 10.1 KB
Newer Older
1
2
3
#include "LeafData.h"
#include "Element.h"
#include "Mesh.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
4
#include "Serializer.h"
5
6
7

namespace AMDiS {

8
9
10
11
  bool LeafDataEstimatable::refineElementData(Element* parent, 
					      Element* child1,
					      Element* child2,
					      int elType)
12
 
13
14
  {
    ElementData::refineElementData(parent, child1, child2, elType);
15

16
    LeafDataEstimatable *child1Data = 
Thomas Witkowski's avatar
Thomas Witkowski committed
17
      new LeafDataEstimatable(child1->getElementData());
18
    LeafDataEstimatable *child2Data = 
Thomas Witkowski's avatar
Thomas Witkowski committed
19
      new LeafDataEstimatable(child2->getElementData());
20

21
22
    child1Data->setErrorEstimate(0, errorEstimate / 2.0);
    child2Data->setErrorEstimate(0, errorEstimate / 2.0);
23

24
25
    child1->setElementData(child1Data);
    child2->setElementData(child2Data);
26

27
28
    return true;
  }
29

30
31
32
33
  void LeafDataEstimatable::coarsenElementData(Element* parent, 
					       Element* thisChild,
					       Element* otherChild,
					       int elTypeParent)
34
 
35
  {    
36
37
38
39
#ifndef NDEBUG
    bool test = 
#endif
       otherChild->deleteElementData(ESTIMATABLE);
40
41
    TEST_EXIT_DBG(test)("couldn't delete LeafDataEstimatable at otherChild");

Thomas Witkowski's avatar
Thomas Witkowski committed
42
    parent->setElementData(new LeafDataEstimatable(parent->getElementData()));
43
44
    ElementData::coarsenElementData(parent, thisChild, otherChild, elTypeParent);
  }
45

Thomas Witkowski's avatar
Thomas Witkowski committed
46
47
48
49
50
51
52
53
54
55
56
57
  void LeafDataEstimatable::serialize(std::ostream& out)
  {
    ElementData::serialize(out);
    SerUtil::serialize(out, errorEstimate);
  }

  void LeafDataEstimatable::deserialize(std::istream& in)
  {
    ElementData::deserialize(in);
    SerUtil::deserialize(in, errorEstimate);
  }
  
58
59
60
61
  bool LeafDataEstimatableVec::refineElementData(Element* parent, 
						 Element* child1,
						 Element* child2,
						 int elType)
62
 
63
64
  {
    ElementData::refineElementData(parent, child1, child2, elType);
Thomas Witkowski's avatar
Thomas Witkowski committed
65
66
    child1->setElementData(new LeafDataEstimatableVec(child1->getElementData()));
    child2->setElementData(new LeafDataEstimatableVec(child2->getElementData()));
67

68
69
70
71
72
73
74
    return true;
  }

  void LeafDataEstimatableVec::coarsenElementData(Element* parent, 
						  Element* thisChild,
						  Element* otherChild,
						  int elTypeParent)
75
 
76
  {
77
    DBG_VAR(bool test =) otherChild->deleteElementData(ESTIMATABLE);
78
    TEST_EXIT_DBG(test)("couldn't delete LeafDataEstimatableVec at otherChild");
Thomas Witkowski's avatar
Thomas Witkowski committed
79
    parent->setElementData(new LeafDataEstimatableVec(parent->getElementData()));
80
    ElementData::coarsenElementData(parent, thisChild, otherChild, elTypeParent);
Thomas Witkowski's avatar
Thomas Witkowski committed
81
  }
82

Thomas Witkowski's avatar
Thomas Witkowski committed
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
  void LeafDataEstimatableVec::serialize(std::ostream& out) 
  {
    ElementData::serialize(out);
    unsigned int size = errorEstimate.size();
    SerUtil::serialize(out, size);
    
    for (std::map<int, double>::iterator it = errorEstimate.begin(); 
	 it != errorEstimate.end(); ++it) {
      SerUtil::serialize(out, it->first);
      SerUtil::serialize(out, it->second);
    }
  }
  
  void LeafDataEstimatableVec::deserialize(std::istream& in) 
  {
    ElementData::deserialize(in);
    unsigned size;
    SerUtil::deserialize(in, size);
    for (unsigned int i = 0; i < size; i++) {
      int index;
      double estimate;
      SerUtil::deserialize(in, index);
      SerUtil::deserialize(in, estimate);
      errorEstimate[index] = estimate;
    }
  }
  
110
111
112
113
114
115
  bool LeafDataCoarsenable::refineElementData(Element* parent, 
					      Element* child1,
					      Element* child2,
					      int elType)
  {
    ElementData::refineElementData(parent, child1, child2, elType);
Thomas Witkowski's avatar
Thomas Witkowski committed
116
117
    child1->setElementData(new LeafDataCoarsenable(child1->getElementData()));
    child2->setElementData(new LeafDataCoarsenable(child2->getElementData()));
118
119
    return true;
  }
120

121
122
123
124
125
  void LeafDataCoarsenable::coarsenElementData(Element* parent, 
					       Element* thisChild,
					       Element* otherChild,
					       int elTypeParent)
  {
126
    DBG_VAR(bool test =) otherChild->deleteElementData(COARSENABLE);
127
    TEST_EXIT_DBG(test)("couldn't delete LeafDataCoarsenable at otherChild");
128

Thomas Witkowski's avatar
Thomas Witkowski committed
129
    parent->setElementData(new LeafDataCoarsenable(parent->getElementData()));
130
131
    ElementData::coarsenElementData(parent, thisChild, otherChild, elTypeParent);
  }
132

Thomas Witkowski's avatar
Thomas Witkowski committed
133
134
135
136
137
138
139
140
141
142
143
144
  void LeafDataCoarsenable::serialize(std::ostream& out) 
  {
    ElementData::serialize(out);
    SerUtil::serialize(out, coarseningError);
  }

  void LeafDataCoarsenable::deserialize(std::istream& in) 
  {
    ElementData::deserialize(in);
    SerUtil::deserialize(in, coarseningError);
  }

145
146
147
148
149
150
  bool LeafDataCoarsenableVec::refineElementData(Element* parent, 
						 Element* child1,
						 Element* child2,
						 int elType)
  {
    ElementData::refineElementData(parent, child1, child2, elType);
Thomas Witkowski's avatar
Thomas Witkowski committed
151
152
    child1->setElementData(new LeafDataCoarsenableVec(child1->getElementData()));
    child2->setElementData(new LeafDataCoarsenableVec(child2->getElementData()));
153
154
155
156
157
158
159
160
    return true;
  }

  void LeafDataCoarsenableVec::coarsenElementData(Element* parent, 
						  Element* thisChild,
						  Element* otherChild,
						  int elTypeParent) 
  {
161
    DBG_VAR(bool test =) otherChild->deleteElementData(COARSENABLE);
162
    TEST_EXIT_DBG(test)("couldn't delete LeafDataCoarsenableVec at otherChild");
Thomas Witkowski's avatar
Thomas Witkowski committed
163
    parent->setElementData(new LeafDataCoarsenableVec(parent->getElementData()));
164
165
    ElementData::coarsenElementData(parent, thisChild, otherChild, elTypeParent);
  }
166

Thomas Witkowski's avatar
Thomas Witkowski committed
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
  void LeafDataCoarsenableVec::serialize(std::ostream& out) 
  {
    ElementData::serialize(out);
    unsigned int size = coarseningError.size();
    SerUtil::serialize(out, size);
    
    for (std::map<int, double>::iterator it = coarseningError.begin(); 
	 it != coarseningError.end(); ++it) {
      SerUtil::serialize(out, it->first);
      SerUtil::serialize(out, it->second);
    }
  }
  
  void LeafDataCoarsenableVec::deserialize(std::istream& in) 
  {
    ElementData::deserialize(in);
    unsigned int size;
    SerUtil::deserialize(in, size);
    for (unsigned int i = 0; i < size; i++) {
      int index;
      double estimate;
      SerUtil::deserialize(in, index);
      SerUtil::deserialize(in, estimate);
      coarseningError[index] = estimate;
    }
  }

194
195
196
197
198
199
  bool LeafDataPeriodic::refineElementData(Element* parent, 
					   Element* child1,
					   Element* child2,
					   int elType) 
  {
    ElementData::refineElementData(parent, child1, child2, elType);
200

201
202
203
    Element* child[2] = {child1, child2};
    int dim = parent->getMesh()->getDim();
    LeafDataPeriodic *ld[2] = {NULL, NULL};
204
    std::list<LeafDataPeriodic::PeriodicInfo>::iterator it;
205

Thomas Witkowski's avatar
Thomas Witkowski committed
206
    for (it = periodicInfoList.begin(); it != periodicInfoList.end(); ++it) {
207
208
209
      BoundaryType type = it->type;
      int parentSide = it->elementSide;
      int mode = it->periodicMode;
210

211
212
213
214
215
216
217
218
219
      // for both children
      for (int i = 0; i < 2; i++) {
	// get childs side
	int sideOfChild = parent->getSideOfChild(i, parentSide, elType);
	if (sideOfChild == -1) 
	  continue;
	
	// create new leaf data if necessary
	if (!ld[i]) {
Thomas Witkowski's avatar
Thomas Witkowski committed
220
	  ld[i] = new LeafDataPeriodic(child[i]->getElementData());
221
222
	  child[i]->setElementData(ld[i]);
	}
223
    
224
	  // create new periodic coords
225
	DimVec<WorldVector<double> > coords(dim - 1, NO_INIT);
226
	
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
	// for each vertex of childs side
	for (int j = 0; j < dim; j++) {
	  // get parents vertex nr.
	  int childVertex = child[i]->getVertexOfPosition(INDEX_OF_DIM(dim - 1, dim),
							  sideOfChild, j);
	  int parentVertex = child[i]->getVertexOfParent(i, childVertex, elType);
	  
	  if (parentVertex == -1) {
	    // create coords for new vertex
	    WorldVector<double> newCoords;
	    newCoords = (*(it->periodicCoords))[0];
	    newCoords += (*(it->periodicCoords))[1];
	    newCoords *= 0.5;
	    coords[j] = newCoords;
	  } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
242
	    int posAtSide = parent->getPositionOfVertex(parentSide, parentVertex);
243
	    coords[j] = (*(it->periodicCoords))[posAtSide];
244
	  }
245
	}
246
247
	
	ld[i]->addPeriodicInfo(mode, type, sideOfChild, &coords);
248
      }
249
    }
250

251
    return false;
252
253
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
  void LeafDataPeriodic::serialize(std::ostream& out) 
  {
    ElementData::serialize(out);
    unsigned int size = periodicInfoList.size();
    SerUtil::serialize(out, size);      
    for (std::list<PeriodicInfo>::iterator it = periodicInfoList.begin(); 
	 it != periodicInfoList.end(); ++it)
      it->serialize(out);
  }
  
  void LeafDataPeriodic::deserialize(std::istream& in) 
  {
    ElementData::deserialize(in);
    unsigned int size;
    SerUtil::deserialize(in, size);
    periodicInfoList.resize(size);
    for (std::list<PeriodicInfo>::iterator it = periodicInfoList.begin(); 
	 it != periodicInfoList.end(); ++it)
      it->deserialize(in);
  }
  
275
276
277
278
279
  LeafDataPeriodic::PeriodicInfo::PeriodicInfo(const PeriodicInfo &rhs) 
  {
    periodicMode = rhs.periodicMode;
    type = rhs.type;
    elementSide = rhs.elementSide;
280
281
    if (rhs.periodicCoords) {
      int dim = rhs.periodicCoords->getSize() - 1;
Thomas Witkowski's avatar
Thomas Witkowski committed
282
      periodicCoords = new DimVec<WorldVector<double> >(dim, NO_INIT);
Thomas Witkowski's avatar
Thomas Witkowski committed
283
      for (int i = 0; i < dim + 1; i++)
284
285
286
287
	(*periodicCoords)[i] = (*(rhs.periodicCoords))[i];
    } else {
      periodicCoords = NULL;
    }
288
  }
Thomas Witkowski's avatar
Thomas Witkowski committed
289
290
  
  LeafDataPeriodic::PeriodicInfo::PeriodicInfo(int mode, BoundaryType t, int side,
291
292
293
294
295
296
					       const DimVec<WorldVector<double> > *coords)
    : periodicMode(mode),
      type(t),
      elementSide(side),
      periodicCoords(NULL)
  {
297
298
    if (coords) {
      int dim = coords->getSize() - 1;
Thomas Witkowski's avatar
Thomas Witkowski committed
299
      periodicCoords = new DimVec<WorldVector<double> >(dim, NO_INIT);
Thomas Witkowski's avatar
Thomas Witkowski committed
300
      for (int i = 0; i < dim + 1; i++)
301
	(*periodicCoords)[i] = (*coords)[i];
302
303
    }
  }
Thomas Witkowski's avatar
Thomas Witkowski committed
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339

  void LeafDataPeriodic::PeriodicInfo::serialize(std::ostream &out) 
  {
    SerUtil::serialize(out, periodicMode);
    SerUtil::serialize(out, type);
    SerUtil::serialize(out, elementSide);
    if (periodicCoords) {
      int size = periodicCoords->getSize();
      SerUtil::serialize(out, size);
      for (int i = 0; i < size; i++)
	(*periodicCoords)[i].serialize(out);
    } else {
      int size = 0;
      SerUtil::serialize(out, size);
    }
  }
  
  void LeafDataPeriodic::PeriodicInfo::deserialize(std::istream &in)
  {
    SerUtil::deserialize(in, periodicMode);
    SerUtil::deserialize(in, type);
    SerUtil::deserialize(in, elementSide);
    
    int size;
    SerUtil::deserialize(in, size);
    if (periodicCoords) 
      delete periodicCoords;
    if (size == 0) {
      periodicCoords = NULL;
    } else {
      periodicCoords = new DimVec<WorldVector<double> >(size-1, NO_INIT);
      for (int i = 0; i < size; i++) 
	(*periodicCoords)[i].deserialize(in);
    }
  }

340
}