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