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