All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Evaluation6.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_EVALUATION6_HPP
32 #define OPM_DENSEAD_EVALUATION6_HPP
33 
34 #include "Evaluation.hpp"
35 #include "Math.hpp"
36 
37 #include <opm/common/Valgrind.hpp>
38 
39 #include <dune/common/version.hh>
40 
41 #include <array>
42 #include <cmath>
43 #include <cassert>
44 #include <cstring>
45 #include <iostream>
46 #include <algorithm>
47 
48 namespace Opm {
49 namespace DenseAd {
50 
51 template <class ValueT>
52 class Evaluation<ValueT, 6>
53 {
54 public:
56  typedef ValueT ValueType;
57 
59  static constexpr int size = 6;
60 
61 protected:
63  static constexpr int length_ = size + 1;
64 
66  static constexpr int valuepos_ = 0;
68  static constexpr int dstart_ = 1;
70  static constexpr int dend_ = length_;
71 
72 public:
74  Evaluation() : data_()
75  {}
76 
78  Evaluation(const Evaluation& other) = default;
79 
80  // create an evaluation which represents a constant function
81  //
82  // i.e., f(x) = c. this implies an evaluation with the given value and all
83  // derivatives being zero.
84  template <class RhsValueType>
85  Evaluation(const RhsValueType& c)
86  {
87  setValue( c );
88  clearDerivatives();
89  Valgrind::CheckDefined( data_ );
90  }
91 
92  // create an evaluation which represents a constant function
93  //
94  // i.e., f(x) = c. this implies an evaluation with the given value and all
95  // derivatives being zero.
96  template <class RhsValueType>
97  Evaluation(const RhsValueType& c, int varPos)
98  {
99  // The variable position must be in represented by the given variable descriptor
100  assert(0 <= varPos && varPos < size);
101 
102  setValue( c );
103  clearDerivatives();
104 
105  data_[varPos + dstart_] = 1.0;
106  Valgrind::CheckDefined(data_);
107  }
108 
109  // set all derivatives to zero
110  void clearDerivatives()
111  {
112  data_[1] = 0.0;
113  data_[2] = 0.0;
114  data_[3] = 0.0;
115  data_[4] = 0.0;
116  data_[5] = 0.0;
117  data_[6] = 0.0;
118  }
119 
120  // create a function evaluation for a "naked" depending variable (i.e., f(x) = x)
121  template <class RhsValueType>
122  static Evaluation createVariable(const RhsValueType& value, int varPos)
123  {
124  // copy function value and set all derivatives to 0, except for the variable
125  // which is represented by the value (which is set to 1.0)
126  return Evaluation( value, varPos );
127  }
128 
129  // "evaluate" a constant function (i.e. a function that does not depend on the set of
130  // relevant variables, f(x) = c).
131  template <class RhsValueType>
132  static Evaluation createConstant(const RhsValueType& value)
133  {
134  return Evaluation( value );
135  }
136 
137  // print the value and the derivatives of the function evaluation
138  void print(std::ostream& os = std::cout) const
139  {
140  // print value
141  os << "v: " << value() << " / d:";
142 
143  // print derivatives
144  for (int varIdx = 0; varIdx < size; ++varIdx) {
145  os << " " << derivative(varIdx);
146  }
147  }
148 
149  // copy all derivatives from other
150  void copyDerivatives(const Evaluation& other)
151  {
152  data_[1] = other.data_[1];
153  data_[2] = other.data_[2];
154  data_[3] = other.data_[3];
155  data_[4] = other.data_[4];
156  data_[5] = other.data_[5];
157  data_[6] = other.data_[6];
158  }
159 
160 
161  // add value and derivatives from other to this values and derivatives
162  Evaluation& operator+=(const Evaluation& other)
163  {
164  data_[0] += other.data_[0];
165  data_[1] += other.data_[1];
166  data_[2] += other.data_[2];
167  data_[3] += other.data_[3];
168  data_[4] += other.data_[4];
169  data_[5] += other.data_[5];
170  data_[6] += other.data_[6];
171 
172  return *this;
173  }
174 
175  // add value from other to this values
176  template <class RhsValueType>
177  Evaluation& operator+=(const RhsValueType& other)
178  {
179  // value is added, derivatives stay the same
180  data_[valuepos_] += other;
181 
182  return *this;
183  }
184 
185  // subtract other's value and derivatives from this values
186  Evaluation& operator-=(const Evaluation& other)
187  {
188  data_[0] -= other.data_[0];
189  data_[1] -= other.data_[1];
190  data_[2] -= other.data_[2];
191  data_[3] -= other.data_[3];
192  data_[4] -= other.data_[4];
193  data_[5] -= other.data_[5];
194  data_[6] -= other.data_[6];
195 
196  return *this;
197  }
198 
199  // subtract other's value from this values
200  template <class RhsValueType>
201  Evaluation& operator-=(const RhsValueType& other)
202  {
203  // for constants, values are subtracted, derivatives stay the same
204  data_[ valuepos_ ] -= other;
205 
206  return *this;
207  }
208 
209  // multiply values and apply chain rule to derivatives: (u*v)' = (v'u + u'v)
210  Evaluation& operator*=(const Evaluation& other)
211  {
212  // while the values are multiplied, the derivatives follow the product rule,
213  // i.e., (u*v)' = (v'u + u'v).
214  const ValueType u = this->value();
215  const ValueType v = other.value();
216 
217  // value
218  data_[valuepos_] *= v ;
219 
220  // derivatives
221  data_[1] = data_[1] * v + other.data_[1] * u;
222  data_[2] = data_[2] * v + other.data_[2] * u;
223  data_[3] = data_[3] * v + other.data_[3] * u;
224  data_[4] = data_[4] * v + other.data_[4] * u;
225  data_[5] = data_[5] * v + other.data_[5] * u;
226  data_[6] = data_[6] * v + other.data_[6] * u;
227 
228  return *this;
229  }
230 
231  // m(c*u)' = c*u'
232  template <class RhsValueType>
233  Evaluation& operator*=(const RhsValueType& other)
234  {
235  data_[0] *= other;
236  data_[1] *= other;
237  data_[2] *= other;
238  data_[3] *= other;
239  data_[4] *= other;
240  data_[5] *= other;
241  data_[6] *= other;
242 
243  return *this;
244  }
245 
246  // m(u*v)' = (vu' - uv')/v^2
247  Evaluation& operator/=(const Evaluation& other)
248  {
249  // values are divided, derivatives follow the rule for division, i.e., (u/v)' = (v'u -
250  // u'v)/v^2.
251  ValueType& u = data_[ valuepos_ ];
252  const ValueType& v = other.value();
253  data_[1] = (v*data_[1] - u*other.data_[1])/(v*v);
254  data_[2] = (v*data_[2] - u*other.data_[2])/(v*v);
255  data_[3] = (v*data_[3] - u*other.data_[3])/(v*v);
256  data_[4] = (v*data_[4] - u*other.data_[4])/(v*v);
257  data_[5] = (v*data_[5] - u*other.data_[5])/(v*v);
258  data_[6] = (v*data_[6] - u*other.data_[6])/(v*v);
259  u /= v;
260 
261  return *this;
262  }
263 
264  // divide value and derivatives by value of other
265  template <class RhsValueType>
266  Evaluation& operator/=(const RhsValueType& other)
267  {
268  const ValueType tmp = 1.0/other;
269 
270  data_[0] *= tmp;
271  data_[1] *= tmp;
272  data_[2] *= tmp;
273  data_[3] *= tmp;
274  data_[4] *= tmp;
275  data_[5] *= tmp;
276  data_[6] *= tmp;
277 
278  return *this;
279  }
280 
281  // add two evaluation objects
282  Evaluation operator+(const Evaluation& other) const
283  {
284  Evaluation result(*this);
285 
286  result += other;
287 
288  return result;
289  }
290 
291  // add constant to this object
292  template <class RhsValueType>
293  Evaluation operator+(const RhsValueType& other) const
294  {
295  Evaluation result(*this);
296 
297  result += other;
298 
299  return result;
300  }
301 
302  // subtract two evaluation objects
303  Evaluation operator-(const Evaluation& other) const
304  {
305  Evaluation result(*this);
306 
307  result -= other;
308 
309  return result;
310  }
311 
312  // subtract constant from evaluation object
313  template <class RhsValueType>
314  Evaluation operator-(const RhsValueType& other) const
315  {
316  Evaluation result(*this);
317 
318  result -= other;
319 
320  return result;
321  }
322 
323  // negation (unary minus) operator
324  Evaluation operator-() const
325  {
326  Evaluation result;
327 
328  // set value and derivatives to negative
329  result.data_[0] = - data_[0];
330  result.data_[1] = - data_[1];
331  result.data_[2] = - data_[2];
332  result.data_[3] = - data_[3];
333  result.data_[4] = - data_[4];
334  result.data_[5] = - data_[5];
335  result.data_[6] = - data_[6];
336 
337  return result;
338  }
339 
340  Evaluation operator*(const Evaluation& other) const
341  {
342  Evaluation result(*this);
343 
344  result *= other;
345 
346  return result;
347  }
348 
349  template <class RhsValueType>
350  Evaluation operator*(const RhsValueType& other) const
351  {
352  Evaluation result(*this);
353 
354  result *= other;
355 
356  return result;
357  }
358 
359  Evaluation operator/(const Evaluation& other) const
360  {
361  Evaluation result(*this);
362 
363  result /= other;
364 
365  return result;
366  }
367 
368  template <class RhsValueType>
369  Evaluation operator/(const RhsValueType& other) const
370  {
371  Evaluation result(*this);
372 
373  result /= other;
374 
375  return result;
376  }
377 
378  template <class RhsValueType>
379  Evaluation& operator=(const RhsValueType& other)
380  {
381  setValue( other );
382  clearDerivatives();
383 
384  return *this;
385  }
386 
387  // copy assignment from evaluation
388  Evaluation& operator=(const Evaluation& other) = default;
389 
390  template <class RhsValueType>
391  bool operator==(const RhsValueType& other) const
392  { return value() == other; }
393 
394  bool operator==(const Evaluation& other) const
395  {
396  for (int idx = 0; idx < length_; ++idx) {
397  if (data_[idx] != other.data_[idx]) {
398  return false;
399  }
400  }
401  return true;
402  }
403 
404  bool operator!=(const Evaluation& other) const
405  { return !operator==(other); }
406 
407  template <class RhsValueType>
408  bool operator>(RhsValueType other) const
409  { return value() > other; }
410 
411  bool operator>(const Evaluation& other) const
412  { return value() > other.value(); }
413 
414  template <class RhsValueType>
415  bool operator<(RhsValueType other) const
416  { return value() < other; }
417 
418  bool operator<(const Evaluation& other) const
419  { return value() < other.value(); }
420 
421  template <class RhsValueType>
422  bool operator>=(RhsValueType other) const
423  { return value() >= other; }
424 
425  bool operator>=(const Evaluation& other) const
426  { return value() >= other.value(); }
427 
428  template <class RhsValueType>
429  bool operator<=(RhsValueType other) const
430  { return value() <= other; }
431 
432  bool operator<=(const Evaluation& other) const
433  { return value() <= other.value(); }
434 
435  // return value of variable
436  const ValueType& value() const
437  { return data_[valuepos_]; }
438 
439  // set value of variable
440  template <class RhsValueType>
441  void setValue(const RhsValueType& val)
442  { data_[valuepos_] = val; }
443 
444  // return varIdx'th derivative
445  const ValueType& derivative(int varIdx) const
446  {
447  assert(0 <= varIdx && varIdx < size);
448 
449  return data_[dstart_ + varIdx];
450  }
451 
452  // set derivative at position varIdx
453  void setDerivative(int varIdx, const ValueType& derVal)
454  {
455  assert(0 <= varIdx && varIdx < size);
456 
457  data_[dstart_ + varIdx] = derVal;
458  }
459 
460 private:
461  std::array<ValueT, length_> data_;
462 };
463 
464 } } // namespace DenseAd, Opm
465 
466 #endif // OPM_DENSEAD_EVALUATION6_HPP
Evaluation()
default constructor
Definition: Evaluation.hpp:79
A number of commonly used algebraic functions for the localized OPM automatic differentiation (AD) fr...
ValueT ValueType
field type
Definition: Evaluation.hpp:61
static constexpr int valuepos_
position index for value
Definition: Evaluation.hpp:71
static constexpr int length_
length of internal data vector
Definition: Evaluation.hpp:68
static constexpr int dstart_
start index for derivatives
Definition: Evaluation.hpp:73
Evaluation()
default constructor
Definition: Evaluation6.hpp:74
Represents a function evaluation and its derivatives w.r.t.
Definition: Evaluation.hpp:57
static constexpr int size
number of derivatives
Definition: Evaluation.hpp:64
static constexpr int dend_
end+1 index for derivatives
Definition: Evaluation.hpp:75
ValueT ValueType
field type
Definition: Evaluation6.hpp:56
Representation of an evaluation of a function and its derivatives w.r.t.