MacroReader.cc 47.1 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
  
  void MacroReader::restoreMacroDofs(MacroInfo& macroInfo, int check)
  {
    FUNCNAME("MacroReader::restoreMacroDofs()");
    
    TEST_EXIT(macroInfo.isInitialized())
      ("Cannot set dof since macro info is not initialized.\n");
      
    Mesh* mesh = macroInfo.mesh;
    int nVertices = macroInfo.nVertices;
    int nElements = macroInfo.nElements;
    int **mel_vertex = macroInfo.mel_vertex;
    DegreeOfFreedom** dof = macroInfo.dof;
    
    TEST_EXIT(mesh->getNumberOfMacros() == nElements) 
      ("Mesh's current macro doesn't match the macro file.\n");
    
    mesh->setNumberOfElements(nElements);
    mesh->setNumberOfLeaves(nElements);
    mesh->setNumberOfVertices(nVertices);
    
    // Vertices dofs
    for (int i = 0; i < nVertices; i++)
      dof[i] = mesh->getDof(VERTEX);
    
    std::deque<MacroElement*>::iterator it = mesh->firstMacroElement();
    
    for (int i = 0; i < mesh->getNumberOfMacros(); i++) 
      for (int k = 0; k < mesh->getGeo(VERTEX); k++) 
	const_cast<Element*>((*(it + i))->getElement())->
	  setDof(k, dof[mel_vertex[i][k]]);
	  
    // Edge and face dofs
    if (mesh->getDim() > 1)
      boundaryDOFs(mesh);
    
    // Center dofs
    if (mesh->getNumberOfDofs(CENTER)) {
      it = mesh->firstMacroElement();
      
      for (int i = 0; i < mesh->getNumberOfMacros(); i++)
	const_cast<Element*>(it[i]->getElement())->
	  setDof(mesh->getNode(CENTER), mesh->getDof(CENTER));
    }
    
    if (check)
      checkMesh(mesh);
  }
89

