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