dune-grid 2.9.0
hierarchicsearch.hh
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (C) DUNE Project contributors, see file LICENSE.md in module root
2// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
3// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
4// vi: set et ts=4 sw=2 sts=2:
5
6#ifndef DUNE_GRID_HIERARCHICSEARCH_HH
7#define DUNE_GRID_HIERARCHICSEARCH_HH
8
15#include <cstddef>
16#include <sstream>
17#include <string>
18#include <utility>
19
20#include <dune/common/classname.hh>
21#include <dune/common/exceptions.hh>
22#include <dune/common/fvector.hh>
23
26
27namespace Dune
28{
29
33 template<class Grid, class IS>
35 {
37 constexpr static int dim = Grid::dimension;
38
40 constexpr static int dimw = Grid::dimensionworld;
41
43 typedef typename Grid::ctype ct;
44
46 typedef typename Grid::template Codim<0>::Entity Entity;
47
49 typedef typename Grid::HierarchicIterator HierarchicIterator;
50
51 static std::string formatEntityInformation ( const Entity &e ) {
52 const typename Entity::Geometry &geo = e.geometry();
53 std::ostringstream info;
54 info << "level=" << e.level() << " "
55 << "partition=" << e.partitionType() << " "
56 << "center=(" << geo.center() << ") "
57 << "corners=[(" << geo.corner(0) << ")";
58 for(int i = 1; i < geo.corners(); ++i)
59 info << " (" << e.geometry().corner(i) << ")";
60 info << "]";
61 return info.str();
62 }
63
74 Entity hFindEntity ( const Entity &entity,
75 const FieldVector<ct,dimw>& global) const
76 {
77 // type of element geometry
78 typedef typename Entity::Geometry Geometry;
79 // type of local coordinate
80 typedef typename Geometry::LocalCoordinate LocalCoordinate;
81
82 const int childLevel = entity.level()+1 ;
83 // loop over all child Entities
84 const HierarchicIterator end = entity.hend( childLevel );
85 for( HierarchicIterator it = entity.hbegin( childLevel ); it != end; ++it )
86 {
87 Entity child = *it;
88 Geometry geo = child.geometry();
89
90 LocalCoordinate local = geo.local(global);
91 if (referenceElement( geo ).checkInside(local))
92 {
93 // return if we found the leaf, else search through the child entites
94 if( indexSet_.contains( child ) )
95 return child;
96 else
97 return hFindEntity( child, global );
98 }
99 }
100 std::ostringstream children;
101 HierarchicIterator it = entity.hbegin( childLevel );
102 if(it != end) {
103 children << "{" << formatEntityInformation(*it) << "}";
104 for( ++it; it != end; ++it )
105 children << " {" << formatEntityInformation(*it) << "}";
106 }
107 DUNE_THROW(Exception, "{" << className(*this) << "} Unexpected "
108 "internal Error: none of the children of the entity "
109 "{" << formatEntityInformation(entity) << "} contains "
110 "coordinate (" << global << "). Children are: "
111 "[" << children.str() << "].");
112 }
113
114 public:
118 HierarchicSearch(const Grid & g, const IS & is) : grid_(g), indexSet_(is) {}
119
127 Entity findEntity(const FieldVector<ct,dimw>& global) const
128 { return findEntity<All_Partition>(global); }
129
137 template<PartitionIteratorType partition>
138 Entity findEntity(const FieldVector<ct,dimw>& global) const
139 {
140 typedef typename Grid::LevelGridView LevelGV;
141 const LevelGV &gv = grid_.levelGridView(0);
142
144 typedef typename LevelGV::template Codim<0>::template Partition<partition>::Iterator LevelIterator;
145
146 // type of element geometry
147 typedef typename Entity::Geometry Geometry;
148 // type of local coordinate
149 typedef typename Geometry::LocalCoordinate LocalCoordinate;
150
151 // loop over macro level
152 const LevelIterator end = gv.template end<0, partition>();
153 for (LevelIterator it = gv.template begin<0, partition>(); it != end; ++it)
154 {
155 Entity entity = *it;
156 Geometry geo = entity.geometry();
157
158 LocalCoordinate local = geo.local( global );
159 if( !referenceElement( geo ).checkInside( local ) )
160 continue;
161
162 if( (int(dim) != int(dimw)) && ((geo.global( local ) - global).two_norm() > 1e-8) )
163 continue;
164
165 // return if we found the leaf, else search through the child entites
166 if( indexSet_.contains( entity ) )
167 return entity;
168 else
169 return hFindEntity( entity, global );
170 }
171 DUNE_THROW( GridError, "Coordinate " << global << " is outside the grid." );
172 }
173
174 private:
175 const Grid& grid_;
176 const IS& indexSet_;
177 };
178
179} // end namespace Dune
180
181#endif // DUNE_GRID_HIERARCHICSEARCH_HH
Include standard header files.
Definition: agrid.hh:60
auto referenceElement(const Geometry< mydim, cdim, GridImp, GeometryImp > &geo) -> decltype(referenceElement(geo, geo.impl()))
Definition: common/geometry.hh:558
GridImp::template Codim< cd >::Geometry Geometry
The corresponding geometry type.
Definition: common/entity.hh:100
Base class for exceptions in Dune grid modules.
Definition: exceptions.hh:20
Wrapper class for geometries.
Definition: common/geometry.hh:71
GlobalCoordinate global(const LocalCoordinate &local) const
Evaluate the map .
Definition: common/geometry.hh:228
FieldVector< ctype, mydim > LocalCoordinate
type of local coordinates
Definition: common/geometry.hh:103
Grid abstract base class.
Definition: common/grid.hh:375
static constexpr int dimension
The dimension of the grid.
Definition: common/grid.hh:387
GridFamily::Traits::HierarchicIterator HierarchicIterator
A type that is a model of Dune::HierarchicIterator A type of iterator that allows to examine,...
Definition: common/grid.hh:482
GridFamily::Traits::LevelGridView LevelGridView
type of view for level grid
Definition: common/grid.hh:402
static constexpr int dimensionworld
The dimension of the world the grid lives in.
Definition: common/grid.hh:390
LevelGridView levelGridView(int level) const
View for a grid level for All_Partition.
Definition: common/grid.hh:598
ct ctype
Define type used for coordinates in grid module.
Definition: common/grid.hh:532
Search an IndexSet for an Entity containing a given point.
Definition: hierarchicsearch.hh:35
Entity findEntity(const FieldVector< ct, dimw > &global) const
Search the IndexSet of this HierarchicSearch for an Entity containing point global.
Definition: hierarchicsearch.hh:127
Entity findEntity(const FieldVector< ct, dimw > &global) const
Search the IndexSet of this HierarchicSearch for an Entity containing point global.
Definition: hierarchicsearch.hh:138
HierarchicSearch(const Grid &g, const IS &is)
Construct a HierarchicSearch object from a Grid and an IndexSet.
Definition: hierarchicsearch.hh:118
Different resources needed by all grid implementations.