MeshStructure.cc 8.46 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
#include "MeshStructure.h"
#include "MeshStructure_ED.h"
#include "PartitionElementData.h"
#include "Mesh.h"
#include "Element.h"
#include "Traverse.h"
#include "ElInfo.h"
#include "RefinementManager.h"

namespace AMDiS {

12
  const int MeshStructure::unsignedLongSize = sizeof(unsigned long int) * 8;
13

14

15
16
  void MeshStructure::insertElement(bool isLeaf) 
  {
17
    // overflow? -> next index
18
19
20
21
    if (pos >= unsignedLongSize) {
      code.push_back(currentCode);
      pos = 0;
      currentCode = 0;
22
23
24
    }

    // insert element in binary code
25
    if (!isLeaf) {
26
      unsigned long int one = 1;
27
      currentCode += (one << pos);
28
29
    } 

30
31
    pos++;
    nElements++;
32
33
  }

34

35
36
  void MeshStructure::clear()
  {
37
38
39
40
41
    currentCode = 0;
    code.resize(0);
    pos = 0;
    nElements = 0;
    currentElement = 0;
42
43
  }

44

45
46
47
  void MeshStructure::init(Mesh *mesh) 
  {
    clear();
48

49
50
    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_EVERY_EL_PREORDER);
51
    while (elInfo) {
52
53
54
55
56
57
58
      insertElement(elInfo->getElement()->isLeaf());
      elInfo = stack.traverseNext(elInfo);
    }
  
    commit();
  }

59
60
61
62
63
64
65

  void MeshStructure::init(BoundaryObject &bound)
  {
    FUNCNAME("MeshStructure::init()");

    Element *el = bound.el;

66
67
    TEST_EXIT_DBG(el)("No element!\n");

68
69
    clear();

70
71
    int s1 = el->getSubObjOfChild(0, bound.subObj, bound.ithObj, bound.elType);
    int s2 = el->getSubObjOfChild(1, bound.subObj, bound.ithObj, bound.elType);
72
73
    
    TEST_EXIT(s1 != -1 || s2 != -1)("This should not happen!\n");
74
75
76
77
78
79
80

    if (debugMode) {
      MSG("addAlondSide(%d, %d, %d, %d)\n",
	  bound.elIndex, bound.ithObj, bound.elType, bound.reverseMode);
      MSG("Element is leaf: %d\n", el->isLeaf());
      MSG("s1 = %d    s2 = %d\n", s1, s2);
    }
81
82
83
    
    if (!el->isLeaf()) {
      if (s1 == -1)
84
	addAlongSide(el->getSecondChild(), bound.subObj, s2, 
85
86
		     el->getChildType(bound.elType), bound.reverseMode);
      else if (s2 == -1)
87
	addAlongSide(el->getFirstChild(), bound.subObj, s1, 
88
89
		     el->getChildType(bound.elType), bound.reverseMode);
      else
90
	addAlongSide(el, bound.subObj, bound.ithObj, bound.elType, bound.reverseMode);
91
    }
92
93
94
95

    commit();    
  }

96
97
98

  void MeshStructure::addAlongSide(Element *el, GeoIndex subObj, int ithObj, 
				   int elType, bool reverseOrder)
99
  {
100
101
102
103
    FUNCNAME("MeshStructure::addAlongSide()");

    if (debugMode) {
      MSG("addAlondSide(%d, %d, %d, %d)\n",
104
	  el->getIndex(), ithObj, elType, reverseOrder);
105
106
107
      MSG("Element is leaf: %d\n", el->isLeaf());
    }
    
108
109
110
    insertElement(el->isLeaf());
    
    if (!el->isLeaf()) {
111
112
      int s1 = el->getSubObjOfChild(0, subObj, ithObj, elType);
      int s2 = el->getSubObjOfChild(1, subObj, ithObj, elType);
113

114
115
116
117
118
119
120
121
      if (debugMode) {
	MSG("Child index %d  %d\n", 
	    el->getFirstChild()->getIndex(),
	    el->getSecondChild()->getIndex());
	MSG("s1 = %d    s2 = %d\n", s1, s2);
	MSG("   \n");
      }

122
123
      if (!reverseOrder) {
	if (s1 != -1) 
124
	  addAlongSide(el->getFirstChild(), subObj, s1, el->getChildType(elType), reverseOrder);
125
	if (s2 != -1)
126
	  addAlongSide(el->getSecondChild(), subObj, s2, el->getChildType(elType), reverseOrder);
127
128
      } else {
	if (s2 != -1)
129
	  addAlongSide(el->getSecondChild(), subObj, s2, el->getChildType(elType), reverseOrder);
130
	if (s1 != -1) 
131
	  addAlongSide(el->getFirstChild(), subObj, s1, el->getChildType(elType), reverseOrder);
132
      }
133
134
135
    }
  }

136

137
138
139
140
141
  void MeshStructure::reset() 
  {
    currentIndex = 0;
    pos = 0;
    currentElement = 0;
142
143
144
145
146

    if (code.size() > 0)
      currentCode = code[0];
    else 
      currentCode = 0;
147
148
  }

149

150
151
  bool MeshStructure::nextElement(MeshStructure *insert)
  {
152
153
154
    FUNCNAME("MeshStructure::nextElement()");

    if (insert)
155
156
      insert->insertElement(isLeafElement());

157
158
    pos++;
    currentElement++;
159

160
161
    if (currentElement >= nElements) 
      return false;
162

163
164
165
166
167
168
    if (pos >= unsignedLongSize) {
      currentIndex++;
      TEST_EXIT_DBG(currentIndex < static_cast<int>(code.size()))
	("End of structure reached!\n");
      pos = 0;
      currentCode = code[currentIndex];
169
    } else {
170
      currentCode >>= 1;
171
    }
172

173
174
175
    return true;
  }

