00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00031 #ifndef OPM_DENSEAD_EVALUATION6_HPP
00032 #define OPM_DENSEAD_EVALUATION6_HPP
00033
00034 #include "Evaluation.hpp"
00035 #include "Math.hpp"
00036
00037 #include <opm/common/Valgrind.hpp>
00038
00039 #include <dune/common/version.hh>
00040
00041 #include <array>
00042 #include <cmath>
00043 #include <cassert>
00044 #include <cstring>
00045 #include <iostream>
00046 #include <algorithm>
00047
00048 namespace Opm {
00049 namespace DenseAd {
00050
00051 template <class ValueT>
00052 class Evaluation<ValueT, 6>
00053 {
00054 public:
00056 typedef ValueT ValueType;
00057
00059 static constexpr int size = 6;
00060
00061 protected:
00063 static constexpr int length_ = size + 1;
00064
00066 static constexpr int valuepos_ = 0;
00068 static constexpr int dstart_ = 1;
00070 static constexpr int dend_ = length_;
00071
00072 public:
00074 Evaluation() : data_()
00075 {}
00076
00078 Evaluation(const Evaluation& other) = default;
00079
00080
00081
00082
00083
00084 template <class RhsValueType>
00085 Evaluation(const RhsValueType& c)
00086 {
00087 setValue( c );
00088 clearDerivatives();
00089 Valgrind::CheckDefined( data_ );
00090 }
00091
00092
00093
00094
00095
00096 template <class RhsValueType>
00097 Evaluation(const RhsValueType& c, int varPos)
00098 {
00099
00100 assert(0 <= varPos && varPos < size);
00101
00102 setValue( c );
00103 clearDerivatives();
00104
00105 data_[varPos + dstart_] = 1.0;
00106 Valgrind::CheckDefined(data_);
00107 }
00108
00109
00110 void clearDerivatives()
00111 {
00112 data_[1] = 0.0;
00113 data_[2] = 0.0;
00114 data_[3] = 0.0;
00115 data_[4] = 0.0;
00116 data_[5] = 0.0;
00117 data_[6] = 0.0;
00118 }
00119
00120
00121 template <class RhsValueType>
00122 static Evaluation createVariable(const RhsValueType& value, int varPos)
00123 {
00124
00125
00126 return Evaluation( value, varPos );
00127 }
00128
00129
00130
00131 template <class RhsValueType>
00132 static Evaluation createConstant(const RhsValueType& value)
00133 {
00134 return Evaluation( value );
00135 }
00136
00137
00138 void print(std::ostream& os = std::cout) const
00139 {
00140
00141 os << "v: " << value() << " / d:";
00142
00143
00144 for (int varIdx = 0; varIdx < size; ++varIdx) {
00145 os << " " << derivative(varIdx);
00146 }
00147 }
00148
00149
00150 void copyDerivatives(const Evaluation& other)
00151 {
00152 data_[1] = other.data_[1];
00153 data_[2] = other.data_[2];
00154 data_[3] = other.data_[3];
00155 data_[4] = other.data_[4];
00156 data_[5] = other.data_[5];
00157 data_[6] = other.data_[6];
00158 }
00159
00160
00161
00162 Evaluation& operator+=(const Evaluation& other)
00163 {
00164 data_[0] += other.data_[0];
00165 data_[1] += other.data_[1];
00166 data_[2] += other.data_[2];
00167 data_[3] += other.data_[3];
00168 data_[4] += other.data_[4];
00169 data_[5] += other.data_[5];
00170 data_[6] += other.data_[6];
00171
00172 return *this;
00173 }
00174
00175
00176 template <class RhsValueType>
00177 Evaluation& operator+=(const RhsValueType& other)
00178 {
00179
00180 data_[valuepos_] += other;
00181
00182 return *this;
00183 }
00184
00185
00186 Evaluation& operator-=(const Evaluation& other)
00187 {
00188 data_[0] -= other.data_[0];
00189 data_[1] -= other.data_[1];
00190 data_[2] -= other.data_[2];
00191 data_[3] -= other.data_[3];
00192 data_[4] -= other.data_[4];
00193 data_[5] -= other.data_[5];
00194 data_[6] -= other.data_[6];
00195
00196 return *this;
00197 }
00198
00199
00200 template <class RhsValueType>
00201 Evaluation& operator-=(const RhsValueType& other)
00202 {
00203
00204 data_[ valuepos_ ] -= other;
00205
00206 return *this;
00207 }
00208
00209
00210 Evaluation& operator*=(const Evaluation& other)
00211 {
00212
00213
00214 const ValueType u = this->value();
00215 const ValueType v = other.value();
00216
00217
00218 data_[valuepos_] *= v ;
00219
00220
00221 data_[1] = data_[1] * v + other.data_[1] * u;
00222 data_[2] = data_[2] * v + other.data_[2] * u;
00223 data_[3] = data_[3] * v + other.data_[3] * u;
00224 data_[4] = data_[4] * v + other.data_[4] * u;
00225 data_[5] = data_[5] * v + other.data_[5] * u;
00226 data_[6] = data_[6] * v + other.data_[6] * u;
00227
00228 return *this;
00229 }
00230
00231
00232 template <class RhsValueType>
00233 Evaluation& operator*=(const RhsValueType& other)
00234 {
00235 data_[0] *= other;
00236 data_[1] *= other;
00237 data_[2] *= other;
00238 data_[3] *= other;
00239 data_[4] *= other;
00240 data_[5] *= other;
00241 data_[6] *= other;
00242
00243 return *this;
00244 }
00245
00246
00247 Evaluation& operator/=(const Evaluation& other)
00248 {
00249
00250
00251 ValueType& u = data_[ valuepos_ ];
00252 const ValueType& v = other.value();
00253 data_[1] = (v*data_[1] - u*other.data_[1])/(v*v);
00254 data_[2] = (v*data_[2] - u*other.data_[2])/(v*v);
00255 data_[3] = (v*data_[3] - u*other.data_[3])/(v*v);
00256 data_[4] = (v*data_[4] - u*other.data_[4])/(v*v);
00257 data_[5] = (v*data_[5] - u*other.data_[5])/(v*v);
00258 data_[6] = (v*data_[6] - u*other.data_[6])/(v*v);
00259 u /= v;
00260
00261 return *this;
00262 }
00263
00264
00265 template <class RhsValueType>
00266 Evaluation& operator/=(const RhsValueType& other)
00267 {
00268 const ValueType tmp = 1.0/other;
00269
00270 data_[0] *= tmp;
00271 data_[1] *= tmp;
00272 data_[2] *= tmp;
00273 data_[3] *= tmp;
00274 data_[4] *= tmp;
00275 data_[5] *= tmp;
00276 data_[6] *= tmp;
00277
00278 return *this;
00279 }
00280
00281
00282 Evaluation operator+(const Evaluation& other) const
00283 {
00284 Evaluation result(*this);
00285
00286 result += other;
00287
00288 return result;
00289 }
00290
00291
00292 template <class RhsValueType>
00293 Evaluation operator+(const RhsValueType& other) const
00294 {
00295 Evaluation result(*this);
00296
00297 result += other;
00298
00299 return result;
00300 }
00301
00302
00303 Evaluation operator-(const Evaluation& other) const
00304 {
00305 Evaluation result(*this);
00306
00307 result -= other;
00308
00309 return result;
00310 }
00311
00312
00313 template <class RhsValueType>
00314 Evaluation operator-(const RhsValueType& other) const
00315 {
00316 Evaluation result(*this);
00317
00318 result -= other;
00319
00320 return result;
00321 }
00322
00323
00324 Evaluation operator-() const
00325 {
00326 Evaluation result;
00327
00328
00329 result.data_[0] = - data_[0];
00330 result.data_[1] = - data_[1];
00331 result.data_[2] = - data_[2];
00332 result.data_[3] = - data_[3];
00333 result.data_[4] = - data_[4];
00334 result.data_[5] = - data_[5];
00335 result.data_[6] = - data_[6];
00336
00337 return result;
00338 }
00339
00340 Evaluation operator*(const Evaluation& other) const
00341 {
00342 Evaluation result(*this);
00343
00344 result *= other;
00345
00346 return result;
00347 }
00348
00349 template <class RhsValueType>
00350 Evaluation operator*(const RhsValueType& other) const
00351 {
00352 Evaluation result(*this);
00353
00354 result *= other;
00355
00356 return result;
00357 }
00358
00359 Evaluation operator/(const Evaluation& other) const
00360 {
00361 Evaluation result(*this);
00362
00363 result /= other;
00364
00365 return result;
00366 }
00367
00368 template <class RhsValueType>
00369 Evaluation operator/(const RhsValueType& other) const
00370 {
00371 Evaluation result(*this);
00372
00373 result /= other;
00374
00375 return result;
00376 }
00377
00378 template <class RhsValueType>
00379 Evaluation& operator=(const RhsValueType& other)
00380 {
00381 setValue( other );
00382 clearDerivatives();
00383
00384 return *this;
00385 }
00386
00387
00388 Evaluation& operator=(const Evaluation& other) = default;
00389
00390 template <class RhsValueType>
00391 bool operator==(const RhsValueType& other) const
00392 { return value() == other; }
00393
00394 bool operator==(const Evaluation& other) const
00395 {
00396 for (int idx = 0; idx < length_; ++idx) {
00397 if (data_[idx] != other.data_[idx]) {
00398 return false;
00399 }
00400 }
00401 return true;
00402 }
00403
00404 bool operator!=(const Evaluation& other) const
00405 { return !operator==(other); }
00406
00407 template <class RhsValueType>
00408 bool operator>(RhsValueType other) const
00409 { return value() > other; }
00410
00411 bool operator>(const Evaluation& other) const
00412 { return value() > other.value(); }
00413
00414 template <class RhsValueType>
00415 bool operator<(RhsValueType other) const
00416 { return value() < other; }
00417
00418 bool operator<(const Evaluation& other) const
00419 { return value() < other.value(); }
00420
00421 template <class RhsValueType>
00422 bool operator>=(RhsValueType other) const
00423 { return value() >= other; }
00424
00425 bool operator>=(const Evaluation& other) const
00426 { return value() >= other.value(); }
00427
00428 template <class RhsValueType>
00429 bool operator<=(RhsValueType other) const
00430 { return value() <= other; }
00431
00432 bool operator<=(const Evaluation& other) const
00433 { return value() <= other.value(); }
00434
00435
00436 const ValueType& value() const
00437 { return data_[valuepos_]; }
00438
00439
00440 template <class RhsValueType>
00441 void setValue(const RhsValueType& val)
00442 { data_[valuepos_] = val; }
00443
00444
00445 const ValueType& derivative(int varIdx) const
00446 {
00447 assert(0 <= varIdx && varIdx < size);
00448
00449 return data_[dstart_ + varIdx];
00450 }
00451
00452
00453 void setDerivative(int varIdx, const ValueType& derVal)
00454 {
00455 assert(0 <= varIdx && varIdx < size);
00456
00457 data_[dstart_ + varIdx] = derVal;
00458 }
00459
00460 private:
00461 std::array<ValueT, length_> data_;
00462 };
00463
00464 } }
00465
00466 #endif // OPM_DENSEAD_EVALUATION6_HPP