dune-localfunctions 2.11
Loading...
Searching...
No Matches
field.hh
Go to the documentation of this file.
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
4// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
5#ifndef DUNE_LOCALFUNCTIONS_UTILITY_FIELD_HH
6#define DUNE_LOCALFUNCTIONS_UTILITY_FIELD_HH
7
8#include <dune/common/deprecated.hh>
9#include <dune/common/gmpfield.hh>
10#include <dune/common/fvector.hh>
11#include <dune/common/fmatrix.hh>
12
13namespace Dune
14{
15
16 // Unity
17 // -----
18
29 template< class Field >
30 struct Unity
31 {
32 operator Field () const
33 {
34 return Field( 1 );
35 }
36 };
37
38 template< class Field >
39 Field operator+ ( const Unity< Field > &u, const Field &f )
40 {
41 return (Field)u + f;
42 }
43
44 template< class Field >
45 Field operator- ( const Unity< Field > &u, const Field &f )
46 {
47 return (Field)u - f;
48 }
49
50 template< class Field >
51 Field operator* ( const Unity< Field > &u, const Field &f )
52 {
53 return f;
54 }
55
56 template< class Field >
57 Field operator/ ( const Unity< Field > &u, const Field &f )
58 {
59 return (Field)u / f;
60 }
61
62
63
64 // Zero
65 // ----
66
78 template< class Field >
79 struct Zero
80 {
81 operator Field () const
82 {
83 return Field( 0 );
84 }
85 static const Field epsilon()
86 {
87 return Field(1e-12);
88 }
89 };
90
91#if HAVE_GMP
92 template< unsigned int precision >
93 struct Zero< GMPField< precision > >
94 {
95 typedef GMPField< precision > Field;
96 operator Field () const
97 {
98 return Field( 0 );
99 }
100 static const Field epsilon()
101 {
102 return Field(1e-20);
103 }
104 };
105#endif
106
107 template< class Field >
108 inline bool operator == ( const Zero< Field > &, const Field &f )
109 {
110 return ( f < Zero<Field>::epsilon() && f > -Zero<Field>::epsilon() );
111 }
112
113 template< class Field >
114 inline bool operator == ( const Field &f, const Zero< Field > &z)
115 {
116 return ( z == f );
117 }
118
119 template< class Field >
120 inline bool operator< ( const Zero< Field > &, const Field &f )
121 {
122 return f > Zero<Field>::epsilon();
123 }
124
125 template< class Field >
126 inline bool operator< ( const Field &f, const Zero< Field > & )
127 {
128 return f < -Zero<Field>::epsilon();
129 }
130
131 template< class Field >
132 inline bool operator> ( const Zero< Field > &z, const Field &f )
133 {
134 return f < z;
135 }
136
137 template< class Field >
138 inline bool operator> ( const Field &f, const Zero< Field > &z )
139 {
140 return z < f;
141 }
142
143
144 // field_cast
145 // ----------
146
159 template< class F2, class F1 >
160 inline void field_cast ( const F1 &f1, F2 &f2 )
161 {
162 f2 = f1;
163 }
164
165#if HAVE_GMP
166 template< unsigned int precision >
167 inline void field_cast ( const Dune::GMPField< precision > &f1, double &f2 )
168 {
169 f2 = f1.get_d();
170 }
171
172 template< unsigned int precision >
173 inline void field_cast ( const Dune::GMPField< precision > &f1, long double &f2 )
174 {
175 f2 = f1.get_d();
176 }
177#endif
178
179 template< class F2, class F1, int dim >
180 inline void field_cast ( const Dune::FieldVector< F1, dim > &f1, Dune::FieldVector< F2, dim > &f2 )
181 {
182 for( int d = 0; d < dim; ++d )
183 field_cast( f1[ d ], f2[ d ] );
184 }
185 template< class F2, class F1 >
186 inline void field_cast ( const Dune::FieldVector< F1, 1 > &f1, F2 &f2 )
187 {
188 field_cast( f1[ 0 ], f2 );
189 }
190 template< class F2, class F1 >
191 inline void field_cast ( const F1 &f1, Dune::FieldVector< F2, 1 > &f2 )
192 {
193 field_cast( f1, f2[ 0 ] );
194 }
195
196 template< class F2, class F1, int rdim, int cdim >
197 inline void field_cast ( const Dune::FieldMatrix< F1, rdim, cdim > &f1, Dune::FieldMatrix< F2, rdim, cdim > &f2 )
198 {
199 for( int r = 0; r < rdim; ++r )
200 field_cast( f1[ r ], f2[ r ] );
201 }
202 template< class F2, class F1 >
203 inline void field_cast ( const Dune::FieldMatrix<F1,1,1> &f1, Dune::FieldMatrix< F2, 1,1 > &f2 )
204 {
205 field_cast( f1[ 0 ][ 0 ], f2[ 0 ][ 0 ] );
206 }
207 template< class F2, class F1 >
208 inline void field_cast ( const Dune::FieldMatrix< F1, 1,1 > &f1, F2 &f2 )
209 {
210 field_cast( f1[ 0 ][ 0 ], f2 );
211 }
212 template< class F2, class F1 >
213 inline void field_cast ( const F1 &f1, Dune::FieldMatrix< F2, 1,1 > &f2 )
214 {
215 field_cast( f1, f2[ 0 ][ 0 ] );
216 }
217 template< class F2, class F1 >
218 inline void field_cast ( const Dune::FieldVector<F1,1> &f1, Dune::FieldMatrix< F2, 1,1 > &f2 )
219 {
220 field_cast( f1[ 0 ], f2[ 0 ][ 0 ] );
221 }
222 template< class F2, class F1 >
223 inline void field_cast ( const Dune::FieldMatrix<F1,1,1> &f1, Dune::FieldVector< F2, 1 > &f2 )
224 {
225 field_cast( f1[ 0 ][ 0 ], f2[ 0 ] );
226 }
227
228 template< class F2, class F1 >
229 inline void field_cast ( const Dune::FieldVector< F1, 1 > &f1, Dune::FieldVector<F2, 1> &f2 )
230 {
231 field_cast( f1[ 0 ], f2[ 0 ] );
232 }
233
234 template< class F2,class V >
236 {
237 typedef F2 type;
238 };
239 template< class F2,class F1,int dim >
240 struct FieldCast< F2, Dune::FieldVector<F1,dim> >
241 {
242 typedef Dune::FieldVector<F2,dim> type;
243 };
244 template< class F2,class F1,int dim1, int dim2>
245 struct FieldCast< F2, Dune::FieldMatrix<F1,dim1,dim2> >
246 {
247 typedef Dune::FieldMatrix<F2,dim1,dim2> type;
248 };
249 template< class F2,class V >
250 inline typename FieldCast<F2,V>::type field_cast ( const V &f1 )
251 {
252 typename FieldCast<F2,V>::type f2;
253 field_cast( f1, f2 );
254 return f2;
255 }
256
257
258 // Precision
259 // this is not a perfect solution to obtain the
260 // precision of a field - definition is not clear
261 // to be removed
262 // ---------
263
264 template <class Field>
265 struct [[deprecated("This class is deprecated and will be removed after 2.11. Use std::numeric_limits<>::digits instead")]] Precision;
266
267DUNE_NO_DEPRECATED_BEGIN
268
269 template<>
270 struct Precision< double >
271 {
272 static const unsigned int value = 64;
273 };
274
275 template<>
276 struct Precision< long double >
277 {
278 static const unsigned int value = 80;
279 };
280
281 template<>
282 struct Precision< float >
283 {
284 static const unsigned int value = 32;
285 };
286
287#if HAVE_GMP
288 template< unsigned int precision >
289 struct Precision< GMPField< precision > >
290 {
291 static const unsigned int value = precision;
292 };
293#endif
294
295DUNE_NO_DEPRECATED_END
296
297 // ComputeField
298 // ------------
299
300 template <class Field,unsigned int sum>
302 {
303 typedef Field Type;
304 };
305
306#if HAVE_GMP
307 template< unsigned int precision, unsigned int sum >
308 struct ComputeField< GMPField< precision >, sum >
309 {
310 typedef GMPField<precision+sum> Type;
311 };
312#endif
313} // namespace Dune
314
315#endif // #ifndef DUNE_LOCALFUNCTIONS_UTILITY_FIELD_HH
Definition bdfmcube.hh:18
Field operator-(const Unity< Field > &u, const Field &f)
Definition field.hh:45
void field_cast(const F1 &f1, F2 &f2)
a helper class to cast from one field to another
Definition field.hh:160
bool operator<(const Zero< Field > &, const Field &f)
Definition field.hh:120
bool operator==(const Zero< Field > &, const Field &f)
Definition field.hh:108
bool operator>(const Zero< Field > &z, const Field &f)
Definition field.hh:132
Field operator+(const Unity< Field > &u, const Field &f)
Definition field.hh:39
Field operator/(const Unity< Field > &u, const Field &f)
Definition field.hh:57
Field operator*(const Unity< Field > &u, const Field &f)
Definition field.hh:51
A class representing the unit of a given Field.
Definition field.hh:31
A class representing the zero of a given Field.
Definition field.hh:80
static const Field epsilon()
Definition field.hh:85
Definition field.hh:236
F2 type
Definition field.hh:237
Dune::FieldVector< F2, dim > type
Definition field.hh:242
Dune::FieldMatrix< F2, dim1, dim2 > type
Definition field.hh:247
Definition field.hh:265
static const unsigned int value
Definition field.hh:272
static const unsigned int value
Definition field.hh:278
static const unsigned int value
Definition field.hh:284
Definition field.hh:302
Field Type
Definition field.hh:303