90
  MacroInfo* MacroReader::readMacro(std::string filename, 
91
				    Mesh* mesh,
92
				    std::string periodicFile,
93
94
				    int check)
  {
95
    FUNCNAME("MacroReader::readMacro()");
96

97
    TEST_EXIT(filename != "")("no file specified; filename NULL pointer\n");  
98

Thomas Witkowski's avatar
Thomas Witkowski committed
99
    MacroInfo *macroInfo = new MacroInfo();
100
101
    macroInfo->readAMDiSMacro(filename, mesh);

102
    std::deque<MacroElement*>::iterator mel = macroInfo->mel.begin();
103
104
105
    int **melVertex = macroInfo->mel_vertex;
    WorldVector<double> *coords = macroInfo->coords;
    DegreeOfFreedom **dof = macroInfo->dof;
106
107

    // === read periodic data =================================
108
    if (periodicFile != "") {   
109
110
      FILE *file = fopen(periodicFile.c_str(), "r");
      TEST_EXIT(file)("can't open file %s\n", periodicFile.c_str());
111
112
113
114
115

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

      int el1, el2;
Thomas Witkowski's avatar
Thomas Witkowski committed
116
117
      int *verticesEl1 = new int[dim];
      int *verticesEl2 = new int[dim];
118
      int mode = -1; // 0: drop dofs, 1: associate dofs
119
      int result = 0;
120
      BoundaryType boundaryType;
121
      MacroReader::PeriodicMap periodicMap;
122

123
124
      result = fscanf(file, "%*s %d", &n);
      result = fscanf(file, "%*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s");
125
   
126
      for (int i = 0; i < n; i++) {
127
128
	std::map<int, int> vertexMapEl1;
	std::map<int, int> vertexMapEl2;
129

130
131
	result = fscanf(file, "%d", &mode);
	TEST_EXIT(result == 1)("mode?\n");
132
      
133
134
	result = fscanf(file, "%d", &boundaryType);
	TEST_EXIT(result == 1)("boundaryType?\n");
135
      
136
137
138
	result = fscanf(file, "%d", &el1);
	TEST_EXIT(result == 1)("el1?\n");

139
	for (int j = 0; j < dim; j++) {
140
141
	  result = fscanf(file, "%d", &verticesEl1[j]);
	  TEST_EXIT(result == 1)("vertEl1[%d]\n", j);
142
	}
143
144
	result = fscanf(file, "%d", &el2);
	TEST_EXIT(result == 1)("el2?\n");
145
	for (int j = 0; j < dim; j++) {
146
147
	  result = fscanf(file, "%d", &verticesEl2[j]);
	  TEST_EXIT(result == 1)("vertEl2[%d]\n", j);
148
	}
149
	for (int j = 0; j < dim; j++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
150
	  if (mode == 0)
151
	    periodicMap.setEntry(melVertex[el1][verticesEl1[j]], 
Thomas Witkowski's avatar
Thomas Witkowski committed
152
				 melVertex[el2][verticesEl2[j]]);	  
153
154
155
156
157
158
	  vertexMapEl1[verticesEl1[j]] = verticesEl2[j];
	  vertexMapEl2[verticesEl2[j]] = verticesEl1[j];
	}

	// calculate sides of periodic vertices
	int sideEl1 = 0, sideEl2 = 0;
159
	if (dim == 1) {
160
161
162
	  sideEl1 = verticesEl1[0];
	  sideEl2 = verticesEl2[0];
	} else {
163
	  for (int j = 0; j < dim + 1; j++) {
164
165
166
	    sideEl1 += j;
	    sideEl2 += j;
	  }
167
	  for (int j = 0; j < dim; j++) {
168
169
170
171
	    sideEl1 -= verticesEl1[j];
	    sideEl2 -= verticesEl2[j];
	  }
	}
172

173
	// create periodic info
174
175
	DimVec<WorldVector<double> > periodicCoordsEl1(dim - 1, NO_INIT);
	DimVec<WorldVector<double> > periodicCoordsEl2(dim - 1, NO_INIT);
176

177
178
	Element *element1 = const_cast<Element*>((*(mel + el1))->getElement());
	Element *element2 = const_cast<Element*>((*(mel + el2))->getElement());
179
     
180
	// for all vertices of this side
181
	for (int j = 0; j < dim; j++) {
182
183
	  periodicCoordsEl1[element1->getPositionOfVertex(sideEl1, verticesEl1[j])] = 
	    coords[melVertex[el2][vertexMapEl1[verticesEl1[j]]]];
184

185
186
187
	  periodicCoordsEl2[element2->getPositionOfVertex(sideEl2, verticesEl2[j])] =
	    coords[melVertex[el1][vertexMapEl2[verticesEl2[j]]]];
	}
188
	     
189
190
191
192
	// decorate leaf data
	ElementData *ld1 = element1->getElementData();
	ElementData *ld2 = element2->getElementData();

193
194
195
196
197
198
199
200
	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());

201
202
203
204
	LeafDataPeriodic *ldp1 = 
	  dynamic_cast<LeafDataPeriodic*>(ld1->getElementData(PERIODIC));
	LeafDataPeriodic *ldp2 = 
	  dynamic_cast<LeafDataPeriodic*>(ld2->getElementData(PERIODIC));
205
206

	if (!ldp1) {
Thomas Witkowski's avatar
Thomas Witkowski committed
207
	  ldp1 = new LeafDataPeriodic(ld1);
208
209
210
211
	  element1->setElementData(ldp1);
	}

	if (!ldp2) {
Thomas Witkowski's avatar
Thomas Witkowski committed
212
	  ldp2 = new LeafDataPeriodic(ld2);
213
214
215
	  element2->setElementData(ldp2);
	}

216
217
	ldp1->addPeriodicInfo(mode, boundaryType, sideEl1, &periodicCoordsEl1);
	ldp2->addPeriodicInfo(mode, boundaryType, sideEl2, &periodicCoordsEl2);
218
219
220

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

222
	  if (!associated) {
Thomas Witkowski's avatar
Thomas Witkowski committed
223
	    associated = new VertexVector(mesh->getVertexAdmin(), "vertex vector");
224
225
	    mesh->periodicAssociations[boundaryType] = associated;
	    VertexVector::Iterator it(associated, ALL_DOFS);
226
	    for (it.reset2(); !it.end(); ++it)
227
228
229
	      *it = it.getDOFIndex();
	  }

230
	  for (int j = 0; j < dim; j++) {
231
232
233
234
235
236
237
238
239
240
241
#ifdef DEBUG
	  {
		  unsigned initData(melVertex[el1][verticesEl1[j]]);
		  unsigned oldData((*associated)[melVertex[el1][verticesEl1[j]]]);
		  unsigned newData(melVertex[el2][vertexMapEl1[verticesEl1[j]]]);
		  if( initData != oldData && newData != oldData ) {
			  MSG("warning: element %d overwrites assoc index %d: %d -> %d\n", 
					  el1, initData, oldData, newData);
		  }
	  }
#endif
242
243
244
245
246
247
248
249
	    (*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
250
251
      delete [] verticesEl1;
      delete [] verticesEl2;
252
253

      // change periodic vertex dofs
254
      for (int i = 0; i < mesh->getNumberOfVertices(); i++) {
255
	if (periodicMap.getEntry(i) != -1) {
256
	  mesh->freeDof(dof[i], VERTEX);
257
258
	  dof[i] = dof[periodicMap.getEntry(i)];

259
260
	  std::map<BoundaryType, VertexVector*>::iterator assoc;
	  std::map<BoundaryType, VertexVector*>::iterator assocEnd =
261
	    mesh->periodicAssociations.end();
262

263
	  for (assoc = mesh->periodicAssociations.begin(); assoc != assocEnd; ++assoc) {
264
265
266
267
268

	    DegreeOfFreedom a = (*(assoc->second))[i];
	    if (a != i) {
	      (*(assoc->second))[i] = i;
	      (*(assoc->second))[a] = periodicMap.getEntry(i);
269
	    }
270
271
	  }

272
273
274
	}
      }

Thomas Witkowski's avatar
Thomas Witkowski committed
275
#if (DEBUG != 0)
276
277
      std::map<BoundaryType, VertexVector*>::iterator assoc;
      std::map<BoundaryType, VertexVector*>::iterator assocEnd =
278
	mesh->periodicAssociations.end();
Thomas Witkowski's avatar
Thomas Witkowski committed
279
280
      for (assoc = mesh->periodicAssociations.begin(); 
	   assoc != assocEnd; ++assoc)
Thomas Witkowski's avatar
Thomas Witkowski committed
281
	for (int i = 0; i < mesh->getNumberOfVertices(); i++)
282
283
	  if (i != (*(assoc->second))[i])
	    MSG("association %d: vertex %d -> vertex %d\n", 
Thomas Witkowski's avatar
Thomas Witkowski committed
284
		assoc->first, i, (*(assoc->second))[i]);
285

286
287
288
289
      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
290
#endif
291
292
293

    } // periodicFile

294

295
    // =========================================================
296
    // Set coordinate
297
298
299
    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]]);
