32 #ifndef OPM_DENSEAD_EVALUATION_DYNAMIC_HPP
33 #define OPM_DENSEAD_EVALUATION_DYNAMIC_HPP
54 template <
class ValueT,
unsigned staticSize>
67 {
return data_.size() - 1; }
72 {
return data_.size(); }
91 Valgrind::CheckDefined(data_[i]);
106 : data_(std::move(other.data_))
112 data_ = std::move(other.data_);
118 : data_(1 + numDerivatives)
125 template <
class RhsValueType>
126 Evaluation(
int numDerivatives,
const RhsValueType& c)
127 : data_(1 + numDerivatives, 0.0)
139 template <
class RhsValueType>
140 Evaluation(
int nVars,
const RhsValueType& c,
int varPos)
141 : data_(1 + nVars, 0.0)
144 assert(0 <= varPos && varPos <
size());
148 data_[varPos +
dstart_()] = 1.0;
154 void clearDerivatives()
180 template <
class RhsValueType>
181 static Evaluation createVariable(
const RhsValueType&,
int)
183 throw std::logic_error(
"Dynamically sized evaluations require that the number of "
184 "derivatives is specified when creating an evaluation");
187 template <
class RhsValueType>
188 static Evaluation createVariable(
int nVars,
const RhsValueType& value,
int varPos)
195 template <
class RhsValueType>
206 template <
class RhsValueType>
207 static Evaluation createConstant(
int nVars,
const RhsValueType& value)
214 template <
class RhsValueType>
215 static Evaluation createConstant(
const RhsValueType&)
217 throw std::logic_error(
"Dynamically-sized evaluation objects require to specify the number of derivatives.");
222 template <
class RhsValueType>
229 void print(std::ostream& os = std::cout)
const
232 os <<
"v: " << value() <<
" / d:";
235 for (
int varIdx = 0; varIdx <
size(); ++varIdx) {
236 os <<
" " << derivative(varIdx);
243 assert(
size() == other.size());
246 data_[i] = other.data_[i];
253 assert(
size() == other.size());
255 for (
int i = 0; i <
length_(); ++i)
256 data_[i] += other.data_[i];
262 template <
class RhsValueType>
263 Evaluation& operator+=(
const RhsValueType& other)
274 assert(
size() == other.size());
276 for (
int i = 0; i <
length_(); ++i)
277 data_[i] -= other.data_[i];
283 template <
class RhsValueType>
284 Evaluation& operator-=(
const RhsValueType& other)
295 assert(
size() == other.size());
307 data_[i] = data_[i] * v + other.data_[i] * u;
313 template <
class RhsValueType>
314 Evaluation& operator*=(
const RhsValueType& other)
316 for (
int i = 0; i <
length_(); ++i)
325 assert(
size() == other.size());
333 const ValueType& vPrime = other.data_[idx];
335 data_[idx] = (v*uPrime - u*vPrime)/(v*v);
343 template <
class RhsValueType>
344 Evaluation& operator/=(
const RhsValueType& other)
348 for (
int i = 0; i <
length_(); ++i)
357 assert(
size() == other.size());
367 template <
class RhsValueType>
368 Evaluation operator+(
const RhsValueType& other)
const
380 assert(
size() == other.size());
390 template <
class RhsValueType>
391 Evaluation operator-(
const RhsValueType& other)
const
406 for (
int i = 0; i <
length_(); ++i)
407 result.data_[i] = - data_[i];
414 assert(
size() == other.size());
423 template <
class RhsValueType>
424 Evaluation operator*(
const RhsValueType& other)
const
435 assert(
size() == other.size());
444 template <
class RhsValueType>
445 Evaluation operator/(
const RhsValueType& other)
const
454 template <
class RhsValueType>
455 Evaluation& operator=(
const RhsValueType& other)
466 template <
class RhsValueType>
467 bool operator==(
const RhsValueType& other)
const
468 {
return value() == other; }
470 bool operator==(
const Evaluation& other)
const
472 assert(
size() == other.size());
474 for (
int idx = 0; idx <
length_(); ++idx) {
475 if (data_[idx] != other.data_[idx]) {
482 bool operator!=(
const Evaluation& other)
const
483 {
return !operator==(other); }
485 template <
class RhsValueType>
486 bool operator!=(
const RhsValueType& other)
const
487 {
return !operator==(other); }
489 template <
class RhsValueType>
490 bool operator>(RhsValueType other)
const
491 {
return value() > other; }
495 assert(
size() == other.size());
497 return value() > other.value();
500 template <
class RhsValueType>
501 bool operator<(RhsValueType other)
const
502 {
return value() < other; }
506 assert(
size() == other.size());
508 return value() < other.value();
511 template <
class RhsValueType>
512 bool operator>=(RhsValueType other)
const
513 {
return value() >= other; }
515 bool operator>=(
const Evaluation& other)
const
517 assert(
size() == other.size());
519 return value() >= other.value();
522 template <
class RhsValueType>
523 bool operator<=(RhsValueType other)
const
524 {
return value() <= other; }
526 bool operator<=(
const Evaluation& other)
const
528 assert(
size() == other.size());
530 return value() <= other.value();
538 template <
class RhsValueType>
539 void setValue(
const RhsValueType& val)
543 const ValueType& derivative(
int varIdx)
const
545 assert(0 <= varIdx && varIdx <
size());
547 return data_[
dstart_() + varIdx];
551 void setDerivative(
int varIdx,
const ValueType& derVal)
553 assert(0 <= varIdx && varIdx <
size());
555 data_[
dstart_() + varIdx] = derVal;
560 FastSmallVector<ValueT, staticSize> data_;
563 template <
class Scalar,
unsigned staticSize = 0>
564 using DynamicEvaluation = Evaluation<Scalar, DynamicSize, staticSize>;
568 template <
class Scalar,
unsigned staticSize>
569 DenseAd::Evaluation<Scalar, -1, staticSize> constant(
int numDerivatives,
const Scalar& value)
570 {
return DenseAd::Evaluation<Scalar, -1, staticSize>::createConstant(numDerivatives, value); }
572 template <
class Scalar,
unsigned staticSize>
573 DenseAd::Evaluation<Scalar, -1, staticSize> variable(
int numDerivatives,
const Scalar& value,
unsigned idx)
574 {
return DenseAd::Evaluation<Scalar, -1, staticSize>::createVariable(numDerivatives, value, idx); }
Representation of an evaluation of a function and its derivatives w.r.t.
An implementation of vector/array based on small object optimization.
A number of commonly used algebraic functions for the localized OPM automatic differentiation (AD) fr...
Some templates to wrap the valgrind client request macros.
constexpr int dstart_() const
start index for derivatives
Definition: DynamicEvaluation.hpp:79
Evaluation(const Evaluation &other)=default
copy other function evaluation
int length_() const
length of internal data vector
Definition: DynamicEvaluation.hpp:71
Evaluation(Evaluation &&other)
move other function evaluation (this only makes sense for dynamically allocated Evaluations)
Definition: DynamicEvaluation.hpp:105
void checkDefined_() const
instruct valgrind to check that the value and all derivatives of the Evaluation object are well-defin...
Definition: DynamicEvaluation.hpp:87
int size() const
number of derivatives
Definition: DynamicEvaluation.hpp:66
int dend_() const
end+1 index for derivatives
Definition: DynamicEvaluation.hpp:82
Evaluation & operator=(Evaluation &&other)
move assignment
Definition: DynamicEvaluation.hpp:110
ValueT ValueType
field type
Definition: DynamicEvaluation.hpp:63
Evaluation()
default constructor
Definition: DynamicEvaluation.hpp:97
constexpr int valuepos_() const
position index for value
Definition: DynamicEvaluation.hpp:76
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
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