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