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