MeshStructure.cc 13.6 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
15
16
17
#include "Debug.h"
#include "DOFVector.h"
#include "Element.h"
#include "ElementDofIterator.h"
#include "ElInfo.h"
18
19
20
21
#include "MeshStructure.h"
#include "MeshStructure_ED.h"
#include "Mesh.h"
#include "RefinementManager.h"
22
23
#include "Traverse.h"

24
25
26

namespace AMDiS {

27
  const int MeshStructure::structureSize = 64;
28

29

30
31
  void MeshStructure::insertElement(bool isLeaf) 
  {
32
    // overflow? -> next index
33
    if (pos >= structureSize) {
34
35
36
      code.push_back(currentCode);
      pos = 0;
      currentCode = 0;
37
38
39
    }

    // insert element in binary code
40
    if (!isLeaf) {
41
      uint64_t one = 1;
42
      currentCode += (one << pos);
43
44
    } 

45
46
    pos++;
    nElements++;
47
48
  }

49

50
51
  void MeshStructure::clear()
  {
52
53
54
55
56
    currentCode = 0;
    code.resize(0);
    pos = 0;
    nElements = 0;
    currentElement = 0;
57
58
  }

59

60
  void MeshStructure::init(Mesh *mesh, int macroElIndex) 
61
62
  {
    clear();
63

64
    TraverseStack stack;
65

Thomas Witkowski's avatar
BLUB    
Thomas Witkowski committed
66
67
68
69
70
71
72
73
74
    ElInfo *elInfo;
    if (macroElIndex == -1)
      elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_EVERY_EL_PREORDER);
    else
      elInfo = stack.traverseFirstOneMacro(mesh, macroElIndex, -1, 
					   Mesh::CALL_EVERY_EL_PREORDER);

    while (elInfo) {
      insertElement(elInfo->getElement()->isLeaf());
75
76
77
78
79
80
      elInfo = stack.traverseNext(elInfo);
    }
  
    commit();
  }

81
82
83
84
85
86
87

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

    Element *el = bound.el;

88
89
    TEST_EXIT_DBG(el)("No element!\n");

90
91
    clear();

92
93
    int s1 = el->getSubObjOfChild(0, bound.subObj, bound.ithObj, bound.elType);
    int s2 = el->getSubObjOfChild(1, bound.subObj, bound.ithObj, bound.elType);
94
95
    
    TEST_EXIT(s1 != -1 || s2 != -1)("This should not happen!\n");
96
97
98
99
100
101
102

    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);
    }
103
104
105
    
    if (!el->isLeaf()) {
      if (s1 == -1)
106
	addAlongSide(el->getSecondChild(), bound.subObj, s2, 
107
108
		     el->getChildType(bound.elType), bound.reverseMode);
      else if (s2 == -1)
109
	addAlongSide(el->getFirstChild(), bound.subObj, s1, 
110
111
		     el->getChildType(bound.elType), bound.reverseMode);
      else
112
	addAlongSide(el, bound.subObj, bound.ithObj, bound.elType, bound.reverseMode);
113
    }
114
115
116
117

    commit();    
  }

118
119
120

  void MeshStructure::addAlongSide(Element *el, GeoIndex subObj, int ithObj, 
				   int elType, bool reverseOrder)
121
  {
122
123
124
125
    FUNCNAME("MeshStructure::addAlongSide()");

    if (debugMode) {
      MSG("addAlondSide(%d, %d, %d, %d)\n",
126
	  el->getIndex(), ithObj, elType, reverseOrder);
127
128
129
      MSG("Element is leaf: %d\n", el->isLeaf());
    }
    
130
131
132
    insertElement(el->isLeaf());
    
    if (!el->isLeaf()) {
133
134
      int s1 = el->getSubObjOfChild(0, subObj, ithObj, elType);
      int s2 = el->getSubObjOfChild(1, subObj, ithObj, elType);
135

136
137
138
139
140
141
142
143
      if (debugMode) {
	MSG("Child index %d  %d\n", 
	    el->getFirstChild()->getIndex(),
	    el->getSecondChild()->getIndex());
	MSG("s1 = %d    s2 = %d\n", s1, s2);
	MSG("   \n");
      }

144
145
      if (!reverseOrder) {
	if (s1 != -1) 
146
147
	  addAlongSide(el->getFirstChild(), subObj, s1, 
		       el->getChildType(elType), reverseOrder);
148
	if (s2 != -1)
149
150
	  addAlongSide(el->getSecondChild(), subObj, s2, 
		       el->getChildType(elType), reverseOrder);
151
152
      } else {
	if (s2 != -1)
153
154
	  addAlongSide(el->getSecondChild(), subObj, s2, 
		       el->getChildType(elType), reverseOrder);
155
	if (s1 != -1) 
156
157
	  addAlongSide(el->getFirstChild(), subObj, s1, 
		       el->getChildType(elType), reverseOrder);
158
      }
159
160
161
    }
  }

