MeshStructure.cc 10.5 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
					 bool checkPartition,
229
230
					 bool debugMode,
					 int macroElIndex) 
231
232
233
234
235
236
237
  {
    FUNCNAME("MeshStructure::fitMeshToStructure()");

    bool cont = true;

    // decorate leaf data
    reset();
238
239
    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_EVERY_EL_PREORDER);
240
    while (elInfo) {
241
242
243
244
245
      if (macroElIndex >= 0 && 
	  elInfo->getMacroElement()->getElement()->getIndex() != macroElIndex) {
	elInfo = stack.traverseNext(elInfo);
	continue;
      }
246
247
248

      Element *element = elInfo->getElement();

249
250
      TEST_EXIT(cont)("unexpected structure code end!\n");

251
      if (isLeafElement()) {
252
	TEST_EXIT(element->isLeaf())("mesh finer than code\n");
253
      }
254

255
      if (element->isLeaf() && !isLeafElement()) {
Thomas Witkowski's avatar
Thomas Witkowski committed
256
	MeshStructure *structure = new MeshStructure();
257
258
259
260
	cont = skipBranch(structure);
	structure->commit();

	bool decorate = true;
261
	if (checkPartition) {
262
263
	  PartitionElementData *partitionData = dynamic_cast<PartitionElementData*>
	    (element->getElementData(PARTITION_ED));
264
	  TEST_EXIT_DBG(partitionData)("no partition element data\n");
265
	  PartitionStatus status = partitionData->getPartitionStatus();
266
	  if (debugMode == false && (status == OUT || status == UNDEFINED))
267
268
269
	    decorate = false;
	}

270
	if (decorate) {
Thomas Witkowski's avatar
Thomas Witkowski committed
271
	  MeshStructure_ED *elData = new MeshStructure_ED(element->getElementData());
272
273
274
	  elData->setStructure(structure);
	  element->setElementData(elData);
	} else {
Thomas Witkowski's avatar
Thomas Witkowski committed
275
	  delete structure;
276
277
278
279
280
281
282
283
284
	}
      } else {
	cont = nextElement();
      }

      elInfo = stack.traverseNext(elInfo);
    }

    // refine mesh
285
    bool finished = true;
286
287
288
289

    do {
      finished = true;
      elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_LEAF_EL);
290
      while (elInfo) {
291
292
293
294
295
296
	if (macroElIndex >= 0 && 
	    elInfo->getMacroElement()->getElement()->getIndex() != macroElIndex) {
	  elInfo = stack.traverseNext(elInfo);
	  continue;
	}

297
	Element *element = elInfo->getElement();
298
	if (element->getElementData(MESH_STRUCTURE) != NULL) {
299
300
301
302
303
304
305
306
	  element->setMark(1);
	  finished = false;
	} else {
	  element->setMark(0);
	}
	elInfo = stack.traverseNext(elInfo);
      }
      manager->refineMesh(mesh);
307
    } while (!finished);
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
  void MeshStructure::print(bool resetCode)
  {
    FUNCNAME("MeshStructure::print()");
    
    std::stringstream oss;
    
    if (empty()) {
      oss << "-" << std::endl;
    }	else {	
      if (resetCode)
	reset();

      bool cont = true;
      while (cont) {
	if (isLeafElement())
	  oss << "0";
	else
	  oss << "1";
	
	cont = nextElement();
      }
    }
    
    MSG("Mesh structure code: %s\n", oss.str().c_str());
  }


338
339
340
341
  bool MeshStructure::compare(MeshStructure &other)
  {
    return (other.getCode() == code);
  }
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359


  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);
    }

360
    file.write(reinterpret_cast<char*>(&nMacroElements), sizeof(nMacroElements));
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
    
    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();
  }


387
388
389
390
391
392
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
421
422
423
424
  void MeshStructure::readMeshFile(Mesh *mesh, 
				   RefinementManager *refManager,
				   std::string filename)
  {
    FUNCNAME("MeshStructure::readMeshFile()");

    std::ifstream file;
    file.open(filename.c_str(), std::ios::in | std::ios::binary);

    int nMacroElements = -1;
    file.read(reinterpret_cast<char*>(&nMacroElements), sizeof(nMacroElements));

    for (int i = 0; i < nMacroElements; i++) {
      int macroElIndex = -1;
      int codeSize = 0;
      code.clear();

      file.read(reinterpret_cast<char*>(&macroElIndex), sizeof(macroElIndex));
      file.read(reinterpret_cast<char*>(&nElements), sizeof(nElements));
      file.read(reinterpret_cast<char*>(&codeSize), sizeof(codeSize));

      for (int j = 0; j < codeSize; j++) {
	unsigned long int readCode = 0;
	file.read(reinterpret_cast<char*>(&readCode), sizeof(readCode));
	code.push_back(readCode);
      }
	
      TEST_EXIT_DBG(macroElIndex >= 0)("Should not happen!\n");

      reset();
      fitMeshToStructure(mesh, refManager, false, false, macroElIndex);
      
    }

    file.close();
  }


425
426
  void MeshStructure::writeMacroElement(std::ofstream& file, int macroElIndex)
  {
427
428
429
430
431
432
    unsigned int size = code.size();

    file.write(reinterpret_cast<char*>(&macroElIndex), sizeof(macroElIndex));
    file.write(reinterpret_cast<char*>(&nElements), sizeof(nElements));
    file.write(reinterpret_cast<char*>(&size), sizeof(size));

433
    for (unsigned int i = 0; i < code.size(); i++)
434
      file.write(reinterpret_cast<char*>(&(code[i])), sizeof(code[i]));
435
436
  }

437
}