MacroReader.cc 45.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/******************************************************************************
 *
 * AMDiS - Adaptive multidimensional simulations
 *
 * Copyright (C) 2013 Dresden University of Technology. All Rights Reserved.
 * Web: https://fusionforge.zih.tu-dresden.de/projects/amdis
 *
 * Authors: 
 * Simon Vey, Thomas Witkowski, Andreas Naumann, Simon Praetorius, et al.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 *
 * This file is part of AMDiS
 *
 * See also license.opensource.txt in the distribution.
 * 
 ******************************************************************************/
20 21


22 23 24 25
#include <string.h>
#include <map>
#include <iostream>
#include <fstream>
26 27 28
#include "MacroReader.h"
#include "MacroWriter.h"
#include "MacroElement.h"
29
#include "MacroInfo.h"
30 31 32 33 34
#include "Boundary.h"
#include "FiniteElemSpace.h"
#include "Mesh.h"
#include "FixVec.h"
#include "ElInfo.h"
35
#include "Initfile.h"
36 37 38 39
#include "DOFIterator.h"
#include "LeafData.h"
#include "VertexVector.h"

40
namespace AMDiS { namespace io {
41

42
  MacroInfo* MacroReader::readMacro(std::string filename, 
43
				    Mesh* mesh,
44
				    std::string periodicFile,
45 46
				    int check)
  {
47
    FUNCNAME("MacroReader::readMacro()");
48

49
    TEST_EXIT(filename != "")("no file specified; filename NULL pointer\n");  
50

Thomas Witkowski's avatar
Thomas Witkowski committed
51
    MacroInfo *macroInfo = new MacroInfo();
52 53
    macroInfo->readAMDiSMacro(filename, mesh);

54
    std::deque<MacroElement*>::iterator mel = macroInfo->mel.begin();
55 56 57
    int **melVertex = macroInfo->mel_vertex;
    WorldVector<double> *coords = macroInfo->coords;
    DegreeOfFreedom **dof = macroInfo->dof;
58 59

    // === read periodic data =================================
60
    if (periodicFile != "") {   
61 62
      FILE *file = fopen(periodicFile.c_str(), "r");
      TEST_EXIT(file)("can't open file %s\n", periodicFile.c_str());
63 64 65 66 67

      int n;
      int dim = mesh->getDim();

      int el1, el2;
Thomas Witkowski's avatar
Thomas Witkowski committed
68 69
      int *verticesEl1 = new int[dim];
      int *verticesEl2 = new int[dim];
70
      int mode = -1; // 0: drop dofs, 1: associate dofs
71
      int result = 0;
72
      BoundaryType boundaryType;
73
      MacroReader::PeriodicMap periodicMap;
74

75 76
      result = fscanf(file, "%*s %d", &n);
      result = fscanf(file, "%*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s");
77
   
78
      for (int i = 0; i < n; i++) {
79 80
	std::map<int, int> vertexMapEl1;
	std::map<int, int> vertexMapEl2;
81

82 83
	result = fscanf(file, "%d", &mode);
	TEST_EXIT(result == 1)("mode?\n");
84
      
85 86
	result = fscanf(file, "%d", &boundaryType);
	TEST_EXIT(result == 1)("boundaryType?\n");
87
      
88 89 90
	result = fscanf(file, "%d", &el1);
	TEST_EXIT(result == 1)("el1?\n");

91
	for (int j = 0; j < dim; j++) {
92 93
	  result = fscanf(file, "%d", &verticesEl1[j]);
	  TEST_EXIT(result == 1)("vertEl1[%d]\n", j);
94
	}
95 96
	result = fscanf(file, "%d", &el2);
	TEST_EXIT(result == 1)("el2?\n");
97
	for (int j = 0; j < dim; j++) {
98 99
	  result = fscanf(file, "%d", &verticesEl2[j]);
	  TEST_EXIT(result == 1)("vertEl2[%d]\n", j);
100
	}
101
	for (int j = 0; j < dim; j++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
102
	  if (mode == 0)
103
	    periodicMap.setEntry(melVertex[el1][verticesEl1[j]], 
Thomas Witkowski's avatar
Thomas Witkowski committed
104
				 melVertex[el2][verticesEl2[j]]);	  
105 106 107 108 109 110
	  vertexMapEl1[verticesEl1[j]] = verticesEl2[j];
	  vertexMapEl2[verticesEl2[j]] = verticesEl1[j];
	}

	// calculate sides of periodic vertices
	int sideEl1 = 0, sideEl2 = 0;
111
	if (dim == 1) {
112 113 114
	  sideEl1 = verticesEl1[0];
	  sideEl2 = verticesEl2[0];
	} else {
115
	  for (int j = 0; j < dim + 1; j++) {
116 117 118
	    sideEl1 += j;
	    sideEl2 += j;
	  }
119
	  for (int j = 0; j < dim; j++) {
120 121 122 123
	    sideEl1 -= verticesEl1[j];
	    sideEl2 -= verticesEl2[j];
	  }
	}
124

125
	// create periodic info
126 127
	DimVec<WorldVector<double> > periodicCoordsEl1(dim - 1, NO_INIT);
	DimVec<WorldVector<double> > periodicCoordsEl2(dim - 1, NO_INIT);
128

129 130
	Element *element1 = const_cast<Element*>((*(mel + el1))->getElement());
	Element *element2 = const_cast<Element*>((*(mel + el2))->getElement());
131
     
132
	// for all vertices of this side
133
	for (int j = 0; j < dim; j++) {
134 135
	  periodicCoordsEl1[element1->getPositionOfVertex(sideEl1, verticesEl1[j])] = 
	    coords[melVertex[el2][vertexMapEl1[verticesEl1[j]]]];
136

137 138 139
	  periodicCoordsEl2[element2->getPositionOfVertex(sideEl2, verticesEl2[j])] =
	    coords[melVertex[el1][vertexMapEl2[verticesEl2[j]]]];
	}
140
	     
141 142 143 144
	// decorate leaf data
	ElementData *ld1 = element1->getElementData();
	ElementData *ld2 = element2->getElementData();

145 146 147 148 149 150 151 152
	TEST_EXIT_DBG(ld1)
	  ("Should not happen: no element data pointer in macro element %d!\n",
	   element1->getIndex());

	TEST_EXIT_DBG(ld2)
	  ("Should not happen: no element data pointer in macro element %d!\n",
	   element2->getIndex());

153 154 155 156
	LeafDataPeriodic *ldp1 = 
	  dynamic_cast<LeafDataPeriodic*>(ld1->getElementData(PERIODIC));
	LeafDataPeriodic *ldp2 = 
	  dynamic_cast<LeafDataPeriodic*>(ld2->getElementData(PERIODIC));
157 158

	if (!ldp1) {
Thomas Witkowski's avatar
Thomas Witkowski committed
159
	  ldp1 = new LeafDataPeriodic(ld1);
160 161 162 163
	  element1->setElementData(ldp1);
	}

	if (!ldp2) {
Thomas Witkowski's avatar
Thomas Witkowski committed
164
	  ldp2 = new LeafDataPeriodic(ld2);
165 166 167
	  element2->setElementData(ldp2);
	}

168 169
	ldp1->addPeriodicInfo(mode, boundaryType, sideEl1, &periodicCoordsEl1);
	ldp2->addPeriodicInfo(mode, boundaryType, sideEl2, &periodicCoordsEl2);
170 171 172

	if (mode != 0) {
	  VertexVector *associated = mesh->periodicAssociations[boundaryType];
173

174
	  if (!associated) {
Thomas Witkowski's avatar
Thomas Witkowski committed
175
	    associated = new VertexVector(mesh->getVertexAdmin(), "vertex vector");
176 177
	    mesh->periodicAssociations[boundaryType] = associated;
	    VertexVector::Iterator it(associated, ALL_DOFS);
178
	    for (it.reset2(); !it.end(); ++it)
179 180 181
	      *it = it.getDOFIndex();
	  }

182
	  for (int j = 0; j < dim; j++) {
183 184 185 186 187 188 189 190
	    (*associated)[melVertex[el1][verticesEl1[j]]] =
	      melVertex[el2][vertexMapEl1[verticesEl1[j]]];
	    (*associated)[melVertex[el2][verticesEl2[j]]] =
	      melVertex[el1][vertexMapEl2[verticesEl2[j]]];
	  }
	}
      }    

Thomas Witkowski's avatar
Thomas Witkowski committed
191 192
      delete [] verticesEl1;
      delete [] verticesEl2;
193 194

      // change periodic vertex dofs
195
      for (int i = 0; i < mesh->getNumberOfVertices(); i++) {
196
	if (periodicMap.getEntry(i) != -1) {
197
	  mesh->freeDof(dof[i], VERTEX);
198 199
	  dof[i] = dof[periodicMap.getEntry(i)];

200 201
	  std::map<BoundaryType, VertexVector*>::iterator assoc;
	  std::map<BoundaryType, VertexVector*>::iterator assocEnd =
202
	    mesh->periodicAssociations.end();
203

204
	  for (assoc = mesh->periodicAssociations.begin(); assoc != assocEnd; ++assoc) {
205 206 207 208 209

	    DegreeOfFreedom a = (*(assoc->second))[i];
	    if (a != i) {
	      (*(assoc->second))[i] = i;
	      (*(assoc->second))[a] = periodicMap.getEntry(i);
210
	    }
211 212
	  }

213 214 215
	}
      }

Thomas Witkowski's avatar
Thomas Witkowski committed
216
#if (DEBUG != 0)
217 218
      std::map<BoundaryType, VertexVector*>::iterator assoc;
      std::map<BoundaryType, VertexVector*>::iterator assocEnd =
219
	mesh->periodicAssociations.end();
Thomas Witkowski's avatar
Thomas Witkowski committed
220 221
      for (assoc = mesh->periodicAssociations.begin(); 
	   assoc != assocEnd; ++assoc)
Thomas Witkowski's avatar
Thomas Witkowski committed
222
	for (int i = 0; i < mesh->getNumberOfVertices(); i++)
223 224
	  if (i != (*(assoc->second))[i])
	    MSG("association %d: vertex %d -> vertex %d\n", 
Thomas Witkowski's avatar
Thomas Witkowski committed
225
		assoc->first, i, (*(assoc->second))[i]);
226

227 228 229 230
      for (int i = 0; i < mesh->getNumberOfVertices(); i++)
	if (periodicMap.getEntry(i) != -1)
	  MSG("identification : vertex %d is now vertex %d\n", 
	      i, periodicMap.getEntry(i));
Thomas Witkowski's avatar
Thomas Witkowski committed
231
#endif
232 233 234

    } // periodicFile

235

236 237
    // =========================================================

238 239 240
    for (int i = 0; i < mesh->getNumberOfMacros(); i++) {
      for (int k = 0; k < mesh->getGeo(VERTEX); k++) {
	(*(mel + i))->setCoord(k, coords[melVertex[i][k]]);
241

242
	const_cast<Element*>((*(mel + i))->getElement())->
243
	  setDof(k, dof[melVertex[i][k]]);
244
      }
245
    }
246

247
    if (!macroInfo->neigh_set) {
248
      TEST_EXIT(periodicFile == "")
249
	("periodic boundary condition => element neighbours must be set\n");
250
      computeNeighbours(mesh);
251
    } else {
252 253 254
      /****************************************************************************/
      /* fill MEL oppVertex values when reading neighbour information form file  */
      /****************************************************************************/
255

256 257 258 259 260
      for (int i = 0; i < mesh->getNumberOfMacros(); i++) {
	for (int k = 0; k < mesh->getGeo(NEIGH); k++) {
	  MacroElement *neigh = const_cast<MacroElement*>(mel[i]->getNeighbour(k));

	  if (neigh) {
261 262
	    int j = 0;
	    for (; j < mesh->getGeo(NEIGH); j++)
263 264
	      if (neigh->getNeighbour(j) == *(mel + i))  
		break;
265
	
266 267 268 269 270
	    TEST_EXIT(j < mesh->getGeo(NEIGH))("el %d no neighbour of neighbour %d\n", 
					       mel[i]->getIndex(), neigh->getIndex());
	    mel[i]->setOppVertex(k, j);
	  } else {
	    mel[i]->setOppVertex(k, -1);
271
	  }
272
	}
273
      }
274
    }
275

276 277
    if (!macroInfo->bound_set)
      macroInfo->dirichletBoundary();    
278
  
279
    if (mesh->getDim() > 1)
280 281 282 283 284 285
      boundaryDOFs(mesh);

    // initial boundary projections
    int numFaces = mesh->getGeo(FACE);
    int dim = mesh->getDim();
    mel = mesh->firstMacroElement();
286
    for (int i = 0; i < mesh->getNumberOfLeaves(); i++) {
287
      MacroElement *macroEl = *(mel + i);
288
      Projection *projector = macroEl->getProjection(0);
289
      if (projector && projector->getType() == VOLUME_PROJECTION) {
Thomas Witkowski's avatar
Thomas Witkowski committed
290 291
	for (int j = 0; j <= dim; j++)
	  projector->project(macroEl->getCoord(j));	
292
      } else {
293
	for (int j = 0; j < mesh->getGeo(EDGE); j++) {
294
	  projector = macroEl->getProjection(numFaces + j);
295
	  if (projector) {
296 297 298 299 300 301 302 303 304 305 306
	    int vertex0 = Global::getReferenceElement(dim)->getVertexOfEdge(j, 0);
	    int vertex1 = Global::getReferenceElement(dim)->getVertexOfEdge(j, 1);
	    projector->project(macroEl->getCoord(vertex0));
	    projector->project(macroEl->getCoord(vertex1));
	  }
	}
      }
    }

    macroInfo->fillBoundaryInfo(mesh);

307
    if (mesh->getNumberOfDofs(CENTER)) {
308
      for (int i = 0; i < mesh->getNumberOfMacros(); i++)
309
	const_cast<Element*>(mel[i]->getElement())->
310
	  setDof(mesh->getNode(CENTER), mesh->getDof(CENTER));
311 312
    }

313
    // === Domain size ===
314

315
    WorldVector<double> x_min, x_max;
316

317 318 319 320 321 322 323
    for (int j = 0; j < Global::getGeo(WORLD); j++) {
      x_min[j] =  1.E30;
      x_max[j] = -1.E30;
    }

    for (int i = 0; i < mesh->getNumberOfVertices(); i++) {
      for (int j = 0; j < Global::getGeo(WORLD); j++) {
324 325
	x_min[j] = std::min(x_min[j], coords[i][j]);
	x_max[j] = std::max(x_max[j], coords[i][j]);
326 327
      }
    }
328

329
    for (int j = 0; j < Global::getGeo(WORLD); j++)
330 331 332 333 334
      mesh->setDiameter(j, x_max[j] - x_min[j]);

    if (check) {
      checkMesh(mesh);

Thomas Witkowski's avatar
Thomas Witkowski committed
335 336
      if (mesh->getDim() > 1)
	macroTest(mesh);
337 338
    }

339
    return macroInfo;
340 341 342 343 344
  }