300

301
	const_cast<Element*>((*(mel + i))->getElement())->
302
	  setDof(k, dof[melVertex[i][k]]);
303
      }
304
    }
305

306
    if (!macroInfo->neigh_set) {
307
      TEST_EXIT(periodicFile == "")
308
	("periodic boundary condition => element neighbours must be set\n");
309
      computeNeighbours(mesh);
310
    } else {
311
312
313
      /****************************************************************************/
      /* fill MEL oppVertex values when reading neighbour information form file  */
      /****************************************************************************/
314

315
316
317
318
319
      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) {
320
321
	    int j = 0;
	    for (; j < mesh->getGeo(NEIGH); j++)
322
323
	      if (neigh->getNeighbour(j) == *(mel + i))  
		break;
324
	
325
326
327
328
329
	    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);
330
	  }
331
	}
332
      }
333
    }
334

335
336
    if (!macroInfo->bound_set)
      macroInfo->dirichletBoundary();    
337
  
338
    if (mesh->getDim() > 1)
339
340
341
342
343
344
      boundaryDOFs(mesh);

    // initial boundary projections
    int numFaces = mesh->getGeo(FACE);
    int dim = mesh->getDim();
    mel = mesh->firstMacroElement();
345
    for (int i = 0; i < mesh->getNumberOfLeaves(); i++) {
346
      MacroElement *macroEl = *(mel + i);
347
      Projection *projector = macroEl->getProjection(0);
348
      if (projector && projector->getType() == VOLUME_PROJECTION) {
Thomas Witkowski's avatar
Thomas Witkowski committed
349
350
	for (int j = 0; j <= dim; j++)
	  projector->project(macroEl->getCoord(j));	
351
      } else {
352
	for (int j = 0; j < mesh->getGeo(EDGE); j++) {
353
	  projector = macroEl->getProjection(numFaces + j);
354
	  if (projector) {
355
356
357
358
359
360
361
362
363
364
365
	    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);

366
    if (mesh->getNumberOfDofs(CENTER)) {
367
      for (int i = 0; i < mesh->getNumberOfMacros(); i++)
368
	const_cast<Element*>(mel[i]->getElement())->
369
	  setDof(mesh->getNode(CENTER), mesh->getDof(CENTER));
370
371
    }

372
    // === Domain size ===
373

374
    WorldVector<double> x_min, x_max;
375

376
377
378
379
380
381
382
    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++) {
383
384
	x_min[j] = std::min(x_min[j], coords[i][j]);
	x_max[j] = std::max(x_max[j], coords[i][j]);
385
386
      }
    }
387

388
    for (int j = 0; j < Global::getGeo(WORLD); j++)
389
390
391
392
393
      mesh->setDiameter(j, x_max[j] - x_min[j]);

    if (check) {
      checkMesh(mesh);

Thomas Witkowski's avatar
Thomas Witkowski committed
394
395
      if (mesh->getDim() > 1)
	macroTest(mesh);
396
397
    }

398
    return macroInfo;
399
  }
