32#ifndef OPM_DENSEAD_EVALUATION_HPP
33#define OPM_DENSEAD_EVALUATION_HPP
44#include <opm/common/utility/gpuDecorators.hpp>
47#include <dune/common/typetraits.hh>
55static constexpr int DynamicSize = -1;
61template <
class ValueT,
int numDerivs,
unsigned staticSize = 0>
73 OPM_HOST_DEVICE
constexpr int size()
const
78 OPM_HOST_DEVICE
constexpr int length_()
const
79 {
return size() + 1; }
86 OPM_HOST_DEVICE
constexpr int dstart_()
const
89 OPM_HOST_DEVICE
constexpr int dend_()
const
97 for (
const auto& v: data_)
115 template <
class RhsValueType>
116 OPM_HOST_DEVICE
constexpr Evaluation(
const RhsValueType& c): data_{}
126 template <
class RhsValueType>
127 OPM_HOST_DEVICE
Evaluation(
const RhsValueType& c,
int varPos)
130 assert(0 <= varPos && varPos <
size());
135 data_[varPos +
dstart_()] = 1.0;
141 OPM_HOST_DEVICE
constexpr void clearDerivatives()
167 template <
class RhsValueType>
168 OPM_HOST_DEVICE
static Evaluation createVariable(
const RhsValueType& value,
int varPos)
175 template <
class RhsValueType>
176 OPM_HOST_DEVICE
static Evaluation createVariable(
int nVars,
const RhsValueType& value,
int varPos)
179 throw std::logic_error(
"This statically-sized evaluation can only represent objects"
180 " with 0 derivatives");
187 template <
class RhsValueType>
188 OPM_HOST_DEVICE
static Evaluation createVariable(
const Evaluation&,
const RhsValueType& value,
int varPos)
198 template <
class RhsValueType>
199 OPM_HOST_DEVICE
static Evaluation createConstant(
int nVars,
const RhsValueType& value)
202 throw std::logic_error(
"This statically-sized evaluation can only represent objects"
203 " with 0 derivatives");
209 template <
class RhsValueType>
210 OPM_HOST_DEVICE
static Evaluation createConstant(
const RhsValueType& value)
217 template <
class RhsValueType>
224 OPM_HOST_DEVICE
void copyDerivatives(
const Evaluation& other)
226 assert(
size() == other.size());
229 data_[i] = other.data_[i];
236 assert(
size() == other.size());
238 for (
int i = 0; i <
length_(); ++i)
239 data_[i] += other.data_[i];
245 template <
class RhsValueType>
246 OPM_HOST_DEVICE
Evaluation& operator+=(
const RhsValueType& other)
257 assert(
size() == other.size());
259 for (
int i = 0; i <
length_(); ++i)
260 data_[i] -= other.data_[i];
266 template <
class RhsValueType>
267 OPM_HOST_DEVICE
Evaluation& operator-=(
const RhsValueType& other)
278 assert(
size() == other.size());
290 data_[i] = data_[i] * v + other.data_[i] * u;
296 template <
class RhsValueType>
297 OPM_HOST_DEVICE
Evaluation& operator*=(
const RhsValueType& other)
299 for (
int i = 0; i <
length_(); ++i)
308 assert(
size() == other.size());
316 const ValueType& vPrime = other.data_[idx];
318 data_[idx] = (v*uPrime - u*vPrime)/(v*v);
326 template <
class RhsValueType>
327 OPM_HOST_DEVICE
Evaluation& operator/=(
const RhsValueType& other)
331 for (
int i = 0; i <
length_(); ++i)
340 assert(
size() == other.size());
350 template <
class RhsValueType>
351 OPM_HOST_DEVICE
Evaluation operator+(
const RhsValueType& other)
const
363 assert(
size() == other.size());
373 template <
class RhsValueType>
374 OPM_HOST_DEVICE
Evaluation operator-(
const RhsValueType& other)
const
389 for (
int i = 0; i <
length_(); ++i)
390 result.data_[i] = - data_[i];
397 assert(
size() == other.size());
406 template <
class RhsValueType>
407 OPM_HOST_DEVICE
Evaluation operator*(
const RhsValueType& other)
const
418 assert(
size() == other.size());
427 template <
class RhsValueType>
428 OPM_HOST_DEVICE
Evaluation operator/(
const RhsValueType& other)
const
437 template <
class RhsValueType>
438 OPM_HOST_DEVICE
Evaluation& operator=(
const RhsValueType& other)
449 template <
class RhsValueType>
450 OPM_HOST_DEVICE
bool operator==(
const RhsValueType& other)
const
451 {
return value() == other; }
453 OPM_HOST_DEVICE
bool operator==(
const Evaluation& other)
const
455 assert(
size() == other.size());
457 for (
int idx = 0; idx <
length_(); ++idx) {
458 if (data_[idx] != other.data_[idx]) {
465 OPM_HOST_DEVICE
bool operator!=(
const Evaluation& other)
const
466 {
return !operator==(other); }
468 template <
class RhsValueType>
469 OPM_HOST_DEVICE
bool operator!=(
const RhsValueType& other)
const
470 {
return !operator==(other); }
472 template <
class RhsValueType>
473 OPM_HOST_DEVICE
bool operator>(RhsValueType other)
const
474 {
return value() > other; }
476 OPM_HOST_DEVICE
bool operator>(
const Evaluation& other)
const
478 assert(
size() == other.size());
480 return value() > other.value();
483 template <
class RhsValueType>
484 OPM_HOST_DEVICE
bool operator<(RhsValueType other)
const
485 {
return value() < other; }
487 OPM_HOST_DEVICE
bool operator<(
const Evaluation& other)
const
489 assert(
size() == other.size());
491 return value() < other.value();
494 template <
class RhsValueType>
495 OPM_HOST_DEVICE
bool operator>=(RhsValueType other)
const
496 {
return value() >= other; }
498 OPM_HOST_DEVICE
bool operator>=(
const Evaluation& other)
const
500 assert(
size() == other.size());
502 return value() >= other.value();
505 template <
class RhsValueType>
506 OPM_HOST_DEVICE
bool operator<=(RhsValueType other)
const
507 {
return value() <= other; }
509 OPM_HOST_DEVICE
bool operator<=(
const Evaluation& other)
const
511 assert(
size() == other.size());
513 return value() <= other.value();
517 OPM_HOST_DEVICE
const ValueType& value()
const
521 template <
class RhsValueType>
522 OPM_HOST_DEVICE
constexpr void setValue(
const RhsValueType& val)
526 OPM_HOST_DEVICE
const ValueType& derivative(
int varIdx)
const
528 assert(0 <= varIdx && varIdx <
size());
530 return data_[
dstart_() + varIdx];
534 OPM_HOST_DEVICE
void setDerivative(
int varIdx,
const ValueType& derVal)
536 assert(0 <= varIdx && varIdx <
size());
538 data_[
dstart_() + varIdx] = derVal;
541 template<
class Serializer>
542 OPM_HOST_DEVICE
void serializeOp(Serializer& serializer)
548 std::array<ValueT, numDerivs + 1> data_;
552template <
class RhsValueType,
class ValueType,
int numVars,
unsigned staticSize>
556template <
class RhsValueType,
class ValueType,
int numVars,
unsigned staticSize>
560template <
class RhsValueType,
class ValueType,
int numVars,
unsigned staticSize>
564template <
class RhsValueType,
class ValueType,
int numVars,
unsigned staticSize>
568template <
class RhsValueType,
class ValueType,
int numVars,
unsigned staticSize>
570{
return a != b.value(); }
572template <
class RhsValueType,
class ValueType,
int numVars,
unsigned staticSize>
580template <
class RhsValueType,
class ValueType,
int numVars,
unsigned staticSize>
586template <
class RhsValueType,
class ValueType,
int numVars,
unsigned staticSize>
594template <
class RhsValueType,
class ValueType,
int numVars,
unsigned staticSize>
605 static constexpr bool value =
false;
608template <
class ValueType,
int numVars,
unsigned staticSize>
611 static constexpr bool value =
true;
614template <
class ValueType,
int numVars,
unsigned staticSize>
615OPM_HOST_DEVICE
void printEvaluation(std::ostream& os,
617 bool withDer =
false);
619template <
class ValueType,
int numVars,
unsigned staticSize>
622 if constexpr (is_evaluation<ValueType>::value)
623 printEvaluation(os, eval.value(),
false);
625 printEvaluation(os, eval,
true);
638 template <
class ValueT,
int numDerivs,
unsigned staticSize>
639 struct IsNumber<Opm::DenseAd::Evaluation<ValueT,numDerivs,staticSize>>
640 :
public std::integral_constant<bool, std::is_arithmetic<ValueT>::value> {
This file includes all specializations for the dense-AD Evaluation class.
Some templates to wrap the valgrind client request macros.
OPM_HOST_DEVICE bool CheckDefined(const T &value)
Make valgrind complain if any of the memory occupied by an object is undefined.
Definition Valgrind.hpp:76
Represents a function evaluation and its derivatives w.r.t.
Definition Evaluation.hpp:63
Scalar ValueType
Definition Evaluation.hpp:70
OPM_HOST_DEVICE constexpr int dstart_() const
start index for derivatives
Definition Evaluation.hpp:86
static const int numVars
Definition Evaluation.hpp:67
OPM_HOST_DEVICE constexpr int length_() const
length of internal data vector
Definition Evaluation.hpp:78
OPM_HOST_DEVICE Evaluation()
default constructor
Definition Evaluation.hpp:104
OPM_HOST_DEVICE constexpr int valuepos_() const
position index for value
Definition Evaluation.hpp:83
Evaluation(const Evaluation &other)=default
copy other function evaluation
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 size() const
number of derivatives
Definition Evaluation.hpp:73
OPM_HOST_DEVICE constexpr int dend_() const
end+1 index for derivatives
Definition Evaluation.hpp:89
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition Exceptions.hpp:30
Definition Evaluation.hpp:604