MeshStructure.cc 13.5 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
179
180
    FUNCNAME("MeshStructure::nextElement()");

    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
231
232
  int MeshStructure::lookAhead(unsigned int n)
  {
    FUNCNAME("MeshStructure::lookAhead()");

    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;
  }


233
234
  bool MeshStructure::skipBranch(MeshStructure *insert)
  {
235
236
    FUNCNAME("MeshStructure::skipBranch()");

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

248

249
250
251
252
  void MeshStructure::merge(MeshStructure *structure1,
			    MeshStructure *structure2,
			    MeshStructure *result)
  {
253
254
    FUNCNAME("MeshStructure::merge()");

255
256
257
258
259
    result->clear();
    structure1->reset();
    structure2->reset();

    bool cont = true;
260
    while (cont) {
261
      bool cont1, cont2;
262
      if (structure1->isLeafElement() == structure2->isLeafElement()) {
263
264
265
	cont1 = structure1->nextElement(result);
	cont2 = structure2->nextElement();
      } else {
266
	if (structure1->isLeafElement()) {
267
268
269
270
271
272
273
	  cont1 = structure1->nextElement();
	  cont2 = structure2->skipBranch(result);
	} else {
	  cont1 = structure1->skipBranch(result);
	  cont2 = structure2->nextElement();
	}
      }
274
      TEST_EXIT_DBG(cont1 == cont2)("Structures don't match!\n");
275
276
277
278
279
280
      cont = cont1;
    }

    result->commit();
  }

281

282
283
  void MeshStructure::fitMeshToStructure(Mesh *mesh,
					 RefinementManager *manager,
284
					 bool debugMode,
Thomas Witkowski's avatar
Thomas Witkowski committed
285
286
					 int macroElIndex,
					 bool ignoreFinerMesh) 
287
288
289
290
291
292
293
  {
    FUNCNAME("MeshStructure::fitMeshToStructure()");

    bool cont = true;

    // decorate leaf data
    reset();
294
    TraverseStack stack;
295
296
297
298
299
    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);
300

301

302
    while (elInfo) {
303
304
      Element *element = elInfo->getElement();

305
306
      TEST_EXIT(cont)("unexpected structure code end!\n");

307
      if (isLeafElement()) {
Thomas Witkowski's avatar
Thomas Witkowski committed
308
309
310
311
312
313
	if (ignoreFinerMesh && !element->isLeaf()) {
	  int level = elInfo->getLevel();
	  while (elInfo && level >= elInfo->getLevel())
	    elInfo = stack.traverseNext(elInfo);
	} else {
	  TEST_EXIT(element->isLeaf())
314
315
	    ("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
316
317
	}
      } 
318

319
320
      TEST_EXIT_DBG(element)("Should not happen!\n");

321
      if (element->isLeaf() && !isLeafElement()) {
Thomas Witkowski's avatar
Thomas Witkowski committed
322
	MeshStructure *structure = new MeshStructure();
323
324
325
	cont = skipBranch(structure);
	structure->commit();

326
327
328
	MeshStructure_ED *elData = new MeshStructure_ED(element->getElementData());
	elData->setStructure(structure);
	element->setElementData(elData);
329
330
331
332
      } else {
	cont = nextElement();
      }

Thomas Witkowski's avatar
Thomas Witkowski committed
333
334
      if (elInfo)
	elInfo = stack.traverseNext(elInfo);
335
336
337
    }

    // refine mesh
338
    bool finished = true;
339

340
    do {
341
      finished = true;
342
343
344
345
      if (macroElIndex == -1)
	elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_LEAF_EL);
      else
	elInfo = stack.traverseFirstOneMacro(mesh, macroElIndex, -1, Mesh::CALL_LEAF_EL);      
346
      while (elInfo) {
347
	Element *element = elInfo->getElement();
348
	if (element->getElementData(MESH_STRUCTURE) != NULL) {
349
350
351
352
353
354
355
	  element->setMark(1);
	  finished = false;
	} else {
	  element->setMark(0);
	}
	elInfo = stack.traverseNext(elInfo);
      }
356
357
358
359
360

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

362
363
364
365
	if (macroElIndex == -1)
	  manager->refineMesh(mesh);
	else
	  manager->refineMacroElement(mesh, macroElIndex);
366

367
368
369
370
371
#if (DEBUG != 0)
	TEST_EXIT(oldMeshIndex != mesh->getChangeIndex())
	  ("Mesh has not been changed by refinement procedure!\n");
#endif
      }
372
    } while (!finished);