  void MacroReader::computeNeighbours(Mesh *mesh)
  {
345
    FUNCNAME("MacroReader::computeNeighbours()");
346

347
    int dim = mesh->getDim();
348
    FixVec<DegreeOfFreedom*, DIMEN> dof(dim, NO_INIT);
349

350 351 352
    for (int i = 0; i < mesh->getNumberOfLeaves(); i++) {
      for (int k = 0; k < mesh->getGeo(NEIGH); k++) {
	mesh->getMacroElement(i)->setOppVertex(k, AMDIS_UNDEFINED);
353
	mesh->getMacroElement(i)->setNeighbour(k, NULL);
354
      }
355
    }
356

357 358 359
    for (int i = 0; i < mesh->getNumberOfLeaves(); i++) {
      for (int k = 0; k < mesh->getGeo(NEIGH); k++) {
	if (mesh->getMacroElement(i)->getBoundary(k) != INTERIOR) {
360
	  mesh->getMacroElement(i)->setNeighbour(k, NULL);
361 362 363
	  mesh->getMacroElement(i)->setOppVertex(k, -1);
	  continue;
	}
364

365 366 367
	if (mesh->getMacroElement(i)->getOppVertex(k) == AMDIS_UNDEFINED) {
	  if (dim == 1) {
	    dof[0] = const_cast<DegreeOfFreedom*>(mesh->getMacroElement(i)->
368
						  getElement()->getDof(k));
369 370 371 372
	  } else {
	    for (int l = 0; l < dim; l++)
	      dof[l] = const_cast<DegreeOfFreedom*>(mesh->getMacroElement(i)->
						    getElement()->
373
						    getDof((k + l + 1) % (dim + 1)));
374
	  }
375 376 377 378 379 380 381 382 383 384 385 386 387
	  
	  int j = 0;
	  for (j = i + 1; j < mesh->getNumberOfLeaves(); j++) {
	    int m = mesh->getMacroElement(j)->getElement()->oppVertex(dof);
	    if (m != -1) {
	      mesh->getMacroElement(i)->setNeighbour(k, mesh->getMacroElement(j));
	      mesh->getMacroElement(j)->setNeighbour(m, mesh->getMacroElement(i));
	      mesh->getMacroElement(i)->setOppVertex(k, m);
	      mesh->getMacroElement(j)->setOppVertex(m, k);
	      break;
	    }
	  }

388 389 390 391 392 393 394
	  if (j >= mesh->getNumberOfLeaves()) {
	    std::cout << "----------- ERROR ------------" << std::endl;
	    std::cout << "Cannot find neighbour " << k << " of element " << i << std::endl;
	    std::cout << "  dim = " << dim << std::endl;
	    std::cout << "  coords of element = ";
	    for (int l = 0; l <= dim; l++) {
	      std::cout << mesh->getMacroElement(i)->getCoord(l);
395 396
	      if (l < dim)
		std::cout << " / ";	      
397 398 399
	    }
	    std::cout << std::endl;
	    std::cout << "  dofs = ";
400 401
	    for (int l = 0; l < dim; l++)
	      std::cout << *(dof[l]) << " ";	    
402 403 404 405
	    std::cout << std::endl;

	    ERROR_EXIT("\n");
	  }    
406
	}
407
      }
408
    }
409 410 411 412 413 414 415 416 417 418 419
  }


