dune-fem  2.4.1-rc
l1norm.hh
Go to the documentation of this file.
1 #ifndef DUNE_FEM_L1NORM_HH
2 #define DUNE_FEM_L1NORM_HH
3 
5 
7 
8 namespace Dune
9 {
10 
11  namespace Fem
12  {
13 
14  // L1Norm
15  // ------
16 
17  template< class GridPart >
18  class L1Norm : public LPNormBase< GridPart, L1Norm< GridPart > >
19  {
22 
23  public:
24  typedef GridPart GridPartType;
25 
26  using BaseType :: gridPart ;
27  using BaseType :: comm ;
28 
29  protected:
30  template< class Function >
31  struct FunctionAbs;
32 
33  template< class UFunction, class VFunction >
35 
36  typedef typename BaseType::EntityType EntityType;
38 
39  const unsigned int order_;
40  public:
41  explicit L1Norm ( const GridPartType &gridPart, const unsigned int order = 0 );
42 
43  template< class DiscreteFunctionType >
44  typename Dune::FieldTraits< typename DiscreteFunctionType::RangeFieldType >::real_type
45  norm ( const DiscreteFunctionType &u ) const;
46 
47  template< class UDiscreteFunctionType, class VDiscreteFunctionType >
48  typename Dune::FieldTraits< typename UDiscreteFunctionType::RangeFieldType >::real_type
49  distance ( const UDiscreteFunctionType &u, const VDiscreteFunctionType &v ) const;
50 
51  template< class LocalFunctionType, class ReturnType >
52  void normLocal ( const EntityType &entity, unsigned int order, const LocalFunctionType &uLocal, ReturnType &sum ) const;
53 
54  template< class ULocalFunctionType, class VLocalFunctionType, class ReturnType >
55  void distanceLocal ( const EntityType &entity, unsigned int order, const ULocalFunctionType &uLocal, const VLocalFunctionType &vLocal, ReturnType &sum ) const;
56  };
57 
58 
59 
60  // Implementation of L1Norm
61  // ------------------------
62 
63  template< class GridPart >
64  inline L1Norm< GridPart >::L1Norm ( const GridPartType &gridPart, const unsigned int order )
65  : BaseType( gridPart ),
66  order_( order )
67  {}
68 
69 
70  template< class GridPart >
71  template< class DiscreteFunctionType >
72  inline typename Dune::FieldTraits< typename DiscreteFunctionType::RangeFieldType >::real_type
73  L1Norm< GridPart >::norm ( const DiscreteFunctionType &u ) const
74  {
75  typedef typename DiscreteFunctionType::RangeFieldType RangeFieldType;
76  typedef typename Dune::FieldTraits< 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. sum
83  return comm().sum( sum[ 0 ] );
84  }
85 
86 
87  template< class GridPart >
88  template< class UDiscreteFunctionType, class VDiscreteFunctionType >
89  inline typename Dune::FieldTraits< typename UDiscreteFunctionType::RangeFieldType >::real_type
91  ::distance ( const UDiscreteFunctionType &u, const VDiscreteFunctionType &v ) const
92  {
93  typedef typename UDiscreteFunctionType::RangeFieldType RangeFieldType;
94  typedef typename Dune::FieldTraits< RangeFieldType >::real_type RealType;
95  typedef FieldVector< RealType, 1 > ReturnType ;
96 
97  // calculate integral over each element
98  ReturnType sum = BaseType :: forEach( u, v, ReturnType(0), order_ );
99 
100  // return result, e.g. sum
101  return comm().sum( sum[ 0 ] );
102  }
103 
104  template< class GridPart >
105  template< class LocalFunctionType, class ReturnType >
106  inline void
107  L1Norm< GridPart >::normLocal ( const EntityType &entity, unsigned int order, const LocalFunctionType &uLocal, ReturnType &sum ) const
108  {
109  Integrator< QuadratureType > integrator( order );
110 
111  FunctionAbs< LocalFunctionType > uLocalAbs( uLocal );
112 
113  integrator.integrateAdd( entity, uLocalAbs, sum );
114  }
115 
116  template< class GridPart >
117  template< class ULocalFunctionType, class VLocalFunctionType, class ReturnType >
118  inline void
119  L1Norm< GridPart >::distanceLocal ( const EntityType &entity, unsigned int order, const ULocalFunctionType &uLocal, const VLocalFunctionType &vLocal, ReturnType &sum ) const
120  {
121  Integrator< QuadratureType > integrator( order );
122 
124 
125  LocalDistanceType dist( uLocal, vLocal );
126  FunctionAbs< LocalDistanceType > distAbs( dist );
127 
128  integrator.integrateAdd( entity, distAbs, sum );
129  }
130 
131 
132  template< class GridPart >
133  template< class Function >
134  struct L1Norm< GridPart >::FunctionAbs
135  {
137 
139  typedef typename Dune::FieldTraits< RangeFieldType >::real_type RealType;
140  typedef FieldVector< RealType, 1 > RangeType;
141 
142  explicit FunctionAbs ( 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.one_norm();
152  }
153 
154  private:
155  const FunctionType &function_;
156  };
157 
158 
159  template< class GridPart >
160  template< class UFunction, class VFunction >
161  struct L1Norm< 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  } // namespace Fem
198 
199 } // namespace Dune
200 
201 #endif // #ifndef DUNE_FEM_L1NORM_HH
Dune::FieldTraits< typename UDiscreteFunctionType::RangeFieldType >::real_type distance(const UDiscreteFunctionType &u, const VDiscreteFunctionType &v) const
Definition: l1norm.hh:91
const unsigned int order_
Definition: l1norm.hh:39
Dune::FieldTraits< RangeFieldType >::real_type RealType
Definition: l1norm.hh:139
FieldVector< RealType, 1 > RangeType
Definition: l1norm.hh:140
Definition: l1norm.hh:34
void jacobian(const Point &x, JacobianRangeType &ret) const
Definition: l1norm.hh:184
static constexpr T sum(T a)
Definition: utility.hh:33
CachingQuadrature< GridPartType, 0 > QuadratureType
Definition: l1norm.hh:37
Definition: lpnorm.hh:26
void evaluate(const Point &x, RangeType &ret) const
Definition: l1norm.hh:147
FunctionSpaceType::RangeType RangeType
range type
Definition: function.hh:68
GridPart GridPartType
Definition: l1norm.hh:24
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
FunctionType::RangeFieldType RangeFieldType
Definition: l1norm.hh:138
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: l1norm.hh:119
BaseType::EntityType EntityType
Definition: l1norm.hh:34
UFunction UFunctionType
Definition: l1norm.hh:163
GridPartType::template Codim< 0 >::EntityType EntityType
Definition: lpnorm.hh:40
Definition: coordinate.hh:4
Definition: l1norm.hh:31
integrator for arbitrary functions providing evaluate
Definition: integrator.hh:28
Function FunctionType
Definition: l1norm.hh:136
GridPartType::CollectiveCommunicationType comm() const
Definition: lpnorm.hh:157
FunctionSpaceType::RangeFieldType RangeFieldType
field type of range
Definition: function.hh:64
Dune::FieldTraits< typename DiscreteFunctionType::RangeFieldType >::real_type norm(const DiscreteFunctionType &u) const
Definition: l1norm.hh:73
FunctionAbs(const FunctionType &function)
Definition: l1norm.hh:142
Abstract class representing a function.
Definition: function.hh:43
L1Norm(const GridPartType &gridPart, const unsigned int order=0)
Definition: l1norm.hh:64
FunctionDistance(const UFunctionType &u, const VFunctionType &v)
Definition: l1norm.hh:170
UFunctionType::RangeFieldType RangeFieldType
Definition: l1norm.hh:166
UFunctionType::JacobianRangeType JacobianRangeType
Definition: l1norm.hh:168
void normLocal(const EntityType &entity, unsigned int order, const LocalFunctionType &uLocal, ReturnType &sum) const
Definition: l1norm.hh:107
void evaluate(const Point &x, RangeType &ret) const
Definition: l1norm.hh:175
VFunction VFunctionType
Definition: l1norm.hh:164
const GridPartType & gridPart() const
Definition: lpnorm.hh:155
UFunctionType::RangeType RangeType
Definition: l1norm.hh:167
Definition: l1norm.hh:18