MeshManipulation.cc 12.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
//
// Software License for AMDiS
//
// Copyright (c) 2010 Dresden University of Technology 
// All rights reserved.
// Authors: Simon Vey, Thomas Witkowski et al.
//
// This file is part of AMDiS
//
// See also license.opensource.txt in the distribution.


13
14
#include "parallel/MeshManipulation.h"
#include "Mesh.h"
15
#include "MeshStructure.h"
16
#include "BasisFunction.h"
17
#include "Traverse.h"
18
#include "Debug.h"
19
20
21

namespace AMDiS {

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
  MeshManipulation::MeshManipulation(FiniteElemSpace *space)
    : feSpace(space),
      mesh(space->getMesh())
  {
    switch (mesh->getDim()) {
    case 2:
      refineManager = new RefinementManager2d();
      break;
    case 3:
      refineManager = new RefinementManager3d();
      break;
    default:
      ERROR_EXIT("invalid dim!\n");
    }
  }


  MeshManipulation::~MeshManipulation()
  {
    delete refineManager;
  }


45
46
  void MeshManipulation::deleteDoubleDofs(std::set<MacroElement*>& newMacroEl, 
					  ElementObjects &objects)
47
  {
48
49
50
51
    FUNCNAME("MeshManipulation::deleteDoubleDofs()");

    TEST_EXIT(mesh->getDim() == 2)("Not yet supported for dim != 2!\n");

52
53
54

    // Create data structure that maps macro element indices to the 
    // corresponding pointers.
55
56
57
58
59
60
61
62
    std::map<int, MacroElement*> macroIndexMap;
    for (std::set<MacroElement*>::iterator it = newMacroEl.begin();
	 it != newMacroEl.end(); ++it)
      macroIndexMap[(*it)->getIndex()] = *it;

    std::set<int> macrosProcessed;
    std::map<const DegreeOfFreedom*, const DegreeOfFreedom*> mapDelDofs;

63
64
65
66
    
    // === Traverse mesh and put all "old" macro element to macrosProcessed  ===
    // === that stores all macro elements which are really conected to the   ===
    // === overall mesh structure.                                           ===
67

68
    TraverseStack stack;
69
    ElInfo *elInfo = stack.traverseFirst(mesh, 0, Mesh::CALL_EL_LEVEL);
70
    while (elInfo) {
71
72
73
74
75
76
77
      if (newMacroEl.count(elInfo->getMacroElement()) == 0) {
	int index = elInfo->getMacroElement()->getIndex();

	macrosProcessed.insert(index);
	macroIndexMap[index] = elInfo->getMacroElement();
      }

78
79
80
81
      elInfo = stack.traverseNext(elInfo);
    }


82
83
84
85
    // === Traverse all new elements and connect them to the overall mesh     ===
    // === structure by deleting all double DOFs on macro element's vertices, ===
    // === edges and faces.                                                   ===

86
87
    for (std::set<MacroElement*>::iterator it = newMacroEl.begin(); 
	 it != newMacroEl.end(); ++it) {
88

89
90
91
92
      for (int i = 0; i < mesh->getGeo(VERTEX); i++) {
	ElementObjectData elObj((*it)->getIndex(), i);
	DegreeOfFreedom vertex = objects.getVertexLocalMap(elObj);
	std::vector<ElementObjectData> &vertexEl = objects.getElements(vertex);
93

94
95
96
97
	for (std::vector<ElementObjectData>::iterator elIt = vertexEl.begin();
	     elIt != vertexEl.end(); ++elIt) {
	  if (elIt->elIndex == (*it)->getIndex())
	    continue;
98

99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
	  if (macrosProcessed.count(elIt->elIndex) == 1) {
	    TEST_EXIT_DBG(macroIndexMap.count(elIt->elIndex) == 1)
	      ("Should not happen!\n");

	    Element *el0 = (*it)->getElement();
	    Element *el1 = macroIndexMap[elIt->elIndex]->getElement();

	    const DegreeOfFreedom *dof0 = el0->getDof(i);
	    const DegreeOfFreedom *dof1 = el1->getDof(elIt->ithObject);

	    mapDelDofs[dof0] = dof1;
    
	    break;
	  } 
	}
      }

      for (int i = 0; i < mesh->getGeo(EDGE); i++) {
	ElementObjectData elObj((*it)->getIndex(), i);
	DofEdge edge = objects.getEdgeLocalMap(elObj);
	std::vector<ElementObjectData> &edgeEl = objects.getElements(edge);

	for (std::vector<ElementObjectData>::iterator elIt = edgeEl.begin();
	     elIt != edgeEl.end(); ++elIt) {
	  if (elIt->elIndex == (*it)->getIndex())
	    continue;

	  if (macrosProcessed.count(elIt->elIndex) == 1) {
	    TEST_EXIT_DBG(macroIndexMap.count(elIt->elIndex) == 1)
	      ("Should not happen!\n");

	    TEST_EXIT(mesh->getDim() == 2)("In 3D set correct reverse mode!\n");

	    TEST_EXIT(feSpace->getBasisFcts()->getDegree() == 1)
	      ("Not yet implemented!\n");

	    Element *el0 = (*it)->getElement();	    
	    Element *el1 = macroIndexMap[elIt->elIndex]->getElement();

	    BoundaryObject b0(el0, 0, EDGE, i, true);
	    BoundaryObject b1(el1, 0, EDGE, elIt->ithObject, false);

	    DofContainer dofs0, dofs1;
	    
	    el0->getVertexDofs(feSpace, b0, dofs0);
	    el1->getVertexDofs(feSpace, b1, dofs1);

#if (DEBUG != 0)
	    debug::testDofsByCoords(feSpace, dofs0, dofs1);
#endif

 	    for (unsigned int i = 0; i < dofs0.size(); i++)
 	      mapDelDofs[dofs0[i]] = dofs1[i];

	    break;
154
155
156
157
	  }
	}
      }

158
      TEST_EXIT(mesh->getDim() == 2)("Add face traverse here!\n");
159

160
161

      macrosProcessed.insert((*it)->getIndex());
162
163
    }

164

165
166
    // === Remove all DOF replacments of the form A -> B, B -> C by A -> C. ===

167
168
169
170
171
172
173
    bool changed = false;
    do {
      changed = false;
      for (std::map<const DegreeOfFreedom*, const DegreeOfFreedom*>::iterator it = mapDelDofs.begin();
	   it != mapDelDofs.end(); ++it) {
	std::map<const DegreeOfFreedom*, const DegreeOfFreedom*>::iterator findIt = mapDelDofs.find(it->second);
	if (findIt != mapDelDofs.end()) {
174
175
176
	  TEST_EXIT_DBG(it->first != findIt->second)
	    ("Cycle %d -> %d and %d -> %d! Should not happen!\n",
	     *(it->first), *(it->second), *(findIt->first), *(findIt->second));
177
178
179
180
181
182
183
	  it->second = findIt->second;   
	  changed = true;
	}
      } 
    } while (changed);


184
185
    // === Set new DOF pointers in all elements of the mesh. ===

186
    elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_EVERY_EL_PREORDER);
187
188
    while (elInfo) {
      for (int i = 0; i < mesh->getGeo(VERTEX); i++)
189
	if (mapDelDofs.count(elInfo->getElement()->getDof(i)) == 1)
190
	  elInfo->getElement()->setDof(i, const_cast<DegreeOfFreedom*>(mapDelDofs[elInfo->getElement()->getDof(i)]));	
191
192
193
194

      elInfo = stack.traverseNext(elInfo);
    }

195

196
197
    // === And delete all double DOFs. ===

198
    for (std::map<const DegreeOfFreedom*, const DegreeOfFreedom*>::iterator it = mapDelDofs.begin();
199
	 it != mapDelDofs.end(); ++it)
200
201
202
      mesh->freeDof(const_cast<DegreeOfFreedom*>(it->first), VERTEX);
  }