176

177
178
  bool MeshStructure::skipBranch(MeshStructure *insert)
  {
179
180
    FUNCNAME("MeshStructure::skipBranch()");

181
    if (isLeafElement()) {
182
183
184
185
      return nextElement(insert);
    } else {
      bool cont = nextElement(insert);
      cont = skipBranch(insert); // left branch
186
      TEST_EXIT_DBG(cont)("Invalid structure!\n");
187
188
189
190
191
      cont = skipBranch(insert); // righ branch
      return cont;
    }
  }

192

193
194
195
196
  void MeshStructure::merge(MeshStructure *structure1,
			    MeshStructure *structure2,
			    MeshStructure *result)
  {
197
198
    FUNCNAME("MeshStructure::merge()");

199
200
201
202
203
    result->clear();
    structure1->reset();
    structure2->reset();

    bool cont = true;
204
    while (cont) {
205
      bool cont1, cont2;
206
      if (structure1->isLeafElement() == structure2->isLeafElement()) {
207
208
209
	cont1 = structure1->nextElement(result);
	cont2 = structure2->nextElement();
      } else {
210
	if (structure1->isLeafElement()) {
211
212
213
214
215
216
217
	  cont1 = structure1->nextElement();
	  cont2 = structure2->skipBranch(result);
	} else {
	  cont1 = structure1->skipBranch(result);
	  cont2 = structure2->nextElement();
	}
      }
218
      TEST_EXIT_DBG(cont1 == cont2)("Structures don't match!\n");
219
220
221
222
223
224
      cont = cont1;
    }

    result->commit();
  }

225

226
227
  void MeshStructure::fitMeshToStructure(Mesh *mesh,
					 RefinementManager *manager,
228
229
					 bool checkPartition,
					 bool debugMode) 
230
231
232
233
234
235
236
  {
    FUNCNAME("MeshStructure::fitMeshToStructure()");

    bool cont = true;

    // decorate leaf data
    reset();
237
238
    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_EVERY_EL_PREORDER);
239
    while (elInfo) {
240
      TEST_EXIT(cont)("unexpected structure code end!\n");
241
242
243

      Element *element = elInfo->getElement();

244
      if (isLeafElement()) {
245
	TEST_EXIT_DBG(element->isLeaf())("mesh finer than code\n");
246
      }
247

248
      if (element->isLeaf() && !isLeafElement()) {
Thomas Witkowski's avatar
Thomas Witkowski committed
249
	MeshStructure *structure = new MeshStructure();
250
251
252
253
	cont = skipBranch(structure);
	structure->commit();

	bool decorate = true;
254
	if (checkPartition) {
255
256
	  PartitionElementData *partitionData = dynamic_cast<PartitionElementData*>
	    (element->getElementData(PARTITION_ED));
257
	  TEST_EXIT_DBG(partitionData)("no partition element data\n");
258
	  PartitionStatus status = partitionData->getPartitionStatus();
259
	  if (debugMode == false && (status == OUT || status == UNDEFINED))
260
261
262
	    decorate = false;
	}

263
	if (decorate) {
Thomas Witkowski's avatar
Thomas Witkowski committed
264
	  MeshStructure_ED *elData = new MeshStructure_ED(element->getElementData());
265
266
267
	  elData->setStructure(structure);
	  element->setElementData(elData);
	} else {
Thomas Witkowski's avatar
Thomas Witkowski committed
268
	  delete structure;
269
270
271
272
273
274
275
276
277
	}
      } else {
	cont = nextElement();
      }

      elInfo = stack.traverseNext(elInfo);
    }

    // refine mesh
278
    bool finished = true;
279
280
281
282

    do {
      finished = true;
      elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_LEAF_EL);
283
      while (elInfo) {
284
	Element *element = elInfo->getElement();
285
	if (element->getElementData(MESH_STRUCTURE) != NULL) {
286
287
288
289
290
291
292
293
	  element->setMark(1);
	  finished = false;
	} else {
	  element->setMark(0);
	}
	elInfo = stack.traverseNext(elInfo);
      }
      manager->refineMesh(mesh);
294
    } while (!finished);
295
296
  }

297

298
299
300
301
  bool MeshStructure::compare(MeshStructure &other)
  {
    return (other.getCode() == code);
  }
302
303
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355


  void MeshStructure::writeMeshFile(Mesh *mesh, std::string filename)
  {
    FUNCNAME("MeshStructure::writeMeshFile()");

    int nMacroElements = 0;
    int macroElIndex = -1;
    std::ofstream file;
    file.open(filename.c_str(), std::ios::out | std::ios::binary | std::ios::trunc);

    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirst(mesh, 0, Mesh::CALL_EL_LEVEL);
    while (elInfo) {
      nMacroElements++;
      elInfo = stack.traverseNext(elInfo);
    }

    file << nMacroElements;
    
    elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_EVERY_EL_PREORDER);
    while (elInfo) {
      if (elInfo->getLevel() == 0) {
	if (macroElIndex != -1) {
	  commit();
	  writeMacroElement(file, macroElIndex);
	}

	clear();
	macroElIndex = elInfo->getElement()->getIndex();
      }

      insertElement(elInfo->getElement()->isLeaf());
      elInfo = stack.traverseNext(elInfo);
    }

    // And write the last macro element to file.
    TEST_EXIT_DBG(macroElIndex != -1)("Should not happen!\n");
    commit();
    writeMacroElement(file, macroElIndex);

    file.close();
  }


  void MeshStructure::writeMacroElement(std::ofstream& file, int macroElIndex)
  {
    file << macroElIndex;
    file << nElements;
    file << code.size();
    for (unsigned int i = 0; i < code.size(); i++)
      file << code[i];
  }

356
}