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

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
58
59
60
61
62
  void MeshStructure::reset() 
  {
    currentIndex = 0;
    currentCode = code[0];
    pos = 0;
    currentElement = 0;
63
64
65
66
  }

  bool MeshStructure::nextElement(MeshStructure *insert)
  {
67
68
69
    FUNCNAME("MeshStructure::nextElement()");

    if (insert)
70
71
      insert->insertElement(isLeafElement());

72
73
    pos++;
    currentElement++;
74

75
76
    if (currentElement >= nElements) 
      return false;
77

78
79
80
81
82
83
    if (pos >= unsignedLongSize) {
      currentIndex++;
      TEST_EXIT_DBG(currentIndex < static_cast<int>(code.size()))
	("End of structure reached!\n");
      pos = 0;
      currentCode = code[currentIndex];
84
    } else {
85
      currentCode >>= 1;
86
    }
87

88
89
90
91
92
    return true;
  }

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

95
    if (isLeafElement()) {
96
97
98
99
      return nextElement(insert);
    } else {
      bool cont = nextElement(insert);
      cont = skipBranch(insert); // left branch
100
      TEST_EXIT_DBG(cont)("Invalid structure!\n");
101
102
103
104
105
106
107
108
109
      cont = skipBranch(insert); // righ branch
      return cont;
    }
  }

  void MeshStructure::merge(MeshStructure *structure1,
			    MeshStructure *structure2,
			    MeshStructure *result)
  {
110
111
    FUNCNAME("MeshStructure::merge()");

112
113
114
115
116
    result->clear();
    structure1->reset();
    structure2->reset();

    bool cont = true;
117
    while (cont) {
118
      bool cont1, cont2;
119
      if (structure1->isLeafElement() == structure2->isLeafElement()) {
120
121
122
	cont1 = structure1->nextElement(result);
	cont2 = structure2->nextElement();
      } else {
123
	if (structure1->isLeafElement()) {
124
125
126
127
128
129
130
	  cont1 = structure1->nextElement();
	  cont2 = structure2->skipBranch(result);
	} else {
	  cont1 = structure1->skipBranch(result);
	  cont2 = structure2->nextElement();
	}
      }
131
      TEST_EXIT_DBG(cont1 == cont2)("Structures don't match!\n");
132
133
134
135
136
137
138
139
      cont = cont1;
    }

    result->commit();
  }

  void MeshStructure::fitMeshToStructure(Mesh *mesh,
					 RefinementManager *manager,
140
141
					 bool checkPartition,
					 bool debugMode) 
142
143
144
145
146
147
148
  {
    FUNCNAME("MeshStructure::fitMeshToStructure()");

    bool cont = true;

    // decorate leaf data
    reset();
149
150
    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_EVERY_EL_PREORDER);
151
    while (elInfo) {
152
      TEST_EXIT(cont)("unexpected structure code end!\n");
153
154
155

      Element *element = elInfo->getElement();

156
157
      if (isLeafElement())
	TEST_EXIT_DBG(element->isLeaf())("mesh finer than code\n");
158

159
      if (element->isLeaf() && !isLeafElement()) {
Thomas Witkowski's avatar
Thomas Witkowski committed
160
	MeshStructure *structure = new MeshStructure();
161
162
163
164
	cont = skipBranch(structure);
	structure->commit();

	bool decorate = true;
165
	if (checkPartition) {
166
167
	  PartitionElementData *partitionData = dynamic_cast<PartitionElementData*>
	    (element->getElementData(PARTITION_ED));
168
	  TEST_EXIT_DBG(partitionData)("no partition element data\n");
169
	  PartitionStatus status = partitionData->getPartitionStatus();
170
	  if (debugMode == false && (status == OUT || status == UNDEFINED))
171
172
173
	    decorate = false;
	}

174
	if (decorate) {
Thomas Witkowski's avatar
Thomas Witkowski committed
175
	  MeshStructure_ED *elData = new MeshStructure_ED(element->getElementData());
176
177
178
	  elData->setStructure(structure);
	  element->setElementData(elData);
	} else {
Thomas Witkowski's avatar
Thomas Witkowski committed
179
	  delete structure;
180
181
182
183
184
185
186
187
188
189
190
191
192
193
	}
      } else {
	cont = nextElement();
      }

      elInfo = stack.traverseNext(elInfo);
    }

    // refine mesh
    bool finished;

    do {
      finished = true;
      elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_LEAF_EL);
194
      while (elInfo) {
195
	Element *element = elInfo->getElement();
196
	if (element->getElementData(MESH_STRUCTURE) != NULL) {
197
198
199
200
201
202
203
204
	  element->setMark(1);
	  finished = false;
	} else {
	  element->setMark(0);
	}
	elInfo = stack.traverseNext(elInfo);
      }
      manager->refineMesh(mesh);
205
    } while (!finished);
206
207
208
  }

}