  /****************************************************************************/
  /*  boundaryDOFs:                                                           */
  /*  adds dof's at the edges of a given macro triangulation and calculates   */
  /*  the number of edges                                                     */
  /****************************************************************************/

  void MacroReader::boundaryDOFs(Mesh *mesh)
  {
420 421 422 423 424
    FUNCNAME("Mesh::boundaryDOFs()");

    int lnode = mesh->getNode(EDGE);
    int k, lne = mesh->getNumberOfLeaves();
    int max_n_neigh = 0, n_neigh, ov;
425
    std::deque<MacroElement*>::iterator mel;
426
    const MacroElement* neigh;
427
    DegreeOfFreedom *dof;
428 429 430 431 432 433

    mesh->setNumberOfEdges(0);
    mesh->setNumberOfFaces(0);

    int dim = mesh->getDim();

Thomas Witkowski's avatar
Thomas Witkowski committed
434
    switch (dim) {
435
    case 2:
436
      for (mel = mesh->firstMacroElement(); mel != mesh->endOfMacroElements(); mel++) {
437 438 439 440 441 442
	// check for periodic boundary
	Element *el = const_cast<Element*>((*mel)->getElement());
	ElementData *ed = el->getElementData(PERIODIC);

	DimVec<bool> periodic(dim, DEFAULT_VALUE, false);

443
	if (ed) {
444
	  std::list<LeafDataPeriodic::PeriodicInfo> &periodicInfos = 
445
	    dynamic_cast<LeafDataPeriodic*>(ed)->getInfoList();
446 447
	  std::list<LeafDataPeriodic::PeriodicInfo>::iterator it;
	  std::list<LeafDataPeriodic::PeriodicInfo>::iterator end = periodicInfos.end();
Thomas Witkowski's avatar
Thomas Witkowski committed
448 449
	  for (it = periodicInfos.begin(); it != end; ++it)
	    if (it->type != 0)
450 451 452
	      periodic[it->elementSide] = true;
	}

453
	for (int i = 0; i < mesh->getGeo(NEIGH); i++) {
454
	  if (!(*mel)->getNeighbour(i) || 
455 456 457 458
	      ((*mel)->getNeighbour(i)->getIndex() < (*mel)->getIndex())) {

	    mesh->incrementNumberOfEdges(1);

459
	    if (mesh->getNumberOfDofs(EDGE)) {
460 461
	      dof = mesh->getDof(EDGE);
	      el->setDof(lnode + i, dof);
462 463
      
	      if ((*mel)->getNeighbour(i)) {
Thomas Witkowski's avatar
Thomas Witkowski committed
464 465
		Element *neigh = 
		  const_cast<Element*>((*mel)->getNeighbour(i)->getElement());
466

Thomas Witkowski's avatar
Thomas Witkowski committed
467
		if (periodic[i])
468
		  neigh->setDof(lnode + (*mel)->getOppVertex(i), mesh->getDof(EDGE));
Thomas Witkowski's avatar
Thomas Witkowski committed
469
		else
470
		  neigh->setDof(lnode + (*mel)->getOppVertex(i), dof);		
471
	      }
472 473
	    }
	  }  
474 475 476 477 478 479
	}
      }
      break;
    case 3:
      lnode = mesh->getNode(FACE);
      mel = mesh->firstMacroElement();
480
      for (int i = 0; i < lne; i++) {
481 482 483 484 485 486 487

	// check for periodic boundary
	Element *el = const_cast<Element*>((*(mel+i))->getElement());
	ElementData *ed = el->getElementData(PERIODIC);

	DimVec<bool> periodic(dim, DEFAULT_VALUE, false);
      
488
	if (ed) {
489
	  std::list<LeafDataPeriodic::PeriodicInfo> &periodicInfos = 
490
	    dynamic_cast<LeafDataPeriodic*>(ed)->getInfoList();
491 492
	  std::list<LeafDataPeriodic::PeriodicInfo>::iterator it;
	  std::list<LeafDataPeriodic::PeriodicInfo>::iterator end = periodicInfos.end();
493 494
	  for (it = periodicInfos.begin(); it != end; ++it)
	    if (it->type != 0)
495 496 497
	      periodic[it->elementSide] = true;
	}

498 499
	for (k = 0; k < mesh->getGeo(EDGE); k++) {
	  // === Check for not counted edges. ===
500 501
	  n_neigh = 1;

Thomas Witkowski's avatar
Thomas Witkowski committed
502
	  if (newEdge(mesh, (*(mel + i)), k, &n_neigh)) {
503
	    mesh->incrementNumberOfEdges(1);
504
	    max_n_neigh = std::max(max_n_neigh, n_neigh);
505 506 507 508
	  }
	}
      
	for (k = 0; k < mesh->getGeo(NEIGH); k++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
509
	  neigh = (*(mel + i))->getNeighbour(k);
510
	  // === Face is counted and dof is added by the element with bigger index. ===
Thomas Witkowski's avatar
Thomas Witkowski committed
511 512
	  if (neigh && (neigh->getIndex() > (*(mel + i))->getIndex()))  
	    continue;
513 514 515
	
	  mesh->incrementNumberOfFaces(1);
	
516
	  if (mesh->getNumberOfDofs(FACE)) {
517
	    TEST_EXIT(!(*(mel + i))->getElement()->getDof(lnode + k))
518
	      ("dof %d on element %d already set\n", 
Thomas Witkowski's avatar
Thomas Witkowski committed
519
	       lnode + k, (*(mel + i))->getIndex());
520
	  
521
	    const_cast<Element*>((*(mel + i))->getElement())->setDof(lnode + k,
522
								     mesh->getDof(FACE));
523 524

	    if (neigh) {
Thomas Witkowski's avatar
Thomas Witkowski committed
525
	      ov = (*(mel + i))->getOppVertex(k);
526
	      TEST_EXIT(!neigh->getElement()->getDof(lnode + ov))
527
		("dof %d on neighbour %d already set\n", 
Thomas Witkowski's avatar
Thomas Witkowski committed
528
		 lnode + ov, neigh->getIndex());
529 530
	    
	      Element *neighEl = 
Thomas Witkowski's avatar
Thomas Witkowski committed
531
		const_cast<Element*>((*(mel + i))->getNeighbour(k)->getElement());
532

Thomas Witkowski's avatar
Thomas Witkowski committed
533
	      if (periodic[k])
534
		neighEl->setDof(lnode+ov, mesh->getDof(FACE));
Thomas Witkowski's avatar
Thomas Witkowski committed
535
	      else
536
		neighEl->setDof(lnode+ov, const_cast<int*>((*(mel + i))->getElement()->
537
							   getDof(lnode + k)));	      
538 539 540 541 542
	    }
	  }
	}
      }
      break;
Thomas Witkowski's avatar
Thomas Witkowski committed
543 544
    default: 
      ERROR_EXIT("invalid dim\n");
545 546
    }
    
547
    if (3 == dim)
Thomas Witkowski's avatar
Thomas Witkowski committed
548
      mesh->setMaxEdgeNeigh(std::max(8, 2 * max_n_neigh));
549
    else
Thomas Witkowski's avatar
Thomas Witkowski committed
550
      mesh->setMaxEdgeNeigh(dim - 1);    
551 552 553 554 555 556 557 558
  }

