All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Evaluation10.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_EVALUATION10_HPP
32 #define OPM_DENSEAD_EVALUATION10_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, 10>
53 {
54 public:
56  typedef ValueT ValueType;
57 
59  static constexpr int size = 10;
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  data_[7] = 0.0;
119  data_[8] = 0.0;
120  data_[9] = 0.0;
121  data_[10] = 0.0;
122  }
123 
124  // create a function evaluation for a "naked" depending variable (i.e., f(x) = x)
125  template <class RhsValueType>
126  static Evaluation createVariable(const RhsValueType& value, int varPos)
127  {
128  // copy function value and set all derivatives to 0, except for the variable
129  // which is represented by the value (which is set to 1.0)
130  return Evaluation( value, varPos );
131  }
132 
133  // "evaluate" a constant function (i.e. a function that does not depend on the set of
134  // relevant variables, f(x) = c).
135  template <class RhsValueType>
136  static Evaluation createConstant(const RhsValueType& value)
137  {
138  return Evaluation( value );
139  }
140 
141  // print the value and the derivatives of the function evaluation
142  void print(std::ostream& os = std::cout) const
143  {
144  // print value
145  os << "v: " << value() << " / d:";
146 
147  // print derivatives
148  for (int varIdx = 0; varIdx < size; ++varIdx) {
149  os << " " << derivative(varIdx);
150  }
151  }
152 
153  // copy all derivatives from other
154  void copyDerivatives(const Evaluation& other)
155  {
156  data_[1] = other.data_[1];
157  data_[2] = other.data_[2];
158  data_[3] = other.data_[3];
159  data_[4] = other.data_[4];
160  data_[5] = other.data_[5];
161  data_[6] = other.data_[6];
162  data_[7] = other.data_[7];
163  data_[8] = other.data_[8];
164  data_[9] = other.data_[9];
165  data_[10] = other.data_[10];
166  }
167 
168 
169  // add value and derivatives from other to this values and derivatives
170  Evaluation& operator+=(const Evaluation& other)
171  {
172  data_[0] += other.data_[0];
173  data_[1] += other.data_[1];
174  data_[2] += other.data_[2];
175  data_[3] += other.data_[3];
176  data_[4] += other.data_[4];
177  data_[5] += other.data_[5];
178  data_[6] += other.data_[6];
179  data_[7] += other.data_[7];
180  data_[8] += other.data_[8];
181  data_[9] += other.data_[9];
182  data_[10] += other.data_[10];
183 
184  return *this;
185  }
186 
187  // add value from other to this values
188  template <class RhsValueType>
189  Evaluation& operator+=(const RhsValueType& other)
190  {
191  // value is added, derivatives stay the same
192  data_[valuepos_] += other;
193 
194  return *this;
195  }
196 
197  // subtract other's value and derivatives from this values
198  Evaluation& operator-=(const Evaluation& other)
199  {
200  data_[0] -= other.data_[0];
201  data_[1] -= other.data_[1];
202  data_[2] -= other.data_[2];
203  data_[3] -= other.data_[3];
204  data_[4] -= other.data_[4];
205  data_[5] -= other.data_[5];
206  data_[6] -= other.data_[6];
207  data_[7] -= other.data_[7];
208  data_[8] -= other.data_[8];
209  data_[9] -= other.data_[9];
210  data_[10] -= other.data_[10];
211 
212  return *this;
213  }
214 
215  // subtract other's value from this values
216  template <class RhsValueType>
217  Evaluation& operator-=(const RhsValueType& other)
218  {
219  // for constants, values are subtracted, derivatives stay the same
220  data_[ valuepos_ ] -= other;
221 
222  return *this;
223  }
224 
225  // multiply values and apply chain rule to derivatives: (u*v)' = (v'u + u'v)
226  Evaluation& operator*=(const Evaluation& other)
227  {
228  // while the values are multiplied, the derivatives follow the product rule,
229  // i.e., (u*v)' = (v'u + u'v).
230  const ValueType u = this->value();
231  const ValueType v = other.value();
232 
233  // value
234  data_[valuepos_] *= v ;
235 
236  // derivatives
237  data_[1] = data_[1] * v + other.data_[1] * u;
238  data_[2] = data_[2] * v + other.data_[2] * u;
239  data_[3] = data_[3] * v + other.data_[3] * u;
240  data_[4] = data_[4] * v + other.data_[4] * u;
241  data_[5] = data_[5] * v + other.data_[5] * u;
242  data_[6] = data_[6] * v + other.data_[6] * u;
243  data_[7] = data_[7] * v + other.data_[7] * u;
244  data_[8] = data_[8] * v + other.data_[8] * u;
245  data_[9] = data_[9] * v + other.data_[9] * u;
246  data_[10] = data_[10] * v + other.data_[10] * u;
247 
248  return *this;
249  }
250 
251  // m(c*u)' = c*u'
252  template <class RhsValueType>
253  Evaluation& operator*=(const RhsValueType& other)
254  {
255  data_[0] *= other;
256  data_[1] *= other;
257  data_[2] *= other;
258  data_[3] *= other;
259  data_[4] *= other;
260  data_[5] *= other;
261  data_[6] *= other;
262  data_[7] *= other;
263  data_[8] *= other;
264  data_[9] *= other;
265  data_[10] *= other;
266 
267  return *this;
268  }
269 
270  // m(u*v)' = (vu' - uv')/v^2
271  Evaluation& operator/=(const Evaluation& other)
272  {
273  // values are divided, derivatives follow the rule for division, i.e., (u/v)' = (v'u -
274  // u'v)/v^2.
275  ValueType& u = data_[ valuepos_ ];
276  const ValueType& v = other.value();
277  data_[1] = (v*data_[1] - u*other.data_[1])/(v*v);
278  data_[2] = (v*data_[2] - u*other.data_[2])/(v*v);
279  data_[3] = (v*data_[3] - u*other.data_[3])/(v*v);
280  data_[4] = (v*data_[4] - u*other.data_[4])/(v*v);
281  data_[5] = (v*data_[5] - u*other.data_[5])/(v*v);
282  data_[6] = (v*data_[6] - u*other.data_[6])/(v*v);
283  data_[7] = (v*data_[7] - u*other.data_[7])/(v*v);
284  data_[8] = (v*data_[8] - u*other.data_[8])/(v*v);
285  data_[9] = (v*data_[9] - u*other.data_[9])/(v*v);
286  data_[10] = (v*data_[10] - u*other.data_[10])/(v*v);
287  u /= v;
288 
289  return *this;
290  }
291 
292  // divide value and derivatives by value of other
293  template <class RhsValueType>
294  Evaluation& operator/=(const RhsValueType& other)
295  {
296  const ValueType tmp = 1.0/other;
297 
298  data_[0] *= tmp;
299  data_[1] *= tmp;
300  data_[2] *= tmp;
301  data_[3] *= tmp;
302  data_[4] *= tmp;
303  data_[5] *= tmp;
304  data_[6] *= tmp;
305  data_[7] *= tmp;
306  data_[8] *= tmp;
307  data_[9] *= tmp;
308  data_[10] *= tmp;
309 
310  return *this;
311  }
312 
313  // add two evaluation objects
314  Evaluation operator+(const Evaluation& other) const
315  {
316  Evaluation result(*this);
317 
318  result += other;
319 
320  return result;
321  }
322 
323  // add constant to this object
324  template <class RhsValueType>
325  Evaluation operator+(const RhsValueType& other) const
326  {
327  Evaluation result(*this);
328 
329  result += other;
330 
331  return result;
332  }
333 
334  // subtract two evaluation objects
335  Evaluation operator-(const Evaluation& other) const
336  {
337  Evaluation result(*this);
338 
339  result -= other;
340 
341  return result;
342  }
343 
344  // subtract constant from evaluation object
345  template <class RhsValueType>
346  Evaluation operator-(const RhsValueType& other) const
347  {
348  Evaluation result(*this);
349 
350  result -= other;
351 
352  return result;
353  }
354 
355  // negation (unary minus) operator
356  Evaluation operator-() const
357  {
358  Evaluation result;
359 
360  // set value and derivatives to negative
361  result.data_[0] = - data_[0];
362  result.data_[1] = - data_[1];
363  result.data_[2] = - data_[2];
364  result.data_[3] = - data_[3];
365  result.data_[4] = - data_[4];
366  result.data_[5] = - data_[5];
367  result.data_[6] = - data_[6];
368  result.data_[7] = - data_[7];
369  result.data_[8] = - data_[8];
370  result.data_[9] = - data_[9];
371  result.data_[10] = - data_[10];
372 
373  return result;
374  }
375 
376  Evaluation operator*(const Evaluation& other) const
377  {
378  Evaluation result(*this);
379 
380  result *= other;
381 
382  return result;
383  }
384 
385  template <class RhsValueType>
386  Evaluation operator*(const RhsValueType& other) const
387  {
388  Evaluation result(*this);
389 
390  result *= other;
391 
392  return result;
393  }
394 
395  Evaluation operator/(const Evaluation& other) const
396  {
397  Evaluation result(*this);
398 
399  result /= other;
400 
401  return result;
402  }
403 
404  template <class RhsValueType>
405  Evaluation operator/(const RhsValueType& other) const
406  {
407  Evaluation result(*this);
408 
409  result /= other;
410 
411  return result;
412  }
413 
414  template <class RhsValueType>
415  Evaluation& operator=(const RhsValueType& other)
416  {
417  setValue( other );
418  clearDerivatives();
419 
420  return *this;
421  }
422 
423  // copy assignment from evaluation
424  Evaluation& operator=(const Evaluation& other) = default;
425 
426  template <class RhsValueType>
427  bool operator==(const RhsValueType& other) const
428  { return value() == other; }
429 
430  bool operator==(const Evaluation& other) const
431  {
432  for (int idx = 0; idx < length_; ++idx) {
433  if (data_[idx] != other.data_[idx]) {
434  return false;
435  }
436  }
437  return true;
438  }
439 
440  bool operator!=(const Evaluation& other) const
441  { return !operator==(other); }
442 
443  template <class RhsValueType>
444  bool operator>(RhsValueType other) const
445  { return value() > other; }
446 
447  bool operator>(const Evaluation& other) const
448  { return value() > other.value(); }
449 
450  template <class RhsValueType>
451  bool operator<(RhsValueType other) const
452  { return value() < other; }
453 
454  bool operator<(const Evaluation& other) const
455  { return value() < other.value(); }
456 
457  template <class RhsValueType>
458  bool operator>=(RhsValueType other) const
459  { return value() >= other; }
460 
461  bool operator>=(const Evaluation& other) const
462  { return value() >= other.value(); }
463 
464  template <class RhsValueType>
465  bool operator<=(RhsValueType other) const
466  { return value() <= other; }
467 
468  bool operator<=(const Evaluation& other) const
469  { return value() <= other.value(); }
470 
471  // return value of variable
472  const ValueType& value() const
473  { return data_[valuepos_]; }
474 
475  // set value of variable
476  template <class RhsValueType>
477  void setValue(const RhsValueType& val)
478  { data_[valuepos_] = val; }
479 
480  // return varIdx'th derivative
481  const ValueType& derivative(int varIdx) const
482  {
483  assert(0 <= varIdx && varIdx < size);
484 
485  return data_[dstart_ + varIdx];
486  }
487 
488  // set derivative at position varIdx
489  void setDerivative(int varIdx, const ValueType& derVal)
490  {
491  assert(0 <= varIdx && varIdx < size);
492 
493  data_[dstart_ + varIdx] = derVal;
494  }
495 
496 private:
497  std::array<ValueT, length_> data_;
498 };
499 
500 } } // namespace DenseAd, Opm
501 
502 #endif // OPM_DENSEAD_EVALUATION10_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
Evaluation()
default constructor
Definition: Evaluation10.hpp:74
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
ValueT ValueType
field type
Definition: Evaluation10.hpp:56
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
Representation of an evaluation of a function and its derivatives w.r.t.