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