dune-fem 2.12-git
Loading...
Searching...
No Matches
common/interpolate.hh
Go to the documentation of this file.
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
12
15
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 >
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 >
75 interpolate ( const GridFunction &u, DiscreteFunction &v, PartitionSet< partitions > ps )
76 {
78 LocalContribution< DiscreteFunction, Assembly::Set > vLocal( v, /* communicate = */ false );
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 {
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 )
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 )
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 )
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() );
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 )
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
typename Impl::voider< Types... >::type void_t
std::ptrdiff_t index() const
constexpr All all
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
static void interpolate(const GridFunction &u, DiscreteFunction &v)
perform native interpolation of a discrete function space
Definition common/interpolate.hh:55
EntityGeometryStorageImpl< Entity, false > EntityStorage
Definition entitygeometry.hh:206
typename Impl::ConstLocalFunction< GridFunction >::Type ConstLocalFunction
Definition const.hh:626
static const Point & coordinate(const Point &x)
Definition coordinate.hh:14
static auto bindGuard(Object &object, Args &&... args) -> std::enable_if_t< isBindable< Object, Args... >::value, BindGuard< Object > >
Definition bindguard.hh:67
Definition common/localcontribution.hh:14
Abstract class representing a function.
Definition common/function.hh:50
BasicGridFunctionAdapter provides local functions for a Function.
Definition gridfunctionadapter.hh:80
determine whether a discrete function space provides a (local) interpolation
Definition space/common/capabilities.hh:176
static const bool v
Definition space/common/capabilities.hh:177
ExplicitFieldVector< FieldMatrix< RangeFieldType, dimDomain, dimDomain >, dimRange > HessianRangeType
Intrinsic type used for the hessian values has a Dune::FieldMatrix type interface.
Definition functionspaceinterface.hh:79
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
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
@ dimDomain
dimension of domain vector space
Definition functionspaceinterface.hh:46
@ dimRange
dimension of range vector space
Definition functionspaceinterface.hh:48
Definition common/localinterpolation.hh:23
const Entity & entity() const
return entity
Definition entitygeometry.hh:101
void unbind()
release entity and geometry object
Definition entitygeometry.hh:164
void bind(const EntityType &entity)
set new entity object and geometry if enabled
Definition entitygeometry.hh:135
actual interface class for quadratures
T forward(T... args)
T transform(T... args)