DUNE-ACFEM (2.5.1)

gridfunctionwrapper.hh
1#ifndef DUNE_ACFEM_GRIDFUNCTIONWRAPPER_HH
2#define DUNE_ACFEM_GRIDFUNCTIONWRAPPER_HH
3
4//#include <dune/fem/function/common/gridfunctionadapter.hh>
5#include "../functions/gridfunctionadapter.hh" // use our own version in order to get rid of a static_assert
6
7#include "../functions/gridfunctionexpressionbase.hh"
8#include "../common/stringhelper.hh"
9
10namespace Dune {
11
12 namespace ACFem {
13
33 template<class FunctionImp, class GridPart>
34 class GridFunctionWrapper;
35
36 template<class FunctionImp, class GridPart>
37 class GridFunctionWrapperTraits
38 : public GridFunctionAdapterTraits<FunctionImp, GridPart>
39 {
40 typedef GridFunctionWrapper<FunctionImp, GridPart> DiscreteFunctionType;
41 };
42
47 template<class FunctionImp, class GridPart>
49 : public GridFunctionExpression<typename FunctionImp::FunctionSpaceType,
50 GridFunctionWrapper<FunctionImp, GridPart> >
51 {
56
57 public:
58 typedef GridFunctionWrapperTraits<FunctionImp, GridPart> Traits;
59
60 typedef GridPart GridPartType;
61 typedef ThisType FunctionType;
62 typedef FunctionImp FunctionImpType;
63
65
68
69 // type of discrete function space
70 typedef typename AdapterType::FunctionSpaceType FunctionSpaceType;
71
73 typedef typename DiscreteFunctionSpaceType::GridType GridType;
74
76 typedef typename DiscreteFunctionSpaceType::DomainFieldType DomainFieldType ;
78 typedef typename DiscreteFunctionSpaceType::RangeFieldType RangeFieldType ;
80 typedef typename DiscreteFunctionSpaceType::DomainType DomainType ;
82 typedef typename DiscreteFunctionSpaceType::RangeType RangeType ;
84 typedef typename DiscreteFunctionSpaceType::JacobianRangeType JacobianRangeType;
86 typedef typename DiscreteFunctionSpaceType::HessianRangeType HessianRangeType;
87
90
91 static const int dimRange = DiscreteFunctionSpaceType::dimRange;
92 static const int dimDomain = GridPart::GridType::dimensionworld;
93 static const int dimLocal = GridPart::GridType::dimension;
94
95 private:
96 class LocalFunction;
97 class LocalFunctionStorage;
98 using ExpressionBaseType::expressionName_;
99
100 public:
102 typedef LocalFunction LocalFunctionType;
103 typedef LocalFunctionStorage LocalFunctionStorageType;
104
105 GridFunctionWrapper(const std::string& name,
106 const FunctionImpType& function,
107 const GridPartType& gridPart,
108 unsigned int order = DiscreteFunctionSpaceType::polynomialOrder)
109 : function_(function),
110 localFunctionStorage_(*this),
111 adapter_(name, function_, gridPart, order)
112 {
113 expressionName_ = makeName(adapter_.name());
114 }
115
116 GridFunctionWrapper(const ThisType& other)
117 : ExpressionBaseType(other),
118 function_(other.function_),
119 localFunctionStorage_(*this),
120 adapter_(other.adapter_.name(), function_, other.space().gridPart(), other.space().order())
121 {}
122
128 void evaluate(const DomainType &global, RangeType &result) const
129 {
130 adapter_.evaluate(global, result);
131 }
132
138 void jacobian(const DomainType &global, JacobianRangeType &result) const
139 {
140 adapter_.jacobian(global, result);
141 }
142
148 void hessian(const DomainType &global, HessianRangeType &result) const
149 {
150 adapter_.hessian(global, result);
151 }
152
154 const LocalFunctionType localFunction(const EntityType &entity) const
155 {
156 return localFunctionStorage().localFunction(entity);
157 }
158
161 {
162 return localFunctionStorage().localFunction(entity);
163 }
164
165 LocalFunctionStorageType &localFunctionStorage () const
166 {
167 return localFunctionStorage_;
168 }
169
172 {
173 return adapter_.space();
174 }
175
176 const GridPartType &gridPart() const
177 {
178 return adapter_.gridPart();
179 }
180
181 const AdapterType& adapter() const {
182 return adapter_;
183 }
184
185 const FunctionImpType& function() const {
186 return function_;
187 }
188
189 protected:
190 // Remove any outer redundant parenthesis and wrap into "W(.)"
191 static std::string makeName(const std::string& name)
192 {
193 std::string result(name);
194 trimParenthesis(result);
195 result = "W(" + result + ")";
196 return result;
197 }
198
199 FunctionImpType function_;
200 mutable LocalFunctionStorageType localFunctionStorage_;
201 GridFunctionAdapter<FunctionImp, GridPart> adapter_;
202 };
203
204 template<class FunctionImp, class GridPart>
205 class GridFunctionWrapper<FunctionImp, GridPart>::LocalFunction
206 : public GridFunctionAdapter<FunctionImp, GridPart>::LocalFunctionType
207 {
208 typedef LocalFunction ThisType;
209 typedef GridFunctionAdapter<FunctionImp, GridPart> AdapterType;
210 typedef typename AdapterType::LocalFunctionType BaseType;
211 public:
212 typedef GridFunctionWrapper<FunctionImp, GridPart> DiscreteFunctionType;
213
214 typedef typename BaseType::EntityType EntityType;
215
217 LocalFunction(const EntityType &entity, const DiscreteFunctionType &df)
218 : BaseType(entity, df.adapter_)
219 {}
220
221 LocalFunction (const DiscreteFunctionType &df)
222 : BaseType(df.adapter_)
223 {}
224
225 LocalFunction(LocalFunctionStorageType &storage)
226 : BaseType(storage)
227 {}
228 };
229
230 // 1-to-1 copy from Fem::GridFunctionAdapter
231 template<class FunctionImp, class GridPart>
232 class GridFunctionWrapper<FunctionImp, GridPart>::LocalFunctionStorage
233 : public GridFunctionAdapter<FunctionImp, GridPart>::LocalFunctionStorageType
234 {
235 typedef LocalFunctionStorage ThisType;
236 typedef GridFunctionWrapper<FunctionImp, GridPart> DiscreteFunctionType;
237 typedef GridFunctionAdapter<FunctionImp, GridPart> AdapterType;
238 typedef typename AdapterType::LocalFunctionStorageType BaseType;
239 public:
241
242 explicit LocalFunctionStorage(DiscreteFunctionType &discreteFunction)
243 : BaseType(discreteFunction.adapter_),
244 discreteFunction_(discreteFunction)
245 {}
246
248 {
249 return LocalFunctionType(function());
250 }
251
252 template<class Entity>
253 const LocalFunctionType localFunction(const Entity &entity) const
254 {
255 return LocalFunctionType(entity, function());
256 }
257
258 template<class Entity>
259 LocalFunctionType localFunction ( const Entity &entity )
260 {
261 return LocalFunctionType(entity, function());
262 }
263
264 DiscreteFunctionType &function()
265 {
266 return discreteFunction_;
267 }
268
269 private:
270 LocalFunctionStorage(const ThisType &);
271 ThisType operator= (const ThisType &);
272
273 DiscreteFunctionType &discreteFunction_;
274 };
275
277 template<class FunctionImp, class GridPart>
279 {
280 protected:
281 template<class Dummy, bool hasLocalFunction = true>
282 struct ConverterHelper
283 {
284 typedef FunctionImp WrappedGridFunctionType;
285 typedef FunctionImp AdaptedGridFunctionType;
286
287 static const WrappedGridFunctionType& wrap(const std::string& name, const FunctionImp& f, const GridPart& g, unsigned order)
288 {
289 return f;
290 }
291
292 static const AdaptedGridFunctionType& adapt(const std::string& name, const FunctionImp& f, const GridPart& g, unsigned order)
293 {
294 return f;
295 }
296 };
297
298 template<class Dummy>
299 struct ConverterHelper<Dummy, false>
300 {
301 typedef GridFunctionWrapper<FunctionImp, GridPart> WrappedGridFunctionType;
302 typedef GridFunctionAdapter<FunctionImp, GridPart> AdaptedGridFunctionType;
303
304 static WrappedGridFunctionType wrap(const std::string& name, const FunctionImp& f, const GridPart& g, unsigned order)
305 {
306 return WrappedGridFunctionType(name, f, g, order);
307 }
308
309 static AdaptedGridFunctionType adapt(const std::string& name, const FunctionImp& f, const GridPart& g, unsigned order)
310 {
311 return AdaptedGridFunctionType(name, f, g, order);
312 }
313 };
314 enum { hasLocalFunction = std::is_base_of<Fem::HasLocalFunction, FunctionImp>::value };
315 typedef ConverterHelper<void, hasLocalFunction> HelperType;
316 public:
317 typedef typename HelperType::WrappedGridFunctionType WrappedGridFunctionType;
318 typedef typename HelperType::AdaptedGridFunctionType AdaptedGridFunctionType;
319
320 static auto wrap(const std::string& name, const FunctionImp& f, const GridPart& g, unsigned order)
321 -> decltype(HelperType::wrap(name, f, g, order))
322 {
323 return HelperType::wrap(name, f, g, order);
324 }
325
326 static auto adapt(const std::string& name, const FunctionImp& f, const GridPart& g, unsigned order)
327 -> decltype(HelperType::adapt(name, f, g, order))
328 {
329 return HelperType::adapt(name, f, g, order);
330 }
331 };
332
340 template<class FunctionImp, class GridPart>
341 static inline
342 auto wrapToGridFunction(const std::string& name, const FunctionImp& f, const GridPart& g, unsigned order = 111)
344 -> decltype(GridFunctionConverter<FunctionImp, GridPart>::wrap(name, f, g, order))
346 {
348 }
349
358 template<class FunctionImp, class GridPart>
359 static inline
360 auto adaptToGridFunction(const std::string& name, const FunctionImp& f, const GridPart& g, unsigned order = 111)
362 -> decltype(GridFunctionConverter<FunctionImp, GridPart>::adapt(name, f, g, order))
364 {
366 }
367
369
371
372 } // ACFem::
373
374} // Dune::
375
376
377#endif // DUN_ACFEM_GRIDFUNCITONWRAPPER_HH
void jacobian(const DomainType &global, JacobianRangeType &result) const
evaluate function on local coordinate local
Definition: gridfunctionadapter.hh:186
LocalFunction LocalFunctionType
type of local function to export
Definition: gridfunctionadapter.hh:157
Traits::DiscreteFunctionSpaceType DiscreteFunctionSpaceType
type of discrete function space
Definition: gridfunctionadapter.hh:127
Traits::EntityType EntityType
type of codim 0 entity
Definition: gridfunctionadapter.hh:149
const DiscreteFunctionSpaceType & space() const
Definition: gridfunctionadapter.hh:221
const std::string & name() const
Definition: gridfunctionadapter.hh:215
void hessian(const DomainType &global, HessianRangeType &result) const
evaluate function on local coordinate local
Definition: gridfunctionadapter.hh:192
void evaluate(const DomainType &global, RangeType &result) const
evaluate function on local coordinate local
Definition: gridfunctionadapter.hh:180
A class providing some basic functionality common to all expressions.
Definition: gridfunctionexpressionbase.hh:35
const std::string & name() const
Return a descriptive name for the function.
Definition: gridfunctionexpressionbase.hh:56
A special version of Fem::GridFunctionAdapter which stores a copy to the function,...
Definition: gridfunctionwrapper.hh:51
DiscreteFunctionSpaceType::DomainFieldType DomainFieldType
domain type (from function space)
Definition: gridfunctionwrapper.hh:76
AdapterType::EntityType EntityType
type of codim 0 entity
Definition: gridfunctionwrapper.hh:89
AdapterType::DiscreteFunctionSpaceType DiscreteFunctionSpaceType
type of discrete function space
Definition: gridfunctionwrapper.hh:67
void jacobian(const DomainType &global, JacobianRangeType &result) const
evaluate the Jacobian of the function
Definition: gridfunctionwrapper.hh:138
const DiscreteFunctionSpaceType & space() const
Definition: gridfunctionwrapper.hh:171
DiscreteFunctionSpaceType::GridType GridType
type of grid
Definition: gridfunctionwrapper.hh:73
DiscreteFunctionSpaceType::JacobianRangeType JacobianRangeType
jacobian type (from function space)
Definition: gridfunctionwrapper.hh:84
DiscreteFunctionSpaceType::HessianRangeType HessianRangeType
hessian type (from function space)
Definition: gridfunctionwrapper.hh:86
DiscreteFunctionSpaceType::DomainType DomainType
domain type (from function space)
Definition: gridfunctionwrapper.hh:80
DiscreteFunctionSpaceType::RangeFieldType RangeFieldType
range type (from function space)
Definition: gridfunctionwrapper.hh:78
const LocalFunctionType localFunction(const EntityType &entity) const
cons
Definition: gridfunctionwrapper.hh:154
void evaluate(const DomainType &global, RangeType &result) const
evaluate the function
Definition: gridfunctionwrapper.hh:128
LocalFunctionType localFunction(const EntityType &entity)
Definition: gridfunctionwrapper.hh:160
void hessian(const DomainType &global, HessianRangeType &result) const
evaluate the Hessian of the function
Definition: gridfunctionwrapper.hh:148
LocalFunction LocalFunctionType
type of local function to export
Definition: gridfunctionwrapper.hh:102
DiscreteFunctionSpaceType::RangeType RangeType
range type (from function space)
Definition: gridfunctionwrapper.hh:82
static void trimParenthesis(std::string &name)
Remove any outer redundant parenthesis.
Definition: stringhelper.hh:40
static auto adaptToGridFunction(const std::string &name, const FunctionImp &f, const GridPart &g, unsigned order=111)
Possibly enhance a function by encapsulating a reference into a GridFunctionAdapter.
Definition: gridfunctionwrapper.hh:360
static auto wrapToGridFunction(const std::string &name, const FunctionImp &f, const GridPart &g, unsigned order=111)
Possibly wrap a function into a GridFunctionWrapper.
Definition: gridfunctionwrapper.hh:342
Helper class for wrapToGridFunction() and adaptToGridFunction()
Definition: gridfunctionwrapper.hh:279
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Aug 13, 22:30, 2024)