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