CoarseningManager3d.cc 16 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#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)
  {
    Tetrahedron     *el = dynamic_cast<Tetrahedron*>(const_cast<Element*>( el_info->getElement()));
    DegreeOfFreedom *edge[2];
    int             n_neigh, bound = 0;

    ElInfo *elinfo =  el_info;
    RCNeighbourList *coarsenList;


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

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

    if (!(el->getChild(0)->isLeaf()) || !(el->getChild(1)->isLeaf())) {
      /****************************************************************************/
      /*  one of the children is not a leaf element; try again later on           */
      /****************************************************************************/
      doMore = true;
      return 0;
    }
    /****************************************************************************/
    /*  get a list for storing all elements at the coarsening edge and fill it  */
    /****************************************************************************/
Thomas Witkowski's avatar
Thomas Witkowski committed
44
    coarsenList = new RCNeighbourList(mesh->getMaxEdgeNeigh());
45
46
47
48
49
50
51
    coarsenList->setCoarseningManager(this);

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

    if (el->getDOF(0,0) < el->getDOF(1,0)) {
52
53
      edge[0] = const_cast<int*>(el->getDOF(0));
      edge[1] = const_cast<int*>(el->getDOF(1));
54
    } else {
55
56
      edge[1] = const_cast<int*>(el->getDOF(0));
      edge[0] = const_cast<int*>(el->getDOF(1));
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
    }

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

    coarsenList->setOppVertex(0,0,0);
    coarsenList->setElType(0, (dynamic_cast<ElInfo3d*>( el_info)->getType()));
    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 ==========================================
    // ==========================================================================

79
    if (coarsenList->doCoarsePatch(n_neigh)) {
80
81
82
83
84
85
      int n_neigh_periodic;

      DegreeOfFreedom *next_edge[2];

      RCNeighbourList *periodicList;

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

92
	TEST_EXIT_DBG(periodicList)("periodicList = NULL\n");
93
94
95
96
97
98
99
100

	coarsenPatch(periodicList, n_neigh_periodic, bound);

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

Thomas Witkowski's avatar
Thomas Witkowski committed
101
    delete coarsenList;
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230

    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, 
					       int              index)
  {
    Tetrahedron *el = dynamic_cast<Tetrahedron*>(const_cast<Element*>( coarsenList->getElement(index))), *child[2];
    Tetrahedron *neigh;
    int      dir, el_type, i, node, opp_v;

    child[0] = dynamic_cast<Tetrahedron*>(const_cast<Element*>( el->getChild(0)));
    child[1] = dynamic_cast<Tetrahedron*>(const_cast<Element*>( el->getChild(1)));
    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   */
	/****************************************************************************/

	if (mesh->getNumberOfDOFs(EDGE)) {
	  node = mesh->getNode(EDGE) + Tetrahedron::nChildEdge[el_type][0][dir];
	  mesh->freeDOF(const_cast<int*>( child[0]->getDOF(node)), EDGE);
	}
	if (mesh->getNumberOfDOFs(FACE))
	  {
	    node = mesh->getNode(FACE) + Tetrahedron::nChildFace[el_type][0][dir];
	    mesh->freeDOF(const_cast<int*>( child[0]->getDOF(node)), FACE);
	    node = mesh->getNode(FACE) + Tetrahedron::nChildFace[el_type][1][dir];
	    mesh->freeDOF(const_cast<int*>( child[1]->getDOF(node)), FACE);
	  }
      }
    }

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

    if (mesh->getNumberOfDOFs(FACE)) {
      node = mesh->getNode(FACE);
      mesh->freeDOF(const_cast<int*>( child[0]->getDOF(node)), FACE);
    }


    if (mesh->getNumberOfDOFs(CENTER)) {
      node = mesh->getNode(CENTER);
      for (i = 0; i < 2; i++)
	mesh->freeDOF(const_cast<int*>( child[i]->getDOF(node)), CENTER);
    }

    /****************************************************************************/
    /*  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);

    return;
  }

  /****************************************************************************/
  /*  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                                   */
  /****************************************************************************/
 
  bool CoarseningManager3d::getCoarsenPatch(ElInfo          *el_info, 
					    DegreeOfFreedom *edge[2],
					    int              dir, 
					    RCNeighbourList *coarsenList, 
					    int             *n_neigh)
  {
    FUNCNAME("CoarseningManager3d::getCoarsenPatch");
    ElInfo *neigh_info;
    Tetrahedron *el, *neigh;
    int         i, j, k, opp_v, edge_no;

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

    el = dynamic_cast<Tetrahedron*>(const_cast<Element*>( el_info->getElement()));

    if ((neigh = dynamic_cast<Tetrahedron*>(const_cast<Element*>( el_info->getNeighbour(3-dir)))) == NULL)  
      return true;

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

    neigh_info = stack->traverseNeighbour3d(el_info, 3-dir);
231
    TEST_EXIT_DBG(neigh == neigh_info->getElement())
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
      ("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);
    coarsenList->setElType(*n_neigh, (dynamic_cast<ElInfo3d*>( neigh_info))->getType()); 
  
    int n_vertices = mesh->getGeo(VERTEX);
			 
    while (neigh != el) {
      for (j = 0; j < n_vertices; j++)
	if (neigh->getDOF(j) == edge[0])  break;
      for (k = 0; k < n_vertices; k++)
	if (neigh->getDOF(k) == edge[1])  break;

      if(j > 3 || k > 3) {
	for (j = 0; j < n_vertices; j++)
	  if (mesh->associated(neigh->getDOF(j, 0), edge[0][0]))  break;
	for (k = 0; k < n_vertices; k++)
	  if (mesh->associated(neigh->getDOF(k, 0), edge[1][0]))  break;
     
256
	TEST_EXIT_DBG(j < n_vertices  &&  k < n_vertices)
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
	  ("dof %d or dof %d not found on element %d with nodes (%d %d %d %d)\n", 
	   edge[0][0], edge[1][0], neigh->getIndex(), neigh->getDOF(0,0),
	   neigh->getDOF(1,0), neigh->getDOF(2,0), neigh->getDOF(3,0));
      }
      edge_no = Tetrahedron::edgeOfDOFs[j][k];
      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);
      if ((neigh = dynamic_cast<Tetrahedron*>(const_cast<Element*>( neigh_info->getNeighbour(i))))) {
	neigh_info = stack->traverseNeighbour3d(neigh_info, i);
278
	TEST_EXIT_DBG(neigh == neigh_info->getElement())
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
	  ("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);
	coarsenList->setElType(*n_neigh, (dynamic_cast<ElInfo3d*>( neigh_info))->getType());
      } else
	break;
    }

    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 {
302
      TEST_EXIT_DBG(neigh_info->getNeighbour(opp_v)  &&  i > 0)
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
	("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, 
					 int              n_neigh, 
					 int              bound)
  {
    Tetrahedron *el = dynamic_cast<Tetrahedron*>(const_cast<Element*>( coarsenList->getElement(0)));
    int      i, node;
    DegreeOfFreedom *dof;

    if (mesh->getNumberOfDOFs(EDGE)) {
      /****************************************************************************/
      /*  get dof for coarsening edge                                             */
      /****************************************************************************/
      node = mesh->getNode(EDGE);
      if (!(dof = const_cast<int*>( el->getDOF(node))))
	dof = mesh->getDOF(EDGE);
    } else
      dof = NULL;

    if (mesh->getNumberOfDOFs(EDGE) || 
	mesh->getNumberOfDOFs(FACE) || 
	mesh->getNumberOfDOFs(CENTER))
      {
	for (i = 0; i < n_neigh; i++)
	  coarsenList->addDOFParent(i, dof);
      }

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

    int iadmin;
    int nrAdmin = mesh->getNumberOfDOFAdmin();
    for(iadmin = 0; iadmin < nrAdmin; iadmin++) {
349
      std::list<DOFIndexedBase*>::iterator it;
350
      DOFAdmin* admin = const_cast<DOFAdmin*>(&mesh->getDOFAdmin(iadmin));
351
      std::list<DOFIndexedBase*>::iterator end = admin->endDOFIndexed();
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
      for(it = admin->beginDOFIndexed(); it != end; ++it)
	(*it)->coarseRestrict(*coarsenList, n_neigh);
    } 

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

    mesh->freeDOF(const_cast<int*>( el->getChild(0)->getDOF(3)), VERTEX);
    mesh->incrementNumberOfVertices(-1);

    if (mesh->getNumberOfDOFs(EDGE))
      {
	node = mesh->getNode(EDGE)+2;
	mesh->freeDOF(const_cast<int*>( el->getChild(0)->getDOF(node)), EDGE);
	mesh->freeDOF(const_cast<int*>( el->getChild(1)->getDOF(node)), EDGE);
      }

    if (coarsenList->getElement(0)->isNewCoordSet()) {
      coarsenList->getElement(0)->eraseNewCoord();
    }

    for (i = 0; i < n_neigh; i++)
      {
	coarsenList->getElement(i)->setNewCoord(NULL);
	coarsenTetrahedron(coarsenList, i);
      }

    /****************************************************************************/
    /*  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));
    }

    return;
  }

}