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