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