32 #ifndef OPM_DENSEAD_EVALUATION_HPP
33 #define OPM_DENSEAD_EVALUATION_HPP
51 static constexpr
int DynamicSize = -1;
57 template <
class ValueT,
int numDerivs,
unsigned staticSize = 0>
75 {
return size() + 1; }
93 for (
const auto& v: data_)
94 Valgrind::CheckDefined(v);
111 template <
class RhsValueType>
124 template <
class RhsValueType>
128 assert(0 <= varPos && varPos <
size());
133 data_[varPos +
dstart_()] = 1.0;
139 void clearDerivatives()
165 template <
class RhsValueType>
166 static Evaluation createVariable(
const RhsValueType& value,
int varPos)
173 template <
class RhsValueType>
174 static Evaluation createVariable(
int nVars,
const RhsValueType& value,
int varPos)
177 throw std::logic_error(
"This statically-sized evaluation can only represent objects"
178 " with 0 derivatives");
185 template <
class RhsValueType>
196 template <
class RhsValueType>
197 static Evaluation createConstant(
int nVars,
const RhsValueType& value)
200 throw std::logic_error(
"This statically-sized evaluation can only represent objects"
201 " with 0 derivatives");
207 template <
class RhsValueType>
208 static Evaluation createConstant(
const RhsValueType& value)
215 template <
class RhsValueType>
222 void print(std::ostream& os = std::cout)
const
225 os <<
"v: " << value() <<
" / d:";
228 for (
int varIdx = 0; varIdx <
size(); ++varIdx) {
229 os <<
" " << derivative(varIdx);
236 assert(
size() == other.size());
239 data_[i] = other.data_[i];
246 assert(
size() == other.size());
248 for (
int i = 0; i <
length_(); ++i)
249 data_[i] += other.data_[i];
255 template <
class RhsValueType>
256 Evaluation& operator+=(
const RhsValueType& other)
267 assert(
size() == other.size());
269 for (
int i = 0; i <
length_(); ++i)
270 data_[i] -= other.data_[i];
276 template <
class RhsValueType>
277 Evaluation& operator-=(
const RhsValueType& other)
288 assert(
size() == other.size());
300 data_[i] = data_[i] * v + other.data_[i] * u;
306 template <
class RhsValueType>
307 Evaluation& operator*=(
const RhsValueType& other)
309 for (
int i = 0; i <
length_(); ++i)
318 assert(
size() == other.size());
326 const ValueType& vPrime = other.data_[idx];
328 data_[idx] = (v*uPrime - u*vPrime)/(v*v);
336 template <
class RhsValueType>
337 Evaluation& operator/=(
const RhsValueType& other)
341 for (
int i = 0; i <
length_(); ++i)
350 assert(
size() == other.size());
360 template <
class RhsValueType>
361 Evaluation operator+(
const RhsValueType& other)
const
373 assert(
size() == other.size());
383 template <
class RhsValueType>
384 Evaluation operator-(
const RhsValueType& other)
const
399 for (
int i = 0; i <
length_(); ++i)
400 result.data_[i] = - data_[i];
407 assert(
size() == other.size());
416 template <
class RhsValueType>
417 Evaluation operator*(
const RhsValueType& other)
const
428 assert(
size() == other.size());
437 template <
class RhsValueType>
438 Evaluation operator/(
const RhsValueType& other)
const
447 template <
class RhsValueType>
448 Evaluation& operator=(
const RhsValueType& other)
459 template <
class RhsValueType>
460 bool operator==(
const RhsValueType& other)
const
461 {
return value() == other; }
463 bool operator==(
const Evaluation& other)
const
465 assert(
size() == other.size());
467 for (
int idx = 0; idx <
length_(); ++idx) {
468 if (data_[idx] != other.data_[idx]) {
475 bool operator!=(
const Evaluation& other)
const
476 {
return !operator==(other); }
478 template <
class RhsValueType>
479 bool operator!=(
const RhsValueType& other)
const
480 {
return !operator==(other); }
482 template <
class RhsValueType>
483 bool operator>(RhsValueType other)
const
484 {
return value() > other; }
488 assert(
size() == other.size());
490 return value() > other.value();
493 template <
class RhsValueType>
494 bool operator<(RhsValueType other)
const
495 {
return value() < other; }
499 assert(
size() == other.size());
501 return value() < other.value();
504 template <
class RhsValueType>
505 bool operator>=(RhsValueType other)
const
506 {
return value() >= other; }
508 bool operator>=(
const Evaluation& other)
const
510 assert(
size() == other.size());
512 return value() >= other.value();
515 template <
class RhsValueType>
516 bool operator<=(RhsValueType other)
const
517 {
return value() <= other; }
519 bool operator<=(
const Evaluation& other)
const
521 assert(
size() == other.size());
523 return value() <= other.value();
531 template <
class RhsValueType>
532 void setValue(
const RhsValueType& val)
536 const ValueType& derivative(
int varIdx)
const
538 assert(0 <= varIdx && varIdx <
size());
540 return data_[
dstart_() + varIdx];
544 void setDerivative(
int varIdx,
const ValueType& derVal)
546 assert(0 <= varIdx && varIdx <
size());
548 data_[
dstart_() + varIdx] = derVal;
553 std::array<ValueT, numDerivs + 1> data_;
557 template <
class RhsValueType,
class ValueType,
int numVars,
unsigned staticSize>
558 bool operator<(
const RhsValueType& a,
const Evaluation<ValueType, numVars, staticSize>& b)
561 template <
class RhsValueType,
class ValueType,
int numVars,
unsigned staticSize>
562 bool operator>(
const RhsValueType& a,
const Evaluation<ValueType, numVars, staticSize>& b)
565 template <
class RhsValueType,
class ValueType,
int numVars,
unsigned staticSize>
566 bool operator<=(
const RhsValueType& a,
const Evaluation<ValueType, numVars, staticSize>& b)
569 template <
class RhsValueType,
class ValueType,
int numVars,
unsigned staticSize>
570 bool operator>=(
const RhsValueType& a,
const Evaluation<ValueType, numVars, staticSize>& b)
573 template <
class RhsValueType,
class ValueType,
int numVars,
unsigned staticSize>
574 bool operator!=(
const RhsValueType& a,
const Evaluation<ValueType, numVars, staticSize>& b)
575 {
return a != b.value(); }
577 template <
class RhsValueType,
class ValueType,
int numVars,
unsigned staticSize>
578 Evaluation<ValueType, numVars, staticSize> operator+(
const RhsValueType& a,
const Evaluation<ValueType, numVars, staticSize>& b)
580 Evaluation<ValueType, numVars, staticSize> result(b);
585 template <
class RhsValueType,
class ValueType,
int numVars,
unsigned staticSize>
586 Evaluation<ValueType, numVars, staticSize> operator-(
const RhsValueType& a,
const Evaluation<ValueType, numVars, staticSize>& b)
591 template <
class RhsValueType,
class ValueType,
int numVars,
unsigned staticSize>
592 Evaluation<ValueType, numVars, staticSize> operator/(
const RhsValueType& a,
const Evaluation<ValueType, numVars, staticSize>& b)
594 Evaluation<ValueType, numVars, staticSize> tmp(a);
599 template <
class RhsValueType,
class ValueType,
int numVars,
unsigned staticSize>
600 Evaluation<ValueType, numVars, staticSize> operator*(
const RhsValueType& a,
const Evaluation<ValueType, numVars, staticSize>& b)
602 Evaluation<ValueType, numVars, staticSize> result(b);
607 template <
class ValueType,
int numVars,
unsigned staticSize>
608 std::ostream& operator<<(std::ostream& os,
const Evaluation<ValueType, numVars, staticSize>& eval)
618 #include <dune/common/ftraits.hh>
621 template <
class ValueType,
int numVars,
unsigned staticSize>
622 struct FieldTraits<Opm::DenseAd::Evaluation<ValueType, numVars, staticSize> >
This file includes all specializations for the dense-AD Evaluation class.
Representation of an evaluation of a function and its derivatives w.r.t.
A number of commonly used algebraic functions for the localized OPM automatic differentiation (AD) fr...
Some templates to wrap the valgrind client request macros.
Represents a function evaluation and its derivatives w.r.t.
Definition: Evaluation.hpp:59
Evaluation()
default constructor
Definition: Evaluation.hpp:100
ValueT ValueType
field type
Definition: Evaluation.hpp:66
void checkDefined_() const
instruct valgrind to check that the value and all derivatives of the Evaluation object are well-defin...
Definition: Evaluation.hpp:90
static const int numVars
the template argument which specifies the number of derivatives (-1 == "DynamicSize" means runtime de...
Definition: Evaluation.hpp:63
constexpr int size() const
number of derivatives
Definition: Evaluation.hpp:69
constexpr int valuepos_() const
position index for value
Definition: Evaluation.hpp:79
Evaluation(const Evaluation &other)=default
copy other function evaluation
constexpr int dend_() const
end+1 index for derivatives
Definition: Evaluation.hpp:85
constexpr int length_() const
length of internal data vector
Definition: Evaluation.hpp:74
constexpr int dstart_() const
start index for derivatives
Definition: Evaluation.hpp:82