DUNE-FEM (unstable)

interpolate.hh
1#ifndef DUNE_FEM_SPACE_COMMON_INTERPOLATE_HH
2#define DUNE_FEM_SPACE_COMMON_INTERPOLATE_HH
3
4#include <algorithm>
5#include <type_traits>
6#include <utility>
7
9
10#include <dune/grid/common/partitionset.hh>
11#include <dune/grid/common/rangegenerators.hh>
12
13#include <dune/fem/common/bindguard.hh>
14#include <dune/fem/storage/entitygeometry.hh>
15
16#include <dune/fem/function/common/discretefunction.hh>
17#include <dune/fem/function/common/gridfunctionadapter.hh>
18#include <dune/fem/function/common/localcontribution.hh>
19#include <dune/fem/function/localfunction/const.hh>
20#include <dune/fem/space/common/capabilities.hh>
21#include <dune/fem/space/common/localinterpolation.hh>
22
23namespace Dune
24{
25
26 namespace Fem
27 {
28
29 // interpolate
30 // -----------
31
54 template< class GridFunction, class DiscreteFunction >
55 static inline void interpolate ( const GridFunction &u, DiscreteFunction &v )
56 {
57 // just call interpolate for the all partition
59 }
60
61 template< class Function, class DiscreteFunction, unsigned int partitions >
62 static inline std::enable_if_t< !std::is_convertible< Function, HasLocalFunction >::value >
63 interpolate ( const Function &u, DiscreteFunction &v, PartitionSet< partitions > ps )
64 {
65 typedef typename DiscreteFunction :: DiscreteFunctionSpaceType :: GridPartType GridPartType;
66 typedef GridFunctionAdapter< Function, GridPartType > GridFunctionType;
67
68 GridFunctionType uGrid( "uGrid", u, v.space().gridPart() );
69
70 interpolate( uGrid, v, ps );
71 }
72
73 template< class GridFunction, class DiscreteFunction, unsigned int partitions >
74 static inline std::enable_if_t< std::is_convertible< GridFunction, HasLocalFunction >::value && Capabilities::hasInterpolation< typename DiscreteFunction::DiscreteFunctionSpaceType >::v >
75 interpolate ( const GridFunction &u, DiscreteFunction &v, PartitionSet< partitions > ps )
76 {
77 ConstLocalFunction< GridFunction > uLocal( u );
78 LocalContribution< DiscreteFunction, Assembly::Set > vLocal( v, /* communicate = */ false );
79 LocalInterpolation< typename DiscreteFunction::DiscreteFunctionSpaceType >
80 interpolation( v.space() );
81
82 // iterate over selected partition
83 for( const auto& entity : elements( v.gridPart(), ps ) )
84 {
85 // initialize u to entity
86 auto uGuard = bindGuard( uLocal, entity );
87
88 // bind v to entity
89 auto vGuard = bindGuard( vLocal, entity );
90
91 // bind interpolation to entity
92 auto iGuard = bindGuard( interpolation, entity );
93
94 // perform local interpolation
95 interpolation( uLocal, vLocal );
96 }
97 }
98
99
100
101 namespace Impl
102 {
103
104 template< class Entity, class FunctionSpace, class Weight >
105 struct WeightLocalFunction : public EntityStorage< Entity >
106 {
107 typedef Entity EntityType;
108 typedef EntityStorage< EntityType > BaseType;
109
110 typedef FunctionSpace FunctionSpaceType;
111
112 typedef typename FunctionSpaceType::DomainFieldType DomainFieldType;
113 typedef typename FunctionSpaceType::RangeFieldType RangeFieldType;
114
115 typedef typename FunctionSpaceType::DomainType DomainType;
116 typedef typename FunctionSpaceType::RangeType RangeType;
117 typedef typename FunctionSpaceType::JacobianRangeType JacobianRangeType;
118 typedef typename FunctionSpaceType::HessianRangeType HessianRangeType;
119
120 typedef typename EntityType::Geometry::LocalCoordinate LocalCoordinateType;
121
122 static constexpr int dimDomain = FunctionSpaceType::dimDomain;
123 static constexpr int dimRange = FunctionSpaceType::dimRange;
124
125 explicit WeightLocalFunction ( Weight &weight, int order )
126 : BaseType(), weight_( weight ), order_(order) {}
127
128 void bind ( const EntityType &entity )
129 {
130 BaseType::bind(entity);
131 weight_.setEntity( entity );
132 }
133
134 using BaseType :: unbind;
135 using BaseType :: entity;
136
137 template< class Point >
138 void evaluate ( const Point &x, RangeType &value ) const
139 {
140 const RangeFieldType weight = weight_( coordinate( x ) );
141 for( int i = 0; i < dimRange; ++i )
142 value[ i ] = weight;
143 }
144 template< class Point >
145 void jacobian ( const Point &x, JacobianRangeType &value ) const
146 {
147 const RangeFieldType weight = weight_( coordinate( x ) );
148 for( int i = 0; i < dimRange; ++i )
149 value[ i ] = weight;
150 }
151
152 template< class Quadrature, class Values >
153 auto evaluateQuadrature ( const Quadrature &quadrature, Values &values ) const
154 -> std::enable_if_t< std::is_same< decltype( values[ 0 ] ), RangeType & >::value >
155 {
156 for( const auto &qp : quadrature )
157 evaluate( qp, values[ qp.index() ] );
158 }
159
160 int order() const { return order_; }
161 private:
162 Weight &weight_;
163 int order_;
164 };
165
166 } // namespace Impl
167
168
169
170 // interpolate with weights
171 // ------------------------
172
173 template< class GridFunction, class DiscreteFunction, class Weight >
174 inline static auto interpolate ( const GridFunction &u, DiscreteFunction &v, Weight &&weight )
175 -> std::enable_if_t< !std::is_const< std::remove_reference_t< Weight > >::value >
176 {
177 DiscreteFunction w( v );
178 interpolate( u, v, std::forward< Weight >( weight ), w );
179 }
180
181 template< class GridFunction, class DiscreteFunction, class Weight >
182 inline static auto interpolate ( const GridFunction &u, DiscreteFunction &v, Weight &&weight )
183 -> std::enable_if_t< !std::is_const< std::remove_reference_t< Weight > >::value && !std::is_base_of< HasLocalFunction, GridFunction >::value >
184 {
185 interpolate( gridFunctionAdapter( u, v.gridPart() ), v, std::forward< Weight >( weight ) );
186 }
187
188 template< class GridFunction, class DiscreteFunction, class Weight >
189 inline static auto interpolate ( const GridFunction &u, DiscreteFunction &v, Weight &&weight, DiscreteFunction &w )
190 -> std::enable_if_t< std::is_base_of< HasLocalFunction, GridFunction >::value && Capabilities::hasInterpolation< typename DiscreteFunction::DiscreteFunctionSpaceType >::v,
191 void_t< decltype( std::declval< Weight >().setEntity( std::declval< const typename DiscreteFunction::DiscreteFunctionSpaceType::EntityType & >() ) ) > >
192 {
193 typedef typename DiscreteFunction::DiscreteFunctionSpaceType::EntityType EntityType;
194
195 const auto &space = w.space();
196 Impl::WeightLocalFunction< EntityType, std::remove_reference_t< typename DiscreteFunction::FunctionSpaceType >, Weight > localWeight( weight, w.order() );
197 LocalInterpolation< typename DiscreteFunction::DiscreteFunctionSpaceType > interpolation( space );
198 interpolate( u, v, [ &interpolation, &localWeight ] ( const EntityType &entity, AddLocalContribution< DiscreteFunction > &w ) {
199 auto weightGuard = bindGuard( localWeight, entity );
200 auto iGuard = bindGuard( interpolation, entity );
201 interpolation( localWeight, w );
202 }, w );
203 }
204
205 template< class GridFunction, class DiscreteFunction, class Weight >
206 inline static auto interpolate ( const GridFunction &u, DiscreteFunction &v, Weight &&weight, DiscreteFunction &w )
207 -> std::enable_if_t< std::is_base_of< HasLocalFunction, GridFunction >::value && Capabilities::hasInterpolation< typename DiscreteFunction::DiscreteFunctionSpaceType >::v,
208 void_t< decltype( std::declval< Weight >()( std::declval< typename DiscreteFunction::DiscreteFunctionSpaceType::EntityType & >(), std::declval< AddLocalContribution< DiscreteFunction > & >() ) ) > >
209 {
210 typedef typename DiscreteFunction::DofType DofType;
211
212 v.clear();
213 w.clear();
214
215 {
216 ConstLocalFunction< GridFunction > uLocal( u );
217 AddLocalContribution< DiscreteFunction > vLocal( v ), wLocal( w );
218 LocalInterpolation< typename DiscreteFunction::DiscreteFunctionSpaceType >
219 interpolation( v.space() );
220
221 for( const auto &entity : v.space() )
222 {
223 auto uGuard = bindGuard( uLocal, entity );
224 auto vGuard = bindGuard( vLocal, entity );
225 auto wGuard = bindGuard( wLocal, entity );
226 auto iGuard = bindGuard( interpolation, entity );
227
228 // interpolate u and store in v
229 interpolation( uLocal, vLocal );
230
231 // evaluate DoF-wise weight
232 weight( entity, wLocal );
233
234 // multiply interpolated values by weight
235 std::transform( vLocal.begin(), vLocal.end(), wLocal.begin(), vLocal.begin(), std::multiplies< DofType >() );
236 }
237 } // ensure the local contributions go out of scope, here (communication)
238
239 std::transform( v.dbegin(), v.dend(), w.dbegin(), v.dbegin(), [] ( DofType v, DofType w ) {
240 // weights are non-negative, so cancellation cannot occur
241 return (w > DofType( 0 ) ? v / w : v);
242 } );
243 }
244
245 } // namespace Fem
246
247} // namespace Dune
248
249#endif // #ifndef DUNE_FEM_SPACE_COMMON_INTERPOLATE_HH
Entity EntityType
entity type
Definition: entitygeometry.hh:39
void bind(const EntityType &entity)
set new entity object and geometry if enabled
Definition: entitygeometry.hh:135
Definition: explicitfieldvector.hh:75
FunctionSpaceTraits::DomainFieldType DomainFieldType
Intrinsic type used for values in the domain field (usually a double)
Definition: functionspaceinterface.hh:60
FunctionSpaceTraits::RangeType RangeType
Type of range vector (using type of range field) has a Dune::FieldVector type interface.
Definition: functionspaceinterface.hh:71
@ dimDomain
dimension of domain vector space
Definition: functionspaceinterface.hh:46
@ dimRange
dimension of range vector space
Definition: functionspaceinterface.hh:48
FunctionSpaceTraits::LinearMappingType JacobianRangeType
Intrinsic type used for the jacobian values has a Dune::FieldMatrix type interface.
Definition: functionspaceinterface.hh:75
FunctionSpaceTraits::DomainType DomainType
Type of domain vector (using type of domain field) has a Dune::FieldVector type interface.
Definition: functionspaceinterface.hh:67
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
actual interface class for quadratures
static void interpolate(const GridFunction &u, DiscreteFunction &v)
perform native interpolation of a discrete function space
Definition: interpolate.hh:55
static GridFunctionAdapter< Function, GridPart > gridFunctionAdapter(std::string name, const Function &function, const GridPart &gridPart, unsigned int order)
convert a function to a grid function
Definition: gridfunctionadapter.hh:385
constexpr All all
PartitionSet for all partitions.
Definition: partitionset.hh:295
Dune namespace.
Definition: alignedallocator.hh:13
A set of PartitionType values.
Definition: partitionset.hh:137
Traits for type conversions and type information.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden & Uni Heidelberg  |  generated with Hugo v0.111.3 (Apr 24, 22:34, 2025)