  /* 
     testet mesh auf auftretende Zyklen
  
     wenn Zyklus auftritt:
     ordnet Eintraege in MacroElement-Struktur um, so dass kein Zyklus auftritt
     erzeugt neue Macro-Datei nameneu mit umgeordnetem Netz 
559
     (wenn nameneu=NULL wird keine MAcro-Datei erzeugt)
560 561
  */      

Thomas Witkowski's avatar
Thomas Witkowski committed
562
  void MacroReader::macroTest(Mesh *mesh)
563
  {
564
    FUNCNAME("MacroReader::macroTest()");
565
   
566 567 568
    int i = macrotest(mesh);

    if (i >= 0) {
Thomas Witkowski's avatar
Thomas Witkowski committed
569 570
      WARNING("There is a cycle beginning in macro element %d\n", i);
      WARNING("Entries in MacroElement structures get reordered\n");
571
      umb(NULL, mesh, umbVkantMacro);
572
    }
573 574 575 576 577 578 579 580 581 582 583 584 585 586
  }
  
  /****************************************************************************/
  /*  macro_test():                              Author: Thomas Kastl (1998)  */
  /****************************************************************************/
  /*
    testet mesh auf auftretende Zyklen
  
    wenn mesh zyklenfrei -> -1
    sonst ->  globaler Index des Macroelementes bei dem ein Zyklus beginnt 
  */

