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