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