  int MacroReader::macrotest(Mesh *mesh)
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
587 588
    std::deque<MacroElement*>::const_iterator macro, mac;
    int flg = 0;
589
    int dim = mesh->getDim();
Thomas Witkowski's avatar
Thomas Witkowski committed
590 591
    int *test = new int[mesh->getNumberOfMacros()];
    int *zykl = new int[mesh->getNumberOfMacros()];
592
 
Thomas Witkowski's avatar
Thomas Witkowski committed
593
    for (int i = 0; i < mesh->getNumberOfMacros(); i++)
594
      test[i] = 0;
595

Thomas Witkowski's avatar
Thomas Witkowski committed
596 597
    int zykstart = -1;
    std::deque<MacroElement*>::const_iterator macrolfd = mesh->firstMacroElement();
598

599 600 601 602
    while (macrolfd != mesh->endOfMacroElements()) {
      if (test[(*macrolfd)->getIndex()] == 1) {
	macrolfd++;
      } else {
603 604
	for (int i = 0; i < mesh->getNumberOfMacros(); i++)
	  zykl[i] = 0;	
605
    
606 607 608 609 610 611 612 613
	macro = macrolfd;
	flg = 2;
	do {
	  if (zykl[(*macro)->getIndex()] == 1) {
	    flg = 0;
	    zykstart = (*macro)->getIndex();
	  } else {
	    zykl[(*macro)->getIndex()] = 1;
614
      
615 616
	    if (test[(*macro)->getIndex()] == 1) {
	      flg = 1;
617
	    } else if ((*macro)->getNeighbour(dim) == NULL) {
618 619
	      flg = 1;
	      test[(*macro)->getIndex()] = 1;
Thomas Witkowski's avatar
Thomas Witkowski committed
620
	    } else if ((*macro) == (*macro)->getNeighbour(dim)->getNeighbour(dim)) {
621 622 623 624
	      flg = 1;
	      test[(*macro)->getIndex()] = 1;
	      test[(*macro)->getNeighbour(dim)->getIndex()] = 1;
	    } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
625 626
	      for (mac = mesh->firstMacroElement(); 
		   (*mac) != (*macro)->getNeighbour(dim); mac++);
627 628 629 630
	      macro = mac;
	    } 
	  }	  
	} while(flg == 2);
631
 
632
	if (flg == 1)
633
	  macrolfd++;
634 635
	else 
	  macrolfd=mesh->endOfMacroElements();	
636
      }
637
    }
638
  
Thomas Witkowski's avatar
Thomas Witkowski committed
639 640
    delete [] zykl;
    delete [] test;
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
 
