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