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