    return zykstart;
  }

  //   waehlt geeignete Verfeinerungskanten, so dass kein Zyklus auftritt (recumb)

  //   ele     Integer-Vektor der Dimension Anzahl der Macro-Elemente
  //           zur Speicherung der neuen Verfeinerungskanten
  //           (wird nur benoetigt, wenn umbvk=umb_vkant_macrodat) 
  
  //   umbvk   Fkt. zur Neuordnung der Verfeinerungskanten
  //           = umb_vkant_macro :
  //               Eintraege in MacroElement-Struktur und Eintraege in macro->el
  //               werden tatsaechlich umgeordnet
  //               -> ALBERT-Routine write_macro kann zum erzeugen einer
  //                  neuen Macro-Datei angewendet werden 
  //           = umb_vkant_macrodat :
  //               Eintraege werden nicht veraendert, es werden nur die lokalen
  //               Indices der Kanten, die zu Verfeinerungskanten werden im
  //               Integer-Vektor ele abgespeichert
  //               -> print_Macrodat zur Erzeugung einer zyklenfreien neuen
  //                  Macro-Datei kann angewendet werden

  void MacroReader::umb(int *ele, Mesh *mesh,
Thomas Witkowski's avatar
Thomas Witkowski committed
665
			void (*umbvk)(Mesh*, MacroElement*, int, int*))
