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