DUNE-FEM (unstable)

l1norm.hh
1#ifndef DUNE_FEM_L1NORM_HH
2#define DUNE_FEM_L1NORM_HH
3
4#include <dune/fem/quadrature/integrator.hh>
5
6#include <dune/fem/misc/domainintegral.hh>
7
8namespace Dune
9{
10
11 namespace Fem
12 {
13
14 // L1Norm
15 // ------
16
17 template< class GridPart >
18 class L1Norm : public IntegralBase< GridPart, L1Norm< GridPart > >
19 {
20 typedef IntegralBase< GridPart, L1Norm< GridPart > > BaseType ;
21 typedef L1Norm< GridPart > ThisType;
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 >
34 struct FunctionDistance;
35
36 typedef typename BaseType::EntityType EntityType;
37 typedef CachingQuadrature< GridPartType, 0 > QuadratureType;
38
39 const unsigned int order_;
40 const bool communicate_;
41 public:
47 explicit L1Norm ( const GridPartType &gridPart,
48 const unsigned int order = 0,
49 const bool communicate = true );
50
51
53 template< class DiscreteFunctionType, class PartitionSet >
54 typename Dune::FieldTraits< typename DiscreteFunctionType::RangeFieldType >::real_type
55 norm ( const DiscreteFunctionType &u, const PartitionSet& partitionSet ) const;
56
58 template< class DiscreteFunctionType >
59 typename Dune::FieldTraits< typename DiscreteFunctionType::RangeFieldType >::real_type
60 norm ( const DiscreteFunctionType &u ) const
61 {
62 return norm( u, Partitions::interior );
63 }
64
66 template< class UDiscreteFunctionType, class VDiscreteFunctionType, class PartitionSet >
67 typename Dune::FieldTraits< typename UDiscreteFunctionType::RangeFieldType >::real_type
68 distance ( const UDiscreteFunctionType &u, const VDiscreteFunctionType &v, const PartitionSet& partitionSet ) const;
69
71 template< class UDiscreteFunctionType, class VDiscreteFunctionType >
72 typename Dune::FieldTraits< typename UDiscreteFunctionType::RangeFieldType >::real_type
73 distance ( const UDiscreteFunctionType &u, const VDiscreteFunctionType &v ) const
74 {
75 return distance( u, v, Partitions::interior );
76 }
77
78 template< class LocalFunctionType, class ReturnType >
79 void normLocal ( const EntityType &entity, unsigned int order, const LocalFunctionType &uLocal, ReturnType &sum ) const;
80
81 template< class ULocalFunctionType, class VLocalFunctionType, class ReturnType >
82 void distanceLocal ( const EntityType &entity, unsigned int order, const ULocalFunctionType &uLocal, const VLocalFunctionType &vLocal, ReturnType &sum ) const;
83 };
84
85
86
87 // Implementation of L1Norm
88 // ------------------------
89
90 template< class GridPart >
91 inline L1Norm< GridPart >::L1Norm ( const GridPartType &gridPart, const unsigned int order, const bool communicate )
92 : BaseType( gridPart ),
93 order_( order ),
94 communicate_( BaseType::checkCommunicateFlag( communicate ) )
95 {}
96
97
98 template< class GridPart >
99 template< class DiscreteFunctionType, class PartitionSet >
100 inline typename Dune::FieldTraits< typename DiscreteFunctionType::RangeFieldType >::real_type
101 L1Norm< GridPart >::norm ( const DiscreteFunctionType &u, const PartitionSet& partitionSet ) const
102 {
103 typedef typename DiscreteFunctionType::RangeFieldType RangeFieldType;
104 typedef typename Dune::FieldTraits< RangeFieldType >::real_type RealType;
105 typedef FieldVector< RealType, 1 > ReturnType ;
106
107 // calculate integral over each element
108 ReturnType sum = BaseType :: forEach( u, ReturnType(0), partitionSet, order_ );
109
110 // communicate_ indicates global norm
111 if( communicate_ )
112 {
113 sum[ 0 ] = comm().sum( sum[ 0 ] );
114 }
115
116 return sum[ 0 ];
117 }
118
119
120 template< class GridPart >
121 template< class UDiscreteFunctionType, class VDiscreteFunctionType, class PartitionSet >
122 inline typename Dune::FieldTraits< typename UDiscreteFunctionType::RangeFieldType >::real_type
123 L1Norm< GridPart >
124 ::distance ( const UDiscreteFunctionType &u, const VDiscreteFunctionType &v, const PartitionSet& partitionSet ) const
125 {
126 typedef typename UDiscreteFunctionType::RangeFieldType RangeFieldType;
127 typedef typename Dune::FieldTraits< RangeFieldType >::real_type RealType;
128 typedef FieldVector< RealType, 1 > ReturnType ;
129
130 // calculate integral over each element
131 ReturnType sum = BaseType :: forEach( u, v, ReturnType(0), partitionSet, order_ );
132
133 // communicate_ indicates global norm
134 if( communicate_ )
135 {
136 sum[ 0 ] = comm().sum( sum[ 0 ] );
137 }
138
139 return sum[ 0 ];
140 }
141
142 template< class GridPart >
143 template< class LocalFunctionType, class ReturnType >
144 inline void
145 L1Norm< GridPart >::normLocal ( const EntityType &entity, unsigned int order, const LocalFunctionType &uLocal, ReturnType &sum ) const
146 {
147 Integrator< QuadratureType > integrator( order );
148
149 FunctionAbs< LocalFunctionType > uLocalAbs( uLocal );
150
151 integrator.integrateAdd( entity, uLocalAbs, sum );
152 }
153
154 template< class GridPart >
155 template< class ULocalFunctionType, class VLocalFunctionType, class ReturnType >
156 inline void
157 L1Norm< GridPart >::distanceLocal ( const EntityType &entity, unsigned int order, const ULocalFunctionType &uLocal, const VLocalFunctionType &vLocal, ReturnType &sum ) const
158 {
159 Integrator< QuadratureType > integrator( order );
160
161 typedef FunctionDistance< ULocalFunctionType, VLocalFunctionType > LocalDistanceType;
162
163 LocalDistanceType dist( uLocal, vLocal );
164 FunctionAbs< LocalDistanceType > distAbs( dist );
165
166 integrator.integrateAdd( entity, distAbs, sum );
167 }
168
169
170 template< class GridPart >
171 template< class Function >
172 struct L1Norm< GridPart >::FunctionAbs
173 {
174 typedef Function FunctionType;
175
176 typedef typename FunctionType::RangeFieldType RangeFieldType;
177 typedef typename Dune::FieldTraits< RangeFieldType >::real_type RealType;
178 typedef FieldVector< RealType, 1 > RangeType;
179
180 explicit FunctionAbs ( const FunctionType &function )
181 : function_( function )
182 {}
183
184 template< class Point >
185 void evaluate ( const Point &x, RangeType &ret ) const
186 {
187 typename FunctionType::RangeType phi;
188 function_.evaluate( x, phi );
189 ret = phi.one_norm();
190 }
191
192 private:
193 const FunctionType &function_;
194 };
195
196
197 template< class GridPart >
198 template< class UFunction, class VFunction >
199 struct L1Norm< GridPart >::FunctionDistance
200 {
201 typedef UFunction UFunctionType;
202 typedef VFunction VFunctionType;
203
204 typedef typename UFunctionType::RangeFieldType RangeFieldType;
205 typedef typename UFunctionType::RangeType RangeType;
206 typedef typename UFunctionType::JacobianRangeType JacobianRangeType;
207
208 FunctionDistance ( const UFunctionType &u, const VFunctionType &v )
209 : u_( u ), v_( v )
210 {}
211
212 template< class Point >
213 void evaluate ( const Point &x, RangeType &ret ) const
214 {
215 RangeType phi;
216 u_.evaluate( x, ret );
217 v_.evaluate( x, phi );
218 ret -= phi;
219 }
220
221 template< class Point >
222 void jacobian ( const Point &x, JacobianRangeType &ret ) const
223 {
224 JacobianRangeType phi;
225 u_.jacobian( x, ret );
226 v_.jacobian( x, phi );
227 ret -= phi;
228 }
229
230 private:
231 const UFunctionType &u_;
232 const VFunctionType &v_;
233 };
234
235 } // namespace Fem
236
237} // namespace Dune
238
239#endif // #ifndef DUNE_FEM_L1NORM_HH
DiscreteFunctionSpaceType::RangeFieldType RangeFieldType
type of range field (usually a float type)
Definition: discretefunction.hh:623
forward declaration
Definition: discretefunction.hh:51
PartitionSet<(1<< p)> partitionSet()
Creates a PartitionSet for the given PartitionType.
Definition: partitionset.hh:221
constexpr void forEach(Range &&range, F &&f)
Range based for loop.
Definition: hybridutilities.hh:256
constexpr Interior interior
PartitionSet for the interior partition.
Definition: partitionset.hh:271
Dune namespace.
Definition: alignedallocator.hh:13
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Jul 27, 22:29, 2024)