666
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
667
    int *test = new int[mesh->getNumberOfMacros()];
668
  
Thomas Witkowski's avatar
Thomas Witkowski committed
669 670
    for (int i = 0; i < static_cast<int>(mesh->getNumberOfMacros()); i++)
      test[i] = 0;
671

672
    recumb(mesh, (*mesh->firstMacroElement()), NULL, test, 0, 0, ele, umbvk);
673

Thomas Witkowski's avatar
Thomas Witkowski committed
674
    delete [] test;
675 676 677 678 679
  }

  bool MacroReader::newEdge(Mesh *mesh, MacroElement *mel,
			    int mel_edge_no, int *n_neigh)
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
680 681
    FUNCNAME("MacroElement::newEdge()"); 
    MacroElement *nei;
682
    const DegreeOfFreedom *dof[2];
683
    DegreeOfFreedom *edge_dof = NULL;
Thomas Witkowski's avatar
Thomas Witkowski committed
684 685
    int j, k, opp_v, node = 0;
    BoundaryType lbound = INTERIOR;
686
    Projection *lproject = NULL;
Thomas Witkowski's avatar
Thomas Witkowski committed
687
    const int max_no_list_el = 100;
688 689 690 691
    BoundaryType *list_bound[100];
    Projection **list_project[100];
    Element *el = const_cast<Element*>(mel->getElement());
    int edge_no = mel_edge_no;
Thomas Witkowski's avatar
Thomas Witkowski committed
692
    static int next_el[6][2] = {{3,2},{1,3},{1,2},{0,3},{0,2},{0,1}};
693 694
    int vertices = mesh->getGeo(VERTEX);

Thomas Witkowski's avatar
Thomas Witkowski committed
695
    int mel_index = mel->getIndex();
696 697 698 699

    list_bound[0] = &(mel->boundary[mesh->getGeo(FACE)+edge_no]);
    list_project[0] = &(mel->projection[mesh->getGeo(FACE)+edge_no]);

