RefinementManager3d.cc 29.4 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
18
19
20
21
22
23
24
25
26
27
#include "RefinementManager.h"
#include "Mesh.h"
#include "Traverse.h"
#include "ElInfo.h"
#include "DOFAdmin.h"
#include "AdaptStationary.h"
#include "AdaptInstationary.h"
#include "FixVec.h"
#include "RCNeighbourList.h"
#include "ProblemStatBase.h"
#include "DOFIndexed.h"
#include "Projection.h"
#include "DOFVector.h"
#include "PeriodicBC.h"
#include "VertexVector.h"
28
#include "Debug.h"
29
30
31

namespace AMDiS {

32
33
  FixRefinementPatch::ConnectedEdges FixRefinementPatch::connectedEdges(0);

34
  void RefinementManager3d::bisectTetrahedron(RCNeighbourList& refineList, 
35
36
37
38
					      int index,
					      DegreeOfFreedom* dof[3],
					      DegreeOfFreedom *edge[2])
  {
39
    Tetrahedron *el = 
40
      dynamic_cast<Tetrahedron*>(const_cast<Element*>(refineList.getElement(index)));
41
    Tetrahedron *child[2];
42
    int el_type = refineList.getType(index);
43
44
45
46

    child[0] = dynamic_cast<Tetrahedron*>(mesh->createNewElement(el));
    child[1] = dynamic_cast<Tetrahedron*>(mesh->createNewElement(el));
  
47
    int mark = std::max(0, el->getMark() - 1);
48
49
50
51
52
53
54
55
56
57
58
59
60
    child[0]->setMark(mark);
    child[1]->setMark(mark);
    el->setMark(0);

    /****************************************************************************/
    /*  transfer hidden data from parent to children                            */
    /****************************************************************************/

    el->refineElementData(child[0], child[1], el_type);

    el->setFirstChild(child[0]);
    el->setSecondChild(child[1]);

61
62
    if (child[0]->getMark() > 0)
      doMoreRecursiveRefine = true;
63
64

    int n_vertices = mesh->getGeo(VERTEX);
65
66
    child[0]->setDof(n_vertices - 1, dof[0]);
    child[1]->setDof(n_vertices - 1, dof[0]);
67

68
    for (int i = 0; i < n_vertices - 1; i++) {
69
      child[0]->
70
	setDof(i, const_cast<int*>(el->getDof(Tetrahedron::childVertex[el_type][0][i])));
71
      child[1]->
72
	setDof(i, const_cast<int*>(el->getDof(Tetrahedron::childVertex[el_type][1][i])));
73
    }
74
75
76
77
78
79
80
81
82
83
84
85
    /****************************************************************************/
    /*  there is one more leaf element and two more hierachical elements        */
    /****************************************************************************/

    mesh->incrementNumberOfLeaves(1);
    mesh->incrementNumberOfElements(2);

    /****************************************************************************/
    /* first set those dof pointers for higher order without neighbour          */
    /* information                                                              */
    /****************************************************************************/

86
    if (mesh->getNumberOfDofs(EDGE)) {
87
      int node = mesh->getNode(EDGE);
88

89
90
91
92
93
      /****************************************************************************/
      /*  set pointers to those dof's that are handed on from the parant          */
      /****************************************************************************/
      
      child[0]->
94
95
	setDof(node, 
	       const_cast<int*>(el->getDof(node + Tetrahedron::childEdge[el_type][0][0])));
96
      child[1]->
97
98
	setDof(node, 
	       const_cast<int*>(el->getDof(node + Tetrahedron::childEdge[el_type][1][0])));
99
      child[0]->
100
101
	setDof(node + 1, 
	       const_cast<int*>(el->getDof(node + Tetrahedron::childEdge[el_type][0][1])));
102
      child[1]->
103
104
	setDof(node + 1, 
	       const_cast<int*>(el->getDof(node + Tetrahedron::childEdge[el_type][1][1])));
105
      child[0]->
106
107
	setDof(node + 3, 
	       const_cast<int*>(el->getDof(node + Tetrahedron::childEdge[el_type][0][3])));
108
      child[1]->
109
110
	setDof(node + 3, 
	       const_cast<int*>(el->getDof(node + Tetrahedron::childEdge[el_type][1][3])));
111
112
113
114
115
      
      /****************************************************************************/
      /*  adjust pointers to the dof's in the refinement edge                     */
      /****************************************************************************/
      
116
117
118
      if (el->getDof(0) == edge[0]) {
	child[0]->setDof(node + 2, dof[1]);
	child[1]->setDof(node + 2, dof[2]);
119
      } else {
120
121
	child[0]->setDof(node + 2, dof[2]);
	child[1]->setDof(node + 2, dof[1]);
122
123
124
      }
    }
    
125
    if (mesh->getNumberOfDofs(FACE)) {
126
      int node = mesh->getNode(FACE);    
127
128
129
130
131
      
      /****************************************************************************/
      /*  set pointers to those dof's that are handed on from the parant          */
      /****************************************************************************/
      
132
133
      child[0]->setDof(node + 3, const_cast<int*>(el->getDof(node + 1)));
      child[1]->setDof(node + 3, const_cast<int*>(el->getDof(node + 0)));
134
135
136
137
138
      
      /****************************************************************************/
      /*  get new dof for the common face of child0 and child1                    */
      /****************************************************************************/
      
139
      DegreeOfFreedom *newDOF = mesh->getDof(FACE);
140
141
      child[0]->setDof(node, static_cast<int*>(newDOF));
      child[1]->setDof(node, static_cast<int*>(newDOF));
142
143
    }
    
144
    if (mesh->getNumberOfDofs(CENTER)) {
145
      int node = mesh->getNode(CENTER);
146
147
      child[0]->setDof(node, const_cast<int*>(mesh->getDof(CENTER)));
      child[1]->setDof(node, const_cast<int*>(mesh->getDof(CENTER)));
148
    }
149

150
    if (mesh->getNumberOfDofs(EDGE) || mesh->getNumberOfDofs(FACE))
151
      fillPatchConnectivity(refineList, index);
152
  }
153

154

155
  void RefinementManager3d::fillPatchConnectivity(RCNeighbourList &refineList,
156
157
						  int index)
  {
158
    FUNCNAME_DBG("RefinementManager3d::fillPatchConnectivity");
Thomas Witkowski's avatar
Thomas Witkowski committed
159

160
161
    Element *el = refineList.getElement(index);
    int el_type = refineList.getType(index);
162
    int n_type = 0;
163
    int dir, adjc, i_neigh, j_neigh;
164
    int node0, node1, oppVertex = 0;
165

166
    for (dir = 0; dir < 2; dir++) {
167
      Element *neigh = refineList.getNeighbourElement(index, dir);
168
      if (neigh) {
169
170
	n_type = refineList.getType(refineList.getNeighbourNr(index, dir));
	oppVertex = refineList.getOppVertex(index, dir);
171
172
      }

173
      if (!neigh || neigh->isLeaf()) {
174
	/****************************************************************************/
175
176
177
	/*  get new dof's in the midedge of the face of el and for the two midpoints*/
	/*  of the sub-faces. If face is an interior face those pointers have to be */
	/*  adjusted by the neighbour element also (see below)                      */
178
179
	/****************************************************************************/

180
	if (mesh->getNumberOfDofs(EDGE)) {
181
182
183
	  node0 = node1 = mesh->getNode(EDGE);
	  node0 += Tetrahedron::nChildEdge[el_type][0][dir];
	  node1 += Tetrahedron::nChildEdge[el_type][1][dir];
184
	  DegreeOfFreedom *newDOF = mesh->getDof(EDGE);
185
186
	  (const_cast<Element*>(el->getFirstChild()))->setDof(node0, newDOF);
	  (const_cast<Element*>(el->getSecondChild()))->setDof(node1, newDOF);
187
	}
188
	if (mesh->getNumberOfDofs(FACE)) {
189
	  node0 = mesh->getNode(FACE) + Tetrahedron::nChildFace[el_type][0][dir];
190
	  (const_cast<Element*>(el->getFirstChild()))->setDof(node0, mesh->getDof(FACE));
191
	  node1 = mesh->getNode(FACE) + Tetrahedron::nChildFace[el_type][1][dir];
192
	  (const_cast<Element*>(el->getSecondChild()))->setDof(node1, mesh->getDof(FACE));
193
	}
194
      } else {
195
	/****************************************************************************/
196
197
	/*  interior face and neighbour has been refined, look for position at the  */
	/*  refinement edge                                                         */
198
	/****************************************************************************/
199
      
200
	if (el->getDof(0) == neigh->getDof(0)) {
201
	  // Same position at refinement edge.
202
203
	  adjc = 0;
	} else {
204
	  // Different position at refinement edge.
205
206
	  adjc = 1;
	}
207

208
209
	for (int i = 0; i < 2; i++) {
	  int j = Tetrahedron::adjacentChild[adjc][i];
210

211
	  i_neigh = Tetrahedron::nChildFace[el_type][i][dir];
212
	  j_neigh = Tetrahedron::nChildFace[n_type][j][oppVertex - 2];
213

214
215
216
217
	  /****************************************************************************/
	  /*  adjust dof pointer in the edge in the common face of el and neigh and   */
	  /*  the dof pointer in the sub-face child_i-child_j (allocated by neigh!)   */
	  /****************************************************************************/
218

219
	  if (mesh->getNumberOfDofs(EDGE)) {
220
	    node0 = mesh->getNode(EDGE) + Tetrahedron::nChildEdge[el_type][i][dir];
221
	    node1 = mesh->getNode(EDGE) + Tetrahedron::nChildEdge[n_type][j][oppVertex - 2];
222

223
	    TEST_EXIT_DBG(neigh->getChild(j)->getDof(node1))
224
225
	      ("no dof on neighbour %d at node %d\n", 
	       neigh->getChild(j)->getIndex(), node1);
226

227
	    (const_cast<Element*>(el->getChild(i)))->
228
	      setDof(node0, const_cast<int*>(neigh->getChild(j)->getDof(node1)));
229
	  }
230
	  if (mesh->getNumberOfDofs(FACE)) {
231
232
	    node0 = mesh->getNode(FACE) + i_neigh;
	    node1 = mesh->getNode(FACE) + j_neigh;
233

234
	    TEST_EXIT_DBG(neigh->getChild(j)->getDof(node1))
Thomas Witkowski's avatar
Thomas Witkowski committed
235
	      ("No DOF on neighbour %d at node %d!\n",
236
	       neigh->getChild(j)->getIndex(), node1);
237

238
	    (const_cast<Element*>(el->getChild(i)))->
239
	      setDof(node0, const_cast<int*>(neigh->getChild(j)->getDof(node1)));
240
241
	  }

242
243
244
	}  /*   for (i = 0; i < 2; i++)                                       */
      }    /*   else of   if (!neigh  ||  !neigh->child[0])                   */
    }      /*   for (dir = 0; dir < 2; dir++)                                 */
245
246
247
  }


248
  void RefinementManager3d::newCoordsFct(ElInfo *elInfo, RCNeighbourList &refineList)
249
  {
250
251
    FUNCNAME("RefinementManager3d::newCoordsFct()");

252
    Element *el = elInfo->getElement();
253
    DegreeOfFreedom *edge[2];
254
    ElInfo *elinfo = elInfo;
255
    int dow = Global::getGeo(WORLD);
256
    Projection *projector = elInfo->getProjection(0);
257

258
    if (!projector || projector->getType() != VOLUME_PROJECTION)
259
      projector = elInfo->getProjection(4);    
260
261

    if (el->getFirstChild() && projector && (!el->isNewCoordSet())) {
Thomas Witkowski's avatar
Thomas Witkowski committed
262
      WorldVector<double> *new_coord = new WorldVector<double>;
263

264
      for (int j = 0; j < dow; j++)
265
	(*new_coord)[j] = (elInfo->getCoord(0)[j] + elInfo->getCoord(1)[j]) * 0.5;
266
267
268
269
270
271
272
273

      projector->project(*new_coord);

      el->setNewCoord(new_coord);
      /****************************************************************************/
      /*  now, information should be passed on to patch neighbours...             */
      /*  get the refinement patch                                                */
      /****************************************************************************/
274
      refineList.setElement(0, el);
275
      refineList.setElType(0, elInfo->getType());
276
      int n_neigh = 1;
277

278
      for (int i = 0; i < 2; i++)
279
	edge[i] = const_cast<int*>(elInfo->getElement()->getDof(i));
280

281
      if (getRefinePatch(&elinfo, edge, 0, refineList, &n_neigh)) {
282
283
	// Domain's boundary was reached while looping around the refinement edge.

284
	getRefinePatch(&elinfo, edge, 1, refineList, &n_neigh);
285
286
      }

287
      for (int i = 1; i < n_neigh; i++) {            /* start with 1, as list[0]=el */
288
289
290
	TEST(!refineList.getElement(i)->isNewCoordSet())
	  ("non-nil new_coord in el %d refineList[%d] el %d (n_neigh=%d)\n",
	   el->getIndex(), i, refineList.getElement(i)->getIndex(), n_neigh);
291
	
292
	refineList.getElement(i)->setNewCoord(el->getNewCoord());
293
      }
294
295
296
    }
  }

297

298
  void RefinementManager3d::setNewCoords(int macroEl)
299
  {
300
    RCNeighbourList refineList(mesh->getMaxEdgeNeigh());
301
302
303
304
305
306
307
308
309
310
311
312
313
    ElInfo *elInfo;

    if (macroEl == -1)
      elInfo = stack->traverseFirst(mesh, -1, 
				    Mesh::CALL_EVERY_EL_PREORDER | 
				    Mesh::FILL_BOUND | Mesh::FILL_COORDS | 
				    Mesh::FILL_NEIGH);
    else
      elInfo = stack->traverseFirstOneMacro(mesh, macroEl, -1,
					    Mesh::CALL_EVERY_EL_PREORDER | 
					    Mesh::FILL_BOUND | Mesh::FILL_COORDS | 
					    Mesh::FILL_NEIGH);
    
314

315
    while (elInfo) {
316
      newCoordsFct(elInfo, refineList);
317
      elInfo = stack->traverseNext(elInfo);
318
319
320
321
322
    }
  }


