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