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