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