700
    if (mesh->getNumberOfDofs(EDGE)) {
701
      node = mesh->getNode(EDGE);
702
      if (el->getDof(node+edge_no)) {
703 704 705 706 707 708
	/****************************************************************************/
	/*  edge was counted by another macro element and dof was added on the      */
	/*  complete patch                                                          */
	/****************************************************************************/
	return false;
      } else {
709 710
	edge_dof = mesh->getDof(EDGE);
	el->setDof(node+edge_no, edge_dof);
711 712 713
      }
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
714
    for (j = 0; j < 2; j++)
715
      dof[j] = el->getDof(el->getVertexOfEdge(edge_no, j));
Thomas Witkowski's avatar
Thomas Witkowski committed
716
    
717 718 719 720 721 722 723 724 725 726 727 728
    /****************************************************************************/
    /*  first look for all neighbours in one direction until a boundary is      */
    /*  reached :-( or we are back to mel :-)                                   */
    /*  if the index of a neighbour is smaller than the element index, the edge */
    /*  is counted by this neighbour, return 0.                                 */
    /*  If we are back to element, return 1, to count the edge                  */
    /****************************************************************************/

    nei = mel->getNeighbour(next_el[edge_no][0]);
    opp_v = mel->getOppVertex(next_el[edge_no][0]);


729
    if (mel->getBoundary(next_el[edge_no][0])) {
730 731 732 733 734 735
      lbound = newBound(mel->getBoundary(next_el[edge_no][0]), lbound);
      lproject = mel->getProjection(next_el[edge_no][0]);
    }

    while (nei  &&  nei != mel) {
      for (j = 0; j < vertices; j++)
736
	if (nei->getElement()->getDof(j) == dof[0])  break;
737
      for (k = 0; k < vertices; k++)
738
	if (nei->getElement()->getDof(k) == dof[1])  break;
739 740

      // check for periodic boundary
741
      if (j == 4 || k == 4) {
742
	nei = NULL;
743 744 745
	break;
      }

746
      if (mesh->getNumberOfDofs(EDGE)) {
747 748
	TEST_EXIT(nei->index > mel_index)
	  ("neighbour index %d < element index %d\n", nei->getIndex(), mel_index);
749
      }
750

751
      if (!mesh->getNumberOfDofs(EDGE) && nei->getIndex() < mel_index) 
752
	return false;
753
    
754
      edge_no = Tetrahedron::edgeOfDofs[j][k];
755 756 757 758 759 760 761 762 763 764

      TEST_EXIT(*n_neigh < max_no_list_el)
	("too many neigbours for local list\n");

      list_bound[(*n_neigh)] = 
	&(nei->boundary[mesh->getGeo(FACE)+edge_no]);

      list_project[(*n_neigh)++] = 
	&(nei->projection[mesh->getGeo(FACE)+edge_no]);

765
      if (mesh->getNumberOfDofs(EDGE))
766
	nei->element->setDof(node+edge_no,edge_dof);      
767

768
      if (next_el[edge_no][0] != opp_v) {
769
	if (nei->getBoundary(next_el[edge_no][0])) {
770 771 772 773 774 775
	  lbound = newBound(nei->getBoundary(next_el[edge_no][0]), lbound);
	  Projection *neiProject = nei->getProjection(next_el[edge_no][0]);
	  if (!lproject) {
	    lproject = neiProject;
	  } else {
	    if (neiProject && (lproject->getID() < neiProject->getID()))
776 777 778
	      lproject = neiProject;
	  }
	}
779 780 781 782 783 784 785 786 787 788
	opp_v = nei->getOppVertex(next_el[edge_no][0]);
	nei = nei->getNeighbour(next_el[edge_no][0]);
      } else {
	if (nei->getBoundary(next_el[edge_no][1])) {
	  lbound = newBound(nei->getBoundary(next_el[edge_no][1]), lbound);
	  Projection *neiProject = nei->getProjection(next_el[edge_no][1]);	
	  if (!lproject) {
	    lproject = neiProject;
	  } else {
	    if (neiProject && (lproject->getID() < neiProject->getID()))
789 790 791
	      lproject = neiProject;
	  }
	}
792 793 794
	opp_v = nei->getOppVertex(next_el[edge_no][1]);
	nei = nei->getNeighbour(next_el[edge_no][1]);
      }
795 796
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818
    if (!nei) {
      /****************************************************************************/
      /*  while looping around the edge the domain's boundary was reached. Now,   */
      /*  loop in the other direction to the domain's boundary		    */
      /****************************************************************************/
      edge_no = mel_edge_no;
      
      nei = mel->getNeighbour(next_el[edge_no][1]);
      opp_v = mel->getOppVertex(next_el[edge_no][1]);
      if (mel->getBoundary(next_el[edge_no][1])) {
	lbound = newBound(mel->getBoundary(next_el[edge_no][1]), lbound); 
	Projection *neiProject =  mel->getProjection(next_el[edge_no][1]);
	if (!lproject) {
	  lproject = neiProject;
	} else {
	  if (neiProject && (lproject->getID() < neiProject->getID()))
	    lproject = neiProject;	  
	}
      }
      
      while (nei) {
	for (j = 0; j < vertices; j++)
819
	  if (nei->getElement()->getDof(j) == dof[0])  break;
Thomas Witkowski's avatar
Thomas Witkowski committed
820
	for (k = 0; k < vertices; k++)
821
	  if (nei->getElement()->getDof(k) == dof[1])  break;
822
	
Thomas Witkowski's avatar
Thomas Witkowski committed
823 824 825 826
	// check for periodic boundary
	if (j == 4 || k == 4)
	  return false;
	
827
	if (mesh->getNumberOfDofs(EDGE)) {
Thomas Witkowski's avatar
Thomas Witkowski committed
828 829 830
	  TEST_EXIT(nei->getIndex() > mel_index)
	    ("neighbour index %d < element index %d\n", nei->getIndex(),
	     mel_index);
831
	}
Thomas Witkowski's avatar
Thomas Witkowski committed
832 833 834 835
	
	if (nei->getIndex() < mel_index)  
	  return false;
	
836
	edge_no = Tetrahedron::edgeOfDofs[j][k];
Thomas Witkowski's avatar
Thomas Witkowski committed
837 838 839 840 841
	
	TEST_EXIT(*n_neigh < max_no_list_el)("too many neigbours for local list\n");
	
	list_bound[(*n_neigh)] = &(nei->boundary[mesh->getGeo(FACE) + edge_no]);
	list_project[(*n_neigh)++] = &(nei->projection[mesh->getGeo(FACE) + edge_no]);
842

843
	if (mesh->getNumberOfDofs(EDGE)) {
844
	  TEST_EXIT(!nei->getElement()->getDof(node+edge_no))
Thomas Witkowski's avatar
Thomas Witkowski committed
845 846 847
	    ("dof %d on element %d is already set, but not on element %d\n",
	     node + edge_no, nei->getIndex(), mel_index);
	  
848
	  nei->element->setDof(node+edge_no, edge_dof);
Thomas Witkowski's avatar
Thomas Witkowski committed
849
	}
850

Thomas Witkowski's avatar
Thomas Witkowski committed
851 852 853 854 855 856 857 858 859
	if (next_el[edge_no][0] != opp_v) {
	  if (nei->getBoundary(next_el[edge_no][0])) {
	    lbound = newBound(nei->getBoundary(next_el[edge_no][0]), lbound);
	    Projection *neiProject = nei->getProjection(next_el[edge_no][0]);
	    if (!lproject) {
	      lproject = neiProject;
	    } else {
	      if (neiProject &&( lproject->getID() < neiProject->getID()))
		lproject = neiProject;	      
860 861
	    }
	  }