162

163
164
165
166
167
  void MeshStructure::reset() 
  {
    currentIndex = 0;
    pos = 0;
    currentElement = 0;
168
169
170
171
172

    if (code.size() > 0)
      currentCode = code[0];
    else 
      currentCode = 0;
173
174
  }

175

176
177
  bool MeshStructure::nextElement(MeshStructure *insert)
  {
178
    FUNCNAME_DBG("MeshStructure::nextElement()");
179
180

    if (insert)
181
182
      insert->insertElement(isLeafElement());

183
184
    pos++;
    currentElement++;
185

186
187
    if (currentElement >= nElements) 
      return false;
188

189
    if (pos >= structureSize) {
190
191
192
193
194
      currentIndex++;
      TEST_EXIT_DBG(currentIndex < static_cast<int>(code.size()))
	("End of structure reached!\n");
      pos = 0;
      currentCode = code[currentIndex];
195
    } else {
196
      currentCode >>= 1;
197
    }
198

199
200
201
    return true;
  }

202

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
  int MeshStructure::lookAhead(unsigned int n)
  {
    int returnValue = 0;

    int tmp_pos = pos;
    int tmp_currentElement = currentElement;
    int tmp_currentIndex = currentIndex;
    uint64_t tmp_currentCode = currentCode;

    for (unsigned int i = 0; i < n; i++) {
      if (nextElement() == false) {
	returnValue = -1;      
	break;
      }
    }

    if (returnValue != -1)
      returnValue = static_cast<int>(!isLeafElement());

    pos = tmp_pos;
    currentElement = tmp_currentElement;
    currentIndex = tmp_currentIndex;
    currentCode = tmp_currentCode;

    return returnValue;
  }


231
232
  bool MeshStructure::skipBranch(MeshStructure *insert)
  {
233
    FUNCNAME_DBG("MeshStructure::skipBranch()");
234

235
    if (isLeafElement()) {
236
237
238
239
      return nextElement(insert);
    } else {
      bool cont = nextElement(insert);
      cont = skipBranch(insert); // left branch
240
      TEST_EXIT_DBG(cont)("Invalid structure!\n");
241
242
243
244
245
      cont = skipBranch(insert); // righ branch
      return cont;
    }
  }

246

247
248
249
250
  void MeshStructure::merge(MeshStructure *structure1,
			    MeshStructure *structure2,
			    MeshStructure *result)
  {
251
    FUNCNAME_DBG("MeshStructure::merge()");
252

253
254
255
256
257
    result->clear();
    structure1->reset();
    structure2->reset();

    bool cont = true;
258
    while (cont) {
259
260
261
262
      bool cont1;
#if DEBUG != 0
      bool cont2;
#endif
263
      if (structure1->isLeafElement() == structure2->isLeafElement()) {
264
	cont1 = structure1->nextElement(result);
265
#if DEBUG != 0
266
	cont2 = structure2->nextElement();
267
#endif
268
      } else {
269
	if (structure1->isLeafElement()) {
270
	  cont1 = structure1->nextElement();
271
#if DEBUG != 0
272
	  cont2 = structure2->skipBranch(result);
273
#endif
274
275
	} else {
	  cont1 = structure1->skipBranch(result);
276
#if DEBUG != 0
277
	  cont2 = structure2->nextElement();
278
#endif
279
280
	}
      }
281
      TEST_EXIT_DBG(cont1 == cont2)("Structures don't match!\n");
282
283
284
285
286
287
      cont = cont1;
    }

    result->commit();
  }

288

289
290
  void MeshStructure::fitMeshToStructure(Mesh *mesh,
					 RefinementManager *manager,
291
					 bool debugMode,
Thomas Witkowski's avatar
Thomas Witkowski committed
292
293
					 int macroElIndex,
					 bool ignoreFinerMesh) 
294
295
296
297
298
299
300
  {
    FUNCNAME("MeshStructure::fitMeshToStructure()");

    bool cont = true;

    // decorate leaf data
    reset();
301
    TraverseStack stack;
302
303
304
305
306
    ElInfo *elInfo = NULL;
    if (macroElIndex == -1)
      elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_EVERY_EL_PREORDER);
    else
      elInfo = stack.traverseFirstOneMacro(mesh, macroElIndex, -1, Mesh::CALL_EVERY_EL_PREORDER);
307

308

