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