203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420

  bool MeshManipulation::startFitElementToMeshCode(MeshStructure &code, 
						   Element *el, 
						   GeoIndex subObj,
						   int ithObj, 
						   int elType,
						   bool reverseMode)
  {
    FUNCNAME("MeshManipulation::startFitElementToMeshCode()");

    TEST_EXIT_DBG(el)("No element given!\n");

    // If the code is empty, the element does not matter and the function can
    // return without chaning the mesh.
    if (code.empty())
      return false;

    // s0 and s1 are the number of the edge/face in both child of the element,
    // which contain the edge/face the function has to traverse through. If the
    // edge/face is not contained in one of the children, s0 or s1 is -1.
    int s0 = el->getSubObjOfChild(0, subObj, ithObj, elType);
    int s1 = el->getSubObjOfChild(1, subObj, ithObj, elType);

    TEST_EXIT_DBG(s0 != -1 || s1 != -1)("This should not happen!\n");

    bool meshChanged = false;
    Flag traverseFlag = 
      Mesh::CALL_EVERY_EL_PREORDER | Mesh::FILL_NEIGH | Mesh::FILL_BOUND;

    // Test for reverse mode, in which the left and right children of elements
    // are flipped.
    if (reverseMode)
      traverseFlag |= Mesh::CALL_REVERSE_MODE;    


    // === If the edge/face is contained in both children. ===

    if (s0 != -1 && s1 != -1) {
      // Create traverse stack and traverse within the mesh until the element,
      // which should be fitted to the mesh structure code, is reached.
      TraverseStack stack;
      ElInfo *elInfo = stack.traverseFirst(el->getMesh(), -1, traverseFlag);
      while (elInfo && elInfo->getElement() != el)
	elInfo = stack.traverseNext(elInfo);      

      TEST_EXIT_DBG(elInfo->getElement() == el)("This should not happen!\n");

      meshChanged = fitElementToMeshCode(code, stack, subObj, ithObj, reverseMode);
      return meshChanged;
    }


    // === The edge/face is contained in only on of the both children. ===

    if (el->isLeaf()) {

      // If element is leaf and code contains only one leaf element, we are finished.
      if (code.getNumElements() == 1 && code.isLeafElement())
	return false;     

      // Create traverse stack and traverse the mesh to the element.
      TraverseStack stack;
      ElInfo *elInfo = stack.traverseFirst(el->getMesh(), -1, traverseFlag);
      while (elInfo && elInfo->getElement() != el)
	elInfo = stack.traverseNext(elInfo);      

      TEST_EXIT_DBG(elInfo)("This should not happen!\n");

      // Code is not leaf, therefore refine the element.
      el->setMark(1);
      refineManager->setMesh(el->getMesh());
      refineManager->setStack(&stack);
      refineManager->refineFunction(elInfo);
      meshChanged = true;
    }

    Element *child0 = el->getFirstChild();
    Element *child1 = el->getSecondChild();
    if (reverseMode) {
      swap(s0, s1);
      swap(child0, child1);    
    }

    // === We know that the edge/face is contained in only one of the children. ===
    // === Therefore, traverse the mesh to this children and fit this element   ===
    // === To the mesh structure code.                                          ===

    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirst(el->getMesh(), -1, traverseFlag);

    if (s0 != -1) {
      while (elInfo && elInfo->getElement() != child0)
	elInfo = stack.traverseNext(elInfo);     

      meshChanged |= fitElementToMeshCode(code, stack, subObj, s0, reverseMode);
    } else {
      while (elInfo && elInfo->getElement() != child1) 
	elInfo = stack.traverseNext(elInfo);      

      meshChanged |= fitElementToMeshCode(code, stack, subObj, s1, reverseMode);
    }


    return meshChanged;
  }


  bool MeshManipulation::fitElementToMeshCode(MeshStructure &code, 
					      TraverseStack &stack,
					      GeoIndex subObj,
					      int ithObj, 
					      bool reverseMode)
  {
    FUNCNAME("MeshManipulation::fitElementToMeshCode()");


    // === Test if there are more elements in stack to check with the code. ===

    ElInfo *elInfo = stack.getElInfo();
    if (!elInfo)
      return false;


    // === Test if code contains a leaf element. If this is the case, the ===
    // === current element is finished. Traverse the mesh to the next     ===
    // === coarser element.                                               ===

    if (code.isLeafElement()) {
      int level = elInfo->getLevel();

      do {
	elInfo = stack.traverseNext(elInfo);
      } while (elInfo && elInfo->getLevel() > level);

      return false;
    }


    bool meshChanged = false;
    Element *el = elInfo->getElement();


    // === If element is leaf (and the code is not), refine the element. ===

    if (el->isLeaf()) {
      TEST_EXIT_DBG(elInfo->getLevel() < 255)("This should not happen!\n");

      el->setMark(1);
      refineManager->setMesh(el->getMesh());
      refineManager->setStack(&stack);
      refineManager->refineFunction(elInfo);
      meshChanged = true;
    }


    // === Continue fitting the mesh structure code to the children of the ===
    // === current element.                                                ===

    int s0 = el->getSubObjOfChild(0, subObj, ithObj, elInfo->getType());
    int s1 = el->getSubObjOfChild(1, subObj, ithObj, elInfo->getType());
    Element *child0 = el->getFirstChild();
    Element *child1 = el->getSecondChild();
    if (reverseMode) {
      swap(s0, s1);
      swap(child0, child1);
    }

    
    // === Traverse left child. ===

    if (s0 != -1) {
      // The edge/face is contained in the left child, therefore fit this
      // child to the mesh structure code.

      stack.traverseNext(elInfo);
      code.nextElement();
      meshChanged |= fitElementToMeshCode(code, stack, subObj, s0, reverseMode);
      elInfo = stack.getElInfo();
    } else {
      // The edge/face is not contained in the left child. Hence we need
      // to traverse through all subelements of the left child until we get
      // the second child of the current element.

      do {
	elInfo = stack.traverseNext(elInfo);
      } while (elInfo && elInfo->getElement() != child1); 

      TEST_EXIT_DBG(elInfo != NULL)("This should not happen!\n");
    }  

    TEST_EXIT_DBG(elInfo->getElement() == child1)
      ("Got wrong child with idx = %d! Searched for child idx = %d\n",
       elInfo->getElement()->getIndex(), child1->getIndex());


    // === Traverse right child. ===

    if (s1 != -1) {
      // The edge/face is contained in the right child, therefore fit this
      // child to the mesh structure code.

      code.nextElement();
      meshChanged |= fitElementToMeshCode(code, stack, subObj, s1, reverseMode);
    } else {
      // The edge/face is not contained in the right child. Hence we need
      // to traverse through all subelements of the right child until we have
      // finished traversing the current element with all its subelements.

      int level = elInfo->getLevel();

      do {
	elInfo = stack.traverseNext(elInfo);
      } while (elInfo && elInfo->getLevel() > level);
    }


    return meshChanged;
  }
421
  
422
}