dune-grid  2.2.1
cachedcoordfunction.hh
Go to the documentation of this file.
1 #ifndef DUNE_GEOGRID_CACHEDCOORDFUNCTION_HH
2 #define DUNE_GEOGRID_CACHEDCOORDFUNCTION_HH
3 
4 #include <cassert>
5 #include <memory>
6 
7 #include <dune/common/typetraits.hh>
8 
10 
14 
15 namespace Dune
16 {
17 
18  // Internal Forward Declarations
19  // -----------------------------
20 
21  template< class HostGrid, class CoordFunction, class Allocator >
23 
24 
25 
26  // GeoGrid::CoordCache
27  // -------------------
28 
29  namespace GeoGrid
30  {
31 
32  template< class HostGrid, class Coordinate, class Allocator = std::allocator< Coordinate > >
33  class CoordCache
34  {
36 
37  static const unsigned int dimension = HostGrid::dimension;
38 
39  typedef typename HostGrid::template Codim< dimension >::Entity Vertex;
40 
42 
43  public:
44  explicit CoordCache ( const HostGrid &hostGrid, const Allocator &allocator = Allocator() )
45  : data_( hostGrid, dimension, allocator )
46  {}
47 
48  template< class Entity >
49  const Coordinate &operator() ( const Entity &entity, unsigned int corner ) const
50  {
51  return data_( entity, corner );
52  }
53 
54  const Coordinate &operator() ( const Vertex &vertex, unsigned int corner ) const
55  {
56  assert( corner == 0 );
57  return data_[ vertex ];
58  }
59 
60  template< class Entity >
61  Coordinate &operator() ( const Entity &entity, unsigned int corner )
62  {
63  return data_( entity,corner) ;
64  }
65 
66  Coordinate &operator() ( const Vertex &vertex, unsigned int corner )
67  {
68  assert( corner == 0 );
69  return data_[ vertex ];
70  }
71 
72  void adapt ()
73  {
74  data_.update();
75  }
76 
77  private:
78  CoordCache ( const This & );
79  This &operator= ( const This & );
80 
81  mutable DataCache data_;
82  };
83 
84  } // namespace GeoGrid
85 
86 
87 
88  // CachedCoordFunction
89  // -------------------
90 
91  template< class HostGrid, class CoordFunction, class Allocator = std::allocator< void > >
92  class CachedCoordFunction
93  : public DiscreteCoordFunction< typename CoordFunction::ctype, CoordFunction::dimRange, CachedCoordFunction< HostGrid, CoordFunction > >
94  {
95  typedef CachedCoordFunction< HostGrid, CoordFunction > This;
96  typedef DiscreteCoordFunction< typename CoordFunction::ctype, CoordFunction::dimRange, This > Base;
97 
98  public:
99  typedef typename Base::ctype ctype;
100 
101  typedef typename Base::RangeVector RangeVector;
102 
103  private:
105 
106  public:
107  explicit
108  CachedCoordFunction ( const HostGrid &hostGrid,
109  const CoordFunction &coordFunction = CoordFunction(),
110  const Allocator &allocator = Allocator() )
111  : hostGrid_( hostGrid ),
112  coordFunction_( coordFunction ),
113  cache_( hostGrid, allocator )
114  {
115  buildCache();
116  }
117 
118  void adapt ()
119  {
120  cache_.adapt();
121  buildCache();
122  }
123 
124  void buildCache ();
125 
126  template< class HostEntity >
127  void insertEntity ( const HostEntity &hostEntity );
128 
129  template< class HostEntity >
130  void evaluate ( const HostEntity &hostEntity, unsigned int corner, RangeVector &y ) const
131  {
132  y = cache_( hostEntity, corner );
133 #ifndef NDEBUG
135  CoordFunctionCaller;
136 
137  RangeVector z;
138  CoordFunctionCaller coordFunctionCaller( hostEntity, coordFunction_ );
139  coordFunctionCaller.evaluate( corner, z );
140  assert( ((y - z).two_norm() < 1e-6) );
141 #endif
142  }
143 
144  private:
145  const HostGrid &hostGrid_;
146  const CoordFunction &coordFunction_;
147  Cache cache_;
148  };
149 
150 
151 
152  // Implementation of CachedCoordFunction
153  // -------------------------------------
154 
155  template< class HostGrid, class CoordFunction, class Allocator >
157  {
158  typedef typename HostGrid::template Codim< 0 >::Entity Element;
159  typedef typename HostGrid::LevelGridView MacroView;
160  typedef typename HostGrid::HierarchicIterator HierarchicIterator;
161 
162  typedef typename MacroView::template Codim< 0 >::template Partition< All_Partition >::Iterator MacroIterator;
163 
164  const MacroView macroView = hostGrid_.levelView( 0 );
165  const int maxLevel = hostGrid_.maxLevel();
166 
167  const MacroIterator mend = macroView.template end< 0, All_Partition >();
168  for( MacroIterator mit = macroView.template begin< 0, All_Partition >(); mit != mend; ++mit )
169  {
170  const Element &macroElement = *mit;
171  insertEntity( macroElement );
172 
173  const HierarchicIterator hend = macroElement.hend( maxLevel );
174  for( HierarchicIterator hit = macroElement.hbegin( maxLevel ); hit != hend; ++hit )
175  insertEntity( *hit );
176  }
177  }
178 
179 
180  template< class HostGrid, class CoordFunction, class Allocator >
181  template< class HostEntity >
183  ::insertEntity ( const HostEntity &hostEntity )
184  {
186  CoordFunctionCaller;
187 
188  CoordFunctionCaller coordFunctionCaller( hostEntity, coordFunction_ );
189  const GenericReferenceElement< ctype, HostEntity::dimension > &refElement
190  = GenericReferenceElements< ctype, HostEntity::dimension >::general( hostEntity.type() );
191 
192  const unsigned int numCorners = refElement.size( HostEntity::dimension );
193  for( unsigned int i = 0; i < numCorners; ++i )
194  coordFunctionCaller.evaluate( i, cache_( hostEntity, i ) );
195  }
196 
197 } // namespace Dune
198 
199 #endif // #ifndef DUNE_GEOGRID_CACHEDCOORDFUNCTION_HH