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