CoarseningManager2d.cc 10.1 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
#include "CoarseningManager2d.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 {

  /****************************************************************************/
  /*  coarseTriangle:  coarses a single element of the coarsening patch; dofs */
  /*  in the interior of the element are removed; dofs for higher order       */
  /*  at the boundary or the coarsening patch still belong to                 */
  /*  the parent. Do not remove them form the mesh!!!                         */
  /****************************************************************************/

  void CoarseningManager2d::coarsenTriangle(Triangle *el)
  {
    FUNCNAME("CoarseningManager2d::coarseTriangle");
    Triangle *child[2];

    child[0] = dynamic_cast<Triangle*>(const_cast<Element*>( el->getChild(0)));
    child[1] = dynamic_cast<Triangle*>(const_cast<Element*>( el->getChild(1))); 

28
    TEST_EXIT_DBG(child[0]->getMark() < 0  &&  child[1]->getMark() < 0)
29
30
31
32
33
34
35
36
37
38
39
40
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
89
90
91
92
93
94
95
96
97
98
99
100
101
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
      ("element %d with children[%d,%d] must not be coarsend!\n",
       el->getIndex(), child[0]->getIndex(), child[1]->getIndex());
  
    if (mesh->getNumberOfDOFs(EDGE))
      {
	/****************************************************************************/
	/*  remove dof from common edge of child[0] and child[1]                    */
	/****************************************************************************/
	mesh->freeDOF(const_cast<int*>( child[0]->getDOF(4)), EDGE);
      }

    if (mesh->getNumberOfDOFs(CENTER))
      {
	/****************************************************************************/
	/*  remove dof from the barycenters of child[0] and child[1]                */
	/****************************************************************************/
	int  node = mesh->getNode(CENTER);

	mesh->freeDOF(const_cast<int*>( child[0]->getDOF(node)), CENTER);
	mesh->freeDOF(const_cast<int*>( child[1]->getDOF(node)), CENTER);
      }

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

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


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

    el->incrementMark();

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

    return;
  }

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

  void CoarseningManager2d::coarsenPatch(RCNeighbourList *coarsenList, 
					 int              n_neigh, 
					 int              bound)
  {
    Triangle *el = dynamic_cast<Triangle*>(const_cast<Element*>( coarsenList->getElement(0)));
    Triangle *neigh = dynamic_cast<Triangle*>(const_cast<Element*>( coarsenList->getElement(1)));
    DegreeOfFreedom *dof[3];

    dof[0] = const_cast<int*>( el->getChild(0)->getDOF(2));
    if (mesh->getNumberOfDOFs(EDGE))
      {
	dof[1] = const_cast<int*>( el->getChild(0)->getDOF(3));
	dof[2] = const_cast<int*>( el->getChild(1)->getDOF(4));
      }

    int node = mesh->getNode(EDGE);
    if (mesh->getNumberOfDOFs(EDGE))
      {
	/****************************************************************************/
	/*  get new dof on el at the midpoint of the coarsening edge                */
	/****************************************************************************/
	if (!el->getDOF(node+2))
	  {
	    el->setDOF(node+2, mesh->getDOF(EDGE));
	    if (neigh) {
	      // 	// periodic boundary ?
	      // 	if(el->getDOF(0) != neigh->getDOF(0) &&
	      // 	   el->getDOF(1) != neigh->getDOF(0)) 
	      // 	{
	      // 	  neigh->setDOF(node+2, mesh->getDOF(EDGE));
	      // 	} else {
	      neigh->setDOF(node+2, const_cast<int*>( el->getDOF(node+2)));
	      // 	}
	    }
	  }
      }

    //   // periodic boundary?
    //   DegreeOfFreedom *neighDOF[3] = {NULL, NULL, NULL};
    //   RCNeighbourList *periodicCoarsenList = NULL;
    //   int n_neigh_periodic;
    //   if(neigh && 
    //      (neigh->getDOF(0) != el->getDOF(1)) &&
    //      (neigh->getDOF(0) != el->getDOF(0))) 
    //   {
    //     neighDOF[0] = const_cast<int*>(neigh->getChild(0)->getDOF(2));
    //     if (mesh->getNumberOfDOFs(EDGE)) {
    //       neighDOF[1] = const_cast<int*>(neigh->getChild(0)->getDOF(3));
    //       neighDOF[2] = const_cast<int*>(neigh->getChild(1)->getDOF(4));
    //       if (!neigh->getDOF(node+2)) {
    // 	neigh->setDOF(node+2, mesh->getDOF(EDGE));
    //       }
    //     }

    //     DegreeOfFreedom *edge[2] = { 
    //       const_cast<DegreeOfFreedom*>(el->getDOF(0)), 
    //       const_cast<DegreeOfFreedom*>(el->getDOF(1)) 
    //     };
    //     DegreeOfFreedom *periodicEdge[2] = { 
    //       const_cast<DegreeOfFreedom*>(neigh->getDOF(0)), 
    //       const_cast<DegreeOfFreedom*>(neigh->getDOF(1)) 
    //     };

    //     periodicCoarsenList = coarsenList->periodicSplit(edge,
    // 						     &n_neigh,
    // 						     periodicEdge,
    // 						     &n_neigh_periodic);
    //   }

    if (mesh->getNumberOfDOFs(EDGE)  ||  mesh->getNumberOfDOFs(CENTER)) {
      coarsenList->addDOFParents(n_neigh);
      //     if(periodicCoarsenList) {
      //       periodicCoarsenList->addDOFParents(n_neigh_periodic);
      //     }
    }

    /****************************************************************************/
    /*  restrict dof vectors to the parents on the patch                        */
    /****************************************************************************/
    int iadmin;
    int nrAdmin = mesh->getNumberOfDOFAdmin();
    for(iadmin = 0; iadmin < nrAdmin; iadmin++) {
156
      std::list<DOFIndexedBase*>::iterator it;
157
      DOFAdmin* admin = const_cast<DOFAdmin*>(&mesh->getDOFAdmin(iadmin));
158
      std::list<DOFIndexedBase*>::iterator end = admin->endDOFIndexed();    
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
      for(it = admin->beginDOFIndexed(); it != end; ++it) {
	(*it)->coarseRestrict(*coarsenList, n_neigh);
	//       if(periodicCoarsenList) {
	// 	(*it)->coarseRestrict(*periodicCoarsenList, n_neigh_periodic);
	//       }
      }
    }

    //   if(periodicCoarsenList) DELETE periodicCoarsenList;

    coarsenTriangle(el);

    if (neigh) coarsenTriangle(neigh);

    /****************************************************************************/
    /*  now, remove those dofs in the coarcening edge                           */
    /****************************************************************************/
    mesh->freeDOF(dof[0], VERTEX);
    if (mesh->getNumberOfDOFs(EDGE))
      {
	mesh->freeDOF(dof[1], EDGE);
	mesh->freeDOF(dof[2], EDGE);
      }

    mesh->incrementNumberOfVertices(-1);
    mesh->incrementNumberOfEdges(-1);

    //   // periodic boundary?
    //   if(neigh && 
    //      (neigh->getDOF(0) != el->getDOF(1)) && 
    //      (neigh->getDOF(0) != el->getDOF(0))) 
    //   {
    //     mesh->freeDOF(neighDOF[0], VERTEX);
    //     if (mesh->getNumberOfDOFs(EDGE)) {
    //       mesh->freeDOF(neighDOF[1], EDGE);
    //       mesh->freeDOF(neighDOF[2], EDGE);
    //     }
    //     mesh->incrementNumberOfVertices(-1);
    //     mesh->incrementNumberOfEdges(-1);    
    //   }

    return;
  }

  int CoarseningManager2d::coarsenFunction(ElInfo *el_info)
  {
205
    Triangle *el = dynamic_cast<Triangle*>(const_cast<Element*>( el_info->getElement()));
206
    DegreeOfFreedom *edge[2];
207
    int n_neigh, bound = 0;
208
209
210
211
    RCNeighbourList coarse_list(2);

    coarse_list.setCoarseningManager(this);

212
213
214
215
216
217
218
219
220
221
222
223
    if (el->getMark() >= 0)
      return 0; // el must not be coarsend, return
    if (!(el->getChild(0))) 
      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;
    }
