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