MeshStructure.cc 5.36 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
  void MeshStructure::init(Element *el, int ithSide, int elType)
  {
    FUNCNAME("MeshStructure::init()");

    clear();

    addAlongSide(el, ithSide, elType);

    commit();    
  }

  void MeshStructure::addAlongSide(Element *el, int ithSide, int elType)
  {
    insertElement(el->isLeaf());
    
    if (!el->isLeaf()) {
      int s1 = el->getSideOfChild(0, ithSide, elType);
      int s2 = el->getSideOfChild(1, ithSide, elType);

      if (s1 != -1) 
	addAlongSide(el->getFirstChild(), s1, el->getChildType(elType));
      if (s2 != -1)
	addAlongSide(el->getSecondChild(), s2, el->getChildType(elType));
    }
  }

82
83
84
85
86
87
  void MeshStructure::reset() 
  {
    currentIndex = 0;
    currentCode = code[0];
    pos = 0;
    currentElement = 0;
88
89
90
91
  }

  bool MeshStructure::nextElement(MeshStructure *insert)
  {
92
93
94
    FUNCNAME("MeshStructure::nextElement()");

    if (insert)
95
96
      insert->insertElement(isLeafElement());

97
98
    pos++;
    currentElement++;
99

100
101
    if (currentElement >= nElements) 
      return false;
102

103
104
105
106
107
108
    if (pos >= unsignedLongSize) {
      currentIndex++;
      TEST_EXIT_DBG(currentIndex < static_cast<int>(code.size()))
	("End of structure reached!\n");
      pos = 0;
      currentCode = code[currentIndex];
109
    } else {
110
      currentCode >>= 1;
111
    }
112

113
114
115
116
117
    return true;
  }

  bool MeshStructure::skipBranch(MeshStructure *insert)
  {
118
119
    FUNCNAME("MeshStructure::skipBranch()");

120
    if (isLeafElement()) {
121
122
123
124
      return nextElement(insert);
    } else {
      bool cont = nextElement(insert);
      cont = skipBranch(insert); // left branch
125
      TEST_EXIT_DBG(cont)("Invalid structure!\n");
126
127
128
129
130
131
132
133
134
      cont = skipBranch(insert); // righ branch
      return cont;
    }
  }

  void MeshStructure::merge(MeshStructure *structure1,
			    MeshStructure *structure2,
			    MeshStructure *result)
  {
135
136
    FUNCNAME("MeshStructure::merge()");

137
138
139
140
141
    result->clear();
    structure1->reset();
    structure2->reset();

    bool cont = true;
142
    while (cont) {
143
      bool cont1, cont2;
144
      if (structure1->isLeafElement() == structure2->isLeafElement()) {
145
146
147
	cont1 = structure1->nextElement(result);
	cont2 = structure2->nextElement();
      } else {
148
	if (structure1->isLeafElement()) {
149
150
151
152
153
154
155
	  cont1 = structure1->nextElement();
	  cont2 = structure2->skipBranch(result);
	} else {
	  cont1 = structure1->skipBranch(result);
	  cont2 = structure2->nextElement();
	}
      }
156
      TEST_EXIT_DBG(cont1 == cont2)("Structures don't match!\n");
157
158
159
160
161
162
163
164
      cont = cont1;
    }

    result->commit();
  }

  void MeshStructure::fitMeshToStructure(Mesh *mesh,
					 RefinementManager *manager,
165
166
					 bool checkPartition,
					 bool debugMode) 
167
168
169
170
171
172
173
  {
    FUNCNAME("MeshStructure::fitMeshToStructure()");

    bool cont = true;

    // decorate leaf data
    reset();
174
175
    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_EVERY_EL_PREORDER);
176
    while (elInfo) {
177
      TEST_EXIT(cont)("unexpected structure code end!\n");
178
179
180

      Element *element = elInfo->getElement();

181
182
      if (isLeafElement())
	TEST_EXIT_DBG(element->isLeaf())("mesh finer than code\n");
183

184
      if (element->isLeaf() && !isLeafElement()) {
Thomas Witkowski's avatar
Thomas Witkowski committed
185
	MeshStructure *structure = new MeshStructure();
186
187
188
189
	cont = skipBranch(structure);
	structure->commit();

	bool decorate = true;
190
	if (checkPartition) {
191
192
	  PartitionElementData *partitionData = dynamic_cast<PartitionElementData*>
	    (element->getElementData(PARTITION_ED));
193
	  TEST_EXIT_DBG(partitionData)("no partition element data\n");
194
	  PartitionStatus status = partitionData->getPartitionStatus();
195
	  if (debugMode == false && (status == OUT || status == UNDEFINED))
196
197
198
	    decorate = false;
	}

199
	if (decorate) {
Thomas Witkowski's avatar
Thomas Witkowski committed
200
	  MeshStructure_ED *elData = new MeshStructure_ED(element->getElementData());
201
202
203
	  elData->setStructure(structure);
	  element->setElementData(elData);
	} else {
Thomas Witkowski's avatar
Thomas Witkowski committed
204
	  delete structure;
205
206
207
208
209
210
211
212
213
214
215
216
217
218
	}
      } else {
	cont = nextElement();
      }

      elInfo = stack.traverseNext(elInfo);
    }

    // refine mesh
    bool finished;

    do {
      finished = true;
      elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_LEAF_EL);
219
      while (elInfo) {
220
	Element *element = elInfo->getElement();
221
	if (element->getElementData(MESH_STRUCTURE) != NULL) {
222
223
224
225
226
227
228
229
	  element->setMark(1);
	  finished = false;
	} else {
	  element->setMark(0);
	}
	elInfo = stack.traverseNext(elInfo);
      }
      manager->refineMesh(mesh);
230
    } while (!finished);
231
232
233
  }

}