4#ifndef DUNE_PDELAB_CONSTRAINTS_HANGINGNODEMANAGER_HH
5#define DUNE_PDELAB_CONSTRAINTS_HANGINGNODEMANAGER_HH
11#include"../common/geometrywrapper.hh"
16 template<
class Gr
id,
class BoundaryFunction>
17 class HangingNodeManager
21 enum{ verbosity = 2 };
23 enum{ verbosity = 0 };
25 typedef typename Grid::LeafIndexSet::IndexType IndexType;
32 unsigned short minimum_level;
35 unsigned short maximum_level;
38 unsigned short minimum_touching_level;
42 void addLevel(
unsigned short level)
44 minimum_level =
std::min(minimum_level,level);
45 maximum_level =
std::max(maximum_level,level);
48 void addTouchingLevel(
unsigned short level)
50 minimum_touching_level =
std::min(minimum_touching_level,level);
53 NodeInfo() : minimum_level(
std::numeric_limits<unsigned short>::
max()),
55 minimum_touching_level(
std::numeric_limits<unsigned short>::
max()),
60 std::vector<NodeInfo> node_info;
66 friend class HangingNodeManager;
73 inline bool isBoundary()
const {
return is_boundary; }
74 inline bool isHanging()
const {
return is_hanging; }
76 NodeState() :is_boundary(false),
84 typedef typename GridView::template Codim<0>::Entity Cell;
86 typedef typename GridView::template Codim<0>::Iterator Iterator;
88 typedef typename GridView::Grid::ctype ctype;
95 const BoundaryFunction & boundaryFunction;
96 CellMapper cell_mapper;
105 node_info = std::vector<NodeInfo>(indexSet.size(dim));
107 GridView gv = grid.leafGridView();
110 for(
const auto& cell : elements(gv)) {
115 const unsigned short level = cell.level();
118 const IndexType v_size = reference_element.size(dim);
123 for(IndexType i=0; i<v_size; ++i){
124 const IndexType v_globalindex = indexSet.subIndex(cell,i,dim);
125 NodeInfo& ni = node_info[v_globalindex];
130 std::cout <<
" cell-id=" << cell_mapper.index(cell);
131 std::cout <<
" level=" << level;
132 std::cout <<
" v_size=" << v_size;
133 std::cout <<
" v_globalindex = " << v_globalindex;
134 std::cout <<
" maximum_level = " << ni.maximum_level;
135 std::cout <<
" minimum_level = " << ni.minimum_level;
136 std::cout << std::endl;
147 unsigned int intersection_index = 0;
148 for(
const auto& intersection : intersections(gv,cell)) {
149 ++intersection_index;
153 const int eLocalIndex = intersection.indexInInside();
154 const int e_level = intersection.inside().level();
157 const int e_v_size = reference_element.size(eLocalIndex,1,dim);
159 if(intersection.boundary()) {
162 for(
int i=0; i<e_v_size;++i){
163 const int e_v_index = reference_element.subEntity(eLocalIndex,1,i,dim);
164 const IndexType v_globalindex = indexSet.subIndex(cell,e_v_index,dim);
166 const FacePoint facelocal_position = reference_face_element.position(i,dim-1);
178 NodeInfo& ni = node_info[v_globalindex];
179 ni.is_boundary = boundaryFunction.isDirichlet(IntersectionGeometry<Intersection>(intersection,intersection_index),facelocal_position);
180 ni.addTouchingLevel(e_level);
187 const int f_level = intersection.outside().level();
190 if(intersection.conforming())
194 assert(e_level != f_level);
198 if(e_level < f_level)
202 for(
int i=0; i<e_v_size;++i){
203 const int e_v_index = reference_element.subEntity(eLocalIndex,1,i,dim);
204 const IndexType v_globalindex = indexSet.subIndex(cell,e_v_index,dim);
206 node_info[v_globalindex].addTouchingLevel(f_level);
214 HangingNodeManager(Grid & _grid,
const BoundaryFunction & _boundaryFunction)
216 boundaryFunction(_boundaryFunction),
220 const std::vector<NodeState> hangingNodes(
const Cell& e)
const
223 std::vector<NodeState> is_hanging;
228 const IndexType v_size = reference_element.size(dim);
231 is_hanging.resize(v_size);
236 for(IndexType i=0; i<v_size; ++i){
237 const IndexType v_globalindex = indexSet.subIndex(e,i,dim);
242 const NodeInfo & v_info = node_info[v_globalindex];
243 if(v_info.minimum_touching_level < v_info.minimum_level){
244 is_hanging[i].is_hanging =
true;
245 is_hanging[i].is_boundary = v_info.is_boundary;
248 const Point & local = reference_element.position(i,dim);
249 const Point global = e.geometry().global(local);
251 std::cout <<
"Found hanging node with id " << v_globalindex <<
" at " << global << std::endl;
256 is_hanging[i].is_hanging =
false;
257 is_hanging[i].is_boundary = v_info.is_boundary;
264 void adaptToIsolatedHangingNodes()
267 std::cout <<
"Begin isolation of hanging nodes" << std::endl;
271 size_t iterations(0);
277 size_t refinements(0);
280 GridView gv = grid.leafGridView();
283 for(
const auto& cell : elements(gv)) {
290 const unsigned short level = cell.level();
295 const IndexType v_size = reference_element.size(dim);
300 for(IndexType i=0; i<v_size; ++i){
302 const IndexType v_globalindex = indexSet.subIndex(cell,i,dim);
304 const NodeInfo & v_info = node_info[v_globalindex];
308 const unsigned short level_diff = v_info.maximum_level - level;
316 std::cout <<
" cell-id=" << cell_mapper.index(cell);
317 std::cout <<
" level=" << level;
318 std::cout <<
" v_size=" << v_size;
319 std::cout <<
" v_globalindex = " << v_globalindex;
320 std::cout << std::endl;
321 std::cout <<
"Refining element nr " << cell_mapper.index(cell)
322 <<
" to isolate hanging nodes. Level diff = "
323 << v_info.maximum_level <<
" - " << level<< std::endl;
330 if( cell.geometry().type().isSimplex() ){
341 bool bJumpOut =
false;
344 for(
const auto& intersection : intersections(gv,cell)) {
347 if(!intersection.boundary()) {
349 const int e_level = intersection.inside().level();
350 const int f_level = intersection.outside().level();
352 if( f_level > e_level ){
358 const int eLocalIndex = intersection.indexInInside();
365 int nEdgeCenters = 0;
382 std::vector<Dune::FieldVector<ctype,dim> >
387 for(
int counter=0; counter<nEdgeCenters; ++counter){
389 int cornerIndex1 = counter % dim;
390 int cornerIndex2 = (counter+1) % dim;
392 const int e_v_index_1 =
393 reference_element.subEntity(eLocalIndex,1,cornerIndex1,dim);
395 const int e_v_index_2 =
396 reference_element.subEntity(eLocalIndex,1,cornerIndex2,dim);
398 auto vertex1 = cell.template subEntity<dim>(e_v_index_1);
400 auto vertex2 = cell.template subEntity<dim>(e_v_index_2);
402 edgecenter[counter] += vertex1.geometry().center();
403 edgecenter[counter] += vertex2.geometry().center();
404 edgecenter[counter] /= ctype( 2 );
411 auto nb_reference_element =
referenceElement(intersection.outside().geometry());
414 const IndexType nb_v_size = nb_reference_element.size(dim);
417 for(IndexType i=0; i<nb_v_size; ++i){
419 auto nb_vertex = intersection.outside().template subEntity<dim>(i);
421 bool doExtraCheck =
false;
425 center_diff -= nb_vertex.geometry().center();
429 if( center_diff.two_norm2() < 5e-12 ){
439 const IndexType nb_v_globalindex = indexSet.index(nb_vertex);
441 const NodeInfo & nb_v_info = node_info[nb_v_globalindex];
443 const unsigned short level_diff = nb_v_info.maximum_level - level;
453 std::cout <<
" cell-id=" << cell_mapper.index(cell);
454 std::cout <<
" level=" << level;
455 std::cout <<
" v_size=" << v_size;
456 std::cout << std::endl;
457 std::cout <<
" Extra refining for element nr " << cell_mapper.index(cell)
458 <<
" to isolate hanging nodes. Level diff = "
459 << nb_v_info.maximum_level <<
" - " << level<< std::endl;
466 if( bJumpOut )
break;
468 if( bJumpOut )
break;
474 if( bJumpOut )
break;
486 std::cout <<
"Re-adapt for isolation of hanging nodes..." << std::endl;
495 std::cout <<
"In iteration " << iterations <<
" there were "
496 << refinements <<
" grid cells to be refined additionally to isolate hanging nodes." << std::endl;
GridFamily::Traits::LeafGridView LeafGridView
type of view for leaf grid
Definition: grid.hh:400
Dune::Intersection< GridImp, IntersectionImp > Intersection
Type of Intersection this IntersectionIterator points to.
Definition: intersectioniterator.hh:111
void update(const GV &gridView)
Recalculates indices after grid adaptation.
Definition: mcmgmapper.hh:308
Different resources needed by all grid implementations.
Various ways to compare floating-point numbers.
Traits::IntersectionIterator IntersectionIterator
type of the intersection iterator
Definition: gridview.hh:92
Traits::IndexSet IndexSet
type of the index set
Definition: gridview.hh:86
Grid< dim, dimworld, ct, GridFamily >::LeafGridView leafGridView(const Grid< dim, dimworld, ct, GridFamily > &grid)
leaf grid view for the given grid
Definition: grid.hh:805
static constexpr int dimension
The dimension of the grid.
Definition: gridview.hh:134
unspecified value type referenceElement(T &&... t)
Returns a reference element for the objects t....
constexpr auto max
Function object that returns the greater of the given values.
Definition: hybridutilities.hh:484
constexpr auto min
Function object that returns the smaller of the given values.
Definition: hybridutilities.hh:506
MCMGLayout mcmgElementLayout()
layout for elements (codim-0 entities)
Definition: mcmgmapper.hh:97
Mapper for multiple codim and multiple geometry types.
Dune namespace.
Definition: alignedallocator.hh:13