373
374
  }

375

Thomas Witkowski's avatar
Thomas Witkowski committed
376
  string MeshStructure::toStr(bool resetCode)
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
  {
    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
396
397
398
399
400
401
402
403
404
405

    return oss.str();
  }


  void MeshStructure::print(bool resetCode)
  {
    FUNCNAME("MeshStructure::print()");
    
    string str = toStr(resetCode);
406
   
Thomas Witkowski's avatar
Thomas Witkowski committed
407
408
    if (str.length() < 255) {
      MSG("Mesh structure code: %s\n", str.c_str());
409
410
    } else {
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
Thomas Witkowski's avatar
Thomas Witkowski committed
411
      std::cout << "[" << MPI::COMM_WORLD.Get_rank() << "]                Mesh structure code: " << str << "\n";
412
#else
Thomas Witkowski's avatar
Thomas Witkowski committed
413
      std::cout << "                Mesh structure code: " << str << "\n";
414
415
#endif
    }
416
417
418
  }


419
420
421
422
  bool MeshStructure::compare(MeshStructure &other)
  {
    return (other.getCode() == code);
  }
423
424


425
  void MeshStructure::getMeshStructureValues(int macroElIndex,
426
427
428
429
430
					     const DOFVector<double>* vec,
					     std::vector<double>& values)
  {
    FUNCNAME("MeshStructure::getMeshStructureValues()");

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

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

453
454
455
456
457
      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 = 
458
	  elInfo->getElement()->getChild(0)->getDof(mesh->getDim(), nVertexPreDofs);
459
460
461
462
463
464
465
466
467
468
469
470
471
	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());
	}
      }
472
473
474
475
476
477

      elInfo = stack.traverseNext(elInfo);
    }
  }


478
  void MeshStructure::setMeshStructureValues(int macroElIndex,
479
480
481
482
483
					     DOFVector<double>* vec,
					     const std::vector<double>& values)
  {
    FUNCNAME("MeshStructure::setMeshStructureValues()");

484
    TEST_EXIT_DBG(vec)("No DOFVector defined!\n");
485

486
487
488
    const FiniteElemSpace *feSpace = vec->getFeSpace();
    Mesh *mesh = feSpace->getMesh();
    bool feSpaceHasNonVertexDofs = (feSpace->getBasisFcts()->getDegree() > 1);
489
    int nVertexPreDofs = feSpace->getAdmin()->getNumberOfPreDofs(VERTEX);
490
    unsigned int counter = 0;
491
492
493
494
495
496
#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
497

498
499
500
501
    TEST_EXIT_DBG(static_cast<int>(values.size()) >= mesh->getGeo(VERTEX))
      ("Should not happen!\n");

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

512
      if (!elInfo->getElement()->isLeaf()) {
513
514
	// If no leaf element set the value of the "new" DOF that is created
	// by bisectioning of this element.
515
516
	TEST_EXIT_DBG(counter < values.size())("Should not happen!\n");
	
517
	(*vec)[elInfo->getElement()->getChild(0)->getDof(mesh->getDim(), nVertexPreDofs)] =
518
	  values[counter++];      
519
520
521
522
523
524
525
526
527
528
529
      } 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());
	}
530
531
532
533
534
      }

      elInfo = stack.traverseNext(elInfo);
    }
      
535
    TEST_EXIT_DBG(values.size() == counter)("Should not happen!\n");
536
537
  }

538
}