CoarseningManager2d.cc 6.49 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
28
29
30
31
32
33
#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)
  {
34
    FUNCNAME("CoarseningManager2d::coarseTriangle()");
35

36
37
38
    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))); 
39

40
    TEST_EXIT_DBG(child[0]->getMark() < 0  &&  child[1]->getMark() < 0)
41
42
      ("element %d with children[%d,%d] must not be coarsend!\n",
       el->getIndex(), child[0]->getIndex(), child[1]->getIndex());
43
44

    // remove dof from common edge of child[0] and child[1]  
45
    if (mesh->getNumberOfDofs(EDGE))
46
      mesh->freeDof(const_cast<int*>(child[0]->getDof(4)), EDGE);
47

48
    // remove dof from the barycenters of child[0] and child[1] 
49
    if (mesh->getNumberOfDofs(CENTER)) {
50
      int node = mesh->getNode(CENTER);
51
      
52
53
      mesh->freeDof(const_cast<int*>(child[0]->getDof(node)), CENTER);
      mesh->freeDof(const_cast<int*>(child[1]->getDof(node)), CENTER);
54
55
    }
    
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
    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);
  }

  /****************************************************************************/
  /*  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, 
77
78
					 int n_neigh, 
					 int bound)
79
  {
80
81
82
83
    Triangle *el = 
      dynamic_cast<Triangle*>(const_cast<Element*>(coarsenList->getElement(0)));
    Triangle *neigh = 
      dynamic_cast<Triangle*>(const_cast<Element*>(coarsenList->getElement(1)));
84
85
    DegreeOfFreedom *dof[3];

86
    dof[0] = const_cast<int*>(el->getChild(0)->getDof(2));
87
    if (mesh->getNumberOfDofs(EDGE)) {
88
89
      dof[1] = const_cast<int*>(el->getChild(0)->getDof(3));
      dof[2] = const_cast<int*>(el->getChild(1)->getDof(4));
90
91
    } else {
      dof[1] = dof[2] = 0;
92
    }
93

94
    if (mesh->getNumberOfDofs(EDGE)) {
95
96
97
      int node = mesh->getNode(EDGE);
      // get new dof on el at the midpoint of the coarsening edge

98
99
      if (!el->getDof(node + 2)) {
	el->setDof(node + 2, mesh->getDof(EDGE));
100
	if (neigh)
101
	  neigh->setDof(node + 2, const_cast<int*>(el->getDof(node + 2)));
102
      }
103
    }
104

105
    if (mesh->getNumberOfDofs(EDGE) || mesh->getNumberOfDofs(CENTER))
106
107
108
      coarsenList->addDOFParents(n_neigh);    

    // restrict dof vectors to the parents on the patch 
109
110

    int nrAdmin = mesh->getNumberOfDOFAdmin();
111
    for (int iadmin = 0; iadmin < nrAdmin; iadmin++) {
112
      DOFAdmin* admin = const_cast<DOFAdmin*>(&mesh->getDofAdmin(iadmin));
113
114
115
      for (std::list<DOFIndexedBase*>::iterator it = admin->beginDOFIndexed(); 
	   it != admin->endDOFIndexed(); ++it)	
	(*it)->coarseRestrict(*coarsenList, n_neigh);            
116
117
118
119
    }

    coarsenTriangle(el);

120
121
    if (neigh) 
      coarsenTriangle(neigh);
122

123
124
    // now, remove those dofs in the coarcening edge 

125
    mesh->freeDof(dof[0], VERTEX);
126
    if (mesh->getNumberOfDofs(EDGE)) {
127
128
      mesh->freeDof(dof[1], EDGE);
      mesh->freeDof(dof[2], EDGE);
129
    }
130
131
132
133
134

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

135

136
137
  int CoarseningManager2d::coarsenFunction(ElInfo *el_info)
  {
138
    Triangle *el = dynamic_cast<Triangle*>(const_cast<Element*>(el_info->getElement()));
139
    DegreeOfFreedom *edge[2];
140
    int n_neigh, bound = 0;
141
142
143
144
    RCNeighbourList coarse_list(2);

    coarse_list.setCoarseningManager(this);

145
146
147
148
149
150
    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) {
151
      // one of the children must not be coarsend; return :( 
152
153
154
      el->setMark(0);
      return 0;
    }
155

156
    if (!el->getChild(0)->isLeaf() || !el->getChild(1)->isLeaf()) {
157
      // one of the children is not a leaf element; try again later on 
158
159
160
      doMore = true;
      return 0;
    }
161

162
    // give the refinement edge the right orientation
163

164
165
166
    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));
167
    } else {
168
169
      edge[1] = const_cast<int*>(el->getDof(0));
      edge[0] = const_cast<int*>(el->getDof(1));
170
    }
171
172
173
174

    coarse_list.setElement(0, el, true);

    n_neigh = 1;
175
176
177
178
    if (coarse_list.setElement(1, el_info->getNeighbour(2))) {
      n_neigh = 2;
      coarse_list.setCoarsePatch(1, el_info->getOppVertex(2) == 2);
    }
179
  
180
    // check wether we can coarsen the patch or not                            
181
182
183
184
185

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

186
    if (coarse_list.doCoarsePatch(n_neigh)) {
187
188
189
190
      int n_neigh_periodic;
      DegreeOfFreedom *next_edge[2];
      RCNeighbourList *periodicList;

191
      while (edge[0] != NULL) {
192
193
194
195
196
	periodicList = coarse_list.periodicSplit(edge, 
						 next_edge,
						 &n_neigh,
						 &n_neigh_periodic);

197
	TEST_EXIT_DBG(periodicList)("periodicList = NULL\n");
198
199
200
201
202
203
204
205
206
207
208
209

	coarsenPatch(periodicList, n_neigh_periodic, bound);

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

    return 0;
  }

}