MeshStructure.cc 7.17 KB
Newer Older
1
2
3
4
5
6
7
8
#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"
9
#include "InteriorBoundary.h"
10
11
12

namespace AMDiS {

13
  const int MeshStructure::unsignedLongSize = sizeof(unsigned long int) * 8;
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
  }

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

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

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

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

    clear();

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
102
    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);
    }
103
104
105
106

    commit();    
  }

107
108
  void MeshStructure::addAlongSide(Element *el, int ithSide, int elType, 
				   bool reverseOrder)
109
  {
110
111
112
113
114
115
116
117
    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());
    }
    
118
119
120
121
122
123
    insertElement(el->isLeaf());
    
    if (!el->isLeaf()) {
      int s1 = el->getSideOfChild(0, ithSide, elType);
      int s2 = el->getSideOfChild(1, ithSide, elType);

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

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

    if (code.size() > 0)
      currentCode = code[0];
    else 
      currentCode = 0;
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
176
177
    return true;
  }

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

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

  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
223
224
      cont = cont1;
    }

    result->commit();
  }

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

    bool cont = true;

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

      Element *element = elInfo->getElement();

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

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

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

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

      elInfo = stack.traverseNext(elInfo);
    }

    // refine mesh
    bool finished;

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

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