dune-spgrid  2.7
hierarchicsearch.hh
Go to the documentation of this file.
1 #ifndef DUNE_SPGRID_HIERARCHICSEARCH_HH
2 #define DUNE_SPGRID_HIERARCHICSEARCH_HH
3 
4 #include <dune/common/fvector.hh>
5 #include <dune/common/exceptions.hh>
6 
7 #include <dune/grid/common/indexidset.hh>
8 #include <dune/grid/common/gridenums.hh>
9 
13 
14 namespace Dune
15 {
16 
17  // External Forward Declarations
18  // -----------------------------
19 
20  template< class Grid >
21  class SPIndexSet;
22 
23  template< class Grid, class IndexSet >
25 
26 
27 
28  // SPBasicHierarchicSearch
29  // -----------------------
30 
31  template< class Grid >
33  {
34  public:
35  typedef typename Grid::ctype ctype;
36  typedef typename Grid::template Codim< 0 >::Entity Entity;
37 
38  static const int dimension = Grid::dimension;
39 
40  typedef FieldVector< ctype, dimension > GlobalVector;
41 
42  SPBasicHierarchicSearch ( const Grid &grid )
43  : grid_( grid )
44  {}
45 
46  Entity findEntity ( const GlobalVector &global, int level ) const
47  {
48  typedef SPEntity< 0, dimension, const Grid > EntityImpl;
49  typedef typename Grid::GridLevel GridLevel;
50  typedef typename GridLevel::PartitionList PartitionList;
51 
52  assert( grid_.domain().contains( global ) );
53  const GridLevel &gridLevel = grid_.gridLevel( level );
54  const PartitionList &partitionList = gridLevel.template partition< All_Partition >();
55 
56  const GlobalVector y = global - grid_.domain().cube().origin();
57  GlobalVector z;
59  gridLevel.template geometryCache< 0 >( *dirIt ).jacobianInverseTransposed().mv( y, z );
60 
61  typename GridLevel::MultiIndex id;
62  for( int i = 0; i < dimension; ++i )
63  id[ i ] = 2*int( z[ i ] ) + 1;
64 
65  const typename PartitionList::Partition *partition = partitionList.findPartition( id );
66  if( partition )
67  return Entity( EntityImpl( gridLevel, id, partition->number() ) );
68  else
69  {
70  for( int i = 0; i < dimension; ++i )
71  {
72  // check upper bound
73  if( id[ i ] - 1 == 2*gridLevel.localMesh().bound( 1 )[ i ] )
74  id[ i ] = 2*int( z[ i ] ) - 1;
75  }
76  const typename PartitionList::Partition *leftPartition = partitionList.findPartition( id );
77 
78  if( leftPartition )
79  return Entity( EntityImpl( gridLevel, id, leftPartition->number() ) );
80  else
81  DUNE_THROW( GridError, "Coordinate " << global << " is outside the grid." );
82  }
83  }
84 
85  protected:
86  const Grid &grid_;
87  };
88 
89 
90 
91  // SPHierarchicSearch
92  // ------------------
93 
94  template< class Grid, class IndexSet >
96  : protected SPBasicHierarchicSearch< Grid >
97  {
99 
100  public:
101  typedef typename Base::ctype ctype;
102  typedef typename Base::Entity Entity;
103 
105 
107 
108  SPHierarchicSearch ( const Grid &grid, const IndexSet &indexSet )
109  : Base( grid ),
110  indexSet_( indexSet )
111  {}
112 
113  Entity findEntity ( const GlobalVector &global ) const
114  {
115  const Entity e = Base::findEntity( global, 0 );
116  return (indexSet_.contains( e ) ? e : hFindEntity( e, global ));
117  }
118 
119  private:
120  typedef GlobalVector LocalVector;
121  typedef typename Entity::HierarchicIterator HierarchicIterator;
122 
123  Entity hFindEntity ( const Entity &e, const GlobalVector &global ) const
124  {
125  // To Do: This method should use the Cartesian structure, too
126  const HierarchicIterator end = e.hend( e.level()+1 );
127  for( HierarchicIterator it = e.hbegin( e.level()+1 ); it != end; ++it )
128  {
129  const Entity &child = *it;
130  LocalVector local = child.geometry().local( global );
131  if( ReferenceElements< ctype, dimension >::cube().checkInside( local ) )
132  return (indexSet_.contains( child ) ? child : hFindEntity( child, global ));
133  }
134  DUNE_THROW( Exception, "Unexpected internal Error" );
135  }
136 
137  protected:
138  using Base::grid_;
139 
140  private:
141  const IndexSet &indexSet_;
142  };
143 
144 
145 
146  // SPHierarchicSearch for SPIndexSet
147  // ---------------------------------
148 
149  template< class Grid >
150  class SPHierarchicSearch< Grid, SPIndexSet< Grid > >
151  : protected SPBasicHierarchicSearch< Grid >
152  {
155 
156  public:
157  typedef typename Base::Entity Entity;
158 
160 
161  SPHierarchicSearch ( const Grid &grid, const IndexSet &indexSet )
162  : Base( grid ),
163  indexSet_( indexSet )
164  {}
165 
166  Entity findEntity ( const GlobalVector &global ) const
167  {
168  return Base::findEntity( global, indexSet_.gridLevel().level() );
169  }
170 
171  private:
172  const IndexSet &indexSet_;
173  };
174 
175 
176 
177  // SPHierarchicSearch for IndexSet< SPIndexSet >
178  // ---------------------------------------------
179 
180  template< class Grid >
181  class SPHierarchicSearch< Grid, IndexSet< Grid, SPIndexSet< Grid >, typename SPIndexSet< Grid >::IndexType > >
182  : public SPHierarchicSearch< Grid, SPIndexSet< Grid > >
183  {
185  typedef Dune::IndexSet< Grid, SPIndexSet< Grid >, typename SPIndexSet< Grid >::IndexType > IndexSet;
186 
187  public:
188  SPHierarchicSearch ( const Grid &grid, const IndexSet &indexSet )
189  : Base( grid, static_cast< const SPIndexSet< Grid > & >( indexSet ) )
190  {}
191  };
192 
193 
194 
195  // HierarchicSearch for SPGrid
196  // ---------------------------
197 
198  template< class ct, int dim, template< int > class Ref, class Comm, class IndexSet >
199  class HierarchicSearch< SPGrid< ct, dim, Ref, Comm >, IndexSet >
200  : public SPHierarchicSearch< SPGrid< ct, dim, Ref, Comm >, IndexSet >
201  {
204 
205  public:
206  typedef typename Base::Entity Entity;
208 
209  HierarchicSearch ( const Grid &grid, const IndexSet &indexSet )
210  : Base( grid, indexSet )
211  {}
212 
213  Entity findEntity ( const GlobalVector &global ) const
214  {
215  return Base::findEntity( global );
216  }
217 
218  template< PartitionIteratorType pitype >
219  Entity findEntity ( const GlobalVector &global ) const
220  {
221  const Entity entity = Base::findEntity( global );
222  if( !contains< pitype >( entity.partitionType() ) )
223  DUNE_THROW( GridError, "Coordinate " << global << " does not belong to partition " << pitype );
224  return entity;
225  }
226 
227  private:
228  template< PartitionIteratorType pitype >
229  static bool contains ( const PartitionType partitionType )
230  {
231  switch( pitype )
232  {
233  case Interior_Partition:
234  return ( partitionType == InteriorEntity );
235 
236  case InteriorBorder_Partition:
237  return ( partitionType == InteriorEntity || partitionType == BorderEntity );
238 
239  case Overlap_Partition:
240  return ( partitionType != FrontEntity && partitionType != GhostEntity );
241 
242  case OverlapFront_Partition:
243  return ( partitionType != GhostEntity );
244 
245  case All_Partition:
246  return true;
247 
248  case Ghost_Partition:
249  return ( partitionType == GhostEntity );
250 
251  default:
252  DUNE_THROW( InvalidStateException, "wrong PartitionIteratorType: " << pitype << "." );
253  }
254  }
255  };
256 
257 } // namespace Dune
258 
259 #endif // #infdef DUNE_SPGRID_HIERARCHICSEARCH_HH
Definition: iostream.hh:7
structured, parallel DUNE grid
Definition: grid.hh:136
Definition: direction.hh:157
Definition: entity.hh:146
Definition: indexset.hh:19
Base::IndexType IndexType
Definition: indexset.hh:26
Definition: hierarchicsearch.hh:24
Definition: hierarchicsearch.hh:33
static const int dimension
Definition: hierarchicsearch.hh:38
FieldVector< ctype, dimension > GlobalVector
Definition: hierarchicsearch.hh:40
SPBasicHierarchicSearch(const Grid &grid)
Definition: hierarchicsearch.hh:42
Grid::ctype ctype
Definition: hierarchicsearch.hh:35
Grid::template Codim< 0 >::Entity Entity
Definition: hierarchicsearch.hh:36
const Grid & grid_
Definition: hierarchicsearch.hh:86
Entity findEntity(const GlobalVector &global, int level) const
Definition: hierarchicsearch.hh:46
Definition: hierarchicsearch.hh:97
Base::Entity Entity
Definition: hierarchicsearch.hh:102
Base::GlobalVector GlobalVector
Definition: hierarchicsearch.hh:106
SPHierarchicSearch(const Grid &grid, const IndexSet &indexSet)
Definition: hierarchicsearch.hh:108
Entity findEntity(const GlobalVector &global) const
Definition: hierarchicsearch.hh:113
Base::ctype ctype
Definition: hierarchicsearch.hh:101
Base::Entity Entity
Definition: hierarchicsearch.hh:157
Base::GlobalVector GlobalVector
Definition: hierarchicsearch.hh:159
Entity findEntity(const GlobalVector &global) const
Definition: hierarchicsearch.hh:166
SPHierarchicSearch(const Grid &grid, const IndexSet &indexSet)
Definition: hierarchicsearch.hh:161
SPHierarchicSearch(const Grid &grid, const IndexSet &indexSet)
Definition: hierarchicsearch.hh:188
Base::Entity Entity
Definition: hierarchicsearch.hh:206
HierarchicSearch(const Grid &grid, const IndexSet &indexSet)
Definition: hierarchicsearch.hh:209
Base::GlobalVector GlobalVector
Definition: hierarchicsearch.hh:207
Entity findEntity(const GlobalVector &global) const
Definition: hierarchicsearch.hh:213
Entity findEntity(const GlobalVector &global) const
Definition: hierarchicsearch.hh:219