opm-common
Evaluation.hpp
Go to the documentation of this file.
1 // -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 // vi: set et ts=4 sw=4 sts=4:
3 /*
4  This file is part of the Open Porous Media project (OPM).
5 
6  OPM is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 2 of the License, or
9  (at your option) any later version.
10 
11  OPM is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with OPM. If not, see <http://www.gnu.org/licenses/>.
18 
19  Consult the COPYING file in the top-level source directory of this
20  module for the precise wording of the license and the list of
21  copyright holders.
22 */
32 #ifndef OPM_DENSEAD_EVALUATION_HPP
33 #define OPM_DENSEAD_EVALUATION_HPP
34 
35 #ifndef NDEBUG
37 #endif
38 
39 #include <array>
40 #include <cassert>
41 #include <iosfwd>
42 #include <stdexcept>
43 
44 #include <opm/common/ErrorMacros.hpp>
45 #include <opm/common/utility/gpuDecorators.hpp>
46 
47 #if HAVE_DUNE_COMMON
48 #include <dune/common/typetraits.hh>
49 #endif
50 
51 namespace Opm {
52 namespace DenseAd {
53 
56 static constexpr int DynamicSize = -1;
57 
62 template <class ValueT, int numDerivs, unsigned staticSize = 0>
64 {
65 public:
68  static const int numVars = numDerivs;
69 
71  typedef ValueT ValueType;
72 
74  OPM_HOST_DEVICE constexpr int size() const
75  { return numDerivs; }
76 
77 protected:
79  OPM_HOST_DEVICE constexpr int length_() const
80  { return size() + 1; }
81 
82 
84  OPM_HOST_DEVICE constexpr int valuepos_() const
85  { return 0; }
87  OPM_HOST_DEVICE constexpr int dstart_() const
88  { return 1; }
90  OPM_HOST_DEVICE constexpr int dend_() const
91  { return length_(); }
92 
95  OPM_HOST_DEVICE constexpr void checkDefined_() const
96  {
97 #ifndef NDEBUG
98  for (const auto& v: data_)
99  Valgrind::CheckDefined(v);
100 #endif
101  }
102 
103 public:
105  OPM_HOST_DEVICE Evaluation() : data_()
106  {}
107 
109  Evaluation(const Evaluation& other) = default;
110 
111 
112  // create an evaluation which represents a constant function
113  //
114  // i.e., f(x) = c. this implies an evaluation with the given value and all
115  // derivatives being zero.
116  template <class RhsValueType>
117  OPM_HOST_DEVICE constexpr Evaluation(const RhsValueType& c): data_{}
118  {
119  setValue(c);
120  clearDerivatives();
121 
122  //checkDefined_();
123  }
124 
125  // create an evaluation representing a variable with the variable position of varPos
126  // The value is set to c, all derivatives are zero except for the one at varPos, which is set to 1.
127  template <class RhsValueType>
128  OPM_HOST_DEVICE Evaluation(const RhsValueType& c, int varPos)
129  {
130  // The variable position must be in represented by the given variable descriptor
131  assert(0 <= varPos && varPos < size());
132 
133  setValue( c );
134  clearDerivatives();
135 
136  data_[varPos + dstart_()] = 1.0;
137 
138  checkDefined_();
139  }
140 
141  // set all derivatives to zero
142  OPM_HOST_DEVICE constexpr void clearDerivatives()
143  {
144  for (int i = dstart_(); i < dend_(); ++i)
145  data_[i] = 0.0;
146  }
147 
148  // create an uninitialized Evaluation object that is compatible with the
149  // argument, but not initialized
150  //
151  // This basically boils down to the copy constructor without copying
152  // anything. If the number of derivatives is known at compile time, this
153  // is equivalent to creating an uninitialized object using the default
154  // constructor, while for dynamic evaluations, it creates an Evaluation
155  // object which exhibits the same number of derivatives as the argument.
156  OPM_HOST_DEVICE static Evaluation createBlank(const Evaluation&)
157  { return Evaluation(); }
158 
159  // create an Evaluation with value and all the derivatives to be zero
160  OPM_HOST_DEVICE static Evaluation createConstantZero(const Evaluation&)
161  { return Evaluation(0.); }
162 
163  // create an Evaluation with value to be one and all the derivatives to be zero
164  OPM_HOST_DEVICE static Evaluation createConstantOne(const Evaluation&)
165  { return Evaluation(1.); }
166 
167  // create a function evaluation for a "naked" depending variable (i.e., f(x) = x)
168  template <class RhsValueType>
169  OPM_HOST_DEVICE static Evaluation createVariable(const RhsValueType& value, int varPos)
170  {
171  // copy function value and set all derivatives to 0, except for the variable
172  // which is represented by the value (which is set to 1.0)
173  return Evaluation(value, varPos);
174  }
175 
176  template <class RhsValueType>
177  OPM_HOST_DEVICE static Evaluation createVariable(int nVars, const RhsValueType& value, int varPos)
178  {
179  if (nVars != 0)
180  throw std::logic_error("This statically-sized evaluation can only represent objects"
181  " with 0 derivatives");
182 
183  // copy function value and set all derivatives to 0, except for the variable
184  // which is represented by the value (which is set to 1.0)
185  return Evaluation(nVars, value, varPos);
186  }
187 
188  template <class RhsValueType>
189  OPM_HOST_DEVICE static Evaluation createVariable(const Evaluation&, const RhsValueType& value, int varPos)
190  {
191  // copy function value and set all derivatives to 0, except for the variable
192  // which is represented by the value (which is set to 1.0)
193  return Evaluation(value, varPos);
194  }
195 
196 
197  // "evaluate" a constant function (i.e. a function that does not depend on the set of
198  // relevant variables, f(x) = c).
199  template <class RhsValueType>
200  OPM_HOST_DEVICE static Evaluation createConstant(int nVars, const RhsValueType& value)
201  {
202  if (nVars != 0)
203  throw std::logic_error("This statically-sized evaluation can only represent objects"
204  " with 0 derivatives");
205  return Evaluation(value);
206  }
207 
208  // "evaluate" a constant function (i.e. a function that does not depend on the set of
209  // relevant variables, f(x) = c).
210  template <class RhsValueType>
211  OPM_HOST_DEVICE static Evaluation createConstant(const RhsValueType& value)
212  {
213  return Evaluation(value);
214  }
215 
216  // "evaluate" a constant function (i.e. a function that does not depend on the set of
217  // relevant variables, f(x) = c).
218  template <class RhsValueType>
219  OPM_HOST_DEVICE static Evaluation createConstant(const Evaluation&, const RhsValueType& value)
220  {
221  return Evaluation(value);
222  }
223 
224  // copy all derivatives from other
225  OPM_HOST_DEVICE void copyDerivatives(const Evaluation& other)
226  {
227  assert(size() == other.size());
228 
229  for (int i = dstart_(); i < dend_(); ++i)
230  data_[i] = other.data_[i];
231  }
232 
233 
234  // add value and derivatives from other to this value and derivatives
235  OPM_HOST_DEVICE Evaluation& operator+=(const Evaluation& other)
236  {
237  assert(size() == other.size());
238 
239  for (int i = 0; i < length_(); ++i)
240  data_[i] += other.data_[i];
241 
242  return *this;
243  }
244 
245  // add value from other to this values
246  template <class RhsValueType>
247  OPM_HOST_DEVICE Evaluation& operator+=(const RhsValueType& other)
248  {
249  // value is added, derivatives stay the same
250  data_[valuepos_()] += other;
251 
252  return *this;
253  }
254 
255  // subtract other's value and derivatives from this values
256  OPM_HOST_DEVICE Evaluation& operator-=(const Evaluation& other)
257  {
258  assert(size() == other.size());
259 
260  for (int i = 0; i < length_(); ++i)
261  data_[i] -= other.data_[i];
262 
263  return *this;
264  }
265 
266  // subtract other's value from this values
267  template <class RhsValueType>
268  OPM_HOST_DEVICE Evaluation& operator-=(const RhsValueType& other)
269  {
270  // for constants, values are subtracted, derivatives stay the same
271  data_[valuepos_()] -= other;
272 
273  return *this;
274  }
275 
276  // multiply values and apply chain rule to derivatives: (u*v)' = (v'u + u'v)
277  OPM_HOST_DEVICE Evaluation& operator*=(const Evaluation& other)
278  {
279  assert(size() == other.size());
280 
281  // while the values are multiplied, the derivatives follow the product rule,
282  // i.e., (u*v)' = (v'u + u'v).
283  const ValueType u = this->value();
284  const ValueType v = other.value();
285 
286  // value
287  data_[valuepos_()] *= v ;
288 
289  // derivatives
290  for (int i = dstart_(); i < dend_(); ++i)
291  data_[i] = data_[i] * v + other.data_[i] * u;
292 
293  return *this;
294  }
295 
296  // m(c*u)' = c*u'
297  template <class RhsValueType>
298  OPM_HOST_DEVICE Evaluation& operator*=(const RhsValueType& other)
299  {
300  for (int i = 0; i < length_(); ++i)
301  data_[i] *= other;
302 
303  return *this;
304  }
305 
306  // m(u*v)' = (vu' - uv')/v^2
307  OPM_HOST_DEVICE Evaluation& operator/=(const Evaluation& other)
308  {
309  assert(size() == other.size());
310 
311  // values are divided, derivatives follow the rule for division, i.e., (u/v)' = (v'u -
312  // u'v)/v^2.
313  ValueType& u = data_[valuepos_()];
314  const ValueType& v = other.value();
315  for (int idx = dstart_(); idx < dend_(); ++idx) {
316  const ValueType& uPrime = data_[idx];
317  const ValueType& vPrime = other.data_[idx];
318 
319  data_[idx] = (v*uPrime - u*vPrime)/(v*v);
320  }
321  u /= v;
322 
323  return *this;
324  }
325 
326  // divide value and derivatives by value of other
327  template <class RhsValueType>
328  OPM_HOST_DEVICE Evaluation& operator/=(const RhsValueType& other)
329  {
330  const ValueType tmp = 1.0/other;
331 
332  for (int i = 0; i < length_(); ++i)
333  data_[i] *= tmp;
334 
335  return *this;
336  }
337 
338  // add two evaluation objects
339  OPM_HOST_DEVICE Evaluation operator+(const Evaluation& other) const
340  {
341  assert(size() == other.size());
342 
343  Evaluation result(*this);
344 
345  result += other;
346 
347  return result;
348  }
349 
350  // add constant to this object
351  template <class RhsValueType>
352  OPM_HOST_DEVICE Evaluation operator+(const RhsValueType& other) const
353  {
354  Evaluation result(*this);
355 
356  result += other;
357 
358  return result;
359  }
360 
361  // subtract two evaluation objects
362  OPM_HOST_DEVICE Evaluation operator-(const Evaluation& other) const
363  {
364  assert(size() == other.size());
365 
366  Evaluation result(*this);
367 
368  result -= other;
369 
370  return result;
371  }
372 
373  // subtract constant from evaluation object
374  template <class RhsValueType>
375  OPM_HOST_DEVICE Evaluation operator-(const RhsValueType& other) const
376  {
377  Evaluation result(*this);
378 
379  result -= other;
380 
381  return result;
382  }
383 
384  // negation (unary minus) operator
385  OPM_HOST_DEVICE Evaluation operator-() const
386  {
387  Evaluation result;
388 
389  // set value and derivatives to negative
390  for (int i = 0; i < length_(); ++i)
391  result.data_[i] = - data_[i];
392 
393  return result;
394  }
395 
396  OPM_HOST_DEVICE Evaluation operator*(const Evaluation& other) const
397  {
398  assert(size() == other.size());
399 
400  Evaluation result(*this);
401 
402  result *= other;
403 
404  return result;
405  }
406 
407  template <class RhsValueType>
408  OPM_HOST_DEVICE Evaluation operator*(const RhsValueType& other) const
409  {
410  Evaluation result(*this);
411 
412  result *= other;
413 
414  return result;
415  }
416 
417  OPM_HOST_DEVICE Evaluation operator/(const Evaluation& other) const
418  {
419  assert(size() == other.size());
420 
421  Evaluation result(*this);
422 
423  result /= other;
424 
425  return result;
426  }
427 
428  template <class RhsValueType>
429  OPM_HOST_DEVICE Evaluation operator/(const RhsValueType& other) const
430  {
431  Evaluation result(*this);
432 
433  result /= other;
434 
435  return result;
436  }
437 
438  template <class RhsValueType>
439  OPM_HOST_DEVICE Evaluation& operator=(const RhsValueType& other)
440  {
441  setValue( other );
442  clearDerivatives();
443 
444  return *this;
445  }
446 
447  // copy assignment from evaluation
448  Evaluation& operator=(const Evaluation& other) = default;
449 
450  template <class RhsValueType>
451  OPM_HOST_DEVICE bool operator==(const RhsValueType& other) const
452  { return value() == other; }
453 
454  OPM_HOST_DEVICE bool operator==(const Evaluation& other) const
455  {
456  assert(size() == other.size());
457 
458  for (int idx = 0; idx < length_(); ++idx) {
459  if (data_[idx] != other.data_[idx]) {
460  return false;
461  }
462  }
463  return true;
464  }
465 
466  OPM_HOST_DEVICE bool operator!=(const Evaluation& other) const
467  { return !operator==(other); }
468 
469  template <class RhsValueType>
470  OPM_HOST_DEVICE bool operator!=(const RhsValueType& other) const
471  { return !operator==(other); }
472 
473  template <class RhsValueType>
474  OPM_HOST_DEVICE bool operator>(RhsValueType other) const
475  { return value() > other; }
476 
477  OPM_HOST_DEVICE bool operator>(const Evaluation& other) const
478  {
479  assert(size() == other.size());
480 
481  return value() > other.value();
482  }
483 
484  template <class RhsValueType>
485  OPM_HOST_DEVICE bool operator<(RhsValueType other) const
486  { return value() < other; }
487 
488  OPM_HOST_DEVICE bool operator<(const Evaluation& other) const
489  {
490  assert(size() == other.size());
491 
492  return value() < other.value();
493  }
494 
495  template <class RhsValueType>
496  OPM_HOST_DEVICE bool operator>=(RhsValueType other) const
497  { return value() >= other; }
498 
499  OPM_HOST_DEVICE bool operator>=(const Evaluation& other) const
500  {
501  assert(size() == other.size());
502 
503  return value() >= other.value();
504  }
505 
506  template <class RhsValueType>
507  OPM_HOST_DEVICE bool operator<=(RhsValueType other) const
508  { return value() <= other; }
509 
510  OPM_HOST_DEVICE bool operator<=(const Evaluation& other) const
511  {
512  assert(size() == other.size());
513 
514  return value() <= other.value();
515  }
516 
517  // return value of variable
518  OPM_HOST_DEVICE const ValueType& value() const
519  { return data_[valuepos_()]; }
520 
521  // set value of variable
522  template <class RhsValueType>
523  OPM_HOST_DEVICE constexpr void setValue(const RhsValueType& val)
524  { data_[valuepos_()] = val; }
525 
526  // return varIdx'th derivative
527  OPM_HOST_DEVICE const ValueType& derivative(int varIdx) const
528  {
529  assert(0 <= varIdx && varIdx < size());
530 
531  return data_[dstart_() + varIdx];
532  }
533 
534  // set derivative at position varIdx
535  OPM_HOST_DEVICE void setDerivative(int varIdx, const ValueType& derVal)
536  {
537  assert(0 <= varIdx && varIdx < size());
538 
539  data_[dstart_() + varIdx] = derVal;
540  }
541 
542  template<class Serializer>
543  OPM_HOST_DEVICE void serializeOp(Serializer& serializer)
544  {
545  serializer(data_);
546  }
547 
548 private:
549  std::array<ValueT, numDerivs + 1> data_;
550 };
551 
552 // the generic operators are only required for the unspecialized case
553 template <class RhsValueType, class ValueType, int numVars, unsigned staticSize>
554 OPM_HOST_DEVICE bool operator<(const RhsValueType& a, const Evaluation<ValueType, numVars, staticSize>& b)
555 { return b > a; }
556 
557 template <class RhsValueType, class ValueType, int numVars, unsigned staticSize>
558 OPM_HOST_DEVICE bool operator>(const RhsValueType& a, const Evaluation<ValueType, numVars, staticSize>& b)
559 { return b < a; }
560 
561 template <class RhsValueType, class ValueType, int numVars, unsigned staticSize>
562 OPM_HOST_DEVICE bool operator<=(const RhsValueType& a, const Evaluation<ValueType, numVars, staticSize>& b)
563 { return b >= a; }
564 
565 template <class RhsValueType, class ValueType, int numVars, unsigned staticSize>
566 OPM_HOST_DEVICE bool operator>=(const RhsValueType& a, const Evaluation<ValueType, numVars, staticSize>& b)
567 { return b <= a; }
568 
569 template <class RhsValueType, class ValueType, int numVars, unsigned staticSize>
570 OPM_HOST_DEVICE bool operator!=(const RhsValueType& a, const Evaluation<ValueType, numVars, staticSize>& b)
571 { return a != b.value(); }
572 
573 template <class RhsValueType, class ValueType, int numVars, unsigned staticSize>
574 OPM_HOST_DEVICE Evaluation<ValueType, numVars, staticSize> operator+(const RhsValueType& a, const Evaluation<ValueType, numVars, staticSize>& b)
575 {
576  Evaluation<ValueType, numVars, staticSize> result(b);
577  result += a;
578  return result;
579 }
580 
581 template <class RhsValueType, class ValueType, int numVars, unsigned staticSize>
582 OPM_HOST_DEVICE Evaluation<ValueType, numVars, staticSize> operator-(const RhsValueType& a, const Evaluation<ValueType, numVars, staticSize>& b)
583 {
584  return -(b - a);
585 }
586 
587 template <class RhsValueType, class ValueType, int numVars, unsigned staticSize>
588 OPM_HOST_DEVICE Evaluation<ValueType, numVars, staticSize> operator/(const RhsValueType& a, const Evaluation<ValueType, numVars, staticSize>& b)
589 {
590  Evaluation<ValueType, numVars, staticSize> tmp(a);
591  tmp /= b;
592  return tmp;
593 }
594 
595 template <class RhsValueType, class ValueType, int numVars, unsigned staticSize>
596 OPM_HOST_DEVICE Evaluation<ValueType, numVars, staticSize> operator*(const RhsValueType& a, const Evaluation<ValueType, numVars, staticSize>& b)
597 {
598  Evaluation<ValueType, numVars, staticSize> result(b);
599  result *= a;
600  return result;
601 }
602 
603 template<class T>
605 {
606  static constexpr bool value = false;
607 };
608 
609 template <class ValueType, int numVars, unsigned staticSize>
610 struct is_evaluation<Evaluation<ValueType,numVars,staticSize>>
611 {
612  static constexpr bool value = true;
613 };
614 
615 template <class ValueType, int numVars, unsigned staticSize>
616 OPM_HOST_DEVICE void printEvaluation(std::ostream& os,
618  bool withDer = false);
619 
620 template <class ValueType, int numVars, unsigned staticSize>
621 OPM_HOST_DEVICE std::ostream& operator<<(std::ostream& os, const Evaluation<ValueType, numVars, staticSize>& eval)
622 {
623  if constexpr (is_evaluation<ValueType>::value)
624  printEvaluation(os, eval.value(), false);
625  else
626  printEvaluation(os, eval, true);
627 
628  return os;
629 }
630 
631 } // namespace DenseAd
632 } // namespace Opm
633 
635 
636 #if HAVE_DUNE_COMMON
637 namespace Dune {
639  template <class ValueT, int numDerivs, unsigned staticSize>
640  struct IsNumber<Opm::DenseAd::Evaluation<ValueT,numDerivs,staticSize>>
641  : public std::integral_constant<bool, std::is_arithmetic<ValueT>::value> {
642  };
643 } // namespace Dune
644 #endif
645 
646 #endif // OPM_DENSEAD_EVALUATION_HPP
OPM_HOST_DEVICE constexpr int dstart_() const
start index for derivatives
Definition: Evaluation.hpp:87
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
Definition: SymmTensor.hpp:23
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
bool operator>(const SummaryConfigNode &lhs, const SummaryConfigNode &rhs)
Greater-than comparison operator for SummaryConfigNode objects.
Definition: SummaryConfig.hpp:302
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition: Exceptions.hpp:30
ValueT ValueType
field type
Definition: Evaluation.hpp:71
OPM_HOST_DEVICE constexpr int size() const
number of derivatives
Definition: Evaluation.hpp:74
bool operator>=(const SummaryConfigNode &lhs, const SummaryConfigNode &rhs)
Greater-than-or-equal comparison operator for SummaryConfigNode objects.
Definition: SummaryConfig.hpp:314
This file includes all specializations for the dense-AD Evaluation class.
static const int numVars
the template argument which specifies the number of derivatives (-1 == "DynamicSize" means runtime de...
Definition: Evaluation.hpp:68
Some templates to wrap the valgrind client request macros.
Represents a function evaluation and its derivatives w.r.t.
Definition: Evaluation.hpp:63
Definition: Evaluation.hpp:604
OPM_HOST_DEVICE constexpr int dend_() const
end+1 index for derivatives
Definition: Evaluation.hpp:90
bool operator!=(const SummaryConfigNode &lhs, const SummaryConfigNode &rhs)
Inequality operator for SummaryConfigNode objects.
Definition: SummaryConfig.hpp:278