dune-grid  2.11
hierarchicsearch.hh
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright © 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 
24 #include <dune/grid/common/grid.hh>
26 
27 namespace Dune
28 {
29 
33  template<class Grid, class IS>
35  {
36  protected:
38  constexpr static int dim = Grid::dimension;
39 
41  constexpr static int dimw = Grid::dimensionworld;
42 
44  typedef typename Grid::ctype ct;
45 
47  typedef typename Grid::template Codim<0>::Entity Entity;
48 
51 
52  static std::string formatEntityInformation ( const Entity &e ) {
53  const typename Entity::Geometry &geo = e.geometry();
54  std::ostringstream info;
55  info << "level=" << e.level() << " "
56  << "partition=" << e.partitionType() << " "
57  << "center=(" << geo.center() << ") "
58  << "corners=[(" << geo.corner(0) << ")";
59  for(int i = 1; i < geo.corners(); ++i)
60  info << " (" << e.geometry().corner(i) << ")";
61  info << "]";
62  return info.str();
63  }
64 
75  Entity hFindEntity ( const Entity &entity,
76  const FieldVector<ct,dimw>& global) const
77  {
78  // type of element geometry
79  typedef typename Entity::Geometry Geometry;
80  // type of local coordinate
81  typedef typename Geometry::LocalCoordinate LocalCoordinate;
82 
83  const int childLevel = entity.level()+1 ;
84  // loop over all child Entities
85  const HierarchicIterator end = entity.hend( childLevel );
86  for( HierarchicIterator it = entity.hbegin( childLevel ); it != end; ++it )
87  {
88  Entity child = *it;
89  Geometry geo = child.geometry();
90 
91  LocalCoordinate local = geo.local(global);
92  if (referenceElement( geo ).checkInside(local))
93  {
94  // return if we found the leaf, else search through the child entities
95  if( indexSet_.contains( child ) )
96  return child;
97  else
98  return hFindEntity( child, global );
99  }
100  }
101  std::ostringstream children;
102  HierarchicIterator it = entity.hbegin( childLevel );
103  if(it != end) {
104  children << "{" << formatEntityInformation(*it) << "}";
105  for( ++it; it != end; ++it )
106  children << " {" << formatEntityInformation(*it) << "}";
107  }
108  DUNE_THROW(Exception, "{" << className(*this) << "} Unexpected "
109  "internal Error: none of the children of the entity "
110  "{" << formatEntityInformation(entity) << "} contains "
111  "coordinate (" << global << "). Children are: "
112  "[" << children.str() << "].");
113  }
114 
115  public:
119  HierarchicSearch(const Grid & g, const IS & is) : grid_(g), indexSet_(is) {}
120 
128  Entity findEntity(const FieldVector<ct,dimw>& global) const
129  { return findEntity<All_Partition>(global); }
130 
138  template<PartitionIteratorType partition>
139  Entity findEntity(const FieldVector<ct,dimw>& global) const
140  {
141  typedef typename Grid::LevelGridView LevelGV;
142  const LevelGV &gv = grid_.levelGridView(0);
143 
145  typedef typename LevelGV::template Codim<0>::template Partition<partition>::Iterator LevelIterator;
146 
147  // type of element geometry
148  typedef typename Entity::Geometry Geometry;
149  // type of local coordinate
150  typedef typename Geometry::LocalCoordinate LocalCoordinate;
151 
152  // loop over macro level
153  const LevelIterator end = gv.template end<0, partition>();
154  for (LevelIterator it = gv.template begin<0, partition>(); it != end; ++it)
155  {
156  Entity entity = *it;
157  Geometry geo = entity.geometry();
158 
159  LocalCoordinate local = geo.local( global );
160  if( !referenceElement( geo ).checkInside( local ) )
161  continue;
162 
163  if( (int(dim) != int(dimw)) && ((geo.global( local ) - global).two_norm() > 1e-8) )
164  continue;
165 
166  // return if we found the leaf, else search through the child entities
167  if( indexSet_.contains( entity ) )
168  return entity;
169  else
170  return hFindEntity( entity, global );
171  }
172  DUNE_THROW( GridError, "Coordinate " << global << " is outside the grid." );
173  }
174 
175  protected:
176  const Grid& grid_;
177  const IS& indexSet_;
178  };
179 
180 } // end namespace Dune
181 
182 #endif // DUNE_GRID_HIERARCHICSEARCH_HH
Wrapper class for geometries.
Definition: common/geometry.hh:70
concept Entity
Model of a grid entity.
Definition: concepts/entity.hh:119
Entity findEntity(const FieldVector< ct, dimw > &global) const
Search the IndexSet of this HierarchicSearch for an Entity containing point global.
Definition: hierarchicsearch.hh:139
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
FieldVector< ctype, mydim > LocalCoordinate
type of local coordinates
Definition: common/geometry.hh:103
LocalCoordinate local(const GlobalCoordinate &global) const
Evaluate the inverse map .
Definition: common/geometry.hh:237
Grid::template Codim< 0 >::Entity Entity
get entity from the grid
Definition: hierarchicsearch.hh:47
static constexpr int dimw
get world dimension from the grid
Definition: hierarchicsearch.hh:41
const Grid & grid_
Definition: hierarchicsearch.hh:176
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
Grid abstract base classThis class is the base class for all grid implementations. Although no virtual functions are used we call it abstract since its methods do not contain an implementation but forward to the methods of the derived class via the Barton-Nackman trick.
Definition: common/grid.hh:375
const IS & indexSet_
Definition: hierarchicsearch.hh:177
GridImp::template Codim< cd >::Geometry Geometry
The corresponding geometry type.
Definition: common/entity.hh:100
concept Geometry
Model of a geometry object.
Definition: concepts/geometry.hh:29
GlobalCoordinate global(const LocalCoordinate &local) const
Evaluate the map .
Definition: common/geometry.hh:228
Entity findEntity(const FieldVector< ct, dimw > &global) const
Search the IndexSet of this HierarchicSearch for an Entity containing point global.
Definition: hierarchicsearch.hh:128
Different resources needed by all grid implementations.
HierarchicSearch(const Grid &g, const IS &is)
Construct a HierarchicSearch object from a Grid and an IndexSet.
Definition: hierarchicsearch.hh:119
Grid::HierarchicIterator HierarchicIterator
type of HierarchicIterator
Definition: hierarchicsearch.hh:50
static constexpr int dim
get dimension from the grid
Definition: hierarchicsearch.hh:38
Search an IndexSet for an Entity containing a given point.
Definition: hierarchicsearch.hh:34
Entity hFindEntity(const Entity &entity, const FieldVector< ct, dimw > &global) const
Definition: hierarchicsearch.hh:75
auto referenceElement(const Geometry< mydim, cdim, GridImp, GeometryImp > &geo) -> decltype(referenceElement(geo, geo.impl()))
Definition: common/geometry.hh:558
Include standard header files.
Definition: agrid.hh:59
Grid::ctype ct
get coord type from the grid
Definition: hierarchicsearch.hh:44
static std::string formatEntityInformation(const Entity &e)
Definition: hierarchicsearch.hh:52
static constexpr int dimension
The dimension of the grid.
Definition: common/grid.hh:387
ct ctype
Define type used for coordinates in grid module.
Definition: common/grid.hh:518
LevelGridView levelGridView(int level) const
View for a grid level for All_Partition.
Definition: common/grid.hh:584
Base class for exceptions in Dune grid modules.
Definition: exceptions.hh:18