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