MeshStructure.cc 7.12 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
66
67

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

    Element *el = bound.el;

    clear();

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

    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);
    }
79
80
81
    
    if (!el->isLeaf()) {
      if (s1 == -1)
82
	addAlongSide(el->getSecondChild(), bound.subObj, s2, 
83
84
		     el->getChildType(bound.elType), bound.reverseMode);
      else if (s2 == -1)
85
	addAlongSide(el->getFirstChild(), bound.subObj, s1, 
86
87
		     el->getChildType(bound.elType), bound.reverseMode);
      else
88
	addAlongSide(el, bound.subObj, bound.ithObj, bound.elType, bound.reverseMode);
89
    }
90
91
92
93

    commit();    
  }

94
95
96

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

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

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

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

134

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

    if (code.size() > 0)
      currentCode = code[0];
    else 
      currentCode = 0;
145
146
  }

147

148
149
  bool MeshStructure::nextElement(MeshStructure *insert)
  {
150
151
152
    FUNCNAME("MeshStructure::nextElement()");

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

155
156
    pos++;
    currentElement++;
157

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

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

171
172
173
    return true;
  }

174

175
176
  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
      cont = skipBranch(insert); // righ branch
      return cont;
    }
  }

190

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

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

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

    result->commit();
  }

223

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

    bool cont = true;

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

      Element *element = elInfo->getElement();

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

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

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

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

      elInfo = stack.traverseNext(elInfo);
    }

    // refine mesh
276
    bool finished = true;
277
278
279
280

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

295

296
297
298
299
  bool MeshStructure::compare(MeshStructure &other)
  {
    return (other.getCode() == code);
  }
300
}