400
  
401
402
  void MacroReader::computeNeighbours(Mesh *mesh)
  {
403
    FUNCNAME("MacroReader::computeNeighbours()");
404

405
    int dim = mesh->getDim();
406
    FixVec<DegreeOfFreedom*, DIMEN> dof(dim, NO_INIT);
407

408
409
410
    for (int i = 0; i < mesh->getNumberOfLeaves(); i++) {
      for (int k = 0; k < mesh->getGeo(NEIGH); k++) {
	mesh->getMacroElement(i)->setOppVertex(k, AMDIS_UNDEFINED);
411
	mesh->getMacroElement(i)->setNeighbour(k, NULL);
412
      }
413
    }
414

415
416
417
    for (int i = 0; i < mesh->getNumberOfLeaves(); i++) {
      for (int k = 0; k < mesh->getGeo(NEIGH); k++) {
	if (mesh->getMacroElement(i)->getBoundary(k) != INTERIOR) {
418
	  mesh->getMacroElement(i)->setNeighbour(k, NULL);
419
420
421
	  mesh->getMacroElement(i)->setOppVertex(k, -1);
	  continue;
	}
422

423
424
425
	if (mesh->getMacroElement(i)->getOppVertex(k) == AMDIS_UNDEFINED) {
	  if (dim == 1) {
	    dof[0] = const_cast<DegreeOfFreedom*>(mesh->getMacroElement(i)->
426
						  getElement()->getDof(k));
427
428
429
430
	  } else {
	    for (int l = 0; l < dim; l++)
	      dof[l] = const_cast<DegreeOfFreedom*>(mesh->getMacroElement(i)->
						    getElement()->
431
						    getDof((k + l + 1) % (dim + 1)));
432
	  }
433
434
435
436
437
438
439
440
441
442
443
444
445
	  
	  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;
	    }
	  }

446
447
448
449
450
451
452
	  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);
453
454
	      if (l < dim)
		std::cout << " / ";	      
455
456
457
	    }
	    std::cout << std::endl;
	    std::cout << "  dofs = ";
458
459
	    for (int l = 0; l < dim; l++)
	      std::cout << *(dof[l]) << " ";	    
460
461
462
463
	    std::cout << std::endl;

	    ERROR_EXIT("\n");
	  }    
464
	}
465
      }
466
    }