224

225
226
227
228
229
230
231
    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;
    }
232
233
234
235
236

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

237
238
239
240
241
242
243
    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));
    } else {
      edge[1] = const_cast<int*>( el->getDOF(0));
      edge[0] = const_cast<int*>( el->getDOF(1));
    }
244
245
246
247

    coarse_list.setElement(0, el, true);

    n_neigh = 1;
248
249
250
251
    if (coarse_list.setElement(1, el_info->getNeighbour(2))) {
      n_neigh = 2;
      coarse_list.setCoarsePatch(1, el_info->getOppVertex(2) == 2);
    }
252
253
254
255
256
257
258
259
260
  
    /****************************************************************************/
    /*  check wether we can coarsen the patch or not                            */
    /****************************************************************************/

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

261
    if (coarse_list.doCoarsePatch(n_neigh)) {
262
263
264
265
266
267
      int n_neigh_periodic;

      DegreeOfFreedom *next_edge[2];

      RCNeighbourList *periodicList;

268
      while (edge[0] != NULL) {
269
270
271
272
273
	periodicList = coarse_list.periodicSplit(edge, 
						 next_edge,
						 &n_neigh,
						 &n_neigh_periodic);

274
	TEST_EXIT_DBG(periodicList)("periodicList = NULL\n");
275
276
277
278
279
280
281
282
283
284
285
286

	coarsenPatch(periodicList, n_neigh_periodic, bound);

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

    return 0;
  }

}