CoarseningManager3d.cc 15.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "CoarseningManager3d.h"
#include "Mesh.h"
#include "AdaptStationary.h"
#include "AdaptInstationary.h"
#include "Traverse.h"
#include "MacroElement.h"
#include "RCNeighbourList.h"
#include "FixVec.h"
#include "DOFIndexed.h"

namespace AMDiS {

  int CoarseningManager3d::coarsenFunction(ElInfo *el_info)
  {
15
    Tetrahedron *el = dynamic_cast<Tetrahedron*>(const_cast<Element*>(el_info->getElement()));
16
    DegreeOfFreedom *edge[2];
17
18
    int n_neigh, bound = 0;
    ElInfo *elinfo = el_info;
19
20
21
    RCNeighbourList *coarsenList;


22
23
24
25
    if (el->getMark() >= 0) 
      return 0; // el must not be coarsend, return :-(
    if (el->isLeaf()) 
      return 0;       // single leaves don't get coarsened
26
27

    if (el->getChild(0)->getMark() >= 0  || el->getChild(1)->getMark() >= 0) {
28
29
      // one of the children must not be coarsend; return :-(

30
31
32
33
34
      el->setMark(0);
      return 0;
    }

    if (!(el->getChild(0)->isLeaf()) || !(el->getChild(1)->isLeaf())) {
35
      // one of the children is not a leaf element; try again later on
36
37
38
      doMore = true;
      return 0;
    }
39

40
41
42
    /****************************************************************************/
    /*  get a list for storing all elements at the coarsening edge and fill it  */
    /****************************************************************************/
Thomas Witkowski's avatar
Thomas Witkowski committed
43
    coarsenList = new RCNeighbourList(mesh->getMaxEdgeNeigh());
44
45
46
47
48
49
    coarsenList->setCoarseningManager(this);

    /****************************************************************************/
    /*  give the refinement edge the right orientation                          */
    /****************************************************************************/

50
51
52
    if (el->getDof(0,0) < el->getDof(1,0)) {
      edge[0] = const_cast<int*>(el->getDof(0));
      edge[1] = const_cast<int*>(el->getDof(1));
53
    } else {
54
55
      edge[1] = const_cast<int*>(el->getDof(0));
      edge[0] = const_cast<int*>(el->getDof(1));
56
57
58
59
60
61
    }

    coarsenList->setElement(0, el, true);
    n_neigh = 1;

    coarsenList->setOppVertex(0,0,0);
62
    coarsenList->setElType(0, el_info->getType());
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
    bound = false;
    if (getCoarsenPatch(elinfo, edge, 0, coarsenList, &n_neigh)) {
      getCoarsenPatch(elinfo, edge, 1, coarsenList, &n_neigh);
      bound = true;
    }
    coarsenList->getNeighOnPatch(n_neigh, bound);

    /****************************************************************************/
    /*  check wether we can coarsen the patch or not                            */
    /****************************************************************************/

    // ==========================================================================
    // === check for periodic boundary ==========================================
    // ==========================================================================

78
    if (coarsenList->doCoarsePatch(n_neigh)) {
79
80
81
82
      int n_neigh_periodic;
      DegreeOfFreedom *next_edge[2];
      RCNeighbourList *periodicList;

83
      while (edge[0] != NULL) {
84
85
86
87
88
	periodicList = coarsenList->periodicSplit(edge, 
						  next_edge,
						  &n_neigh,
						  &n_neigh_periodic);

89
	TEST_EXIT_DBG(periodicList)("periodicList = NULL\n");
90
91
92
93
94
95
96
97

	coarsenPatch(periodicList, n_neigh_periodic, bound);

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

Thomas Witkowski's avatar
Thomas Witkowski committed
98
    delete coarsenList;
99
100
101
102
103
104
105
106
107
108
109
110

    return 0;
  }

  /*****************************************************************************/
  /*  coarsenTetrahedron:  coarses a single element of the coarsening patch;   */
  /*  dofs in the interior of the element are removed and dofs in the faces of */
  /*  the element are removed if neighbour has been coarsend or if the face    */
  /*  is part of the domains boundary                                          */
  /*****************************************************************************/

  void CoarseningManager3d::coarsenTetrahedron(RCNeighbourList *coarsenList, 
111
					       int index)
112
  {
113
114
    Tetrahedron *el = dynamic_cast<Tetrahedron*>(const_cast<Element*>(coarsenList->getElement(index)));
    Tetrahedron *child[2];
115
    Tetrahedron *neigh;
116
    int dir, el_type, i, node, opp_v;
117

118
119
    child[0] = dynamic_cast<Tetrahedron*>(const_cast<Element*>(el->getChild(0)));
    child[1] = dynamic_cast<Tetrahedron*>(const_cast<Element*>(el->getChild(1)));
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
    el_type = coarsenList->getType(index);

    /****************************************************************************/
    /*  Information about patch neighbours is still valid! But edge and face    */
    /*  dof's in a common face of patch neighbours have to be removed           */
    /****************************************************************************/

    for (dir = 0; dir < 2; dir++) {
      neigh = dynamic_cast<Tetrahedron*>(const_cast<Element*>( coarsenList->getNeighbourElement(index, dir)));
      opp_v = coarsenList->getOppVertex(index, dir);

      if (!neigh  ||  neigh->isLeaf()) {
	/****************************************************************************/
	/*  boundary face or  neigh has been coarsend: free the dof's in the face   */
	/****************************************************************************/

136
	if (mesh->getNumberOfDofs(EDGE)) {
137
	  node = mesh->getNode(EDGE) + Tetrahedron::nChildEdge[el_type][0][dir];
138
	  mesh->freeDof(const_cast<int*>( child[0]->getDof(node)), EDGE);
139
	}
140
	if (mesh->getNumberOfDofs(FACE)) {
141
	  node = mesh->getNode(FACE) + Tetrahedron::nChildFace[el_type][0][dir];
142
	  mesh->freeDof(const_cast<int*>( child[0]->getDof(node)), FACE);
143
	  node = mesh->getNode(FACE) + Tetrahedron::nChildFace[el_type][1][dir];
144
	  mesh->freeDof(const_cast<int*>( child[1]->getDof(node)), FACE);
145
146
147
148
149
150
151
152
153
	}
      }
    }

    /****************************************************************************/
    /*  finally remove the interior dof's: in the common face of child[0] and   */
    /*  child[1] and at the two barycenter                                      */
    /****************************************************************************/

154
    if (mesh->getNumberOfDofs(FACE)) {
155
      node = mesh->getNode(FACE);
156
      mesh->freeDof(const_cast<int*>( child[0]->getDof(node)), FACE);
157
158
159
    }


160
    if (mesh->getNumberOfDofs(CENTER)) {
161
162
      node = mesh->getNode(CENTER);
      for (i = 0; i < 2; i++)
163
	mesh->freeDof(const_cast<int*>( child[i]->getDof(node)), CENTER);
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
    }

    /****************************************************************************/
    /*  get new data on parent and transfer data from children to parent        */
    /****************************************************************************/

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

    el->setFirstChild(NULL);
    el->setSecondChild(NULL);

    mesh->freeElement(child[0]);
    mesh->freeElement(child[1]);

    el->incrementMark();
  
    mesh->incrementNumberOfLeaves(-1);
    mesh->incrementNumberOfElements(-2);
  }

  /****************************************************************************/
  /*  get_coarse_patch:  gets the patch for coarsening starting on element    */
  /*  el_info->el in direction of neighbour [3-dir]; returns 1 if a boundary  */
  /*  reached and 0 if we come back to the starting element.                  */
  /*                                                                          */
  /*  if NEIGH_IN_EL we only can find the complete coarsening patch if the    */
  /*  can be coarsend; otherwise neighbour information is not valid for       */
  /*  parents; in such situation we stop looping around the edge and return 0 */
  /*                                                                          */
  /*  if !NEIGH_IN_EL we complete the loop also in the case of a incompatible */
  /*  coarsening patch since then all marks of patch elements are reset by    */
  /*  do_coarse_patch() and this minimizes calls of traverse_neighbour();     */
  /*  if we reach a boundary while looping around the edge we loop back to    */
  /*  the starting element before we return                                   */
  /****************************************************************************/
 
200
  bool CoarseningManager3d::getCoarsenPatch(ElInfo *el_info, 
201
					    DegreeOfFreedom *edge[2],
202
					    int dir, 
203
					    RCNeighbourList *coarsenList, 
204
					    int *n_neigh)
205
206
207
208
  {
    FUNCNAME("CoarseningManager3d::getCoarsenPatch");
    ElInfo *neigh_info;
    Tetrahedron *el, *neigh;
209
    int i, j, k, opp_v, edge_no;
210
211
212
213
214
215
216
217

    static unsigned char next_el[6][2] = {{3,2},
					  {1,3},
					  {1,2},
					  {0,3},
					  {0,2},
					  {0,1}};

218
219
220
    el = dynamic_cast<Tetrahedron*>(const_cast<Element*>(el_info->getElement()));
    neigh = dynamic_cast<Tetrahedron*>(const_cast<Element*>(el_info->getNeighbour(3 - dir)));
    if (neigh == NULL)
221
222
223
224
225
      return true;

    opp_v = el_info->getOppVertex(3-dir);

    neigh_info = stack->traverseNeighbour3d(el_info, 3-dir);
226
    TEST_EXIT_DBG(neigh == neigh_info->getElement())
227
228
229
230
231
232
233
234
      ("neigh %d and neigh_info->el %d are not identical\n", 
       neigh->getIndex(), neigh_info->getElement()->getIndex());
    /****************************************************************************/
    /*  we have to go back to the starting element via opp_v values             */
    /*  correct information is produce by get_neigh_on_patch()                  */
    /****************************************************************************/
    coarsenList->setOppVertex(*n_neigh, 0, opp_v);  
    coarsenList->setElement(*n_neigh, neigh);
235
    coarsenList->setElType(*n_neigh, neigh_info->getType()); 
236
237
238
239
240
  
    int n_vertices = mesh->getGeo(VERTEX);
			 
    while (neigh != el) {
      for (j = 0; j < n_vertices; j++)
241
	if (neigh->getDof(j) == edge[0])  break;
242
      for (k = 0; k < n_vertices; k++)
243
	if (neigh->getDof(k) == edge[1])  break;
244

245
      if (j > 3 || k > 3) {
246
	for (j = 0; j < n_vertices; j++)
247
	  if (mesh->associated(neigh->getDof(j, 0), edge[0][0]))  break;
248
	for (k = 0; k < n_vertices; k++)
249
	  if (mesh->associated(neigh->getDof(k, 0), edge[1][0]))  break;
250
     
251
	TEST_EXIT_DBG(j < n_vertices  &&  k < n_vertices)
252
	  ("dof %d or dof %d not found on element %d with nodes (%d %d %d %d)\n", 
253
254
	   edge[0][0], edge[1][0], neigh->getIndex(), neigh->getDof(0,0),
	   neigh->getDof(1,0), neigh->getDof(2,0), neigh->getDof(3,0));
255
      }
256
      edge_no = Tetrahedron::edgeOfDofs[j][k];
257
258
259
260
261
262
263
264
265
266
267
268
269
270
      coarsenList->setCoarsePatch(*n_neigh, edge_no == 0);

      /****************************************************************************/
      /*  get the direction of the next neighbour				    */
      /****************************************************************************/

      if (next_el[edge_no][0] != opp_v)
	i = next_el[edge_no][0];
      else
	i = next_el[edge_no][1];

      ++*n_neigh;

      opp_v = neigh_info->getOppVertex(i);
271
272
      neigh = dynamic_cast<Tetrahedron*>(const_cast<Element*>(neigh_info->getNeighbour(i)));
      if (neigh) {
273
	neigh_info = stack->traverseNeighbour3d(neigh_info, i);
274
	TEST_EXIT_DBG(neigh == neigh_info->getElement())
275
276
277
278
279
280
281
282
	  ("neigh %d and neigh_info->el %d are not identical\n", 
	   neigh->getIndex(), neigh_info->getElement()->getIndex());
	/****************************************************************************/
	/*  we have to go back to the starting element via opp_v values             */
	/*  correct information is produce by get_neigh_on_patch()                  */
	/****************************************************************************/
	coarsenList->setOppVertex(*n_neigh, 0, opp_v);  
	coarsenList->setElement(*n_neigh, neigh);
283
284
	coarsenList->setElType(*n_neigh, neigh_info->getType());
      } else {
285
	break;
286
      }
287
288
289
290
291
292
293
294
295
296
297
298
    }

    if (neigh == el)  
      return false;

    /****************************************************************************/
    /*  the domain's boundary is reached; loop back to the starting el          */
    /****************************************************************************/

    i = *n_neigh-1;
    opp_v = coarsenList->getOppVertex(i, 0);
    do {
299
      TEST_EXIT_DBG(neigh_info->getNeighbour(opp_v)  &&  i > 0)
300
301
302
303
304
305
306
307
308
309
310
311
312
313
	("while looping back domains boundary was reached or i == 0\n");
      opp_v = coarsenList->getOppVertex(i--, 0);
      neigh_info = stack->traverseNeighbour3d(neigh_info, opp_v);
    } while(neigh_info->getElement() != el);

    return true;
  }

  /****************************************************************************/
  /*  coarse_patch: first rebuild the dofs on the parents then do restriction */
  /*  of data (if possible) and finally coarsen the patch elements            */
  /****************************************************************************/

  void CoarseningManager3d::coarsenPatch(RCNeighbourList *coarsenList, 
314
315
					 int n_neigh, 
					 int bound)
316
317
  {
    Tetrahedron *el = dynamic_cast<Tetrahedron*>(const_cast<Element*>( coarsenList->getElement(0)));
318
    int i, node;
319
320
    DegreeOfFreedom *dof;

321
    if (mesh->getNumberOfDofs(EDGE)) {
322
323
324
325
      /****************************************************************************/
      /*  get dof for coarsening edge                                             */
      /****************************************************************************/
      node = mesh->getNode(EDGE);
326
      if (!(dof = const_cast<int*>( el->getDof(node))))
327
	dof = mesh->getDof(EDGE);
328
    } else {
329
      dof = NULL;
330
    }
331

332
333
334
    if (mesh->getNumberOfDofs(EDGE) || 
	mesh->getNumberOfDofs(FACE) || 
	mesh->getNumberOfDofs(CENTER)) {     
335
336
337
338
      for (i = 0; i < n_neigh; i++)
	coarsenList->addDOFParent(i, dof);
    }
      
339
340
341
342
343
344

    /****************************************************************************/
    /*  restrict dof vectors to the parents on the patch                        */
    /****************************************************************************/

    int nrAdmin = mesh->getNumberOfDOFAdmin();
345
    for (int iadmin = 0; iadmin < nrAdmin; iadmin++) {
346
      std::list<DOFIndexedBase*>::iterator it;
347
      DOFAdmin* admin = const_cast<DOFAdmin*>(&mesh->getDofAdmin(iadmin));
348
      std::list<DOFIndexedBase*>::iterator end = admin->endDOFIndexed();
349
      for (it = admin->beginDOFIndexed(); it != end; ++it)
350
351
352
353
354
355
356
357
358
	(*it)->coarseRestrict(*coarsenList, n_neigh);
    } 

    /****************************************************************************/
    /*  and now start to coarsen the patch:                                     */
    /****************************************************************************/
    /*  remove dof's of the coarsening edge                                     */
    /****************************************************************************/

359
    mesh->freeDof(const_cast<int*>( el->getChild(0)->getDof(3)), VERTEX);
360
361
    mesh->incrementNumberOfVertices(-1);

362
    if (mesh->getNumberOfDofs(EDGE)) {
363
      node = mesh->getNode(EDGE) + 2;
364
365
      mesh->freeDof(const_cast<int*>( el->getChild(0)->getDof(node)), EDGE);
      mesh->freeDof(const_cast<int*>( el->getChild(1)->getDof(node)), EDGE);
366
367
    }

368
369
370
371
372
373
374
    if (coarsenList->getElement(0)->isNewCoordSet())
      coarsenList->getElement(0)->eraseNewCoord();   

    for (i = 0; i < n_neigh; i++) {
      coarsenList->getElement(i)->setNewCoord(NULL);
      coarsenTetrahedron(coarsenList, i);
    }
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391

    /****************************************************************************/
    /*  if the coarsening edge is an interior edge there are  n_neigh + 1 edges */
    /*  and 2*n_neigh + 1 faces removed; if it is a boundary edge it is one     */
    /*  more edge and one more face                                             */
    /****************************************************************************/

    if (bound) {
      mesh->incrementNumberOfEdges(-(n_neigh + 2));
      mesh->incrementNumberOfFaces(-(2*n_neigh + 1));
    } else {
      mesh->incrementNumberOfEdges(-(n_neigh + 1));
      mesh->incrementNumberOfFaces(-(2*n_neigh));
    }
  }

}