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