MeshStructure.cc 7.14 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
  void MeshStructure::insertElement(bool isLeaf) 
  {
16
    // overflow? -> next index
17
18
19
20
    if (pos >= unsignedLongSize) {
      code.push_back(currentCode);
      pos = 0;
      currentCode = 0;
21
22
23
    }

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

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

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

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

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

56
  void MeshStructure::init(Element *el, int ithSide, int elType, bool reverseOrder)
57
58
59
60
61
  {
    FUNCNAME("MeshStructure::init()");

    clear();

62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
    int s1 = el->getSideOfChild(0, ithSide, elType);
    int s2 = el->getSideOfChild(1, ithSide, elType);
    
    TEST_EXIT(s1 != -1 || s2 != -1)("This should not happen!\n");
    
    if (!el->isLeaf()) {
      if (s1 == -1)
	addAlongSide(el->getSecondChild(), s2, el->getChildType(elType), reverseOrder);
      else if (s2 == -1)
	addAlongSide(el->getFirstChild(), s1, el->getChildType(elType), reverseOrder);
      else
	addAlongSide(el, ithSide, elType, reverseOrder);
    }

    commit();    
  }

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

    Element *el = bound.el;

    clear();

    int s1 = el->getSideOfChild(0, bound.ithObj, bound.elType);
    int s2 = el->getSideOfChild(1, bound.ithObj, bound.elType);
    
    TEST_EXIT(s1 != -1 || s2 != -1)("This should not happen!\n");
    
    if (!el->isLeaf()) {
      if (s1 == -1)
	addAlongSide(el->getSecondChild(), s2, 
		     el->getChildType(bound.elType), bound.reverseMode);
      else if (s2 == -1)
	addAlongSide(el->getFirstChild(), s1, 
		     el->getChildType(bound.elType), bound.reverseMode);
      else
	addAlongSide(el, bound.ithObj, bound.elType, bound.reverseMode);
    }
102
103
104
105

    commit();    
  }

106
107
  void MeshStructure::addAlongSide(Element *el, int ithSide, int elType, 
				   bool reverseOrder)
108
  {
109
110
111
112
113
114
115
116
    FUNCNAME("MeshStructure::addAlongSide()");

    if (debugMode) {
      MSG("addAlondSide(%d, %d, %d, %d)\n",
	  el->getIndex(), ithSide, elType, reverseOrder);
      MSG("Element is leaf: %d\n", el->isLeaf());
    }
    
117
118
119
120
121
122
    insertElement(el->isLeaf());
    
    if (!el->isLeaf()) {
      int s1 = el->getSideOfChild(0, ithSide, elType);
      int s2 = el->getSideOfChild(1, ithSide, elType);

123
124
      if (!reverseOrder) {
	if (s1 != -1) 
125
	  addAlongSide(el->getFirstChild(), s1, el->getChildType(elType), false);
126
	if (s2 != -1)
127
	  addAlongSide(el->getSecondChild(), s2, el->getChildType(elType), false);
128
129
      } else {
	if (s2 != -1)
130
	  addAlongSide(el->getSecondChild(), s2, el->getChildType(elType), false);
131
	if (s1 != -1) 
132
	  addAlongSide(el->getFirstChild(), s1, el->getChildType(elType), false);
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
  }

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

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

156
157
    pos++;
    currentElement++;
158

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

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

172
173
174
175
176
    return true;
  }

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

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

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

196
197
198
199
200
    result->clear();
    structure1->reset();
    structure2->reset();

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

    result->commit();
  }

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

    bool cont = true;

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

      Element *element = elInfo->getElement();

240
241
      if (isLeafElement())
	TEST_EXIT_DBG(element->isLeaf())("mesh finer than code\n");
242

243
      if (element->isLeaf() && !isLeafElement()) {
Thomas Witkowski's avatar
Thomas Witkowski committed
244
	MeshStructure *structure = new MeshStructure();
245
246
247
248
	cont = skipBranch(structure);
	structure->commit();

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

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

      elInfo = stack.traverseNext(elInfo);
    }

    // refine mesh
    bool finished;

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

292
293
294
295
  bool MeshStructure::compare(MeshStructure &other)
  {
    return (other.getCode() == code);
  }
296
}