309
    while (elInfo) {
310
311
      Element *element = elInfo->getElement();

312
313
      TEST_EXIT(cont)("unexpected structure code end!\n");

314
      if (isLeafElement()) {
Thomas Witkowski's avatar
Thomas Witkowski committed
315
316
317
318
319
320
	if (ignoreFinerMesh && !element->isLeaf()) {
	  int level = elInfo->getLevel();
	  while (elInfo && level >= elInfo->getLevel())
	    elInfo = stack.traverseNext(elInfo);
	} else {
	  TEST_EXIT(element->isLeaf())
321
322
	    ("Mesh is finer than strucutre code! (Element index: %d Macro element index: %d)\n", 
	     element->getIndex(), elInfo->getMacroElement()->getIndex());
Thomas Witkowski's avatar
Thomas Witkowski committed
323
324
	}
      } 
325

326
327
      TEST_EXIT_DBG(element)("Should not happen!\n");

328
      if (element->isLeaf() && !isLeafElement()) {
Thomas Witkowski's avatar
Thomas Witkowski committed
329
	MeshStructure *structure = new MeshStructure();
330
331
332
	cont = skipBranch(structure);
	structure->commit();

333
334
335
	MeshStructure_ED *elData = new MeshStructure_ED(element->getElementData());
	elData->setStructure(structure);
	element->setElementData(elData);
336
337
338
339
      } else {
	cont = nextElement();
      }

Thomas Witkowski's avatar
Thomas Witkowski committed
340
341
      if (elInfo)
	elInfo = stack.traverseNext(elInfo);
342
343
344
    }

    // refine mesh
345
    bool finished = true;
346

347
    do {
348
      finished = true;
349
350
351
352
      if (macroElIndex == -1)
	elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_LEAF_EL);
      else
	elInfo = stack.traverseFirstOneMacro(mesh, macroElIndex, -1, Mesh::CALL_LEAF_EL);      
353
      while (elInfo) {
354
	Element *element = elInfo->getElement();
355
	if (element->getElementData(MESH_STRUCTURE) != NULL) {
356
357
358
359
360
361
362
	  element->setMark(1);
	  finished = false;
	} else {
	  element->setMark(0);
	}
	elInfo = stack.traverseNext(elInfo);
      }
363
364
365
366
367

      if (!finished) {
#if (DEBUG != 0)
	int oldMeshIndex = mesh->getChangeIndex();
#endif
368

369
370
371
372
	if (macroElIndex == -1)
	  manager->refineMesh(mesh);
	else
	  manager->refineMacroElement(mesh, macroElIndex);
373

374
375
376
377
378
#if (DEBUG != 0)
	TEST_EXIT(oldMeshIndex != mesh->getChangeIndex())
	  ("Mesh has not been changed by refinement procedure!\n");
#endif
      }
379
    } while (!finished);
380
381
  }

382

