dune-grid  3.0-git
cachedcoordfunction.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 #ifndef DUNE_GEOGRID_CACHEDCOORDFUNCTION_HH
4 #define DUNE_GEOGRID_CACHEDCOORDFUNCTION_HH
5 
6 #include <cassert>
7 #include <memory>
8 
9 #include <dune/common/typetraits.hh>
10 
12 
16 
17 namespace Dune
18 {
19 
20  // Internal Forward Declarations
21  // -----------------------------
22 
23  template< class HostGrid, class CoordFunction >
25 
26 
27 
28  // GeoGrid::CoordCache
29  // -------------------
30 
31  namespace GeoGrid
32  {
33 
34  template< class HostGrid, class Coordinate >
35  class CoordCache
36  {
38 
39  static const unsigned int dimension = HostGrid::dimension;
40 
41  typedef typename HostGrid::template Codim< dimension >::Entity Vertex;
42 
44 
45  public:
46  explicit CoordCache ( const HostGrid &hostGrid )
47  : data_( hostGrid, dimension )
48  {}
49 
50  template< class Entity >
51  const Coordinate &operator() ( const Entity &entity, unsigned int corner ) const
52  {
53  return data_( entity, corner );
54  }
55 
56  const Coordinate &operator() ( const Vertex &vertex, unsigned int corner ) const
57  {
58  assert( corner == 0 );
59  return data_[ vertex ];
60  }
61 
62  template< class Entity >
63  Coordinate &operator() ( const Entity &entity, unsigned int corner )
64  {
65  return data_( entity,corner) ;
66  }
67 
68  Coordinate &operator() ( const Vertex &vertex, unsigned int corner )
69  {
70  assert( corner == 0 );
71  return data_[ vertex ];
72  }
73 
74  void adapt ()
75  {
76  data_.resize();
77  data_.shrinkToFit();
78  }
79 
80  private:
81  CoordCache ( const This & );
82  This &operator= ( const This & );
83 
84  DataCache data_;
85  };
86 
87  } // namespace GeoGrid
88 
89 
90 
91  // CachedCoordFunction
92  // -------------------
93 
94  template< class HostGrid, class CoordFunction >
95  class CachedCoordFunction
96  : public DiscreteCoordFunction< typename CoordFunction::ctype, CoordFunction::dimRange, CachedCoordFunction< HostGrid, CoordFunction > >
97  {
98  typedef CachedCoordFunction< HostGrid, CoordFunction > This;
99  typedef DiscreteCoordFunction< typename CoordFunction::ctype, CoordFunction::dimRange, This > Base;
100 
101  public:
102  typedef typename Base::ctype ctype;
103 
104  typedef typename Base::RangeVector RangeVector;
105 
106  private:
108 
109  public:
110  explicit
111  CachedCoordFunction ( const HostGrid &hostGrid,
112  const CoordFunction &coordFunction = CoordFunction() )
113  : hostGrid_( hostGrid ),
114  coordFunction_( coordFunction ),
115  cache_( hostGrid )
116  {
117  buildCache();
118  }
119 
120  void adapt ()
121  {
122  cache_.adapt();
123  buildCache();
124  }
125 
126  void buildCache ();
127 
128  template< class HostEntity >
129  void insertEntity ( const HostEntity &hostEntity );
130 
131  template< class HostEntity >
132  void evaluate ( const HostEntity &hostEntity, unsigned int corner, RangeVector &y ) const
133  {
134  y = cache_( hostEntity, corner );
135 #ifndef NDEBUG
137  CoordFunctionCaller;
138 
139  RangeVector z;
140  CoordFunctionCaller coordFunctionCaller( hostEntity, coordFunction_ );
141  coordFunctionCaller.evaluate( corner, z );
142  assert( ((y - z).two_norm() < 1e-6) );
143 #endif
144  }
145 
146  private:
147  const HostGrid &hostGrid_;
148  const CoordFunction &coordFunction_;
149  Cache cache_;
150  };
151 
152 
153 
154  // Implementation of CachedCoordFunction
155  // -------------------------------------
156 
157  template< class HostGrid, class CoordFunction >
159  {
160  typedef typename HostGrid::template Codim< 0 >::Entity Element;
161  typedef typename HostGrid::LevelGridView MacroView;
162  typedef typename HostGrid::HierarchicIterator HierarchicIterator;
163 
164  typedef typename MacroView::template Codim< 0 >::template Partition< All_Partition >::Iterator MacroIterator;
165 
166  const MacroView macroView = hostGrid_.levelGridView( 0 );
167  const int maxLevel = hostGrid_.maxLevel();
168 
169  const MacroIterator mend = macroView.template end< 0, All_Partition >();
170  for( MacroIterator mit = macroView.template begin< 0, All_Partition >(); mit != mend; ++mit )
171  {
172  const Element &macroElement = *mit;
173  insertEntity( macroElement );
174 
175  const HierarchicIterator hend = macroElement.hend( maxLevel );
176  for( HierarchicIterator hit = macroElement.hbegin( maxLevel ); hit != hend; ++hit )
177  insertEntity( *hit );
178  }
179  }
180 
181 
182  template< class HostGrid, class CoordFunction >
183  template< class HostEntity >
185  ::insertEntity ( const HostEntity &hostEntity )
186  {
188  CoordFunctionCaller;
189 
190  CoordFunctionCaller coordFunctionCaller( hostEntity, coordFunction_ );
191  const ReferenceElement< ctype, HostEntity::dimension > &refElement
192  = ReferenceElements< ctype, HostEntity::dimension >::general( hostEntity.type() );
193 
194  const unsigned int numCorners = refElement.size( HostEntity::dimension );
195  for( unsigned int i = 0; i < numCorners; ++i )
196  coordFunctionCaller.evaluate( i, cache_( hostEntity, i ) );
197  }
198 
199 } // namespace Dune
200 
201 #endif // #ifndef DUNE_GEOGRID_CACHEDCOORDFUNCTION_HH
Dune::CachedCoordFunction::evaluate
void evaluate(const HostEntity &hostEntity, unsigned int corner, RangeVector &y) const
Definition: cachedcoordfunction.hh:132
Dune::GeoGrid::Entity
DUNE-conform implementation of the entity.
Definition: geometrygrid/entity.hh:47
Dune
Include standard header files.
Definition: agrid.hh:59
Dune::CachedCoordFunction::insertEntity
void insertEntity(const HostEntity &hostEntity)
Definition: cachedcoordfunction.hh:185
Dune::GeoGrid::CoordFunctionCaller
Definition: coordfunctioncaller.hh:19
Dune::GeoGrid::CoordCache
Definition: cachedcoordfunction.hh:35
Dune::PersistentContainerMap::resize
void resize(const Value &value=Value())
Definition: persistentcontainermap.hh:86
capabilities.hh
Dune::DiscreteCoordFunction::RangeVector
Base ::RangeVector RangeVector
Definition: coordfunction.hh:214
Dune::PersistentContainer< HostGrid, Coordinate >
Dune::GeoGrid::CoordCache::operator()
const Coordinate & operator()(const Entity &entity, unsigned int corner) const
Definition: cachedcoordfunction.hh:51
Dune::DiscreteCoordFunctionInterface::ctype
ct ctype
field type of the coordinate vector
Definition: coordfunction.hh:149
gridenums.hh
Dune::CachedCoordFunction::ctype
Base::ctype ctype
Definition: cachedcoordfunction.hh:102
Dune::Alberta::Element
ALBERTA EL Element
Definition: misc.hh:51
Dune::CachedCoordFunction
Definition: cachedcoordfunction.hh:24
Dune::PersistentContainerMap::shrinkToFit
void shrinkToFit()
Definition: persistentcontainermap.hh:91
Dune::GeoGrid::CoordCache::CoordCache
CoordCache(const HostGrid &hostGrid)
Definition: cachedcoordfunction.hh:46
Dune::CachedCoordFunction::adapt
void adapt()
Definition: cachedcoordfunction.hh:120
persistentcontainer.hh
Dune::CachedCoordFunction::CachedCoordFunction
CachedCoordFunction(const HostGrid &hostGrid, const CoordFunction &coordFunction=CoordFunction())
Definition: cachedcoordfunction.hh:111
Dune::CachedCoordFunction::RangeVector
Base::RangeVector RangeVector
Definition: cachedcoordfunction.hh:104
Dune::CachedCoordFunction::buildCache
void buildCache()
Definition: cachedcoordfunction.hh:158
Dune::VTK::vertex
Definition: common.hh:179
Dune::GeoGrid::CoordCache::adapt
void adapt()
Definition: cachedcoordfunction.hh:74
coordfunctioncaller.hh