dune-spgrid  2.7
partition.hh
Go to the documentation of this file.
1 #ifndef DUNE_SPGRID_PARTITION_HH
2 #define DUNE_SPGRID_PARTITION_HH
3 
4 #include <array>
5 #include <limits>
6 
8 
11 #include <dune/grid/spgrid/mesh.hh>
13 
14 namespace Dune
15 {
16 
17  // SPBasicPartition
18  // ----------------
19 
20  template< int dim >
22  {
24 
25  public:
26  static const int dimension = dim;
27 
29 
31 
32  SPBasicPartition ( const MultiIndex &begin, const MultiIndex &end ) : bound_{{ begin, end }} {}
33 
34  const MultiIndex &begin () const { return bound( 0 ); }
35  const MultiIndex &end () const { return bound( 1 ); }
36 
37  const MultiIndex &bound ( unsigned int b ) const { assert( b == (b & 1) ); return bound_[ b ]; }
38 
39  int bound ( unsigned int b, int i, unsigned int d ) const
40  {
41  assert( d == (d & 1) );
42  return bound( b )[ i ] - (2*b-1) * ((bound( b )[ i ] ^ d) & 1);
43  }
44 
45  int bound ( const SPNormalId< dimension > &id ) const { return bound( id.face() & 1 )[ id.axis() ]; }
46 
47  This intersect ( const This &other ) const
48  {
49  return This( std::max( begin(), other.begin() ), std::min( end(), other.end() ) );
50  }
51 
52  bool contains ( const MultiIndex &id ) const;
53 
54  bool empty () const;
55  bool empty ( Direction dir ) const;
56 
57  int volume () const;
58  MultiIndex width () const;
59  int width ( int i ) const { return std::max( (end()[ i ]+1)/2 - begin()[ i ]/2, 0 ); }
60 
61  template< class char_type, class traits >
62  void print ( std::basic_ostream< char_type, traits > &out ) const;
63 
64  template< class char_type, class traits >
65  void print ( std::basic_ostream< char_type, traits > &out, const int i ) const;
66 
67  private:
68  std::array< MultiIndex, 2 > bound_;
69  };
70 
71 
72 
73  // SPPartition
74  // -----------
75 
76  template< int dim >
78  : public SPBasicPartition< dim >
79  {
80  typedef SPPartition< dim > This;
82 
83  typedef unsigned int Flags;
84 
85  public:
86  static const int dimension = dim;
87 
88  typedef typename Base::MultiIndex MultiIndex;
90 
91  SPPartition ( const Base &base, const unsigned int number );
92  SPPartition ( const MultiIndex &begin, const MultiIndex &end,
93  const unsigned int number );
94  SPPartition ( const MultiIndex &begin, const MultiIndex &end,
95  const Mesh &globalMesh, const unsigned int number );
96 
97  unsigned int number () const;
98  const unsigned int &neighbor ( const int face ) const;
99  unsigned int &neighbor ( const int face );
100 
101  bool hasNeighbor ( const int face ) const;
102  Flags boundary () const;
103  bool boundary ( const int face ) const;
104 
105  private:
106  unsigned int number_;
107  unsigned int neighbor_[ 2*dimension ];
108  Flags boundary_;
109  };
110 
111 
112 
113  // Implementation of SPBasicPartition
114  // ----------------------------------
115 
116  template< int dim >
117  inline bool SPBasicPartition< dim >::contains ( const MultiIndex &id ) const
118  {
119  bool contains = true;
120  for( int i = 0; i < dimension; ++i )
121  contains &= (id[ i ] >= begin()[ i ]) && (id[ i ] <= end()[ i ]);
122  return contains;
123  }
124 
125 
126  template< int dim >
127  inline bool SPBasicPartition< dim >::empty () const
128  {
129  bool empty = false;
130  for( int i = 0; i < dimension; ++i )
131  empty |= (begin()[ i ] > end()[ i ]);
132  return empty;
133  }
134 
135 
136  template< int dim >
137  inline bool SPBasicPartition< dim >::empty ( Direction dir ) const
138  {
139  bool empty = false;
140  for( int i = 0; i < dimension; ++i )
141  empty |= (bound( 0, i, dir[ i ] ) > bound( 1, i, dir[ i ] ));
142  return empty;
143  }
144 
145 
146  template< int dim >
148  {
149  int volume = 1;
150  for( int i = 0; i < dimension; ++i )
151  volume *= width( i );
152  return volume;
153  }
154 
155 
156  template< int dim >
159  {
160  MultiIndex w;
161  for( int i = 0; i < dimension; ++i )
162  w[ i ] = width( i );
163  return w;
164  }
165 
166 
167  template< int dim >
168  template< class char_type, class traits >
169  inline void SPBasicPartition< dim >
170  ::print ( std::basic_ostream< char_type, traits > &out ) const
171  {
172  print( out, 0 );
173  for( int i = 1; i < dimension; ++i )
174  {
175  out << " x ";
176  print( out, i );
177  }
178  }
179 
180 
181  template< int dim >
182  template< class char_type, class traits >
183  inline void SPBasicPartition< dim >
184  ::print ( std::basic_ostream< char_type, traits > &out, const int i ) const
185  {
186  const int b = begin()[ i ];
187  const int e= end()[ i ];
188 
189  const char left = '[' + (b & 1)*(']'-'[');
190  const char right = ']' + (e & 1)*('['-']');
191 
192  out << left << ' ' << (b/2) << ", " << ((e+1)/2) << ' ' << right;
193  }
194 
195 
196 
197  // Implementation of SPPartition
198  // -----------------------------
199 
200  template< int dim >
201  SPPartition< dim >::SPPartition ( const Base &base, const unsigned int number )
202  : Base( base ),
203  number_( number ),
204  boundary_( ((Flags( 1 ) << (2*dimension-1))-1) | (Flags( 1 ) << (2*dimension-1)) )
205  {
206  for( int face = 0; face < 2*dimension; ++face )
207  neighbor_[ face ] = std::numeric_limits< unsigned int >::max();
208  }
209 
210 
211  template< int dim >
213  ::SPPartition ( const MultiIndex &begin, const MultiIndex &end,
214  const unsigned int number )
215  : Base( begin, end ),
216  number_( number ),
217  boundary_( ((Flags( 1 ) << (2*dimension-1))-1) | (Flags( 1 ) << (2*dimension-1)) )
218  {
219  for( int face = 0; face < 2*dimension; ++face )
220  neighbor_[ face ] = std::numeric_limits< unsigned int >::max();
221  }
222 
223 
224  template< int dim >
226  ::SPPartition ( const MultiIndex &begin, const MultiIndex &end,
227  const Mesh &globalMesh, const unsigned int number )
228  : Base( begin, end ),
229  number_( number ),
230  boundary_( 0 )
231  {
232  for( int i = 0; i < dimension; ++i )
233  {
234  neighbor_[ 2*i ] = neighbor_[ 2*i+1 ] = std::numeric_limits< unsigned int >::max();
235  boundary_ |= Flags( begin[ i ] == 2*globalMesh.begin()[ i ] ) << (2*i);
236  boundary_ |= Flags( end[ i ] == 2*globalMesh.end()[ i ] ) << (2*i+1);
237  }
238  }
239 
240 
241  template< int dim >
242  inline unsigned int SPPartition< dim >::number () const
243  {
244  return number_;
245  }
246 
247 
248  template< int dim >
249  inline const unsigned int &
250  SPPartition< dim >::neighbor ( const int face ) const
251  {
252  assert( (face >= 0) && (face < 2*dimension) );
253  return neighbor_[ face ];
254  }
255 
256 
257  template< int dim >
258  inline unsigned int &
259  SPPartition< dim >::neighbor ( const int face )
260  {
261  assert( (face >= 0) && (face < 2*dimension) );
262  return neighbor_[ face ];
263  }
264 
265 
266  template< int dim >
267  inline bool SPPartition< dim >::hasNeighbor ( const int face ) const
268  {
269  return (neighbor( face ) < std::numeric_limits< unsigned int >::max());
270  }
271 
272 
273  template< int dim >
274  inline typename SPPartition< dim >::Flags
276  {
277  return boundary_;
278  }
279 
280 
281  template< int dim >
282  inline bool SPPartition< dim >::boundary ( const int face ) const
283  {
284  assert( (face >= 0) && (face < 2*dimension) );
285  return bool( (boundary_ >> face) & 1 );
286  }
287 
288 
289 
290  // Auxilliary functions for SPBasicPartition
291  // -----------------------------------------
292 
293  template< class char_type, class traits, int dim >
294  inline std::basic_ostream< char_type, traits > &
295  operator<< ( std::basic_ostream< char_type, traits > &out,
296  const SPBasicPartition< dim > &partition )
297  {
298  partition.print( out );
299  return out;
300  }
301 
302 } // namespace Dune
303 
304 #endif // #ifndef DUNE_SPGRID_PARTITION_HH
Definition: iostream.hh:7
std::basic_ostream< char_type, traits > & operator<<(std::basic_ostream< char_type, traits > &out, const SPCube< ct, dim > &cube)
Definition: cube.hh:148
Definition: direction.hh:18
const MultiIndex & end() const
Definition: mesh.hh:37
const MultiIndex & begin() const
Definition: mesh.hh:36
Definition: partition.hh:22
const MultiIndex & begin() const
Definition: partition.hh:34
SPBasicPartition(const MultiIndex &begin, const MultiIndex &end)
Definition: partition.hh:32
SPDirection< dimension > Direction
Definition: partition.hh:30
int bound(unsigned int b, int i, unsigned int d) const
Definition: partition.hh:39
SPMultiIndex< dimension > MultiIndex
Definition: partition.hh:28
int width(int i) const
Definition: partition.hh:59
This intersect(const This &other) const
Definition: partition.hh:47
static const int dimension
Definition: partition.hh:26
int bound(const SPNormalId< dimension > &id) const
Definition: partition.hh:45
const MultiIndex & end() const
Definition: partition.hh:35
const MultiIndex & bound(unsigned int b) const
Definition: partition.hh:37
Definition: partition.hh:79
unsigned int number() const
Definition: partition.hh:242
bool hasNeighbor(const int face) const
Definition: partition.hh:267
const unsigned int & neighbor(const int face) const
Definition: partition.hh:250
SPMesh< dimension > Mesh
Definition: partition.hh:89
SPPartition(const Base &base, const unsigned int number)
Definition: partition.hh:201
Flags boundary() const
Definition: partition.hh:275
static const int dimension
Definition: partition.hh:86
Base::MultiIndex MultiIndex
Definition: partition.hh:88