32 #ifndef OPM_DENSEAD_EVALUATION_DYNAMIC_HPP 33 #define OPM_DENSEAD_EVALUATION_DYNAMIC_HPP 44 #include <opm/common/utility/gpuDecorators.hpp> 53 template <
class ValueT,
unsigned staticSize>
65 OPM_HOST_DEVICE
int size()
const 66 {
return data_.size() - 1; }
71 {
return data_.size(); }
78 OPM_HOST_DEVICE constexpr
int dstart_()
const 81 OPM_HOST_DEVICE
int dend_()
const 90 Valgrind::CheckDefined(data_[i]);
105 : data_(
std::move(other.data_))
111 data_ = std::move(other.data_);
119 template <
class RhsValueType>
120 OPM_HOST_DEVICE
Evaluation(
int numDerivatives,
const RhsValueType& c)
121 : data_(1 + numDerivatives, 0.0)
134 template <
class RhsValueType>
135 OPM_HOST_DEVICE
Evaluation(
const RhsValueType& c)
142 template <
class RhsValueType>
143 OPM_HOST_DEVICE
Evaluation(
int nVars,
const RhsValueType& c,
int varPos)
144 : data_(1 + nVars, 0.0)
147 assert(0 <= varPos && varPos <
size());
151 data_[varPos +
dstart_()] = 1.0;
157 OPM_HOST_DEVICE constexpr
void clearDerivatives()
183 template <
class RhsValueType>
184 OPM_HOST_DEVICE
static Evaluation createVariable(
const RhsValueType&,
int)
186 throw std::logic_error(
"Dynamically sized evaluations require that the number of " 187 "derivatives is specified when creating an evaluation");
190 template <
class RhsValueType>
191 OPM_HOST_DEVICE
static Evaluation createVariable(
int nVars,
const RhsValueType&
value,
int varPos)
198 template <
class RhsValueType>
209 template <
class RhsValueType>
210 OPM_HOST_DEVICE
static Evaluation createConstant(
int nVars,
const RhsValueType&
value)
217 template <
class RhsValueType>
218 OPM_HOST_DEVICE
static Evaluation createConstant(
const RhsValueType&
value)
226 template <
class RhsValueType>
233 OPM_HOST_DEVICE
void copyDerivatives(
const Evaluation& other)
235 assert(
size() == other.size());
238 data_[i] = other.data_[i];
245 const int thisSize =
size();
246 const int otherSize = other.size();
249 assert(thisSize == otherSize || thisSize == 0 || otherSize == 0);
251 if (thisSize == otherSize) {
252 for (
int i = 0; i <
length_(); ++i)
253 data_[i] += other.data_[i];
256 if (otherSize == 0) {
257 *
this += other.value();
261 assert(otherSize > 0);
262 this->appendDerivativesToConstant(otherSize);
265 data_[i] = other.data_[i];
268 throw std::logic_error(
269 "Cannot operate Evaluations with different number of derivatives " 270 "unless one of them has no derivatives" 275 template <
class RhsValueType>
276 OPM_HOST_DEVICE
Evaluation& operator+=(
const RhsValueType& other)
287 const int thisSize =
size();
288 const int otherSize = other.size();
291 assert(thisSize == otherSize || thisSize == 0 || otherSize == 0);
293 if (thisSize == otherSize) {
294 for (
int i = 0; i <
length_(); ++i)
295 data_[i] -= other.data_[i];
298 if (otherSize == 0) {
299 *
this -= other.value();
303 assert(otherSize > 0);
304 this->appendDerivativesToConstant(otherSize);
305 for (
int i = 0; i < other.length_(); ++i)
306 data_[i] -= other.data_[i];
309 throw std::logic_error(
310 "Cannot operate Evaluations with different number of derivatives " 311 "unless one of them has no derivatives" 316 template <
class RhsValueType>
317 OPM_HOST_DEVICE
Evaluation& operator-=(
const RhsValueType& other)
328 const int thisSize =
size();
329 const int otherSize = other.size();
332 assert(thisSize == otherSize || thisSize == 0 || otherSize == 0);
334 if (thisSize == otherSize) {
345 data_[i] = data_[i] * v + other.data_[i] * u;
350 if (otherSize == 0) {
351 *
this *= other.value();
356 assert(otherSize > 0);
357 this->appendDerivativesToConstant(otherSize);
359 for (
int i = 0; i <
length_(); ++i) {
360 data_[i] = u * other.data_[i];
364 throw std::logic_error(
365 "Cannot operate Evaluations with different number of derivatives " 366 "unless one of them has no derivatives" 371 template <
class RhsValueType>
372 OPM_HOST_DEVICE
Evaluation& operator*=(
const RhsValueType& other)
374 for (
int i = 0; i <
length_(); ++i)
383 const int thisSize =
size();
384 const int otherSize = other.size();
387 assert(thisSize == otherSize || thisSize == 0 || otherSize == 0);
389 if (thisSize == otherSize) {
398 const ValueType& vPrime = other.data_[idx];
400 data_[idx] = (v*uPrime - u*vPrime)/(v*v);
409 if (otherSize == 0) {
410 *
this /= other.value();
415 assert(otherSize > 0);
417 this->appendDerivativesToConstant(otherSize);
424 const ValueType& vPrime = other.data_[idx];
425 data_[idx] = -u * vPrime / (v * v);
431 throw std::logic_error(
432 "Cannot operate Evaluations with different number of derivatives " 433 "unless one of them has no derivatives" 438 template <
class RhsValueType>
439 OPM_HOST_DEVICE
Evaluation& operator/=(
const RhsValueType& other)
443 for (
int i = 0; i <
length_(); ++i)
453 const int thisSize =
size();
454 const int otherSize = other.size();
457 assert(thisSize == otherSize || thisSize == 0 || otherSize == 0);
468 template <
class RhsValueType>
469 OPM_HOST_DEVICE
Evaluation operator+(
const RhsValueType& other)
const 482 const int thisSize =
size();
483 const int otherSize = other.size();
486 assert(thisSize == otherSize || thisSize == 0 || otherSize == 0);
497 template <
class RhsValueType>
498 OPM_HOST_DEVICE
Evaluation operator-(
const RhsValueType& other)
const 513 for (
int i = 0; i <
length_(); ++i)
514 result.data_[i] = - data_[i];
522 const int thisSize =
size();
523 const int otherSize = other.size();
526 assert(thisSize == otherSize || thisSize == 0 || otherSize == 0);
536 template <
class RhsValueType>
537 OPM_HOST_DEVICE
Evaluation operator*(
const RhsValueType& other)
const 549 const int thisSize =
size();
550 const int otherSize = other.size();
553 assert(thisSize == otherSize || thisSize == 0 || otherSize == 0);
563 template <
class RhsValueType>
564 OPM_HOST_DEVICE
Evaluation operator/(
const RhsValueType& other)
const 573 template <
class RhsValueType>
574 OPM_HOST_DEVICE
Evaluation& operator=(
const RhsValueType& other)
585 template <
class RhsValueType>
586 OPM_HOST_DEVICE
bool operator==(
const RhsValueType& other)
const 587 {
return value() == other; }
589 OPM_HOST_DEVICE
bool operator==(
const Evaluation& other)
const 591 const int thisSize =
size();
592 const int otherSize = other.size();
595 assert(thisSize == otherSize || thisSize == 0 || otherSize == 0);
597 if (thisSize == otherSize) {
598 for (
int idx = 0; idx <
length_(); ++idx) {
599 if (data_[idx] != other.data_[idx]) {
604 return value() == other.value();
609 OPM_HOST_DEVICE
bool operator!=(
const Evaluation& other)
const 610 {
return !operator==(other); }
612 template <
class RhsValueType>
613 OPM_HOST_DEVICE
bool operator!=(
const RhsValueType& other)
const 614 {
return !operator==(other); }
616 template <
class RhsValueType>
617 OPM_HOST_DEVICE
bool operator>(RhsValueType other)
const 618 {
return value() > other; }
620 OPM_HOST_DEVICE
bool operator>(
const Evaluation& other)
const 622 assert(
size() == other.size() ||
size() == 0 || other.size() == 0);
624 return value() > other.value();
627 template <
class RhsValueType>
628 OPM_HOST_DEVICE
bool operator<(RhsValueType other)
const 629 {
return value() < other; }
631 OPM_HOST_DEVICE
bool operator<(
const Evaluation& other)
const 633 assert(
size() == other.size() ||
size() == 0 || other.size() == 0);
635 return value() < other.value();
638 template <
class RhsValueType>
639 OPM_HOST_DEVICE
bool operator>=(RhsValueType other)
const 640 {
return value() >= other; }
642 OPM_HOST_DEVICE
bool operator>=(
const Evaluation& other)
const 644 assert(
size() == other.size() ||
size() == 0 || other.size() == 0);
646 return value() >= other.value();
649 template <
class RhsValueType>
650 OPM_HOST_DEVICE
bool operator<=(RhsValueType other)
const 651 {
return value() <= other; }
653 OPM_HOST_DEVICE
bool operator<=(
const Evaluation& other)
const 655 assert(
size() == other.size() ||
size() == 0 || other.size() == 0);
657 return value() <= other.value();
665 template <
class RhsValueType>
666 OPM_HOST_DEVICE constexpr
void setValue(
const RhsValueType& val)
668 if (data_.size() == 0) {
675 OPM_HOST_DEVICE
const ValueType& derivative(
int varIdx)
const 677 assert(
size() == 0 || (0 <= varIdx && varIdx <
size()) );
684 return data_[
dstart_() + varIdx];
688 OPM_HOST_DEVICE
void setDerivative(
int varIdx,
const ValueType& derVal,
const int nVars = -1)
692 assert( (
size() == 0 && nVars > 0) ||
693 ((
size() > 0 && (nVars < 0 || nVars ==
size()))) );
697 throw std::logic_error(
"Cannot set derivative for a DynamicEvaluation initialized from a scalar " 698 "without specifying a positive number of derivatives");
701 this->appendDerivativesToConstant(nVars);
704 assert(0 <= varIdx && varIdx <
size());
706 data_[
dstart_() + varIdx] = derVal;
709 template<
class Serializer>
710 OPM_HOST_DEVICE
void serializeOp(Serializer& serializer)
716 FastSmallVector<ValueT, staticSize> data_;
718 void appendDerivativesToConstant(
size_t numDer) {
721 data_.resize(1 + numDer);
726 template <
class Scalar,
unsigned staticSize = 0>
727 using DynamicEvaluation = Evaluation<Scalar, DynamicSize, staticSize>;
731 template <
class Scalar,
unsigned staticSize>
732 OPM_HOST_DEVICE DenseAd::Evaluation<Scalar, -1, staticSize> constant(
int numDerivatives,
const Scalar&
value)
733 {
return DenseAd::Evaluation<Scalar, -1, staticSize>::createConstant(numDerivatives,
value); }
735 template <
class Scalar,
unsigned staticSize>
736 OPM_HOST_DEVICE DenseAd::Evaluation<Scalar, -1, staticSize> variable(
int numDerivatives,
const Scalar&
value,
unsigned idx)
737 {
return DenseAd::Evaluation<Scalar, -1, staticSize>::createVariable(numDerivatives,
value, idx); }
741 #endif // OPM_DENSEAD_EVALUATION_DYNAMIC_HPP OPM_HOST_DEVICE constexpr int dstart_() const
start index for derivatives
Definition: Evaluation.hpp:86
OPM_HOST_DEVICE Evaluation & operator=(Evaluation &&other)
move assignment
Definition: DynamicEvaluation.hpp:109
OPM_HOST_DEVICE int length_() const
length of internal data vector
Definition: DynamicEvaluation.hpp:70
OPM_HOST_DEVICE int dend_() const
end+1 index for derivatives
Definition: DynamicEvaluation.hpp:81
OPM_HOST_DEVICE constexpr void checkDefined_() const
instruct valgrind to check that the value and all derivatives of the Evaluation object are well-defin...
Definition: DynamicEvaluation.hpp:86
OPM_HOST_DEVICE int size() const
number of derivatives
Definition: DynamicEvaluation.hpp:65
OPM_HOST_DEVICE constexpr void checkDefined_() const
instruct valgrind to check that the value and all derivatives of the Evaluation object are well-defin...
Definition: Evaluation.hpp:94
OPM_HOST_DEVICE constexpr int valuepos_() const
position index for value
Definition: Evaluation.hpp:83
OPM_HOST_DEVICE Evaluation()
default constructor
Definition: Evaluation.hpp:104
OPM_HOST_DEVICE constexpr int length_() const
length of internal data vector
Definition: Evaluation.hpp:78
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition: Exceptions.hpp:30
ValueT ValueType
field type
Definition: DynamicEvaluation.hpp:62
ValueT ValueType
field type
Definition: Evaluation.hpp:70
OPM_HOST_DEVICE constexpr int size() const
number of derivatives
Definition: Evaluation.hpp:73
static const int numVars
the template argument which specifies the number of derivatives (-1 == "DynamicSize" means runtime de...
Definition: Evaluation.hpp:67
OPM_HOST_DEVICE Evaluation()
default constructor
Definition: DynamicEvaluation.hpp:96
OPM_HOST_DEVICE constexpr int dstart_() const
start index for derivatives
Definition: DynamicEvaluation.hpp:78
OPM_HOST_DEVICE constexpr int valuepos_() const
position index for value
Definition: DynamicEvaluation.hpp:75
Some templates to wrap the valgrind client request macros.
Represents a function evaluation and its derivatives w.r.t.
Definition: Evaluation.hpp:62
An implementation of vector/array based on small object optimization.
OPM_HOST_DEVICE constexpr int dend_() const
end+1 index for derivatives
Definition: Evaluation.hpp:89
OPM_HOST_DEVICE Evaluation(Evaluation &&other)
move other function evaluation (this only makes sense for dynamically allocated Evaluations) ...
Definition: DynamicEvaluation.hpp:104