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