Dune Core Modules (unstable)

cachedcoordfunction.hh
1// SPDX-FileCopyrightText: Copyright © 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
12
13#include <dune/grid/common/gridenums.hh>
14
15#include <dune/grid/geometrygrid/capabilities.hh>
16#include <dune/grid/geometrygrid/coordfunctioncaller.hh>
17#include <dune/grid/utility/persistentcontainer.hh>
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 >
37 class CoordCache
38 {
39 typedef CoordCache< HostGrid, Coordinate > This;
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 >
97 class CachedCoordFunction
98 : public DiscreteCoordFunction< typename CoordFunction::ctype, CoordFunction::dimRange, CachedCoordFunction< HostGrid, CoordFunction > >
99 {
100 typedef CachedCoordFunction< HostGrid, CoordFunction > This;
101 typedef DiscreteCoordFunction< typename CoordFunction::ctype, CoordFunction::dimRange, This > Base;
102
103 public:
104 typedef typename Base::ctype ctype;
105
106 typedef typename Base::RangeVector RangeVector;
107
108 private:
109 typedef GeoGrid::CoordCache< HostGrid, RangeVector > Cache;
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
138 typedef GeoGrid::CoordFunctionCaller< HostEntity, typename CoordFunction::Interface >
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 >
160 inline void CachedCoordFunction< HostGrid, CoordFunction >::buildCache ()
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 >
186 inline void CachedCoordFunction< HostGrid, CoordFunction >
187 ::insertEntity ( const HostEntity &hostEntity )
188 {
189 typedef GeoGrid::CoordFunctionCaller< HostEntity, typename CoordFunction::Interface >
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
ct ctype
field type of the coordinate vector
Definition: coordfunction.hh:185
constexpr GeometryType vertex
GeometryType representing a vertex.
Definition: type.hh:492
Dune namespace.
Definition: alignedallocator.hh:13
Traits for type conversions and type information.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Dec 22, 23:30, 2024)