467
468
469
470
471
472
473
474
475
476
477
  }


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

  void MacroReader::boundaryDOFs(Mesh *mesh)
  {
478
479
480
481
482
    FUNCNAME("Mesh::boundaryDOFs()");

    int lnode = mesh->getNode(EDGE);
    int k, lne = mesh->getNumberOfLeaves();
    int max_n_neigh = 0, n_neigh, ov;
483
    std::deque<MacroElement*>::iterator mel;
484
    const MacroElement* neigh;
485
    DegreeOfFreedom *dof;
486
487
488
489
490
491

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

    int dim = mesh->getDim();

Thomas Witkowski's avatar
Thomas Witkowski committed
492
    switch (dim) {
493
    case 2:
494
      for (mel = mesh->firstMacroElement(); mel != mesh->endOfMacroElements(); mel++) {
495
496
497
498
499
500
	// check for periodic boundary
	Element *el = const_cast<Element*>((*mel)->getElement());
	ElementData *ed = el->getElementData(PERIODIC);

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

501
	if (ed) {
502
	  std::list<LeafDataPeriodic::PeriodicInfo> &periodicInfos = 
503
	    dynamic_cast<LeafDataPeriodic*>(ed)->getInfoList();
504
505
	  std::list<LeafDataPeriodic::PeriodicInfo>::iterator it;
	  std::list<LeafDataPeriodic::PeriodicInfo>::iterator end = periodicInfos.end();
Thomas Witkowski's avatar
Thomas Witkowski committed
506
507
	  for (it = periodicInfos.begin(); it != end; ++it)
	    if (it->type != 0)
508
509
510
	      periodic[it->elementSide] = true;
	}

511
	for (int i = 0; i < mesh->getGeo(NEIGH); i++) {
512
	  if (!(*mel)->getNeighbour(i) || 
513
514
515
516
	      ((*mel)->getNeighbour(i)->getIndex() < (*mel)->getIndex())) {

	    mesh->incrementNumberOfEdges(1);

517
	    if (mesh->getNumberOfDofs(EDGE)) {
518
519
	      dof = mesh->getDof(EDGE);
	      el->setDof(lnode + i, dof);
520
521
      
	      if ((*mel)->getNeighbour(i)) {
Thomas Witkowski's avatar
Thomas Witkowski committed
522
523
		Element *neigh = 
		  const_cast<Element*>((*mel)->getNeighbour(i)->getElement());
524

Thomas Witkowski's avatar
Thomas Witkowski committed
525
		if (periodic[i])
526
		  neigh->setDof(lnode + (*mel)->getOppVertex(i), mesh->getDof(EDGE));
Thomas Witkowski's avatar
Thomas Witkowski committed
527
		else
528
		  neigh->setDof(lnode + (*mel)->getOppVertex(i), dof);		
529
	      }
530
531
	    }
	  }  
532
533
534
535
536
537
	}
      }
      break;
    case 3:
      lnode = mesh->getNode(FACE);
      mel = mesh->firstMacroElement();
538
      for (int i = 0; i < lne; i++) {
539
540
541

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

	DimVec<bool> periodic(dim, DEFAULT_VALUE, false);
      
547
	if (ed) {
548
	  std::list<LeafDataPeriodic::PeriodicInfo> &periodicInfos = 
549
	    dynamic_cast<LeafDataPeriodic*>(ed)->getInfoList();
550
551
	  std::list<LeafDataPeriodic::PeriodicInfo>::iterator it;
	  std::list<LeafDataPeriodic::PeriodicInfo>::iterator end = periodicInfos.end();
552
553
	  for (it = periodicInfos.begin(); it != end; ++it)
	    if (it->type != 0)
554
555
556
	      periodic[it->elementSide] = true;
	}

557
558
	for (k = 0; k < mesh->getGeo(EDGE); k++) {
	  // === Check for not counted edges. ===
559
560
	  n_neigh = 1;

Thomas Witkowski's avatar
Thomas Witkowski committed
561
	  if (newEdge(mesh, (*(mel + i)), k, &n_neigh)) {
562
	    mesh->incrementNumberOfEdges(1);
563
	    max_n_neigh = std::max(max_n_neigh, n_neigh);
564
565
566
567
	  }
	}
      
	for (k = 0; k < mesh->getGeo(NEIGH); k++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
568
	  neigh = (*(mel + i))->getNeighbour(k);
569
	  // === Face is counted and dof is added by the element with bigger index. ===
Thomas Witkowski's avatar
Thomas Witkowski committed
570
571
	  if (neigh && (neigh->getIndex() > (*(mel + i))->getIndex()))  
	    continue;
572
573
574
	
	  mesh->incrementNumberOfFaces(1);
	
575
	  if (mesh->getNumberOfDofs(FACE)) {
576
	    TEST_EXIT(!(*(mel + i))->getElement()->getDof(lnode + k))
577
	      ("dof %d on element %d already set\n", 
Thomas Witkowski's avatar
Thomas Witkowski committed
578
	       lnode + k, (*(mel + i))->getIndex());
579
	  
580
	    const_cast<Element*>((*(mel + i))->getElement())->setDof(lnode + k,
581
								     mesh->getDof(FACE));
582
583

	    if (neigh) {
Thomas Witkowski's avatar
Thomas Witkowski committed
584
	      ov = (*(mel + i))->getOppVertex(k);
585
	      TEST_EXIT(!neigh->getElement()->getDof(lnode + ov))
586
		("dof %d on neighbour %d already set\n", 
Thomas Witkowski's avatar
Thomas Witkowski committed
587
		 lnode + ov, neigh->getIndex());
588
589
	    
	      Element *neighEl = 
Thomas Witkowski's avatar
Thomas Witkowski committed
590
		const_cast<Element*>((*(mel + i))->getNeighbour(k)->getElement());
591

Thomas Witkowski's avatar
Thomas Witkowski committed
592
	      if (periodic[k])
593
		neighEl->setDof(lnode+ov, mesh->getDof(FACE));
Thomas Witkowski's avatar
Thomas Witkowski committed
594
	      else
595
		neighEl->setDof(lnode+ov, const_cast<int*>((*(mel + i))->getElement()->
596
							   getDof(lnode + k)));	      
597
598
599
600
601
	    }
	  }
	}
      }
      break;
Thomas Witkowski's avatar
Thomas Witkowski committed
602
603
    default: 
      ERROR_EXIT("invalid dim\n");
604
605
    }
    
606
    if (3 == dim)
Thomas Witkowski's avatar
Thomas Witkowski committed
607
      mesh->setMaxEdgeNeigh(std::max(8, 2 * max_n_neigh));
608
    else
Thomas Witkowski's avatar
Thomas Witkowski committed
609
      mesh->setMaxEdgeNeigh(dim - 1);    
610
611
612
613
614
615
616
617
  }

  /* 
     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 
618
     (wenn nameneu=NULL wird keine MAcro-Datei erzeugt)
619
620
  */      

