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