1#ifndef DUNE_SPGRID_REFINEMENT_HH
2#define DUNE_SPGRID_REFINEMENT_HH
8#include <dune/common/iostream.hh>
12#include <dune/grid/spgrid/declaration.hh>
14#include <dune/grid/spgrid/multiindex.hh>
23 class SPIsotropicRefinementPolicy
25 typedef SPIsotropicRefinementPolicy< dim > This;
28 static const int dimension = dim;
30 unsigned int weight ()
const
35 unsigned int factor (
const int i )
const
40 static std::string type () {
return "isotropic"; }
42 template<
class char_type,
class traits >
43 friend std::basic_ostream< char_type, traits > &
44 operator<< ( std::basic_ostream< char_type, traits > &out,
const This &policy )
46 return out << ((1 << dimension)-1);
49 template<
class char_type,
class traits >
50 friend std::basic_istream< char_type, traits > &
51 operator>> ( std::basic_istream< char_type, traits > &in, This &policy )
53 in >> match( (1 << dimension)-1 );
64 class SPAnisotropicRefinementPolicy
66 typedef SPAnisotropicRefinementPolicy< dim > This;
68 friend class SPAnisotropicRefinement< dim >;
71 static const int dimension = dim;
73 SPAnisotropicRefinementPolicy ()
74 : refDir_( (1u << dimension) - 1u )
77 SPAnisotropicRefinementPolicy ( std::bitset< dimension > refDir )
81 unsigned int weight ()
const
83 return refDir_.count();
86 unsigned int factor (
const int i )
const
88 assert( (i >= 0) && (i < dimension) );
89 return refDir_[ i ]+1;
92 static std::string type () {
return "anisotropic"; }
94 template<
class char_type,
class traits >
95 friend std::basic_ostream< char_type, traits > &
96 operator<< ( std::basic_ostream< char_type, traits > &out,
const This &policy )
98 return out << policy.refDir_.to_ulong();
101 template<
class char_type,
class traits >
102 friend std::basic_istream< char_type, traits > &
103 operator>> ( std::basic_istream< char_type, traits > &in, This &policy )
105 unsigned long refDir;
108 policy.refDir_ = refDir;
113 std::bitset< dimension > refDir_;
122 class SPBisectionRefinementPolicy
124 typedef SPBisectionRefinementPolicy< dim > This;
126 friend class SPBisectionRefinement< dim >;
129 static const int dimension = dim;
131 SPBisectionRefinementPolicy ()
135 explicit SPBisectionRefinementPolicy (
int dir )
138 if( (dir < 0) || (dir >= dimension) )
139 DUNE_THROW( GridError,
"Trying to create bisection refinement policy for invalid direction " << dir <<
"." );
143 SPBisectionRefinementPolicy (
const This &father,
const This &policy )
144 : dir_( policy.dir_ < 0 ? (father.dir_ + 1) % dimension : policy.dir_ )
148 unsigned int weight ()
const
153 unsigned int factor (
const int i )
const
155 assert( (i >= 0) && (i < dimension) );
156 return (i == dir_ ? 2 : 1);
159 static std::string type () {
return "bisection"; }
161 template<
class char_type,
class traits >
162 friend std::basic_ostream< char_type, traits > &
163 operator<< ( std::basic_ostream< char_type, traits > &out,
const This &policy )
165 assert( (policy.dir_ >= 0) && (policy.dir_ < dimension) );
166 return out << (1u << policy.dir_);
169 template<
class char_type,
class traits >
170 friend std::basic_istream< char_type, traits > &
171 operator>> ( std::basic_istream< char_type, traits > &in, This &policy )
173 unsigned long rd( 0 );
175 std::bitset< dimension > refDir( rd );
178 if( refDir.count() != 1 )
179 DUNE_THROW( GridError,
"Trying to create bisection refinement with multiple refined directions." );
180 for( policy.dir_ = 0; !refDir[ policy.dir_ ]; ++policy.dir_ )
196 class SPArbitraryRefinementPolicy
198 typedef SPArbitraryRefinementPolicy< dim > This;
201 static const int dimension = dim;
205 explicit SPArbitraryRefinementPolicy (
int factor = 2 )
208 DUNE_THROW( GridError,
"Trying to create arbitrary refinement policy with non-positive factor." );
209 for(
int i = 0; i < dimension; ++i )
210 factor_[ i ] = factor;
213 explicit SPArbitraryRefinementPolicy (
const MultiIndex &factor )
216 for(
int i = 0; i < dimension; ++i )
218 if( factor_[ i ] <= 0 )
219 DUNE_THROW( GridError,
"Trying to create arbitrary refinement policy with non-positive factor." );
223 constexpr unsigned int weight ()
const {
return dimension; }
225 unsigned int factor (
int i )
const {
return factor_[ i ]; }
227 static std::string type () {
return "arbitrary"; }
229 template<
class char_type,
class traits >
230 friend std::basic_ostream< char_type, traits > &
231 operator<< ( std::basic_ostream< char_type, traits > &out,
const This &policy )
233 return out << policy.factor_;
236 template<
class char_type,
class traits >
237 friend std::basic_istream< char_type, traits > &
238 operator>> ( std::basic_istream< char_type, traits > &in, This &policy )
240 return in >> policy.factor_;
253 class SPDefaultRefinement
255 typedef SPDefaultRefinement< P > This;
260 static const int dimension = Policy::dimension;
264 explicit SPDefaultRefinement (
const Policy &policy = Policy() ) : policy_( policy ) {}
266 SPDefaultRefinement (
const This &father,
const Policy &policy ) : policy_( policy ) {}
268 static std::string type () {
return Policy::type(); }
270 unsigned int factor (
int i )
const {
return policy().factor( i ); }
272 unsigned int numChildren ()
const
274 unsigned int numChildren = 1;
275 for(
int i = 0; i < dimension; ++i )
276 numChildren *= factor( i );
280 void father ( MultiIndex &
id )
const
282 for(
int i = 0; i < dimension; ++i )
283 id[ i ] = (
id[ i ] / factor( i )) | (
id[ i ] & 1);
286 bool hasFather (
const MultiIndex &
id )
const
288 bool hasFather =
true;
289 for(
int i = 0; i < dimension; ++i )
290 hasFather &= (
id[ i ] & 1) | ((
id[ i ] % (2*factor( i ))) == 0);
294 void child ( MultiIndex &
id,
unsigned int index )
const
296 assert( index < numChildren() );
297 for(
int i = 0; i < dimension; ++i )
299 const unsigned int alpha = factor( i );
300 id[ i ] = ((
id[ i ] + (index % alpha)) * alpha) - (alpha - 1);
305 unsigned int childIndex (
const MultiIndex &
id )
const
307 unsigned int index = 0;
308 for(
int i = dimension-1; i >= 0; --i )
310 const unsigned int alpha = factor( i );
311 index = (index * alpha) + ((
id[ i ] >> 1) % alpha);
313 assert( index < numChildren() );
317 void firstChild ( MultiIndex &
id )
const
319 for(
int i = 0; i < dimension; ++i )
320 id[ i ] = (factor( i ) * (
id[ i ] & ~1)) | (
id[ i ] & 1);
323 bool nextChild ( MultiIndex &
id )
const
325 for(
int i = 0; i < dimension; ++i )
327 const unsigned int alpha = factor( i );
328 const int step = 2*(
id[ i ] & 1);
330 if( ((
id[ i ] % (2*alpha)) & ~1) != 0 )
332 id[ i ] -= alpha*step;
337 bool isCopy (
const MultiIndex
id )
const
340 for(
int i = 0; i < dimension; ++i )
342 const unsigned int alpha = factor( i );
343 copy &= (alpha == 1) | ((
id[ i ] % (2*alpha)) == 0);
348 template<
class ctype >
352 for(
int i = 0; i < dimension; ++i )
353 h[ i ] = ctype( 1 ) / ctype( factor( i ) );
357 template<
class ctype >
361 for(
int i = 0; i < dimension; ++i )
363 const unsigned int alpha = factor( i );
364 origin[ i ] = ctype( index % alpha ) / ctype( alpha );
370 const Policy &policy ()
const {
return policy_; }
382 class SPBinaryRefinement
383 :
public SPDefaultRefinement< P >
385 typedef SPBinaryRefinement< P > This;
386 typedef SPDefaultRefinement< P > Base;
389 using Base::dimension;
391 typedef typename Base::Policy Policy;
392 typedef typename Base::MultiIndex MultiIndex;
394 explicit SPBinaryRefinement (
const Policy &policy = Policy() ) : Base ( policy ) {}
396 SPBinaryRefinement (
const This &father,
const Policy &policy ) : Base( father, policy ) {}
400 bool hasFather (
const MultiIndex &
id )
const
402 bool hasFather =
true;
403 for(
int i = 0; i < dimension; ++i )
404 hasFather &= ((
id[ i ] & 3) != 2);
408 void firstChild ( MultiIndex &
id )
const
410 for(
int i = 0; i < dimension; ++i )
412 const unsigned int alpha = factor( i );
413 id[ i ] = alpha*
id[ i ] - (alpha / 2)*(
id[ i ] & 1);
417 bool nextChild ( MultiIndex &
id )
const
419 for(
int i = 0; i < dimension; ++i )
421 if( factor( i ) < 2 )
423 id[ i ] ^= 2*(
id[ i ] & 1);
424 if( (
id[ i ] & 2) != 0 )
430 bool isCopy (
const MultiIndex
id )
const
433 for(
int i = 0; i < dimension; ++i )
434 copy &= (factor( i ) == 1) | ((
id[ i ] & 3) == 0);
452 :
public SPBinaryRefinement< SPIsotropicRefinementPolicy< dim > >
455 typedef SPBinaryRefinement< SPIsotropicRefinementPolicy< dim > > Base;
458 using Base::dimension;
460 typedef typename Base::Policy Policy;
466 constexpr unsigned int numChildren ()
const {
return (1 << dimension); }
482 :
public SPBinaryRefinement< SPAnisotropicRefinementPolicy< dim > >
485 typedef SPBinaryRefinement< SPAnisotropicRefinementPolicy< dim > > Base;
488 using Base::dimension;
490 typedef typename Base::Policy Policy;
498 unsigned int numChildren ()
const {
return (1u << policy().refDir_.count()); }
516 :
public SPDefaultRefinement< SPBisectionRefinementPolicy< dim > >
519 typedef SPDefaultRefinement< SPBisectionRefinementPolicy< dim > > Base;
522 using Base::dimension;
525 typedef typename Base::Policy Policy;
530 : Base( Policy( father.policy(), policy ) )
535 unsigned int numChildren ()
const
542 const int dir = policy().dir_;
544 id[ dir ] = (
id[ dir ] / 2) | (
id[ dir ] & 1);
549 const int dir = policy().dir_;
551 return ((
id[ dir ] & 3) != 2);
554 void child (
MultiIndex &
id,
unsigned int index )
const
556 assert( index < numChildren() );
557 const int dir = policy().dir_;
559 id[ dir ] = 2*(
id[ dir ] + index) - 1;
562 unsigned int childIndex (
const MultiIndex &
id )
const
564 const int dir = policy().dir_;
566 const unsigned int index = (
id[ dir ] >> 1) % 2;
567 assert( index < numChildren() );
573 const int dir = policy().dir_;
575 id[ dir ] = 2*
id[ dir ] - (
id[ dir ] & 1);
580 const int dir = policy().dir_;
582 id[ dir ] ^= 2*(
id[ dir ] & 1);
583 return ((
id[ dir ] & 2) != 0);
588 const int dir = policy().dir_;
590 return ((
id[ dir ] & 3) == 0);
600 class SPArbitraryRefinement
601 :
public SPDefaultRefinement< SPArbitraryRefinementPolicy< dim > >
603 typedef SPArbitraryRefinement< dim > This;
604 typedef SPDefaultRefinement< SPArbitraryRefinementPolicy< dim > > Base;
607 typedef typename Base::Policy Policy;
609 SPArbitraryRefinement () : Base( Policy() ) {}
611 explicit SPArbitraryRefinement (
const This &father,
const Policy &policy ) : Base( policy ) {}
the user may choose freely along which axes the grid should be refined
Definition: refinement.hh:483
each element is split into 2 children.
Definition: refinement.hh:517
each element is split into 2dim children.
Definition: refinement.hh:453
Different resources needed by all grid implementations.
miscellaneous helper functions
Implements a vector constructed from a given type representing a field and a compile-time given size.
Stream & operator>>(Stream &stream, std::tuple< Ts... > &t)
Read a std::tuple.
Definition: streamoperators.hh:43
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
Dune namespace.
Definition: alignedallocator.hh:13