| 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 |
|
| 13 |
namespace Dune |
| 14 |
{ |
| 15 |
|
| 16 |
// Unity |
| 17 |
// ----- |
| 18 |
|
| 19 |
/** |
| 20 |
* @brief A class representing the unit of a given Field |
| 21 |
* |
| 22 |
* This class can be used to assign the unit element to an |
| 23 |
* instance of a given Field also operators for +/- with |
| 24 |
* unit element are provided. Also 1/f can be evaluated. |
| 25 |
* Through specialization this class can be used also in the case that the |
| 26 |
* integer 1 is not automatically converted to the unit |
| 27 |
* element of the Field - the default implementation |
| 28 |
**/ |
| 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 |
|
| 67 |
/** |
| 68 |
* @brief A class representing the zero of a given Field |
| 69 |
* |
| 70 |
* This class can be used to assign the zero element to an |
| 71 |
* instance of a given Field. An epsilon is also |
| 72 |
* provided for the comparison operators. |
| 73 |
* This class can be used also in the case that the |
| 74 |
* integer 0 is not automatically converted to the zero |
| 75 |
* element of the Field and the epsilon can be changed |
| 76 |
* depending on the accuracy of the Field type. |
| 77 |
**/ |
| 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 |
|
| 147 |
/** |
| 148 |
* @brief a helper class to cast from one field |
| 149 |
* to another |
| 150 |
* |
| 151 |
* This cast can be used for assignment between |
| 152 |
* different field types, including for example |
| 153 |
* between FieldVectors with different fields. |
| 154 |
* Specially the conversion from a special type |
| 155 |
* e.g. gmp to build in types are provided, the |
| 156 |
* other direction can be more easily handled by |
| 157 |
* the special field type implementation. |
| 158 |
**/ |
| 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 > |
| 235 |
struct FieldCast |
| 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 |
|
| 267 |
DUNE_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 |
|
| 295 |
DUNE_NO_DEPRECATED_END |
| 296 |
|
| 297 |
// ComputeField |
| 298 |
// ------------ |
| 299 |
|
| 300 |
template <class Field,unsigned int sum> |
| 301 |
struct ComputeField |
| 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 |