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