MeshStructure.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 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 65
    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_EVERY_EL_PREORDER);
66
    while (elInfo) {
67 68 69 70
      if (macroElIndex == -1 ||
	  elInfo->getMacroElement()->getIndex() == macroElIndex)
	insertElement(elInfo->getElement()->isLeaf());

71 72 73 74 75 76
      elInfo = stack.traverseNext(elInfo);
    }
  
    commit();
  }

77 78 79 80 81 82 83

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

    Element *el = bound.el;

84 85
    TEST_EXIT_DBG(el)("No element!\n");

86 87
    clear();

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

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

    commit();    
  }

114 115 116

  void MeshStructure::addAlongSide(Element *el, GeoIndex subObj, int ithObj, 
				   int elType, bool reverseOrder)
117
  {
118 119 120 121
    FUNCNAME("MeshStructure::addAlongSide()");

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

132 133 134 135 136 137 138 139
      if (debugMode) {
	MSG("Child index %d  %d\n", 
	    el->getFirstChild()->getIndex(),
	    el->getSecondChild()->getIndex());
	MSG("s1 = %d    s2 = %d\n", s1, s2);
	MSG("   \n");
      }

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

158

159 160 161 162 163
  void MeshStructure::reset() 
  {
    currentIndex = 0;
    pos = 0;
    currentElement = 0;
164 165 166 167 168

    if (code.size() > 0)
      currentCode = code[0];
    else 
      currentCode = 0;
169 170
  }

171

172 173
  bool MeshStructure::nextElement(MeshStructure *insert)
  {
174 175 176
    FUNCNAME("MeshStructure::nextElement()");

    if (insert)
177 178
      insert->insertElement(isLeafElement());

179 180
    pos++;
    currentElement++;
181

182 183
    if (currentElement >= nElements) 
      return false;
184

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

195 196 197
    return true;
  }

198

199 200 201 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
  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;
  }


229 230
  bool MeshStructure::skipBranch(MeshStructure *insert)
  {
231 232
    FUNCNAME("MeshStructure::skipBranch()");

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

244

245 246 247 248
  void MeshStructure::merge(MeshStructure *structure1,
			    MeshStructure *structure2,
			    MeshStructure *result)
  {
249 250
    FUNCNAME("MeshStructure::merge()");

251 252 253 254 255
    result->clear();
    structure1->reset();
    structure2->reset();

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

    result->commit();
  }

277

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

    bool cont = true;

    // decorate leaf data
    reset();
290
    TraverseStack stack;
291 292 293 294 295
    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);
296

297

298
    while (elInfo) {
299 300
      Element *element = elInfo->getElement();

301 302
      TEST_EXIT(cont)("unexpected structure code end!\n");

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

315 316
      TEST_EXIT_DBG(element)("Should not happen!\n");

317
      if (element->isLeaf() && !isLeafElement()) {
Thomas Witkowski's avatar
Thomas Witkowski committed
318
	MeshStructure *structure = new MeshStructure();
319 320 321
	cont = skipBranch(structure);
	structure->commit();

322 323 324
	MeshStructure_ED *elData = new MeshStructure_ED(element->getElementData());
	elData->setStructure(structure);
	element->setElementData(elData);
325 326 327 328
      } else {
	cont = nextElement();
      }

Thomas Witkowski's avatar
Thomas Witkowski committed
329 330
      if (elInfo)
	elInfo = stack.traverseNext(elInfo);
331 332 333
    }

    // refine mesh
334
    bool finished = true;
335

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

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

358 359 360 361
	if (macroElIndex == -1)
	  manager->refineMesh(mesh);
	else
	  manager->refineMacroElement(mesh, macroElIndex);
362

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

371

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

    return oss.str();
  }


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


415 416 417 418
  bool MeshStructure::compare(MeshStructure &other)
  {
    return (other.getCode() == code);
  }
419 420


421
  void MeshStructure::getMeshStructureValues(int macroElIndex,
422 423 424 425 426
					     const DOFVector<double>* vec,
					     std::vector<double>& values)
  {
    FUNCNAME("MeshStructure::getMeshStructureValues()");

427
    TEST_EXIT_DBG(vec)("No DOFVector defined!\n");
428 429 430 431
  
    const FiniteElemSpace *feSpace = vec->getFeSpace();
    Mesh *mesh = feSpace->getMesh();
    bool feSpaceHasNonVertexDofs = (feSpace->getBasisFcts()->getDegree() > 1);
432 433
    values.clear();

434
    ElementDofIterator elDofIter(feSpace);
435 436 437 438
    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirstOneMacro(mesh, macroElIndex, -1, 
						 Mesh::CALL_EVERY_EL_PREORDER);
    while (elInfo) {
439
      // For the macro element the mesh structure code stores all vertex values.
440
      if (elInfo->getLevel() == 0)
441 442
	for (int i = 0; i < mesh->getGeo(VERTEX); i++)
	  values.push_back((*vec)[elInfo->getElement()->getDof(i, 0)]);
443

444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
      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 = 
	  elInfo->getElement()->getChild(0)->getDof(mesh->getDim(), 0);
	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());
	}
      }
463 464 465 466 467 468

      elInfo = stack.traverseNext(elInfo);
    }
  }


469
  void MeshStructure::setMeshStructureValues(int macroElIndex,
470 471 472 473 474
					     DOFVector<double>* vec,
					     const std::vector<double>& values)
  {
    FUNCNAME("MeshStructure::setMeshStructureValues()");

475
    TEST_EXIT_DBG(vec)("No DOFVector defined!\n");
476

477 478 479
    const FiniteElemSpace *feSpace = vec->getFeSpace();
    Mesh *mesh = feSpace->getMesh();
    bool feSpaceHasNonVertexDofs = (feSpace->getBasisFcts()->getDegree() > 1);
480 481
    unsigned int counter = 0;

482 483 484 485
    TEST_EXIT_DBG(static_cast<int>(values.size()) >= mesh->getGeo(VERTEX))
      ("Should not happen!\n");

    ElementDofIterator elDofIter(feSpace);
486 487 488 489
    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirstOneMacro(mesh, macroElIndex, -1, 
						 Mesh::CALL_EVERY_EL_PREORDER);
    while (elInfo) {
490
      // For the macro element all vertex nodes are set first.
491
      if (elInfo->getLevel() == 0)
492
	for (int i = 0; i < mesh->getGeo(VERTEX); i++)
493
	  (*vec)[elInfo->getElement()->getDof(i, 0)] = values[counter++];      
494

495
      if (!elInfo->getElement()->isLeaf()) {
496 497
	// If no leaf element set the value of the "new" DOF that is created
	// by bisectioning of this element.
498 499 500 501
	TEST_EXIT_DBG(counter < values.size())("Should not happen!\n");
	
	(*vec)[elInfo->getElement()->getChild(0)->getDof(mesh->getDim(), 0)] =
	  values[counter++];      
502 503 504 505 506 507 508 509 510 511 512
      } 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());
	}
513 514 515 516 517
      }

      elInfo = stack.traverseNext(elInfo);
    }
      
518
    TEST_EXIT_DBG(values.size() == counter)("Should not happen!\n");
519 520
  }

521
}