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