Thomas Witkowski's avatar
Thomas Witkowski committed
383
  string MeshStructure::toStr(bool resetCode)
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
  {
    std::stringstream oss;
    
    if (empty()) {
      oss << "-" << std::endl;
    }	else {	
      if (resetCode)
	reset();

      bool cont = true;
      while (cont) {
	if (isLeafElement())
	  oss << "0";
	else
	  oss << "1";
	
	cont = nextElement();
      }
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
403
404
405
406
407
408
409
410
411
412

    return oss.str();
  }


  void MeshStructure::print(bool resetCode)
  {
    FUNCNAME("MeshStructure::print()");
    
    string str = toStr(resetCode);
413
   
Thomas Witkowski's avatar
Thomas Witkowski committed
414
415
    if (str.length() < 255) {
      MSG("Mesh structure code: %s\n", str.c_str());
416
417
    } else {
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
Thomas Witkowski's avatar
Thomas Witkowski committed
418
      std::cout << "[" << MPI::COMM_WORLD.Get_rank() << "]                Mesh structure code: " << str << "\n";
419
#else
Thomas Witkowski's avatar
Thomas Witkowski committed
420
      std::cout << "                Mesh structure code: " << str << "\n";
421
422
#endif
    }
423
424
425
  }


426
427
428
429
  bool MeshStructure::compare(MeshStructure &other)
  {
    return (other.getCode() == code);
  }
430
431


432
  void MeshStructure::getMeshStructureValues(int macroElIndex,
433
434
435
					     const DOFVector<double>* vec,
					     std::vector<double>& values)
  {
436
    FUNCNAME_DBG("MeshStructure::getMeshStructureValues()");
437

438
    TEST_EXIT_DBG(vec)("No DOFVector defined!\n");
439
440
441
  
    const FiniteElemSpace *feSpace = vec->getFeSpace();
    Mesh *mesh = feSpace->getMesh();
442
    int nVertexPreDofs = feSpace->getAdmin()->getNumberOfPreDofs(VERTEX);
443
    bool feSpaceHasNonVertexDofs = (feSpace->getBasisFcts()->getDegree() > 1);
444
    values.clear();
445
446
447
448
#if (DEBUG != 0)    
    // In debug mode we add the macro element index to the value code.
    values.push_back(static_cast<double>(macroElIndex));
#endif
449

450
    ElementDofIterator elDofIter(feSpace);
451
452
453
454
    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirstOneMacro(mesh, macroElIndex, -1, 
						 Mesh::CALL_EVERY_EL_PREORDER);
    while (elInfo) {
455
      // For the macro element the mesh structure code stores all vertex values.
456
      if (elInfo->getLevel() == 0)
457
	for (int i = 0; i < mesh->getGeo(VERTEX); i++)
458
	  values.push_back((*vec)[elInfo->getElement()->getDof(i, nVertexPreDofs)]);
459

460
461
462
463
464
      if (!elInfo->getElement()->isLeaf()) {
	// If no leaf element store the value of the "new" DOF that is created
	// by bisectioning of this element.

	DegreeOfFreedom dof0 = 
465
	  elInfo->getElement()->getChild(0)->getDof(mesh->getDim(), nVertexPreDofs);
466
467
468
469
470
471
472
473
474
475
476
477
478
	values.push_back((*vec)[dof0]);
      } else {
	// If leaf element store all non vertex values of this element, thus
	// only relevant for higher order basis functions.

	if (feSpaceHasNonVertexDofs) {
	  elDofIter.reset(elInfo->getElement());
	  do {
	    if (elDofIter.getPosIndex() != VERTEX) 
	      values.push_back((*vec)[elDofIter.getDof()]);
	  } while (elDofIter.next());
	}
      }
479
480
481
482
483
484

      elInfo = stack.traverseNext(elInfo);
    }
  }


485
  void MeshStructure::setMeshStructureValues(int macroElIndex,
486
487
488
					     DOFVector<double>* vec,
					     const std::vector<double>& values)
  {
489
    FUNCNAME_DBG("MeshStructure::setMeshStructureValues()");
490

491
    TEST_EXIT_DBG(vec)("No DOFVector defined!\n");
492

493
494
495
    const FiniteElemSpace *feSpace = vec->getFeSpace();
    Mesh *mesh = feSpace->getMesh();
    bool feSpaceHasNonVertexDofs = (feSpace->getBasisFcts()->getDegree() > 1);
496
    int nVertexPreDofs = feSpace->getAdmin()->getNumberOfPreDofs(VERTEX);
497
    unsigned int counter = 0;
498
499
500
501
502
503
#if (DEBUG != 0)    
    TEST_EXIT(static_cast<int>(values[0]) == macroElIndex)
      ("Value structure code was created for macro element index %d, but should be set to macro element index %d\n",
       static_cast<int>(values[0]), macroElIndex);
    counter++;
#endif
504

505
506
507
508
    TEST_EXIT_DBG(static_cast<int>(values.size()) >= mesh->getGeo(VERTEX))
      ("Should not happen!\n");

    ElementDofIterator elDofIter(feSpace);
509
510
511
512
    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirstOneMacro(mesh, macroElIndex, -1, 
						 Mesh::CALL_EVERY_EL_PREORDER);
    while (elInfo) {
513
      // For the macro element all vertex nodes are set first.
514
      if (elInfo->getLevel() == 0)
515
	for (int i = 0; i < mesh->getGeo(VERTEX); i++)
516
517
	  (*vec)[elInfo->getElement()->getDof(i, nVertexPreDofs)] = 
	    values[counter++];
518

519
      if (!elInfo->getElement()->isLeaf()) {
520
521
	// If no leaf element set the value of the "new" DOF that is created
	// by bisectioning of this element.
522
523
	TEST_EXIT_DBG(counter < values.size())("Should not happen!\n");
	
524
	(*vec)[elInfo->getElement()->getChild(0)->getDof(mesh->getDim(), nVertexPreDofs)] =
525
	  values[counter++];      
526
527
528
529
530
531
532
533
534
535
536
      } else {
	// On leaf elements set all non vertex values (thus DOFs of higher order
	// basis functions).

	if (feSpaceHasNonVertexDofs) {
	  elDofIter.reset(elInfo->getElement());
	  do {
	    if (elDofIter.getPosIndex() != VERTEX) 
	      (*vec)[elDofIter.getDof()] = values[counter++];
	  } while (elDofIter.next());
	}
537
538
539
540
541
      }

      elInfo = stack.traverseNext(elInfo);
    }
      
542
    TEST_EXIT_DBG(values.size() == counter)("Should not happen!\n");
543
544
  }

545
}