Loading [MathJax]/extensions/tex2jax.js

DUNE-FEM (unstable)

interpolation.hh
1#ifndef DUNE_FEM_SPACE_LOCALFINITEELEMENT_INTERPOLATION_HH
2#define DUNE_FEM_SPACE_LOCALFINITEELEMENT_INTERPOLATION_HH
3
4#include <cstddef>
5
6#include <utility>
7#include <vector>
8#include <optional>
9
11
12#include <dune/fem/misc/threads/threadsafevalue.hh>
13
14#include <dune/fem/function/common/localcontribution.hh>
15#include <dune/fem/space/basisfunctionset/transformed.hh>
16#include <dune/fem/space/basisfunctionset/vectorial.hh>
17#include <dune/fem/space/combinedspace/interpolation.hh>
18#include <dune/fem/space/common/localinterpolation.hh>
19
20namespace Dune
21{
22
23 namespace Fem
24 {
25
26 namespace Impl
27 {
28 // LocalFunctionWrapper
29 // --------------------
30 template< class LocalFunction, class BasisFunctionSet >
31 struct LocalFunctionWrapper
32 {
33 struct Traits
34 {
35 typedef typename LocalFunction::DomainType DomainType;
36 typedef typename LocalFunction::RangeType RangeType;
37 };
38 typedef typename LocalFunction::DomainType DomainType;
39 typedef typename LocalFunction::RangeType RangeType;
40
41 LocalFunctionWrapper ( const LocalFunction &lf, const BasisFunctionSet &bset ) : lf_( lf ) {}
42
43 template< class Arg >
44 void evaluate ( const Arg &x, typename Traits::RangeType &y ) const
45 {
46 lf_.evaluate( x, y );
47 }
48 template< class Arg >
49 typename Traits::RangeType operator()(const Arg &x) const
50 {
51 typename Traits::RangeType y;
52 evaluate(x,y);
53 return y;
54 }
55
56 private:
57 const LocalFunction &lf_;
58 };
59 // LocalFunctionWrapper
60 // --------------------
61 template< class LocalFunction, class Entity, class ShapeFunctionSet, class Transformation >
62 struct LocalFunctionWrapper< LocalFunction, TransformedBasisFunctionSet< Entity, ShapeFunctionSet, Transformation > >
63 {
64 typedef TransformedBasisFunctionSet< Entity, ShapeFunctionSet, Transformation > BasisFunctionSetType;
65
66 struct Traits
67 {
68 typedef typename LocalFunction::DomainType DomainType;
69 typedef typename LocalFunction::RangeType RangeType;
70 };
71 typedef typename LocalFunction::DomainType DomainType;
72 typedef typename LocalFunction::RangeType RangeType;
73
74 LocalFunctionWrapper ( const LocalFunction &lf, const BasisFunctionSetType &bset )
75 : lf_( lf ), bset_( bset )
76 {
77 bset_.geometry();
78 }
79
80 template< class Arg >
81 void evaluate ( const Arg &x, typename Traits::RangeType &y ) const
82 {
83 typename Traits::RangeType help;
84 lf_.evaluate( x, help );
85 // geometry needs to come from local function. For some reasons the
86 // geometry from the basis set was invalid. Something to investigate.
87 // typename Transformation::InverseTransformationType transf( lf_.geometry(), x );
88 typename Transformation::InverseTransformationType transf( bset_.geometry(), x );
89 y = transf.apply( help );
90 }
91 template< class Arg >
92 typename Traits::RangeType operator() ( const Arg &x ) const
93 {
94 typename Traits::RangeType y;
95 evaluate(x,y);
96 return y;
97 }
98
99 private:
100 const LocalFunction &lf_;
101 const BasisFunctionSetType &bset_;
102 };
103
104 } // namespace Impl
105
106
107 // LocalFiniteElementInterpolation
108 // -------------------------------
109
110 template< class Space, class LocalInterpolation, bool scalarSFS >
111 class LocalFiniteElementInterpolation;
112
113 // a vector valued shape basis function set taken from
114 // dune-localfunction - the vector value 'blow up' is not yet supported
115 // for this case
116 template< class Space, class LocalInterpolation >
117 class LocalFiniteElementInterpolation<Space,LocalInterpolation,false>
118 {
119 typedef LocalFiniteElementInterpolation< Space, LocalInterpolation,false > ThisType;
120
121 public:
122 typedef typename Space::BasisFunctionSetType BasisFunctionSetType;
123 typedef LocalInterpolation LocalInterpolationType;
124
125 private:
126 typedef typename BasisFunctionSetType::FunctionSpaceType FunctionSpaceType;
127
128 typedef typename FunctionSpaceType::RangeType RangeType;
129 typedef typename FunctionSpaceType::RangeFieldType RangeFieldType;
130 static const int dimRange = FunctionSpaceType::dimRange;
131
132 typedef std::size_t size_type;
133
134 template< class LocalFunction >
135 using LocalFunctionWrapper = Impl::LocalFunctionWrapper< LocalFunction, BasisFunctionSetType >;
136
137 public:
138 LocalFiniteElementInterpolation()
139 : basisFunctionSet_()
140 , localInterpolation_()
141 {
142 }
143
144 explicit LocalFiniteElementInterpolation ( const BasisFunctionSetType &basisFunctionSet,
145 const LocalInterpolationType &localInterpolation = LocalInterpolationType() )
146 : basisFunctionSet_( basisFunctionSet ),
147 localInterpolation_( localInterpolation )
148 {}
149
150 ThisType& operator=( const ThisType& other )
151 {
152 basisFunctionSet_ = other.basisFunctionSet_;
153 localInterpolation_.emplace( other.localInterpolation() );
154 return *this;
155 }
156
157 void unbind()
158 {
159 // basisFunctionSet_ = BasisFunctionSetType();
160 }
161
162 template< class LocalFunction, class Dof >
163 void operator() ( const LocalFunction &localFunction, std::vector< Dof > &localDofVector ) const
164 {
165 LocalFunctionWrapper< LocalFunction > wrapper( localFunction, basisFunctionSet_ );
166 localInterpolation().interpolate( wrapper, localDofVector );
167 }
168
169 template< class LocalFunction, class Dof, class Allocator >
170 void operator() ( const LocalFunction &localFunction, Dune::DynamicVector< Dof, Allocator > &localDofVector ) const
171 {
172 (*this)(localFunction,localDofVector.container() );
173 }
174
175 template< class LocalFunction, class DiscreteFunction, template< class > class Assembly >
176 void operator() ( const LocalFunction &localFunction, LocalContribution< DiscreteFunction, Assembly > &localContribution ) const
177 {
178 (*this)(localFunction,localContribution.localDofVector());
179 }
180
181 BasisFunctionSetType basisFunctionSet () const { return basisFunctionSet_; }
182 const LocalInterpolationType &localInterpolation () const
183 {
184 assert( localInterpolation_.has_value() );
185 return *localInterpolation_;
186 }
187
188 private:
189 BasisFunctionSetType basisFunctionSet_;
190 std::optional< LocalInterpolationType > localInterpolation_;
191 };
192
193 namespace Impl
194 {
195 template <int dimRange>
196 struct RangeConverter
197 {
198 RangeConverter ( std::size_t range ) : range_( range ) {}
199
200 template< class T >
201 FieldVector< T, 1 > operator() ( const FieldVector< T, dimRange > &in ) const
202 {
203 return in[ range_ ];
204 }
205
206 template< class T, int j >
207 FieldMatrix< T, 1, j > operator() ( const FieldMatrix< T, dimRange, j > &in ) const
208 {
209 // return in[ range_ ]; // implicit conversion fails
210 FieldMatrix<T,1,j> ret;
211 ret[0] = in[range_];
212 return ret;
213 }
214
215 protected:
216 std::size_t range_;
217 };
218 template <class DofVector, class DofAlignment>
219 struct SubDofVectorWrapper
220 : public SubDofVector< DofVector, DofAlignment >
221 {
222 typedef SubDofVector< DofVector, DofAlignment > BaseType;
223
224 SubDofVectorWrapper( DofVector& dofs, int coordinate, const DofAlignment &dofAlignment )
225 : BaseType( dofs, coordinate, dofAlignment )
226 {}
227
229 void clear() {}
230 void resize( const unsigned int) {}
231 };
232
233 }
234
235 // a scalar valued shape basis function set taken from
236 // dune-localfunction - for this the vector value 'blow up' is supported
237 // as with other spaces
238 template< class Space, class LocalInterpolation >
239 class LocalFiniteElementInterpolation<Space,LocalInterpolation,true>
240 {
241 typedef LocalFiniteElementInterpolation< Space, LocalInterpolation,true > ThisType;
242
243 public:
244 typedef typename Space::BasisFunctionSetType BasisFunctionSetType;
245 typedef LocalInterpolation LocalInterpolationType;
246
247 private:
248 typedef typename BasisFunctionSetType::FunctionSpaceType FunctionSpaceType;
249 // typedef typename Space::FunctionSpaceType FunctionSpaceType;
250
251 typedef typename FunctionSpaceType::RangeType RangeType;
252 typedef typename FunctionSpaceType::RangeFieldType RangeFieldType;
253 static const int dimRange = FunctionSpaceType::dimRange;
254 static const int dimR = Space::FunctionSpaceType::dimRange;
255
256 typedef std::size_t size_type;
257
258 typedef VerticalDofAlignment< BasisFunctionSetType, RangeType> DofAlignmentType;
259
260 public:
261 LocalFiniteElementInterpolation ()
262 : basisFunctionSet_(),
263 localInterpolation_(),
264 dofAlignment_()
265 {}
266
267 explicit LocalFiniteElementInterpolation ( const BasisFunctionSetType &basisFunctionSet,
268 const LocalInterpolationType &localInterpolation = LocalInterpolationType() )
269 : basisFunctionSet_( basisFunctionSet ),
270 localInterpolation_( localInterpolation ),
271 dofAlignment_( basisFunctionSet_ )
272 {}
273
274 LocalFiniteElementInterpolation ( const ThisType& other )
275 : basisFunctionSet_( other.basisFunctionSet_ ),
276 localInterpolation_( other.localInterpolation_ ),
277 dofAlignment_( basisFunctionSet_ )
278 {}
279
280 ThisType& operator=( const ThisType& other )
281 {
282 basisFunctionSet_ = other.basisFunctionSet_;
283 localInterpolation_.emplace( other.localInterpolation() );
284 return *this;
285 }
286
287 void unbind()
288 {
289 // basisFunctionSet_ = BasisFunctionSetType();
290 }
291
292 template< class LocalFunction, class Vector>
293 void operator() ( const LocalFunction &localFunction, Vector &localDofVector ) const
294 {
295 // clear dofs before something is adedd
296 // localDofVector.clear(); // does not exist on DynVector so use 'fill' instead
297 std::fill(localDofVector.begin(),localDofVector.end(),0);
298 for( std::size_t i = 0; i < dimR; ++i )
299 {
300 Impl::SubDofVectorWrapper< Vector, DofAlignmentType > subLdv( localDofVector, i, dofAlignment_ );
301 (*this)(
302 localFunctionConverter( localFunction, Impl::RangeConverter<dimR>(i) ),
303 subLdv, PriorityTag<42>()
304 );
305 }
306 }
307
308 template< class LocalFunction, class Dof, class Allocator >
309 void operator() ( const LocalFunction &localFunction, Dune::DynamicVector< Dof, Allocator > &localDofVector ) const
310 {
311 (*this)(localFunction,localDofVector.container() );
312 }
313
314 template< class LocalFunction, class DiscreteFunction, template< class > class Assembly >
315 void operator() ( const LocalFunction &localFunction, LocalContribution< DiscreteFunction, Assembly > &localContribution ) const
316 {
317 (*this)(localFunction,localContribution.localDofVector());
318 }
319
320 BasisFunctionSetType basisFunctionSet () const { return basisFunctionSet_; }
321 const LocalInterpolationType &localInterpolation () const
322 {
323 assert( localInterpolation_.has_value() );
324 return *localInterpolation_;
325 }
326
327 private:
328 template< class LocalFunction, class Vector>
329 auto operator() ( const LocalFunction &localFunction, Vector &localDofVector, PriorityTag<1> ) const
330 -> void_t< decltype(
331 std::declval<LocalInterpolationType>().interpolate(
332 localFunction, localDofVector)) >
333 {
334 localInterpolation().interpolate( localFunction, localDofVector );
335 }
336 template< class LocalFunction, class Vector>
337 void operator() ( const LocalFunction &localFunction, Vector &localDofVector, PriorityTag<0> ) const
338 {
339 std::vector<typename Vector::value_type> tmp(basisFunctionSet_.size());
340 localInterpolation().interpolate( localFunction, tmp);
341 for (unsigned int i=0;i<tmp.size();++i)
342 localDofVector[i] = tmp[i];
343 }
344 BasisFunctionSetType basisFunctionSet_;
345 std::optional< LocalInterpolationType > localInterpolation_;
346 DofAlignmentType dofAlignment_;
347 };
348
349 template <class DiscreteFunctionSpace >
350 class LocalFEInterpolationWrapper
351 : public LocalInterpolationWrapper< DiscreteFunctionSpace >
352 {
353 typedef LocalInterpolationWrapper< DiscreteFunctionSpace > BaseType;
354 protected:
355 using BaseType :: interpolation_;
356 typedef std::vector< typename DiscreteFunctionSpace::RangeFieldType >
357 TemporarayDofVectorType;
359
360 using BaseType::interpolation;
361 public:
362 LocalFEInterpolationWrapper( const DiscreteFunctionSpace& space )
363 : BaseType( space )
364 {}
365
366 using BaseType :: bind;
367 using BaseType :: unbind;
368
369 template< class LocalFunction, class Dof >
370 void operator() ( const LocalFunction &localFunction, std::vector< Dof > &localDofVector ) const
371 {
372 // interpolation only works for std::vector storage
373 interpolation()( localFunction, localDofVector );
374 }
375
376 template< class LocalFunction, class Dof, class Allocator >
377 void operator() ( const LocalFunction &localFunction, Dune::DynamicVector< Dof, Allocator > &localDofVector ) const
378 {
379 // DynamicVector internally stores a std::vector
380 (*this)(localFunction, localDofVector.container() );
381 }
382
383
384 template< class LocalFunction, class DiscreteFunction, template< class > class Assembly >
385 void operator() ( const LocalFunction &localFunction, LocalContribution< DiscreteFunction, Assembly > &localContribution ) const
386 {
387 // LocalContribution internally stores a std::vector
388 (*this)(localFunction, localContribution.localDofVector());
389 }
390
392 template< class LocalFunction, class LocalDofVector >
393 void operator () ( const LocalFunction &localFunction, LocalDofVector &dofs ) const
394 {
395 const int size = dofs.size();
396 TemporarayDofVectorType& tmpDofs = *tmpDofs_;
397 tmpDofs.resize( size );
398 (*this)(localFunction, tmpDofs );
399
400 // copy to dof vector
401 for( int i=0; i<size; ++i )
402 {
403 dofs[ i ] = tmpDofs[ i ];
404 }
405 }
406 };
407
408 } // namespace Fem
409
410} // namespace Dune
411
412#endif // #ifndef DUNE_FEM_SPACE_LOCALFINITEELEMENT_INTERPOLATION_HH
discrete function space
Construct a vector with a dynamic size.
Definition: dynvector.hh:59
FunctionSpaceTraits::RangeType RangeType
Type of range vector (using type of range field) has a Dune::FieldVector type interface.
Definition: functionspaceinterface.hh:71
@ dimRange
dimension of range vector space
Definition: functionspaceinterface.hh:48
FunctionSpaceTraits::RangeFieldType RangeFieldType
Intrinsic type used for values in the range field (usually a double)
Definition: functionspaceinterface.hh:63
A vector valued function space.
Definition: functionspace.hh:60
FunctionSpaceType::DomainType DomainType
type of domain vectors, i.e., type of coordinates
Definition: localfunction.hh:108
FunctionSpaceType::RangeType RangeType
type of range vectors, i.e., type of function values
Definition: localfunction.hh:110
Implements a vector constructed from a given type representing a field and a compile-time given size.
typename Impl::voider< Types... >::type void_t
Is void for all valid input types. The workhorse for C++11 SFINAE-techniques.
Definition: typetraits.hh:40
static constexpr IntegralRange< std::decay_t< T > > range(T &&from, U &&to) noexcept
free standing function for setting up a range based for loop over an integer range for (auto i: range...
Definition: rangeutilities.hh:294
Dune namespace.
Definition: alignedallocator.hh:13
constexpr std::integral_constant< std::size_t, sizeof...(II)> size(std::integer_sequence< T, II... >)
Return the size of the sequence.
Definition: integersequence.hh:75
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden & Uni Heidelberg  |  generated with Hugo v0.111.3 (Apr 28, 22:35, 2025)