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