  DegreeOfFreedom RefinementManager3d::refinePatch(DegreeOfFreedom *edge[2], 
323
						   RCNeighbourList &refineList,
324
325
						   int n_neigh, bool bound)
  {
326
    Tetrahedron *el = 
327
      dynamic_cast<Tetrahedron*>(const_cast<Element*>(refineList.getElement(0)));
328
329
330
331
332
333
334
    /* first element in the list */
    DegreeOfFreedom *dof[3] = {NULL, NULL, NULL};

    /****************************************************************************/
    /*  get new dof's in the refinement edge                                    */
    /****************************************************************************/

335
    dof[0] = mesh->getDof(VERTEX);
336
337
    mesh->incrementNumberOfVertices(1);
  
338
    if (mesh->getNumberOfDofs(EDGE)) {
339
340
      dof[1] = mesh->getDof(EDGE);
      dof[2] = mesh->getDof(EDGE);
341
    }
342

343
    for (int i = 0; i < n_neigh; i++)
344
345
346
347
348
      bisectTetrahedron(refineList, i, dof, edge);

    /****************************************************************************/
    /*  if there are functions to interpolate data to the finer grid, do so     */
    /****************************************************************************/
349
    for (int iadmin = 0; iadmin < mesh->getNumberOfDOFAdmin(); iadmin++) {
350
      std::list<DOFIndexedBase*>::iterator it;
351
      DOFAdmin* admin = const_cast<DOFAdmin*>(&mesh->getDofAdmin(iadmin));
352
      std::list<DOFIndexedBase*>::iterator end = admin->endDOFIndexed();
353
      for (it = admin->beginDOFIndexed(); it != end; it++)
354
	(*it)->refineInterpol(refineList, n_neigh);
355
356
    }

357
358
359
360
361
    if (!mesh->queryCoarseDOFs()) {
      /****************************************************************************/
      /*  if there should be no dof information on interior leaf elements remove  */
      /*  dofs from edges, faces and the centers of parents                       */
      /****************************************************************************/
362
      if (mesh->getNumberOfDofs(EDGE)) {
363
	/****************************************************************************/
364
	/*  remove dof of the midpoint of the common refinement edge                */
365
366
	/****************************************************************************/

367
	el = dynamic_cast<Tetrahedron*>(const_cast<Element*>(refineList.getElement(0)));
368
	mesh->freeDof(const_cast<int*>(el->getDof(mesh->getNode(EDGE))), EDGE);
369
      }
370
      
371
372
373
      if (mesh->getNumberOfDofs(EDGE) || 
	  mesh->getNumberOfDofs(FACE) ||
	  mesh->getNumberOfDofs(CENTER)) {
374
	for (int i = 0; i < n_neigh; i++)
375
	  refineList.removeDOFParent(i);
376
377
      }
    }
378
379
380
381
382
383
384


    /****************************************************************************/
    /*  update the number of edges and faces; depends whether refinement edge   */
    /*  is a boundary edge or not                                               */
    /****************************************************************************/

385
386
    if (bound) {
      mesh->incrementNumberOfEdges(n_neigh + 2);
387
      mesh->incrementNumberOfFaces(2 * n_neigh + 1);
388
389
    } else {
      mesh->incrementNumberOfEdges(n_neigh + 1);
390
      mesh->incrementNumberOfFaces(2 * n_neigh);
391
392
    }
    
393
394
395
396
    return dof[0][0];
  }


397
398
399
400
401
  bool RefinementManager3d::getRefinePatch(ElInfo **elInfo, 
					   DegreeOfFreedom *edge[2], 
					   int direction,
					   RCNeighbourList &refineList, 
					   int *n_neigh)
402
  {
403
    FUNCNAME_DBG("RefinementManager3d::getRefinePatch()");
404

405
    int localNeighbour = 3 - direction;
406
    Tetrahedron *el = 
407
      dynamic_cast<Tetrahedron*>(const_cast<Element*>((*elInfo)->getElement()));
408

409
410
    if ((*elInfo)->getNeighbour(localNeighbour) == NULL)
      return true;    
411
  
412
    int oppVertex = (*elInfo)->getOppVertex(localNeighbour);
413
#if DEBUG
414
    int testIndex = (*elInfo)->getNeighbour(localNeighbour)->getIndex();
415
#endif
416
    ElInfo *neighInfo = stack->traverseNeighbour3d((*elInfo), localNeighbour);
417
418
419
420
    int neighElType = neighInfo->getType();

    TEST_EXIT_DBG(neighInfo->getElement()->getIndex() == testIndex)
      ("Should not happen!\n");
421

422
    Tetrahedron *neigh = 
423
      dynamic_cast<Tetrahedron*>(const_cast<Element*>(neighInfo->getElement())); 
424
425
426
    int vertices = mesh->getGeo(VERTEX);

    while (neigh != el) {
427
428
429
      // === Determine the common edge of the element and its neighbour. ===

      int edgeDof0, edgeDof1;
430
431
432

      // get local/elementwise DOF indices of Start and End Vertices of EDGE
      // on Neighbour Element
433
      for (edgeDof0 = 0; edgeDof0 < vertices; edgeDof0++)
434
	if (neigh->getDof(edgeDof0) == edge[0])
435
	  break;
436
      for (edgeDof1 = 0; edgeDof1 < vertices; edgeDof1++)
437
	if (neigh->getDof(edgeDof1) == edge[1])
438
	  break;
439

440
441
      if (edgeDof0 > 3 || edgeDof1 > 3) {
	for (edgeDof0 = 0; edgeDof0 < vertices; edgeDof0++)
442
	  if (mesh->associated(neigh->getDof(edgeDof0, 0), edge[0][0]))  
443
	    break;
444
	for (edgeDof1 = 0; edgeDof1 < vertices; edgeDof1++)
445
	  if (mesh->associated(neigh->getDof(edgeDof1, 0), edge[1][0]))  
446
	    break;
447
	    
448
449
	if (edgeDof0 > 3 || edgeDof1 > 3) {
	  for (edgeDof0 = 0; edgeDof0 < vertices; edgeDof0++)
450
	    if (mesh->indirectlyAssociated(neigh->getDof(edgeDof0, 0), edge[0][0]))  
451
	      break;
452
	  for (edgeDof1 = 0; edgeDof1 < vertices; edgeDof1++)
453
	    if (mesh->indirectlyAssociated(neigh->getDof(edgeDof1, 0), edge[1][0]))  
454
	      break;
455
	    
456
	  TEST_EXIT_DBG(edgeDof0 < vertices)
457
 	    ("DOF %d not found on element %d with nodes (%d %d %d %d)\n", 
458
 	     edge[0][0], neigh->getIndex(), neigh->getDof(0, 0),
459
 	     neigh->getDof(1, 0), neigh->getDof(2, 0), neigh->getDof(3, 0));
460

461
	  TEST_EXIT_DBG(edgeDof1 < vertices)
462
 	    ("DOF %d not found on element %d with nodes (%d %d %d %d)\n", 
463
464
 	     edge[1][0], neigh->getIndex(), neigh->getDof(0, 0),
 	     neigh->getDof(1, 0), neigh->getDof(2, 0), neigh->getDof(3, 0));
465
466
467
	}
      }

468
      TEST_EXIT_DBG(edgeDof0 < vertices && edgeDof1 < vertices)
469
	("DOF %d or DOF %d not found on element %d with nodes (%d %d %d %d)\n", 
470
471
	 edge[0][0], edge[1][0], neigh->getIndex(), neigh->getDof(0, 0),
	 neigh->getDof(1, 0), neigh->getDof(2, 0), neigh->getDof(3, 0));
472
473
474
475
476
477

      int edgeNo = Tetrahedron::edgeOfDofs[edgeDof0][edgeDof1];

      if (edgeNo) {
	// Only 0 can be a compatible commen refinement edge. Thus, neigh has not 
	// a compatible refinement edge. Refine it first.
478

479
	neigh->setMark(std::max(neigh->getMark(), 1));
480
481
482
483
484
485

	neighInfo = refineFunction(neighInfo);

	// === Now, go to a child at the edge and determine the opposite vertex ===
	// === for  this child; continue the looping around the edge with this  ===
	// === element.                                                         ===
486
	
487
488
489
	neighInfo = stack->traverseNext(neighInfo);
	neighElType = neighInfo->getType();
	bool reverseMode = stack->getTraverseFlag().isSet(Mesh::CALL_REVERSE_MODE);
490
	
491
	switch (edgeNo) {
492
	case 1: 
493
494
495
496
497
498
	  if (reverseMode) {
	    neighInfo = stack->traverseNext(neighInfo);
	    neighElType = neighInfo->getType();
	  }
	    
	  oppVertex = (oppVertex == 1 ? 3 : 2);
499
500
	  break;
	case 2: 
501
502
503
504
505
506
	  if (reverseMode) {
	    neighInfo = stack->traverseNext(neighInfo);
	    neighElType = neighInfo->getType();
	  }

	  oppVertex = (oppVertex == 2 ? 1 : 3);
507
508
	  break;
	case 3: 
509
510
511
512
513
514
515
	  if (!reverseMode) {
	    neighInfo = stack->traverseNext(neighInfo);
	    neighElType = neighInfo->getType();
	  }

	  if (neighElType != 1)
	    oppVertex = (oppVertex == 0 ? 3 : 2);
516
	  else
517
	    oppVertex = (oppVertex == 0 ? 3 : 1);
518
519
	  break;
	case 4:
520
521
522
523
524
525
526
	  if (!reverseMode) {
	    neighInfo = stack->traverseNext(neighInfo);
	    neighElType = neighInfo->getType();
	  }

	  if (neighElType != 1)
	    oppVertex = (oppVertex == 0 ? 3 : 1);
527
	  else
528
	    oppVertex = (oppVertex == 0 ? 3 : 2);
529
530
	  break;
	case 5:
531
532
533
534
535
	  if (neighElType != 1) {
	    if (!reverseMode) {
	      neighInfo = stack->traverseNext(neighInfo);
	      neighElType = neighInfo->getType();
	    }
536
	  }
537
	  oppVertex = 3;
538
	  break;
539
	}
540

541
	neigh = 
542
	  dynamic_cast<Tetrahedron*>(const_cast<Element*>(neighInfo->getElement()));
543
      } else {
544
545
546
	// Neigh is compatible devisible. Put neigh to the list of patch elements 
	// and go to next neighbour.

547
548
	TEST_EXIT_DBG(*n_neigh < mesh->getMaxEdgeNeigh())
	  ("too many neighbours %d in refine patch\n", *n_neigh);
549
		
550
551
	refineList.setElement(*n_neigh, neigh);
	refineList.setElType(*n_neigh, neighElType);
552
	
553
	// We have to go back to the starting element via oppVertex values.
554

555
	refineList.setOppVertex(*n_neigh, 0, oppVertex); 
556
	
557
558
559
560
561
562
	(*n_neigh)++;

	int i = (oppVertex != 3 ? 3 : 2);

	if (neighInfo->getNeighbour(i)) {
	  oppVertex = neighInfo->getOppVertex(i);
563
    
564
#if DEBUG
565
	  int testIndex = neighInfo->getNeighbour(i)->getIndex();
566
#endif
567
568
569
570
571
572
573

	  neighInfo = stack->traverseNeighbour3d(neighInfo, i);

	  TEST_EXIT_DBG(neighInfo->getElement()->getIndex() == testIndex)
	    ("Should not happen!\n");

	  neighElType = neighInfo->getType();
574
	  neigh = 
575
576
	    dynamic_cast<Tetrahedron*>(const_cast<Element*>(neighInfo->getElement()));
	} else {
577
	  break;
578
	}
579
      }
580
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
581
   
582
    if (neigh == el) {
583
      (*elInfo) = neighInfo;
584

585
      return false;
586
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
587
588

   
589
    // The domain's boundary is reached. Loop back to the starting el.
590
    
591
    int i = *n_neigh - 1;
592
    oppVertex = refineList.getOppVertex(i, 0);
593
    do {
594
      TEST_EXIT_DBG(neighInfo->getNeighbour(oppVertex) && i > 0)
Thomas Witkowski's avatar
Thomas Witkowski committed
595
	("While looping back domains boundary was reached or i == 0\n");
596
      oppVertex = refineList.getOppVertex(i--, 0);
597

598
#if DEBUG
599
      int testIndex = neighInfo->getNeighbour(oppVertex)->getIndex();
600
#endif
601
602
603
604
      neighInfo = stack->traverseNeighbour3d(neighInfo, oppVertex);

      int edgeDof0, edgeDof1;
      for (edgeDof0 = 0; edgeDof0 < vertices; edgeDof0++)
605
	if (neigh->getDof(edgeDof0) == edge[0])
606
607
	  break;
      for (edgeDof1 = 0; edgeDof1 < vertices; edgeDof1++)
608
	if (neigh->getDof(edgeDof1) == edge[1])
609
610
611
612
613
	  break;

      TEST_EXIT_DBG(neighInfo->getElement()->getIndex() == testIndex)
	("Should not happen!\n");
    } while (neighInfo->getElement() != el);
Thomas Witkowski's avatar
Thomas Witkowski committed
614

615
    (*elInfo) = neighInfo;
616
    
617
    return true;
618
619
620
  }


621
  ElInfo* RefinementManager3d::refineFunction(ElInfo* elInfo)
622
  {
623
    FUNCNAME_DBG("RefinementManager3d::refineFunction()");
Thomas Witkowski's avatar
Thomas Witkowski committed
624

625
    Element *el = elInfo->getElement();
626
627

    if (el->getMark() <= 0)  
628
629
      return elInfo;

630
    int bound = false;
631
    DegreeOfFreedom *edge[2];
632

633
634
    // === Get memory for a list of all elements at the refinement edge. ===

635
    RCNeighbourList refineList(mesh->getMaxEdgeNeigh());
636
    refineList.setElType(0, elInfo->getType());
637
    refineList.setElement(0, el);
638
639
    int n_neigh = 1;

640
641
    if (elInfo->getProjection(0) && 
	elInfo->getProjection(0)->getType() == VOLUME_PROJECTION)
642
      newCoords = true;
643
644


645
646
    // === Give the refinement edge the right orientation. ===

647
648
649
    if (el->getDof(0, 0) < el->getDof(1, 0)) {
      edge[0] = const_cast<DegreeOfFreedom*>(el->getDof(0));
      edge[1] = const_cast<DegreeOfFreedom*>(el->getDof(1));
650
    } else {
651
652
      edge[1] = const_cast<DegreeOfFreedom*>(el->getDof(0));
      edge[0] = const_cast<DegreeOfFreedom*>(el->getDof(1));
653
    }
654

655

656
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
Thomas Witkowski's avatar
Thomas Witkowski committed
657
658
    vector<FixRefinementPatch::EdgeInEl> refineEdges;
    FixRefinementPatch::getOtherEl(stack, refineEdges);
659
660
661
662
663
664
//     if (refineEdges.size()) {
//       MSG("FIX REFINEMENT PATH ON ELEMENT %d %d: %d additional edges\n", 
// 	  elInfo->getElement()->getIndex(),
// 	  elInfo->getMacroElement()->getIndex(),
// 	  refineEdges.size());
//     }
665
#endif
666
667
668

    // === Traverse and refine the refinement patch. ====

669
    if (getRefinePatch(&elInfo, edge, 0, refineList, &n_neigh)) {      
670
      // Domain's boundary was reached while looping around the refinement edge
671
      getRefinePatch(&elInfo, edge, 1, refineList, &n_neigh);
672
673
      bound = true;
    }
674

675
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
Thomas Witkowski's avatar
Thomas Witkowski committed
676
677
678
679
    // === If the refinement edge must be fixed, add also the other part  ===
    // === of this edge to the refinement patch.                          ===

    for (int edgeIndex = 0; 
680
 	 edgeIndex < static_cast<unsigned int>(refineEdges.size()); edgeIndex++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
681
      Element *otherEl = refineEdges[edgeIndex].first;   
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
      TraverseStack stack2;
      ElInfo *elInfo2 = 
	stack2.traverseFirstOneMacro(mesh, otherEl->getIndex(), -1, 
				     Mesh::CALL_LEAF_EL | 
				     Mesh::FILL_NEIGH | 
				     Mesh::FILL_BOUND);

      bool foundEdge = false;

      while (elInfo2) {
	for (int i = 0; i < 6; i++) {
	  DofEdge edge2 = elInfo2->getElement()->getEdge(i);
	  if (edge2.first == *(edge[0]) &&
	      edge2.second == *(edge[1])) {

697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
	    // We found the edge on the other element side on leaf level. Two
	    // cases may occure: either it is already a refinement edge, i.e,
	    // it has local edge number 0, or it is not a refinement edge. In
	    // the first case, we are fine and this leaf element may be set
	    // to all elements on the refinement edge. Otherwise, the element
	    // must be refined at least once to get a refinement edge.

	    if (i == 0) {
	      // Edge is refinement edge, so add it to refine list.

	      refineList.setElType(n_neigh, elInfo2->getType());
	      refineList.setElement(n_neigh, elInfo2->getElement());
	      n_neigh++;
	      
	      TraverseStack *tmpStack = stack;
	      stack = &stack2;
	      
	      if (getRefinePatch(&elInfo2, edge, 0, refineList, &n_neigh)) {
		getRefinePatch(&elInfo2, edge, 1, refineList, &n_neigh);
		bound = true;
	      }
	      
	      stack = tmpStack;
720
	      foundEdge = true;
721
722
723
	      break;
	    } else {
	      // Edge i not refinement edge, so refine the element further.
724

725
	      Element *el2 = elInfo2->getElement();
726
727
728
729
730
731
732
733
734
735
	      el2->setMark(std::max(el2->getMark(), 1));

	      TraverseStack *tmpStack = stack;
	      stack = &stack2;

	      elInfo2 = refineFunction(elInfo2);

	      stack = tmpStack;
	      break;
	    }
736
737
738
	  }
	}

739
740
741
	if (foundEdge)
	  break;

742
743
744
745
746
747
748
	elInfo2 = stack2.traverseNext(elInfo2);
      }

      TEST_EXIT_DBG(foundEdge)("Should not happen!\n");
    }
#endif

749
    // fill neighbour information inside the patch in the refinement list
750
    refineList.fillNeighbourRelations(n_neigh, bound);
751

752
    // ============ Check for periodic boundary ============
753

754
    DegreeOfFreedom *next_edge[2] = {NULL, NULL};
755
756
    DegreeOfFreedom *first_edge[2] = {edge[0], edge[1]};
    DegreeOfFreedom *last_edge[2] = {NULL, NULL};
757
    int n_neigh_periodic = 0;
758

759
760
    DegreeOfFreedom lastNewDof = -1;
    DegreeOfFreedom firstNewDof = -1;
761

762
    RCNeighbourList periodicList;
763

Thomas Witkowski's avatar
Thomas Witkowski committed
764
    while (edge[0] != NULL) {
765
766
767
      refineList.periodicSplit(edge, next_edge, 
			       &n_neigh, &n_neigh_periodic, 
			       periodicList);
768

769
770
      DegreeOfFreedom newDof = 
	refinePatch(edge, periodicList, n_neigh_periodic, bound);
771

772
773
      if (firstNewDof == -1)
	firstNewDof = newDof;
774

775
776
777
      if (lastNewDof != -1) {
	for (std::map<int, VertexVector*>::iterator it = mesh->getPeriodicAssociations().begin();
	     it != mesh->getPeriodicAssociations().end(); ++it) {
778
779
	  if (it->second) {
	    if (((*(it->second))[edge[0][0]] == last_edge[0][0] &&
Thomas Witkowski's avatar
Thomas Witkowski committed
780
781
782
		 (*(it->second))[edge[1][0]] == last_edge[1][0]) || 
		((*(it->second))[edge[0][0]] == last_edge[1][0] &&
		 (*(it->second))[edge[1][0]] == last_edge[0][0])) {
783
784
	      (*(it->second))[lastNewDof] = newDof;
	      (*(it->second))[newDof] = lastNewDof;
Thomas Witkowski's avatar
Thomas Witkowski committed
785
	    } 
786
787
788
	  }
	}
      }
789
      lastNewDof = newDof;
790
791
792
793
794
795
796
797

      last_edge[0] = edge[0];
      last_edge[1] = edge[1];

      edge[0] = next_edge[0];
      edge[1] = next_edge[1];
    }

798
799
800
    if (lastNewDof != firstNewDof) {
      for (std::map<int, VertexVector*>::iterator it = mesh->getPeriodicAssociations().begin();
	   it != mesh->getPeriodicAssociations().end(); ++it) {
801
802
	if (it->second) {
	  if (((*(it->second))[first_edge[0][0]] == last_edge[0][0] &&
Thomas Witkowski's avatar
Thomas Witkowski committed
803
804
805
	       (*(it->second))[first_edge[1][0]] == last_edge[1][0]) || 
	      ((*(it->second))[first_edge[0][0]] == last_edge[1][0] &&
	       (*(it->second))[first_edge[1][0]] == last_edge[0][0])) {	    
806
807
	    (*(it->second))[lastNewDof] = firstNewDof;
	    (*(it->second))[firstNewDof] = lastNewDof;
Thomas Witkowski's avatar
Thomas Witkowski committed
808
	  }
809
810
811
	}
      }
    }
812

813
814
    stack->update();

815
816
817
818
819
    return elInfo;
  }


  void FixRefinementPatch::getOtherEl(TraverseStack *stack, 
Thomas Witkowski's avatar
Thomas Witkowski committed
820
				      vector<EdgeInEl>& refineEdges)
821
  {
822
    FUNCNAME_DBG("FixRefinementPatch::getOtherEl()");
823
824
825

    if (!FixRefinementPatch::connectedEdges.empty()) {
      // === Get stack of current traverse. ===
Thomas Witkowski's avatar
Thomas Witkowski committed
826
827
      vector<ElInfo*> elInfos;
      vector<int> infos;      
828
829
830
831
832
833
834
835
836
837
      int stackUsed = stack->getStackData(elInfos, infos);         
      int checkIndex = stackUsed;
      int localEdgeNo = 0;
      ElInfo *elInfo = elInfos[stackUsed];

      // === Calculate the refinement edge number on the macro element level. ===
      
      for (int i = 0; i < elInfo->getLevel(); i++) {
	TEST_EXIT_DBG(checkIndex >= 1)("Should not happen!\n");
	
838

839
840
841
842
843
	int parentType = elInfos[checkIndex - 1]->getType();
	int ithParentChild = 0;
	if (elInfos[checkIndex - 1]->getElement()->getChild(1) ==
	    elInfos[checkIndex]->getElement())
	  ithParentChild = 1;
844
845
846
847
848
849
850

	// If local edge number if equal or greater to 4, its a new edge and
	// thus cannot be part of the parent edge.
 	if (localEdgeNo >= 4) {
 	  localEdgeNo = -1;
 	  break;
 	}
851