dune-fem  2.4.1-rc
l2norm.hh
Go to the documentation of this file.
1 #ifndef DUNE_FEM_L2NORM_HH
2 #define DUNE_FEM_L2NORM_HH
3 
6 
8 
9 namespace Dune
10 {
11 
12  namespace Fem
13  {
14 
15  // L2Norm
16  // ------
17 
18  template< class GridPart >
19  class L2Norm : public LPNormBase< GridPart, L2Norm< GridPart > >
20  {
23 
24  public:
25  typedef GridPart GridPartType;
26 
27  using BaseType :: gridPart ;
28  using BaseType :: comm ;
29 
30  template< class Function >
32 
33  template< class UFunction, class VFunction >
34  struct FunctionDistance;
35 
36  protected:
37  typedef typename BaseType::EntityType EntityType;
40 
41  const unsigned int order_;
42  public:
43  explicit L2Norm ( const GridPartType &gridPart, const unsigned int order = 0 );
44 
45  template< class DiscreteFunctionType >
46  typename Dune::FieldTraits< typename DiscreteFunctionType::RangeFieldType >::real_type
47  norm ( const DiscreteFunctionType &u ) const;
48 
49  template< class UDiscreteFunctionType, class VDiscreteFunctionType >
50  typename Dune::FieldTraits< typename UDiscreteFunctionType::RangeFieldType >::real_type
51  distance ( const UDiscreteFunctionType &u, const VDiscreteFunctionType &v ) const;
52 
53  template< class ULocalFunctionType, class VLocalFunctionType, class ReturnType >
54  void distanceLocal ( const EntityType &entity, unsigned int order, const ULocalFunctionType &uLocal, const VLocalFunctionType &vLocal, ReturnType &sum ) const;
55 
56  template< class LocalFunctionType, class ReturnType >
57  void normLocal ( const EntityType &entity, unsigned int order, const LocalFunctionType &uLocal, ReturnType &sum ) const;
58  };
59 
60 
61  // Implementation of L2Norm
62  // ------------------------
63 
64  template< class GridPart >
65  inline L2Norm< GridPart >::L2Norm ( const GridPartType &gridPart, const unsigned int order )
66  : BaseType( gridPart ),
67  order_( order )
68  {}
69 
70 
71  template< class GridPart >
72  template< class DiscreteFunctionType >
73  typename Dune::FieldTraits< typename DiscreteFunctionType::RangeFieldType >::real_type
74  L2Norm< GridPart >::norm ( const DiscreteFunctionType &u ) const
75  {
76  typedef typename Dune::FieldTraits< typename DiscreteFunctionType::RangeFieldType >::real_type RealType;
77  typedef FieldVector< RealType, 1 > ReturnType ;
78 
79  // calculate integral over each element
80  ReturnType sum = BaseType :: forEach( u, ReturnType(0), order_ );
81 
82  // return result, e.g. sqrt of calculated sum
83  return sqrt( comm().sum( sum[ 0 ] ) );
84  }
85 
86 
87  template< class GridPart >
88  template< class UDiscreteFunctionType, class VDiscreteFunctionType >
89  typename Dune::FieldTraits< typename UDiscreteFunctionType::RangeFieldType >::real_type
91  ::distance ( const UDiscreteFunctionType &u, const VDiscreteFunctionType &v ) const
92  {
93  typedef typename Dune::FieldTraits< typename UDiscreteFunctionType::RangeFieldType >::real_type RealType;
94  typedef FieldVector< RealType, 1 > ReturnType ;
95 
96  // calculate integral over each element
97  ReturnType sum = BaseType :: forEach( u, v, ReturnType(0), order_ );
98 
99  // return result, e.g. sqrt of calculated sum
100  return sqrt( comm().sum( sum[ 0 ] ) );
101  }
102 
103  template< class GridPart >
104  template< class LocalFunctionType, class ReturnType >
105  inline void L2Norm< GridPart >::normLocal ( const EntityType &entity, unsigned int order, const LocalFunctionType &uLocal, ReturnType &sum ) const
106  {
107  // evaluate norm locally
108  IntegratorType integrator( order );
109 
110  FunctionSquare< LocalFunctionType > uLocal2( uLocal );
111 
112  integrator.integrateAdd( entity, uLocal2, sum );
113  }
114 
115  template< class GridPart >
116  template< class ULocalFunctionType, class VLocalFunctionType, class ReturnType >
117  inline void
118  L2Norm< GridPart >::distanceLocal ( const EntityType &entity, unsigned int order, const ULocalFunctionType &uLocal, const VLocalFunctionType &vLocal, ReturnType &sum ) const
119  {
120  // evaluate norm locally
121  IntegratorType integrator( order );
122 
124 
125  LocalDistanceType dist( uLocal, vLocal );
127 
128  integrator.integrateAdd( entity, dist2, sum );
129  }
130 
131 
132  template< class GridPart >
133  template< class Function >
134  struct L2Norm< GridPart >::FunctionSquare
135  {
137 
139  typedef typename Dune::FieldTraits< RangeFieldType >::real_type RealType;
140  typedef FieldVector< RealType, 1 > RangeType;
141 
142  explicit FunctionSquare ( const FunctionType &function )
143  : function_( function )
144  {}
145 
146  template< class Point >
147  void evaluate ( const Point &x, RangeType &ret ) const
148  {
149  typename FunctionType::RangeType phi;
150  function_.evaluate( x, phi );
151  ret = phi.two_norm2();
152  }
153 
154  private:
155  const FunctionType &function_;
156  };
157 
158 
159  template< class GridPart >
160  template< class UFunction, class VFunction >
161  struct L2Norm< GridPart >::FunctionDistance
162  {
163  typedef UFunction UFunctionType;
164  typedef VFunction VFunctionType;
165 
166  typedef typename UFunctionType::RangeFieldType RangeFieldType;
167  typedef typename UFunctionType::RangeType RangeType;
168  typedef typename UFunctionType::JacobianRangeType JacobianRangeType;
169 
170  FunctionDistance ( const UFunctionType &u, const VFunctionType &v )
171  : u_( u ), v_( v )
172  {}
173 
174  template< class Point >
175  void evaluate ( const Point &x, RangeType &ret ) const
176  {
177  RangeType phi;
178  u_.evaluate( x, ret );
179  v_.evaluate( x, phi );
180  ret -= phi;
181  }
182 
183  template< class Point >
184  void jacobian ( const Point &x, JacobianRangeType &ret ) const
185  {
186  JacobianRangeType phi;
187  u_.jacobian( x, ret );
188  v_.jacobian( x, phi );
189  ret -= phi;
190  }
191 
192  private:
193  const UFunctionType &u_;
194  const VFunctionType &v_;
195  };
196 
197 
198 
199  // WeightedL2Norm
200  // --------------
201 
202  template< class WeightFunction >
204  : public L2Norm< typename WeightFunction::DiscreteFunctionSpaceType::GridPartType >
205  {
208 
209  public:
210  typedef WeightFunction WeightFunctionType;
211 
212  typedef typename WeightFunctionType::DiscreteFunctionSpaceType WeightFunctionSpaceType;
213  typedef typename WeightFunctionSpaceType::GridPartType GridPartType;
214 
215  protected:
216  template< class Function >
218 
219  typedef typename WeightFunctionType::LocalFunctionType LocalWeightFunctionType;
220  typedef typename WeightFunctionType::RangeType WeightType;
221 
224 
225  using BaseType::gridPart;
226  using BaseType::comm;
227 
228  public:
229 
230  using BaseType::norm;
231  using BaseType::distance;
232 
233  explicit WeightedL2Norm ( const WeightFunctionType &weightFunction, const unsigned int order = 0 );
234 
235  template< class LocalFunctionType, class ReturnType >
236  void normLocal ( const EntityType &entity, unsigned int order, const LocalFunctionType &uLocal, ReturnType &sum ) const;
237 
238  template< class ULocalFunctionType, class VLocalFunctionType, class ReturnType >
239  void distanceLocal ( const EntityType &entity, unsigned int order, const ULocalFunctionType &uLocal, const VLocalFunctionType &vLocal, ReturnType &sum ) const;
240 
241  private:
242  const WeightFunctionType &weightFunction_;
243  };
244 
245 
246 
247 
248  // Implementation of WeightedL2Norm
249  // --------------------------------
250 
251  template< class WeightFunction >
253  ::WeightedL2Norm ( const WeightFunctionType &weightFunction, const unsigned int order )
254  : BaseType( weightFunction.space().gridPart(), order ),
255  weightFunction_( weightFunction )
256  {
257  static_assert( (WeightFunctionSpaceType::dimRange == 1),
258  "Wight function must be scalar." );
259  }
260 
261 
262  template< class WeightFunction >
263  template< class LocalFunctionType, class ReturnType >
264  inline void
265  WeightedL2Norm< WeightFunction >::normLocal ( const EntityType &entity, unsigned int order, const LocalFunctionType &uLocal, ReturnType &sum ) const
266  {
267  // !!!! order !!!!
268  IntegratorType integrator( order );
269 
270  LocalWeightFunctionType wfLocal = weightFunction_.localFunction( entity );
271 
272  WeightedFunctionSquare< LocalFunctionType > uLocal2( wfLocal, uLocal );
273 
274  integrator.integrateAdd( entity, uLocal2, sum );
275  }
276 
277 
278  template< class WeightFunction >
279  template< class ULocalFunctionType, class VLocalFunctionType, class ReturnType >
280  inline void
281  WeightedL2Norm< WeightFunction >::distanceLocal ( const EntityType& entity, unsigned int order, const ULocalFunctionType &uLocal, const VLocalFunctionType &vLocal, ReturnType& sum ) const
282  {
283  typedef typename BaseType::template FunctionDistance< ULocalFunctionType, VLocalFunctionType > LocalDistanceType;
284 
285  // !!!! order !!!!
286  IntegratorType integrator( order );
287 
288  LocalWeightFunctionType wfLocal = weightFunction_.localFunction( entity );
289 
290  LocalDistanceType dist( uLocal, vLocal );
291  WeightedFunctionSquare< LocalDistanceType > dist2( wfLocal, dist );
292 
293  integrator.integrateAdd( entity, dist2, sum );
294  }
295 
296 
297  template< class WeightFunction >
298  template< class Function >
299  struct WeightedL2Norm< WeightFunction >::WeightedFunctionSquare
300  {
302 
304  typedef typename Dune::FieldTraits< RangeFieldType >::real_type RealType;
305  typedef FieldVector< RealType, 1 > RangeType;
306 
308  const FunctionType &function )
309  : weightFunction_( weightFunction ),
310  function_( function )
311  {}
312 
313  template< class Point >
314  void evaluate ( const Point &x, RangeType &ret ) const
315  {
316  WeightType weight;
317  weightFunction_.evaluate( x, weight );
318 
319  typename FunctionType::RangeType phi;
320  function_.evaluate( x, phi );
321  ret = weight[ 0 ] * (phi * phi);
322  }
323 
324  private:
325  const LocalWeightFunctionType &weightFunction_;
326  const FunctionType &function_;
327  };
328 
329  } // end namespace Fem
330 
331 } // end namespace Dune
332 
333 #endif // #ifndef DUNE_FEM_L2NORM_HH
FunctionSquare(const FunctionType &function)
Definition: l2norm.hh:142
Definition: l2norm.hh:34
static double sqrt(const Double &v)
Definition: double.hh:870
BaseType::IntegratorType IntegratorType
Definition: l2norm.hh:223
WeightFunction WeightFunctionType
Definition: l2norm.hh:210
Definition: l2norm.hh:31
FunctionType::RangeFieldType RangeFieldType
Definition: l2norm.hh:303
WeightFunctionType::LocalFunctionType LocalWeightFunctionType
Definition: l2norm.hh:217
UFunction UFunctionType
Definition: l2norm.hh:163
UFunctionType::RangeType RangeType
Definition: l2norm.hh:167
FunctionType::RangeFieldType RangeFieldType
Definition: l2norm.hh:138
BaseType::EntityType EntityType
Definition: l2norm.hh:222
void evaluate(const Point &x, RangeType &ret) const
Definition: l2norm.hh:175
UFunctionType::RangeFieldType RangeFieldType
Definition: l2norm.hh:166
static constexpr T sum(T a)
Definition: utility.hh:33
WeightFunctionSpaceType::GridPartType GridPartType
Definition: l2norm.hh:213
Definition: lpnorm.hh:26
Integrator< QuadratureType > IntegratorType
Definition: l2norm.hh:39
Dune::FieldTraits< RangeFieldType >::real_type RealType
Definition: l2norm.hh:304
WeightedL2Norm(const WeightFunctionType &weightFunction, const unsigned int order=0)
Definition: l2norm.hh:253
const unsigned int order_
Definition: l2norm.hh:41
L2Norm(const GridPartType &gridPart, const unsigned int order=0)
Definition: l2norm.hh:65
BaseType::EntityType EntityType
Definition: l2norm.hh:34
FunctionSpaceType::RangeType RangeType
range type
Definition: function.hh:68
GridPart GridPartType
Definition: l2norm.hh:25
void integrateAdd(const EntityType &entity, const Function &function, typename Function::RangeType &ret) const
add the integral over an entity to a variable
Definition: integrator.hh:71
void jacobian(const Point &x, JacobianRangeType &ret) const
Definition: l2norm.hh:184
ReturnType forEach(const DiscreteFunctionType &u, const ReturnType &initialValue, unsigned int order=0) const
Definition: lpnorm.hh:106
void distanceLocal(const EntityType &entity, unsigned int order, const ULocalFunctionType &uLocal, const VLocalFunctionType &vLocal, ReturnType &sum) const
Definition: l2norm.hh:281
Function FunctionType
Definition: l2norm.hh:301
Definition: l2norm.hh:203
FunctionDistance(const UFunctionType &u, const VFunctionType &v)
Definition: l2norm.hh:170
VFunction VFunctionType
Definition: l2norm.hh:164
void normLocal(const EntityType &entity, unsigned int order, const LocalFunctionType &uLocal, ReturnType &sum) const
Definition: l2norm.hh:105
void evaluate(const Point &x, RangeType &ret) const
Definition: l2norm.hh:314
WeightedFunctionSquare(const LocalWeightFunctionType &weightFunction, const FunctionType &function)
Definition: l2norm.hh:307
Definition: coordinate.hh:4
WeightFunctionType::DiscreteFunctionSpaceType WeightFunctionSpaceType
Definition: l2norm.hh:212
void normLocal(const EntityType &entity, unsigned int order, const LocalFunctionType &uLocal, ReturnType &sum) const
Definition: l2norm.hh:265
integrator for arbitrary functions providing evaluate
Definition: integrator.hh:28
CachingQuadrature< GridPartType, 0 > QuadratureType
Definition: l2norm.hh:38
FieldVector< RealType, 1 > RangeType
Definition: l2norm.hh:305
Definition: l2norm.hh:19
FieldVector< RealType, 1 > RangeType
Definition: l2norm.hh:140
GridPartType::CollectiveCommunicationType comm() const
Definition: lpnorm.hh:157
WeightFunctionType::RangeType WeightType
Definition: l2norm.hh:220
FunctionSpaceType::RangeFieldType RangeFieldType
field type of range
Definition: function.hh:64
Abstract class representing a function.
Definition: function.hh:43
Dune::FieldTraits< typename UDiscreteFunctionType::RangeFieldType >::real_type distance(const UDiscreteFunctionType &u, const VDiscreteFunctionType &v) const
Definition: l2norm.hh:91
Function FunctionType
Definition: l2norm.hh:136
UFunctionType::JacobianRangeType JacobianRangeType
Definition: l2norm.hh:168
void distanceLocal(const EntityType &entity, unsigned int order, const ULocalFunctionType &uLocal, const VLocalFunctionType &vLocal, ReturnType &sum) const
Definition: l2norm.hh:118
void evaluate(const Point &x, RangeType &ret) const
Definition: l2norm.hh:147
Dune::FieldTraits< RangeFieldType >::real_type RealType
Definition: l2norm.hh:139
const GridPartType & gridPart() const
Definition: lpnorm.hh:155
Dune::FieldTraits< typename DiscreteFunctionType::RangeFieldType >::real_type norm(const DiscreteFunctionType &u) const
Definition: l2norm.hh:74