Thomas Witkowski's avatar
Thomas Witkowski committed
621
  void MacroReader::macroTest(Mesh *mesh)
622
  {
623
    FUNCNAME("MacroReader::macroTest()");
624
   
625
626
627
    int i = macrotest(mesh);

    if (i >= 0) {
Thomas Witkowski's avatar
Thomas Witkowski committed
628
629
      WARNING("There is a cycle beginning in macro element %d\n", i);
      WARNING("Entries in MacroElement structures get reordered\n");
630
      umb(NULL, mesh, umbVkantMacro);
631
    }
632
633
634
635
636
637
638
639
640
641
642
643
644
645
  }
  
  /****************************************************************************/
  /*  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
646
647
    std::deque<MacroElement*>::const_iterator macro, mac;
    int flg = 0;
648
    int dim = mesh->getDim();
Thomas Witkowski's avatar
Thomas Witkowski committed
649
650
    int *test = new int[mesh->getNumberOfMacros()];
    int *zykl = new int[mesh->getNumberOfMacros()];
651
 
Thomas Witkowski's avatar
Thomas Witkowski committed
652
    for (int i = 0; i < mesh->getNumberOfMacros(); i++)
653
      test[i] = 0;
654

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

658
659
660
661
    while (macrolfd != mesh->endOfMacroElements()) {
      if (test[(*macrolfd)->getIndex()] == 1) {
	macrolfd++;
      } else {
662
663
	for (int i = 0; i < mesh->getNumberOfMacros(); i++)
	  zykl[i] = 0;	
664
    
665
666
667
668
669
670
671
672
	macro = macrolfd;
	flg = 2;
	do {
	  if (zykl[(*macro)->getIndex()] == 1) {
	    flg = 0;
	    zykstart = (*macro)->getIndex();
	  } else {
	    zykl[(*macro)->getIndex()] = 1;
673
      
674
675
	    if (test[(*macro)->getIndex()] == 1) {
	      flg = 1;
676
	    } else if ((*macro)->getNeighbour(dim) == NULL) {
677
678
	      flg = 1;
	      test[(*macro)->getIndex()] = 1;
Thomas Witkowski's avatar
Thomas Witkowski committed
679
	    } else if ((*macro) == (*macro)->getNeighbour(dim)->getNeighbour(dim)) {
680
681
682
683
	      flg = 1;
	      test[(*macro)->getIndex()] = 1;
	      test[(*macro)->getNeighbour(dim)->getIndex()] = 1;
	    } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
684
685
	      for (mac = mesh->firstMacroElement(); 
		   (*mac) != (*macro)->getNeighbour(dim); mac++);
686
687
688
689
	      macro = mac;
	    } 
	  }	  
	} while(flg == 2);
690
 
691
	if (flg == 1)
692
	  macrolfd++;
693
694
	else 
	  macrolfd=mesh->endOfMacroElements();	
695
      }
696
    }
697
  
Thomas Witkowski's avatar
Thomas Witkowski committed
698
699
    delete [] zykl;
    delete [] test;
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
 
    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
724
			void (*umbvk)(Mesh*, MacroElement*, int, int*))
725
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
726
    int *test = new int[mesh->getNumberOfMacros()];
727
  
Thomas Witkowski's avatar
Thomas Witkowski committed
728
729
    for (int i = 0; i < static_cast<int>(mesh->getNumberOfMacros()); i++)
      test[i] = 0;
730

731
    recumb(mesh, (*mesh->firstMacroElement()), NULL, test, 0, 0, ele, umbvk);
732

Thomas Witkowski's avatar
Thomas Witkowski committed
733
    delete [] test;
734
735
736
737
738
  }

  bool MacroReader::newEdge(Mesh *mesh, MacroElement *mel,
			    int mel_edge_no, int *n_neigh)
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
739
740
    FUNCNAME("MacroElement::newEdge()"); 
    MacroElement *nei;
741
    const DegreeOfFreedom *dof[2];
742
    DegreeOfFreedom *edge_dof = NULL;
Thomas Witkowski's avatar
Thomas Witkowski committed
743
744
    int j, k, opp_v, node = 0;
    BoundaryType lbound = INTERIOR;
745
    Projection *lproject = NULL;
Thomas Witkowski's avatar
Thomas Witkowski committed
746
    const int max_no_list_el = 100;
747
748
749
750
    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
751
    static int next_el[6][2] = {{3,2},{1,3},{1,2},{0,3},{0,2},{0,1}};
752
753
    int vertices = mesh->getGeo(VERTEX);

Thomas Witkowski's avatar
Thomas Witkowski committed
754
    int mel_index = mel->getIndex();
755
756
757
758

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

759
    if (mesh->getNumberOfDofs(EDGE)) {
760
      node = mesh->getNode(EDGE);
761
      if (el->getDof(node+edge_no)) {
762
763
764
765
766
767
	/****************************************************************************/
	/*  edge was counted by another macro element and dof was added on the      */
	/*  complete patch                                                          */
	/****************************************************************************/
	return false;
      } else {
768
769
	edge_dof = mesh->getDof(EDGE);
	el->setDof(node+edge_no, edge_dof);
770
771
772
      }
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
773
    for (j = 0; j < 2; j++)
