32 #ifndef OPM_DENSEAD_EVALUATION_DYNAMIC_HPP 33 #define OPM_DENSEAD_EVALUATION_DYNAMIC_HPP 45 #include <opm/common/ErrorMacros.hpp> 46 #include <opm/common/utility/gpuDecorators.hpp> 55 template <
class ValueT,
unsigned staticSize>
67 OPM_HOST_DEVICE
int size()
const 68 {
return data_.size() - 1; }
73 {
return data_.size(); }
80 OPM_HOST_DEVICE constexpr
int dstart_()
const 83 OPM_HOST_DEVICE
int dend_()
const 92 Valgrind::CheckDefined(data_[i]);
107 : data_(
std::move(other.data_))
113 data_ = std::move(other.data_);
121 template <
class RhsValueType>
122 OPM_HOST_DEVICE
Evaluation(
int numDerivatives,
const RhsValueType& c)
123 : data_(1 + numDerivatives, 0.0)
136 template <
class RhsValueType>
137 OPM_HOST_DEVICE
Evaluation(
const RhsValueType& c)
144 template <
class RhsValueType>
145 OPM_HOST_DEVICE
Evaluation(
int nVars,
const RhsValueType& c,
int varPos)
146 : data_(1 + nVars, 0.0)
149 assert(0 <= varPos && varPos <
size());
153 data_[varPos +
dstart_()] = 1.0;
159 OPM_HOST_DEVICE constexpr
void clearDerivatives()
185 template <
class RhsValueType>
186 OPM_HOST_DEVICE
static Evaluation createVariable(
const RhsValueType&,
int)
188 OPM_THROW(std::logic_error,
"Dynamically sized evaluations require that the number of " 189 "derivatives is specified when creating an evaluation");
192 template <
class RhsValueType>
193 OPM_HOST_DEVICE
static Evaluation createVariable(
int nVars,
const RhsValueType&
value,
int varPos)
200 template <
class RhsValueType>
211 template <
class RhsValueType>
212 OPM_HOST_DEVICE
static Evaluation createConstant(
int nVars,
const RhsValueType&
value)
219 template <
class RhsValueType>
220 OPM_HOST_DEVICE
static Evaluation createConstant(
const RhsValueType&
value)
228 template <
class RhsValueType>
235 OPM_HOST_DEVICE
void copyDerivatives(
const Evaluation& other)
237 assert(
size() == other.size());
240 data_[i] = other.data_[i];
247 const int thisSize =
size();
248 const int otherSize = other.size();
251 assert(thisSize == otherSize || thisSize == 0 || otherSize == 0);
253 if (thisSize == otherSize) {
254 for (
int i = 0; i <
length_(); ++i)
255 data_[i] += other.data_[i];
258 if (otherSize == 0) {
259 *
this += other.value();
263 assert(otherSize > 0);
264 this->appendDerivativesToConstant(otherSize);
267 data_[i] = other.data_[i];
270 OPM_THROW(std::logic_error,
271 "Cannot operate Evaluations with different number of derivatives " 272 "unless one of them has no derivatives");
276 template <
class RhsValueType>
277 OPM_HOST_DEVICE
Evaluation& operator+=(
const RhsValueType& other)
288 const int thisSize =
size();
289 const int otherSize = other.size();
292 assert(thisSize == otherSize || thisSize == 0 || otherSize == 0);
294 if (thisSize == otherSize) {
295 for (
int i = 0; i <
length_(); ++i)
296 data_[i] -= other.data_[i];
299 if (otherSize == 0) {
300 *
this -= other.value();
304 assert(otherSize > 0);
305 this->appendDerivativesToConstant(otherSize);
306 for (
int i = 0; i < other.length_(); ++i)
307 data_[i] -= other.data_[i];
310 OPM_THROW(std::logic_error,
311 "Cannot operate Evaluations with different number of derivatives " 312 "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 OPM_THROW(std::logic_error,
365 "Cannot operate Evaluations with different number of derivatives " 366 "unless one of them has no derivatives");
370 template <
class RhsValueType>
371 OPM_HOST_DEVICE
Evaluation& operator*=(
const RhsValueType& other)
373 for (
int i = 0; i <
length_(); ++i)
382 const int thisSize =
size();
383 const int otherSize = other.size();
386 assert(thisSize == otherSize || thisSize == 0 || otherSize == 0);
388 if (thisSize == otherSize) {
397 const ValueType& vPrime = other.data_[idx];
399 data_[idx] = (v*uPrime - u*vPrime)/(v*v);
408 if (otherSize == 0) {
409 *
this /= other.value();
414 assert(otherSize > 0);
416 this->appendDerivativesToConstant(otherSize);
423 const ValueType& vPrime = other.data_[idx];
424 data_[idx] = -u * vPrime / (v * v);
430 OPM_THROW(std::logic_error,
431 "Cannot operate Evaluations with different number of derivatives " 432 "unless one of them has no derivatives");
436 template <
class RhsValueType>
437 OPM_HOST_DEVICE
Evaluation& operator/=(
const RhsValueType& other)
441 for (
int i = 0; i <
length_(); ++i)
451 const int thisSize =
size();
452 const int otherSize = other.size();
455 assert(thisSize == otherSize || thisSize == 0 || otherSize == 0);
466 template <
class RhsValueType>
467 OPM_HOST_DEVICE
Evaluation operator+(
const RhsValueType& other)
const 480 const int thisSize =
size();
481 const int otherSize = other.size();
484 assert(thisSize == otherSize || thisSize == 0 || otherSize == 0);
495 template <
class RhsValueType>
496 OPM_HOST_DEVICE
Evaluation operator-(
const RhsValueType& other)
const 511 for (
int i = 0; i <
length_(); ++i)
512 result.data_[i] = - data_[i];
520 const int thisSize =
size();
521 const int otherSize = other.size();
524 assert(thisSize == otherSize || thisSize == 0 || otherSize == 0);
534 template <
class RhsValueType>
535 OPM_HOST_DEVICE
Evaluation operator*(
const RhsValueType& other)
const 547 const int thisSize =
size();
548 const int otherSize = other.size();
551 assert(thisSize == otherSize || thisSize == 0 || otherSize == 0);
561 template <
class RhsValueType>
562 OPM_HOST_DEVICE
Evaluation operator/(
const RhsValueType& other)
const 571 template <
class RhsValueType>
572 OPM_HOST_DEVICE
Evaluation& operator=(
const RhsValueType& other)
583 template <
class RhsValueType>
584 OPM_HOST_DEVICE
bool operator==(
const RhsValueType& other)
const 585 {
return value() == other; }
587 OPM_HOST_DEVICE
bool operator==(
const Evaluation& other)
const 589 const int thisSize =
size();
590 const int otherSize = other.size();
593 assert(thisSize == otherSize || thisSize == 0 || otherSize == 0);
595 if (thisSize == otherSize) {
596 for (
int idx = 0; idx <
length_(); ++idx) {
597 if (data_[idx] != other.data_[idx]) {
602 return value() == other.value();
607 OPM_HOST_DEVICE
bool operator!=(
const Evaluation& other)
const 608 {
return !operator==(other); }
610 template <
class RhsValueType>
611 OPM_HOST_DEVICE
bool operator!=(
const RhsValueType& other)
const 612 {
return !operator==(other); }
614 template <
class RhsValueType>
615 OPM_HOST_DEVICE
bool operator>(RhsValueType other)
const 616 {
return value() > other; }
618 OPM_HOST_DEVICE
bool operator>(
const Evaluation& other)
const 620 assert(
size() == other.size() ||
size() == 0 || other.size() == 0);
622 return value() > other.value();
625 template <
class RhsValueType>
626 OPM_HOST_DEVICE
bool operator<(RhsValueType other)
const 627 {
return value() < other; }
629 OPM_HOST_DEVICE
bool operator<(
const Evaluation& other)
const 631 assert(
size() == other.size() ||
size() == 0 || other.size() == 0);
633 return value() < other.value();
636 template <
class RhsValueType>
637 OPM_HOST_DEVICE
bool operator>=(RhsValueType other)
const 638 {
return value() >= other; }
640 OPM_HOST_DEVICE
bool operator>=(
const Evaluation& other)
const 642 assert(
size() == other.size() ||
size() == 0 || other.size() == 0);
644 return value() >= other.value();
647 template <
class RhsValueType>
648 OPM_HOST_DEVICE
bool operator<=(RhsValueType other)
const 649 {
return value() <= other; }
651 OPM_HOST_DEVICE
bool operator<=(
const Evaluation& other)
const 653 assert(
size() == other.size() ||
size() == 0 || other.size() == 0);
655 return value() <= other.value();
663 template <
class RhsValueType>
664 OPM_HOST_DEVICE constexpr
void setValue(
const RhsValueType& val)
666 if (data_.size() == 0) {
673 OPM_HOST_DEVICE
const ValueType& derivative(
int varIdx)
const 675 assert(
size() == 0 || (0 <= varIdx && varIdx <
size()) );
682 return data_[
dstart_() + varIdx];
686 OPM_HOST_DEVICE
void setDerivative(
int varIdx,
const ValueType& derVal,
const int nVars = -1)
690 assert( (
size() == 0 && nVars > 0) ||
691 ((
size() > 0 && (nVars < 0 || nVars ==
size()))) );
695 OPM_THROW(std::logic_error,
"Cannot set derivative for a DynamicEvaluation initialized from a scalar " 696 "without specifying a positive number of derivatives");
699 this->appendDerivativesToConstant(nVars);
702 assert(0 <= varIdx && varIdx <
size());
704 data_[
dstart_() + varIdx] = derVal;
707 template<
class Serializer>
708 OPM_HOST_DEVICE
void serializeOp(Serializer& serializer)
714 FastSmallVector<ValueT, staticSize> data_;
716 void appendDerivativesToConstant(std::size_t numDer) {
719 data_.resize(1 + numDer);
724 template <
class Scalar,
unsigned staticSize = 0>
725 using DynamicEvaluation = Evaluation<Scalar, DynamicSize, staticSize>;
729 template <
class Scalar,
unsigned staticSize>
730 OPM_HOST_DEVICE DenseAd::Evaluation<Scalar, -1, staticSize> constant(
int numDerivatives,
const Scalar&
value)
731 {
return DenseAd::Evaluation<Scalar, -1, staticSize>::createConstant(numDerivatives,
value); }
733 template <
class Scalar,
unsigned staticSize>
734 OPM_HOST_DEVICE DenseAd::Evaluation<Scalar, -1, staticSize> variable(
int numDerivatives,
const Scalar&
value,
unsigned idx)
735 {
return DenseAd::Evaluation<Scalar, -1, staticSize>::createVariable(numDerivatives,
value, idx); }
739 #endif // OPM_DENSEAD_EVALUATION_DYNAMIC_HPP OPM_HOST_DEVICE constexpr int dstart_() const
start index for derivatives
Definition: Evaluation.hpp:87
OPM_HOST_DEVICE Evaluation & operator=(Evaluation &&other)
move assignment
Definition: DynamicEvaluation.hpp:111
OPM_HOST_DEVICE int length_() const
length of internal data vector
Definition: DynamicEvaluation.hpp:72
OPM_HOST_DEVICE int dend_() const
end+1 index for derivatives
Definition: DynamicEvaluation.hpp:83
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:88
OPM_HOST_DEVICE int size() const
number of derivatives
Definition: DynamicEvaluation.hpp:67
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:95
OPM_HOST_DEVICE constexpr int valuepos_() const
position index for value
Definition: Evaluation.hpp:84
OPM_HOST_DEVICE Evaluation()
default constructor
Definition: Evaluation.hpp:105
OPM_HOST_DEVICE constexpr int length_() const
length of internal data vector
Definition: Evaluation.hpp:79
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:64
ValueT ValueType
field type
Definition: Evaluation.hpp:71
OPM_HOST_DEVICE constexpr int size() const
number of derivatives
Definition: Evaluation.hpp:74
static const int numVars
the template argument which specifies the number of derivatives (-1 == "DynamicSize" means runtime de...
Definition: Evaluation.hpp:68
OPM_HOST_DEVICE Evaluation()
default constructor
Definition: DynamicEvaluation.hpp:98
OPM_HOST_DEVICE constexpr int dstart_() const
start index for derivatives
Definition: DynamicEvaluation.hpp:80
OPM_HOST_DEVICE constexpr int valuepos_() const
position index for value
Definition: DynamicEvaluation.hpp:77
Some templates to wrap the valgrind client request macros.
Represents a function evaluation and its derivatives w.r.t.
Definition: Evaluation.hpp:63
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:90
OPM_HOST_DEVICE Evaluation(Evaluation &&other)
move other function evaluation (this only makes sense for dynamically allocated Evaluations) ...
Definition: DynamicEvaluation.hpp:106