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