774
      dof[j] = el->getDof(el->getVertexOfEdge(edge_no, j));
Thomas Witkowski's avatar
Thomas Witkowski committed
775
    
776
777
778
779
780
781
782
783
784
785
786
787
    /****************************************************************************/
    /*  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]);


788
    if (mel->getBoundary(next_el[edge_no][0])) {
789
790
791
792
793
794
      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++)
795
	if (nei->getElement()->getDof(j) == dof[0])  break;
796
      for (k = 0; k < vertices; k++)
797
	if (nei->getElement()->getDof(k) == dof[1])  break;
798
799

      // check for periodic boundary
800
      if (j == 4 || k == 4) {
801
	nei = NULL;
802
803
804
	break;
      }

805
      if (mesh->getNumberOfDofs(EDGE)) {
806
807
	TEST_EXIT(nei->index > mel_index)
	  ("neighbour index %d < element index %d\n", nei->getIndex(), mel_index);
808
      }
809

810
      if (!mesh->getNumberOfDofs(EDGE) && nei->getIndex() < mel_index) 
811
	return false;
812
    
813
      edge_no = Tetrahedron::edgeOfDofs[j][k];
814
815
816
817
818
819
820
821
822
823

      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]);

824
      if (mesh->getNumberOfDofs(EDGE))
825
	nei->element->setDof(node+edge_no,edge_dof);      
826

827
      if (next_el[edge_no][0] != opp_v) {
828
	if (nei->getBoundary(next_el[edge_no][0])) {
829
830
831
832
833
834
	  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()))
835
836
837
	      lproject = neiProject;
	  }
	}
838
839
840
841
842
843
844
845
846
847
	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()))
848
849
850
	      lproject = neiProject;
	  }
	}
851
852
853
	opp_v = nei->getOppVertex(next_el[edge_no][1]);
	nei = nei->getNeighbour(next_el[edge_no][1]);
      }
854
855
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
    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++)
878
	  if (nei->getElement()->getDof(j) == dof[0])  break;
Thomas Witkowski's avatar
Thomas Witkowski committed
879
	for (k = 0; k < vertices; k++)
880
	  if (nei->getElement()->getDof(k) == dof[1])  break;
881
	
Thomas Witkowski's avatar
Thomas Witkowski committed
882
883
884
885
	// check for periodic boundary
	if (j == 4 || k == 4)
	  return false;
	
886
	if (mesh->getNumberOfDofs(EDGE)) {
Thomas Witkowski's avatar
Thomas Witkowski committed
887
888
889
	  TEST_EXIT(nei->getIndex() > mel_index)
	    ("neighbour index %d < element index %d\n", nei->getIndex(),
	     mel_index);
890
	}
Thomas Witkowski's avatar
Thomas Witkowski committed
891
892
893
894
	
	if (nei->getIndex() < mel_index)  
	  return false;
	
895
	edge_no = Tetrahedron::edgeOfDofs[j][k];
Thomas Witkowski's avatar
Thomas Witkowski committed
896
897
898
899
900
	
	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]);
901

902
	if (mesh->getNumberOfDofs(EDGE)) {
903
	  TEST_EXIT(!nei->getElement()->getDof(node+edge_no))
Thomas Witkowski's avatar
Thomas Witkowski committed
904
905
906
	    ("dof %d on element %d is already set, but not on element %d\n",
	     node + edge_no, nei->getIndex(), mel_index);
	  
907
	  nei->element->setDof(node+edge_no, edge_dof);
Thomas Witkowski's avatar
Thomas Witkowski committed
908
	}
909

Thomas Witkowski's avatar
Thomas Witkowski committed
910
911
912
913
914
915
916
917
918
	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;	      
919
920
	    }
	  }
Thomas Witkowski's avatar
Thomas Witkowski committed
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
	  
	  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()))
		lproject = neiProject;	      
	    }
	  }
	  
	  opp_v = nei->getOppVertex(next_el[edge_no][1]);
	  nei = nei->getNeighbour(next_el[edge_no][1]);
	}
939
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
940
941
    }
    
942
943
944
945
946
947
948
949
950
951
952
    for (j = 0; j < *n_neigh; j++) {
      *(list_bound[j]) = lbound;
      *(list_project[j]) = lproject;
    }
  
    return true;
  }

  void MacroReader::fillMelBoundary(Mesh *mesh, MacroElement *mel, 
				    FixVec<BoundaryType ,NEIGH> ind)
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
953
954
    for (int i = 0; i < mesh->getGeo(NEIGH); i++)
      mel->boundary[i] = ind[i];        
955
956
957
958
  }


  void MacroReader::fillMelNeigh(MacroElement *mel,
959
				 std::deque<MacroElement*>& macro_elements, 
960
961
962
963
				 FixVec<int,NEIGH> ind)
  {
    int dim = mel->element->getMesh()->getDim();

964
965
966
967
    for (int k = 0; k < Global::getGeo(NEIGH, dim); k++) {
      if (ind[k] >= 0) 
	mel->neighbour[k] = macro_elements[ind[k]];
      else
968
	mel->neighbour[k] = NULL;