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