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_EVALUATION3_HPP
00032 #define OPM_DENSEAD_EVALUATION3_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, 3>
00053 {
00054 public:
00056 typedef ValueT ValueType;
00057
00059 static constexpr int size = 3;
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 }
00116
00117
00118 template <class RhsValueType>
00119 static Evaluation createVariable(const RhsValueType& value, int varPos)
00120 {
00121
00122
00123 return Evaluation( value, varPos );
00124 }
00125
00126
00127
00128 template <class RhsValueType>
00129 static Evaluation createConstant(const RhsValueType& value)
00130 {
00131 return Evaluation( value );
00132 }
00133
00134
00135 void print(std::ostream& os = std::cout) const
00136 {
00137
00138 os << "v: " << value() << " / d:";
00139
00140
00141 for (int varIdx = 0; varIdx < size; ++varIdx) {
00142 os << " " << derivative(varIdx);
00143 }
00144 }
00145
00146
00147 void copyDerivatives(const Evaluation& other)
00148 {
00149 data_[1] = other.data_[1];
00150 data_[2] = other.data_[2];
00151 data_[3] = other.data_[3];
00152 }
00153
00154
00155
00156 Evaluation& operator+=(const Evaluation& other)
00157 {
00158 data_[0] += other.data_[0];
00159 data_[1] += other.data_[1];
00160 data_[2] += other.data_[2];
00161 data_[3] += other.data_[3];
00162
00163 return *this;
00164 }
00165
00166
00167 template <class RhsValueType>
00168 Evaluation& operator+=(const RhsValueType& other)
00169 {
00170
00171 data_[valuepos_] += other;
00172
00173 return *this;
00174 }
00175
00176
00177 Evaluation& operator-=(const Evaluation& other)
00178 {
00179 data_[0] -= other.data_[0];
00180 data_[1] -= other.data_[1];
00181 data_[2] -= other.data_[2];
00182 data_[3] -= other.data_[3];
00183
00184 return *this;
00185 }
00186
00187
00188 template <class RhsValueType>
00189 Evaluation& operator-=(const RhsValueType& other)
00190 {
00191
00192 data_[ valuepos_ ] -= other;
00193
00194 return *this;
00195 }
00196
00197
00198 Evaluation& operator*=(const Evaluation& other)
00199 {
00200
00201
00202 const ValueType u = this->value();
00203 const ValueType v = other.value();
00204
00205
00206 data_[valuepos_] *= v ;
00207
00208
00209 data_[1] = data_[1] * v + other.data_[1] * u;
00210 data_[2] = data_[2] * v + other.data_[2] * u;
00211 data_[3] = data_[3] * v + other.data_[3] * u;
00212
00213 return *this;
00214 }
00215
00216
00217 template <class RhsValueType>
00218 Evaluation& operator*=(const RhsValueType& other)
00219 {
00220 data_[0] *= other;
00221 data_[1] *= other;
00222 data_[2] *= other;
00223 data_[3] *= other;
00224
00225 return *this;
00226 }
00227
00228
00229 Evaluation& operator/=(const Evaluation& other)
00230 {
00231
00232
00233 ValueType& u = data_[ valuepos_ ];
00234 const ValueType& v = other.value();
00235 data_[1] = (v*data_[1] - u*other.data_[1])/(v*v);
00236 data_[2] = (v*data_[2] - u*other.data_[2])/(v*v);
00237 data_[3] = (v*data_[3] - u*other.data_[3])/(v*v);
00238 u /= v;
00239
00240 return *this;
00241 }
00242
00243
00244 template <class RhsValueType>
00245 Evaluation& operator/=(const RhsValueType& other)
00246 {
00247 const ValueType tmp = 1.0/other;
00248
00249 data_[0] *= tmp;
00250 data_[1] *= tmp;
00251 data_[2] *= tmp;
00252 data_[3] *= tmp;
00253
00254 return *this;
00255 }
00256
00257
00258 Evaluation operator+(const Evaluation& other) const
00259 {
00260 Evaluation result(*this);
00261
00262 result += other;
00263
00264 return result;
00265 }
00266
00267
00268 template <class RhsValueType>
00269 Evaluation operator+(const RhsValueType& other) const
00270 {
00271 Evaluation result(*this);
00272
00273 result += other;
00274
00275 return result;
00276 }
00277
00278
00279 Evaluation operator-(const Evaluation& other) const
00280 {
00281 Evaluation result(*this);
00282
00283 result -= other;
00284
00285 return result;
00286 }
00287
00288
00289 template <class RhsValueType>
00290 Evaluation operator-(const RhsValueType& other) const
00291 {
00292 Evaluation result(*this);
00293
00294 result -= other;
00295
00296 return result;
00297 }
00298
00299
00300 Evaluation operator-() const
00301 {
00302 Evaluation result;
00303
00304
00305 result.data_[0] = - data_[0];
00306 result.data_[1] = - data_[1];
00307 result.data_[2] = - data_[2];
00308 result.data_[3] = - data_[3];
00309
00310 return result;
00311 }
00312
00313 Evaluation operator*(const Evaluation& other) const
00314 {
00315 Evaluation result(*this);
00316
00317 result *= other;
00318
00319 return result;
00320 }
00321
00322 template <class RhsValueType>
00323 Evaluation operator*(const RhsValueType& other) const
00324 {
00325 Evaluation result(*this);
00326
00327 result *= other;
00328
00329 return result;
00330 }
00331
00332 Evaluation operator/(const Evaluation& other) const
00333 {
00334 Evaluation result(*this);
00335
00336 result /= other;
00337
00338 return result;
00339 }
00340
00341 template <class RhsValueType>
00342 Evaluation operator/(const RhsValueType& other) const
00343 {
00344 Evaluation result(*this);
00345
00346 result /= other;
00347
00348 return result;
00349 }
00350
00351 template <class RhsValueType>
00352 Evaluation& operator=(const RhsValueType& other)
00353 {
00354 setValue( other );
00355 clearDerivatives();
00356
00357 return *this;
00358 }
00359
00360
00361 Evaluation& operator=(const Evaluation& other) = default;
00362
00363 template <class RhsValueType>
00364 bool operator==(const RhsValueType& other) const
00365 { return value() == other; }
00366
00367 bool operator==(const Evaluation& other) const
00368 {
00369 for (int idx = 0; idx < length_; ++idx) {
00370 if (data_[idx] != other.data_[idx]) {
00371 return false;
00372 }
00373 }
00374 return true;
00375 }
00376
00377 bool operator!=(const Evaluation& other) const
00378 { return !operator==(other); }
00379
00380 template <class RhsValueType>
00381 bool operator>(RhsValueType other) const
00382 { return value() > other; }
00383
00384 bool operator>(const Evaluation& other) const
00385 { return value() > other.value(); }
00386
00387 template <class RhsValueType>
00388 bool operator<(RhsValueType other) const
00389 { return value() < other; }
00390
00391 bool operator<(const Evaluation& other) const
00392 { return value() < other.value(); }
00393
00394 template <class RhsValueType>
00395 bool operator>=(RhsValueType other) const
00396 { return value() >= other; }
00397
00398 bool operator>=(const Evaluation& other) const
00399 { return value() >= other.value(); }
00400
00401 template <class RhsValueType>
00402 bool operator<=(RhsValueType other) const
00403 { return value() <= other; }
00404
00405 bool operator<=(const Evaluation& other) const
00406 { return value() <= other.value(); }
00407
00408
00409 const ValueType& value() const
00410 { return data_[valuepos_]; }
00411
00412
00413 template <class RhsValueType>
00414 void setValue(const RhsValueType& val)
00415 { data_[valuepos_] = val; }
00416
00417
00418 const ValueType& derivative(int varIdx) const
00419 {
00420 assert(0 <= varIdx && varIdx < size);
00421
00422 return data_[dstart_ + varIdx];
00423 }
00424
00425
00426 void setDerivative(int varIdx, const ValueType& derVal)
00427 {
00428 assert(0 <= varIdx && varIdx < size);
00429
00430 data_[dstart_ + varIdx] = derVal;
00431 }
00432
00433 private:
00434 std::array<ValueT, length_> data_;
00435 };
00436
00437 } }
00438
00439 #endif // OPM_DENSEAD_EVALUATION3_HPP