opm-common
Evaluation3.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 */
31 #ifndef OPM_DENSEAD_EVALUATION3_HPP
32 #define OPM_DENSEAD_EVALUATION3_HPP
33 
34 #ifndef NDEBUG
36 #endif
37 
38 #include <array>
39 #include <cassert>
40 #include <iosfwd>
41 #include <stdexcept>
42 
43 #include <opm/common/ErrorMacros.hpp>
44 #include <opm/common/utility/gpuDecorators.hpp>
45 
46 namespace Opm {
47 namespace DenseAd {
48 
49 template <class ValueT>
50 class Evaluation<ValueT, 3>
51 {
52 public:
55  static const int numVars = 3;
56 
58  typedef ValueT ValueType;
59 
61  OPM_HOST_DEVICE constexpr int size() const
62  { return 3; };
63 
64 protected:
66  OPM_HOST_DEVICE constexpr int length_() const
67  { return size() + 1; }
68 
69 
71  OPM_HOST_DEVICE constexpr int valuepos_() const
72  { return 0; }
74  OPM_HOST_DEVICE constexpr int dstart_() const
75  { return 1; }
77  OPM_HOST_DEVICE constexpr int dend_() const
78  { return length_(); }
79 
82  OPM_HOST_DEVICE constexpr void checkDefined_() const
83  {
84 #ifndef NDEBUG
85  for (const auto& v: data_)
86  Valgrind::CheckDefined(v);
87 #endif
88  }
89 
90 public:
92  OPM_HOST_DEVICE Evaluation() : data_()
93  {}
94 
96  Evaluation(const Evaluation& other) = default;
97 
98 
99  // create an evaluation which represents a constant function
100  //
101  // i.e., f(x) = c. this implies an evaluation with the given value and all
102  // derivatives being zero.
103  template <class RhsValueType>
104  OPM_HOST_DEVICE constexpr Evaluation(const RhsValueType& c): data_{}
105  {
106  setValue(c);
107  clearDerivatives();
108 
109  //checkDefined_();
110  }
111 
112  // create an evaluation representing a variable with the variable position of varPos
113  // The value is set to c, all derivatives are zero except for the one at varPos, which is set to 1.
114  template <class RhsValueType>
115  OPM_HOST_DEVICE Evaluation(const RhsValueType& c, int varPos)
116  {
117  // The variable position must be in represented by the given variable descriptor
118  assert(0 <= varPos && varPos < size());
119 
120  setValue( c );
121  clearDerivatives();
122 
123  data_[varPos + dstart_()] = 1.0;
124 
125  checkDefined_();
126  }
127 
128  // set all derivatives to zero
129  OPM_HOST_DEVICE constexpr void clearDerivatives()
130  {
131  data_[1] = 0.0;
132  data_[2] = 0.0;
133  data_[3] = 0.0;
134  }
135 
136  // create an uninitialized Evaluation object that is compatible with the
137  // argument, but not initialized
138  //
139  // This basically boils down to the copy constructor without copying
140  // anything. If the number of derivatives is known at compile time, this
141  // is equivalent to creating an uninitialized object using the default
142  // constructor, while for dynamic evaluations, it creates an Evaluation
143  // object which exhibits the same number of derivatives as the argument.
144  OPM_HOST_DEVICE static Evaluation createBlank(const Evaluation&)
145  { return Evaluation(); }
146 
147  // create an Evaluation with value and all the derivatives to be zero
148  OPM_HOST_DEVICE static Evaluation createConstantZero(const Evaluation&)
149  { return Evaluation(0.); }
150 
151  // create an Evaluation with value to be one and all the derivatives to be zero
152  OPM_HOST_DEVICE static Evaluation createConstantOne(const Evaluation&)
153  { return Evaluation(1.); }
154 
155  // create a function evaluation for a "naked" depending variable (i.e., f(x) = x)
156  template <class RhsValueType>
157  OPM_HOST_DEVICE static Evaluation createVariable(const RhsValueType& value, int varPos)
158  {
159  // copy function value and set all derivatives to 0, except for the variable
160  // which is represented by the value (which is set to 1.0)
161  return Evaluation(value, varPos);
162  }
163 
164  template <class RhsValueType>
165  OPM_HOST_DEVICE static Evaluation createVariable(int nVars, const RhsValueType& value, int varPos)
166  {
167  if (nVars != 3)
168  throw std::logic_error("This statically-sized evaluation can only represent objects"
169  " with 3 derivatives");
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(nVars, value, varPos);
174  }
175 
176  template <class RhsValueType>
177  OPM_HOST_DEVICE static Evaluation createVariable(const Evaluation&, const RhsValueType& value, int varPos)
178  {
179  // copy function value and set all derivatives to 0, except for the variable
180  // which is represented by the value (which is set to 1.0)
181  return Evaluation(value, varPos);
182  }
183 
184 
185  // "evaluate" a constant function (i.e. a function that does not depend on the set of
186  // relevant variables, f(x) = c).
187  template <class RhsValueType>
188  OPM_HOST_DEVICE static Evaluation createConstant(int nVars, const RhsValueType& value)
189  {
190  if (nVars != 3)
191  throw std::logic_error("This statically-sized evaluation can only represent objects"
192  " with 3 derivatives");
193  return Evaluation(value);
194  }
195 
196  // "evaluate" a constant function (i.e. a function that does not depend on the set of
197  // relevant variables, f(x) = c).
198  template <class RhsValueType>
199  OPM_HOST_DEVICE static Evaluation createConstant(const RhsValueType& value)
200  {
201  return Evaluation(value);
202  }
203 
204  // "evaluate" a constant function (i.e. a function that does not depend on the set of
205  // relevant variables, f(x) = c).
206  template <class RhsValueType>
207  OPM_HOST_DEVICE static Evaluation createConstant(const Evaluation&, const RhsValueType& value)
208  {
209  return Evaluation(value);
210  }
211 
212  // copy all derivatives from other
213  OPM_HOST_DEVICE void copyDerivatives(const Evaluation& other)
214  {
215  assert(size() == other.size());
216 
217  data_[1] = other.data_[1];
218  data_[2] = other.data_[2];
219  data_[3] = other.data_[3];
220  }
221 
222 
223  // add value and derivatives from other to this value and derivatives
224  OPM_HOST_DEVICE Evaluation& operator+=(const Evaluation& other)
225  {
226  assert(size() == other.size());
227 
228  data_[0] += other.data_[0];
229  data_[1] += other.data_[1];
230  data_[2] += other.data_[2];
231  data_[3] += other.data_[3];
232 
233  return *this;
234  }
235 
236  // add value from other to this values
237  template <class RhsValueType>
238  OPM_HOST_DEVICE Evaluation& operator+=(const RhsValueType& other)
239  {
240  // value is added, derivatives stay the same
241  data_[valuepos_()] += other;
242 
243  return *this;
244  }
245 
246  // subtract other's value and derivatives from this values
247  OPM_HOST_DEVICE Evaluation& operator-=(const Evaluation& other)
248  {
249  assert(size() == other.size());
250 
251  data_[0] -= other.data_[0];
252  data_[1] -= other.data_[1];
253  data_[2] -= other.data_[2];
254  data_[3] -= other.data_[3];
255 
256  return *this;
257  }
258 
259  // subtract other's value from this values
260  template <class RhsValueType>
261  OPM_HOST_DEVICE Evaluation& operator-=(const RhsValueType& other)
262  {
263  // for constants, values are subtracted, derivatives stay the same
264  data_[valuepos_()] -= other;
265 
266  return *this;
267  }
268 
269  // multiply values and apply chain rule to derivatives: (u*v)' = (v'u + u'v)
270  OPM_HOST_DEVICE Evaluation& operator*=(const Evaluation& other)
271  {
272  assert(size() == other.size());
273 
274  // while the values are multiplied, the derivatives follow the product rule,
275  // i.e., (u*v)' = (v'u + u'v).
276  const ValueType u = this->value();
277  const ValueType v = other.value();
278 
279  // value
280  data_[valuepos_()] *= v ;
281 
282  // derivatives
283  data_[1] = data_[1] * v + other.data_[1] * u;
284  data_[2] = data_[2] * v + other.data_[2] * u;
285  data_[3] = data_[3] * v + other.data_[3] * u;
286 
287  return *this;
288  }
289 
290  // m(c*u)' = c*u'
291  template <class RhsValueType>
292  OPM_HOST_DEVICE Evaluation& operator*=(const RhsValueType& other)
293  {
294  data_[0] *= other;
295  data_[1] *= other;
296  data_[2] *= other;
297  data_[3] *= other;
298 
299  return *this;
300  }
301 
302  // m(u*v)' = (vu' - uv')/v^2
303  OPM_HOST_DEVICE Evaluation& operator/=(const Evaluation& other)
304  {
305  assert(size() == other.size());
306 
307  // values are divided, derivatives follow the rule for division, i.e., (u/v)' = (v'u -
308  // u'v)/v^2.
309  ValueType& u = data_[valuepos_()];
310  const ValueType& v = other.value();
311  data_[1] = (v*data_[1] - u*other.data_[1])/(v*v);
312  data_[2] = (v*data_[2] - u*other.data_[2])/(v*v);
313  data_[3] = (v*data_[3] - u*other.data_[3])/(v*v);
314  u /= v;
315 
316  return *this;
317  }
318 
319  // divide value and derivatives by value of other
320  template <class RhsValueType>
321  OPM_HOST_DEVICE Evaluation& operator/=(const RhsValueType& other)
322  {
323  const ValueType tmp = 1.0/other;
324 
325  data_[0] *= tmp;
326  data_[1] *= tmp;
327  data_[2] *= tmp;
328  data_[3] *= tmp;
329 
330  return *this;
331  }
332 
333  // add two evaluation objects
334  OPM_HOST_DEVICE Evaluation operator+(const Evaluation& other) const
335  {
336  assert(size() == other.size());
337 
338  Evaluation result(*this);
339 
340  result += other;
341 
342  return result;
343  }
344 
345  // add constant to this object
346  template <class RhsValueType>
347  OPM_HOST_DEVICE Evaluation operator+(const RhsValueType& other) const
348  {
349  Evaluation result(*this);
350 
351  result += other;
352 
353  return result;
354  }
355 
356  // subtract two evaluation objects
357  OPM_HOST_DEVICE Evaluation operator-(const Evaluation& other) const
358  {
359  assert(size() == other.size());
360 
361  Evaluation result(*this);
362 
363  result -= other;
364 
365  return result;
366  }
367 
368  // subtract constant from evaluation object
369  template <class RhsValueType>
370  OPM_HOST_DEVICE Evaluation operator-(const RhsValueType& other) const
371  {
372  Evaluation result(*this);
373 
374  result -= other;
375 
376  return result;
377  }
378 
379  // negation (unary minus) operator
380  OPM_HOST_DEVICE Evaluation operator-() const
381  {
382  Evaluation result;
383 
384  // set value and derivatives to negative
385  result.data_[0] = - data_[0];
386  result.data_[1] = - data_[1];
387  result.data_[2] = - data_[2];
388  result.data_[3] = - data_[3];
389 
390  return result;
391  }
392 
393  OPM_HOST_DEVICE Evaluation operator*(const Evaluation& other) const
394  {
395  assert(size() == other.size());
396 
397  Evaluation result(*this);
398 
399  result *= other;
400 
401  return result;
402  }
403 
404  template <class RhsValueType>
405  OPM_HOST_DEVICE Evaluation operator*(const RhsValueType& other) const
406  {
407  Evaluation result(*this);
408 
409  result *= other;
410 
411  return result;
412  }
413 
414  OPM_HOST_DEVICE Evaluation operator/(const Evaluation& other) const
415  {
416  assert(size() == other.size());
417 
418  Evaluation result(*this);
419 
420  result /= other;
421 
422  return result;
423  }
424 
425  template <class RhsValueType>
426  OPM_HOST_DEVICE Evaluation operator/(const RhsValueType& other) const
427  {
428  Evaluation result(*this);
429 
430  result /= other;
431 
432  return result;
433  }
434 
435  template <class RhsValueType>
436  OPM_HOST_DEVICE Evaluation& operator=(const RhsValueType& other)
437  {
438  setValue( other );
439  clearDerivatives();
440 
441  return *this;
442  }
443 
444  // copy assignment from evaluation
445  Evaluation& operator=(const Evaluation& other) = default;
446 
447  template <class RhsValueType>
448  OPM_HOST_DEVICE bool operator==(const RhsValueType& other) const
449  { return value() == other; }
450 
451  OPM_HOST_DEVICE bool operator==(const Evaluation& other) const
452  {
453  assert(size() == other.size());
454 
455  for (int idx = 0; idx < length_(); ++idx) {
456  if (data_[idx] != other.data_[idx]) {
457  return false;
458  }
459  }
460  return true;
461  }
462 
463  OPM_HOST_DEVICE bool operator!=(const Evaluation& other) const
464  { return !operator==(other); }
465 
466  template <class RhsValueType>
467  OPM_HOST_DEVICE bool operator!=(const RhsValueType& other) const
468  { return !operator==(other); }
469 
470  template <class RhsValueType>
471  OPM_HOST_DEVICE bool operator>(RhsValueType other) const
472  { return value() > other; }
473 
474  OPM_HOST_DEVICE bool operator>(const Evaluation& other) const
475  {
476  assert(size() == other.size());
477 
478  return value() > other.value();
479  }
480 
481  template <class RhsValueType>
482  OPM_HOST_DEVICE bool operator<(RhsValueType other) const
483  { return value() < other; }
484 
485  OPM_HOST_DEVICE bool operator<(const Evaluation& other) const
486  {
487  assert(size() == other.size());
488 
489  return value() < other.value();
490  }
491 
492  template <class RhsValueType>
493  OPM_HOST_DEVICE bool operator>=(RhsValueType other) const
494  { return value() >= other; }
495 
496  OPM_HOST_DEVICE bool operator>=(const Evaluation& other) const
497  {
498  assert(size() == other.size());
499 
500  return value() >= other.value();
501  }
502 
503  template <class RhsValueType>
504  OPM_HOST_DEVICE bool operator<=(RhsValueType other) const
505  { return value() <= other; }
506 
507  OPM_HOST_DEVICE bool operator<=(const Evaluation& other) const
508  {
509  assert(size() == other.size());
510 
511  return value() <= other.value();
512  }
513 
514  // return value of variable
515  OPM_HOST_DEVICE const ValueType& value() const
516  { return data_[valuepos_()]; }
517 
518  // set value of variable
519  template <class RhsValueType>
520  OPM_HOST_DEVICE constexpr void setValue(const RhsValueType& val)
521  { data_[valuepos_()] = val; }
522 
523  // return varIdx'th derivative
524  OPM_HOST_DEVICE const ValueType& derivative(int varIdx) const
525  {
526  assert(0 <= varIdx && varIdx < size());
527 
528  return data_[dstart_() + varIdx];
529  }
530 
531  // set derivative at position varIdx
532  OPM_HOST_DEVICE void setDerivative(int varIdx, const ValueType& derVal)
533  {
534  assert(0 <= varIdx && varIdx < size());
535 
536  data_[dstart_() + varIdx] = derVal;
537  }
538 
539  template<class Serializer>
540  OPM_HOST_DEVICE void serializeOp(Serializer& serializer)
541  {
542  serializer(data_);
543  }
544 
545 private:
546  std::array<ValueT, 4> data_;
547 };
548 
549 } // namespace DenseAd
550 } // namespace Opm
551 
552 #endif // OPM_DENSEAD_EVALUATION3_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
OPM_HOST_DEVICE constexpr int length_() const
length of internal data vector
Definition: Evaluation3.hpp:66
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
OPM_HOST_DEVICE constexpr int dstart_() const
start index for derivatives
Definition: Evaluation3.hpp:74
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
OPM_HOST_DEVICE constexpr int dend_() const
end+1 index for derivatives
Definition: Evaluation3.hpp:77
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 constexpr int valuepos_() const
position index for value
Definition: Evaluation3.hpp:71
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: Evaluation3.hpp:82
ValueT ValueType
field type
Definition: Evaluation3.hpp:58
OPM_HOST_DEVICE constexpr int size() const
number of derivatives
Definition: Evaluation3.hpp:61
OPM_HOST_DEVICE Evaluation()
default constructor
Definition: Evaluation3.hpp:92
Some templates to wrap the valgrind client request macros.
Represents a function evaluation and its derivatives w.r.t.
Definition: Evaluation.hpp:63