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