My Project
Loading...
Searching...
No Matches
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#ifndef NDEBUG
36#endif
37
38#include <array>
39#include <cassert>
40#include <iosfwd>
41#include <stdexcept>
42
43namespace Opm {
44namespace DenseAd {
45
46template <class ValueT>
47class Evaluation<ValueT, 2>
48{
49public:
52 static const int numVars = 2;
53
55 typedef ValueT ValueType;
56
58 constexpr int size() const
59 { return 2; };
60
61protected:
63 constexpr int length_() const
64 { return size() + 1; }
65
66
68 constexpr int valuepos_() const
69 { return 0; }
71 constexpr int dstart_() const
72 { return 1; }
74 constexpr int dend_() const
75 { return length_(); }
76
79 void checkDefined_() const
80 {
81#ifndef NDEBUG
82 for (const auto& v: data_)
83 Valgrind::CheckDefined(v);
84#endif
85 }
86
87public:
89 Evaluation() : data_()
90 {}
91
93 Evaluation(const Evaluation& other) = default;
94
95
96 // create an evaluation which represents a constant function
97 //
98 // i.e., f(x) = c. this implies an evaluation with the given value and all
99 // derivatives being zero.
100 template <class RhsValueType>
101 Evaluation(const RhsValueType& c)
102 {
103 setValue(c);
104 clearDerivatives();
105
107 }
108
109 // create an evaluation which represents a constant function
110 //
111 // i.e., f(x) = c. this implies an evaluation with the given value and all
112 // derivatives being zero.
113 template <class RhsValueType>
114 Evaluation(const RhsValueType& c, int varPos)
115 {
116 // The variable position must be in represented by the given variable descriptor
117 assert(0 <= varPos && varPos < size());
118
119 setValue( c );
120 clearDerivatives();
121
122 data_[varPos + dstart_()] = 1.0;
123
125 }
126
127 // set all derivatives to zero
128 void clearDerivatives()
129 {
130 data_[1] = 0.0;
131 data_[2] = 0.0;
132 }
133
134 // create an uninitialized Evaluation object that is compatible with the
135 // argument, but not initialized
136 //
137 // This basically boils down to the copy constructor without copying
138 // anything. If the number of derivatives is known at compile time, this
139 // is equivalent to creating an uninitialized object using the default
140 // constructor, while for dynamic evaluations, it creates an Evaluation
141 // object which exhibits the same number of derivatives as the argument.
142 static Evaluation createBlank(const Evaluation&)
143 { return Evaluation(); }
144
145 // create an Evaluation with value and all the derivatives to be zero
146 static Evaluation createConstantZero(const Evaluation&)
147 { return Evaluation(0.); }
148
149 // create an Evaluation with value to be one and all the derivatives to be zero
150 static Evaluation createConstantOne(const Evaluation&)
151 { return Evaluation(1.); }
152
153 // create a function evaluation for a "naked" depending variable (i.e., f(x) = x)
154 template <class RhsValueType>
155 static Evaluation createVariable(const RhsValueType& value, int varPos)
156 {
157 // copy function value and set all derivatives to 0, except for the variable
158 // which is represented by the value (which is set to 1.0)
159 return Evaluation(value, varPos);
160 }
161
162 template <class RhsValueType>
163 static Evaluation createVariable(int nVars, const RhsValueType& value, int varPos)
164 {
165 if (nVars != 2)
166 throw std::logic_error("This statically-sized evaluation can only represent objects"
167 " with 2 derivatives");
168
169 // copy function value and set all derivatives to 0, except for the variable
170 // which is represented by the value (which is set to 1.0)
171 return Evaluation(nVars, value, varPos);
172 }
173
174 template <class RhsValueType>
175 static Evaluation createVariable(const Evaluation&, const RhsValueType& value, int varPos)
176 {
177 // copy function value and set all derivatives to 0, except for the variable
178 // which is represented by the value (which is set to 1.0)
179 return Evaluation(value, varPos);
180 }
181
182
183 // "evaluate" a constant function (i.e. a function that does not depend on the set of
184 // relevant variables, f(x) = c).
185 template <class RhsValueType>
186 static Evaluation createConstant(int nVars, const RhsValueType& value)
187 {
188 if (nVars != 2)
189 throw std::logic_error("This statically-sized evaluation can only represent objects"
190 " with 2 derivatives");
191 return Evaluation(value);
192 }
193
194 // "evaluate" a constant function (i.e. a function that does not depend on the set of
195 // relevant variables, f(x) = c).
196 template <class RhsValueType>
197 static Evaluation createConstant(const RhsValueType& value)
198 {
199 return Evaluation(value);
200 }
201
202 // "evaluate" a constant function (i.e. a function that does not depend on the set of
203 // relevant variables, f(x) = c).
204 template <class RhsValueType>
205 static Evaluation createConstant(const Evaluation&, const RhsValueType& value)
206 {
207 return Evaluation(value);
208 }
209
210 // copy all derivatives from other
211 void copyDerivatives(const Evaluation& other)
212 {
213 assert(size() == other.size());
214
215 data_[1] = other.data_[1];
216 data_[2] = other.data_[2];
217 }
218
219
220 // add value and derivatives from other to this values and derivatives
221 Evaluation& operator+=(const Evaluation& other)
222 {
223 assert(size() == other.size());
224
225 data_[0] += other.data_[0];
226 data_[1] += other.data_[1];
227 data_[2] += other.data_[2];
228
229 return *this;
230 }
231
232 // add value from other to this values
233 template <class RhsValueType>
234 Evaluation& operator+=(const RhsValueType& other)
235 {
236 // value is added, derivatives stay the same
237 data_[valuepos_()] += other;
238
239 return *this;
240 }
241
242 // subtract other's value and derivatives from this values
243 Evaluation& operator-=(const Evaluation& other)
244 {
245 assert(size() == other.size());
246
247 data_[0] -= other.data_[0];
248 data_[1] -= other.data_[1];
249 data_[2] -= other.data_[2];
250
251 return *this;
252 }
253
254 // subtract other's value from this values
255 template <class RhsValueType>
256 Evaluation& operator-=(const RhsValueType& other)
257 {
258 // for constants, values are subtracted, derivatives stay the same
259 data_[valuepos_()] -= other;
260
261 return *this;
262 }
263
264 // multiply values and apply chain rule to derivatives: (u*v)' = (v'u + u'v)
265 Evaluation& operator*=(const Evaluation& other)
266 {
267 assert(size() == other.size());
268
269 // while the values are multiplied, the derivatives follow the product rule,
270 // i.e., (u*v)' = (v'u + u'v).
271 const ValueType u = this->value();
272 const ValueType v = other.value();
273
274 // value
275 data_[valuepos_()] *= v ;
276
277 // derivatives
278 data_[1] = data_[1] * v + other.data_[1] * u;
279 data_[2] = data_[2] * v + other.data_[2] * u;
280
281 return *this;
282 }
283
284 // m(c*u)' = c*u'
285 template <class RhsValueType>
286 Evaluation& operator*=(const RhsValueType& other)
287 {
288 data_[0] *= other;
289 data_[1] *= other;
290 data_[2] *= other;
291
292 return *this;
293 }
294
295 // m(u*v)' = (vu' - uv')/v^2
296 Evaluation& operator/=(const Evaluation& other)
297 {
298 assert(size() == other.size());
299
300 // values are divided, derivatives follow the rule for division, i.e., (u/v)' = (v'u -
301 // u'v)/v^2.
302 ValueType& u = data_[valuepos_()];
303 const ValueType& v = other.value();
304 data_[1] = (v*data_[1] - u*other.data_[1])/(v*v);
305 data_[2] = (v*data_[2] - u*other.data_[2])/(v*v);
306 u /= v;
307
308 return *this;
309 }
310
311 // divide value and derivatives by value of other
312 template <class RhsValueType>
313 Evaluation& operator/=(const RhsValueType& other)
314 {
315 const ValueType tmp = 1.0/other;
316
317 data_[0] *= tmp;
318 data_[1] *= tmp;
319 data_[2] *= tmp;
320
321 return *this;
322 }
323
324 // add two evaluation objects
325 Evaluation operator+(const Evaluation& other) const
326 {
327 assert(size() == other.size());
328
329 Evaluation result(*this);
330
331 result += other;
332
333 return result;
334 }
335
336 // add constant to this 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 // subtract two evaluation objects
348 Evaluation operator-(const Evaluation& other) const
349 {
350 assert(size() == other.size());
351
352 Evaluation result(*this);
353
354 result -= other;
355
356 return result;
357 }
358
359 // subtract constant from evaluation object
360 template <class RhsValueType>
361 Evaluation operator-(const RhsValueType& other) const
362 {
363 Evaluation result(*this);
364
365 result -= other;
366
367 return result;
368 }
369
370 // negation (unary minus) operator
371 Evaluation operator-() const
372 {
373 Evaluation result;
374
375 // set value and derivatives to negative
376 result.data_[0] = - data_[0];
377 result.data_[1] = - data_[1];
378 result.data_[2] = - data_[2];
379
380 return result;
381 }
382
383 Evaluation operator*(const Evaluation& other) const
384 {
385 assert(size() == other.size());
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 assert(size() == other.size());
407
408 Evaluation result(*this);
409
410 result /= other;
411
412 return result;
413 }
414
415 template <class RhsValueType>
416 Evaluation operator/(const RhsValueType& other) const
417 {
418 Evaluation result(*this);
419
420 result /= other;
421
422 return result;
423 }
424
425 template <class RhsValueType>
426 Evaluation& operator=(const RhsValueType& other)
427 {
428 setValue( other );
429 clearDerivatives();
430
431 return *this;
432 }
433
434 // copy assignment from evaluation
435 Evaluation& operator=(const Evaluation& other) = default;
436
437 template <class RhsValueType>
438 bool operator==(const RhsValueType& other) const
439 { return value() == other; }
440
441 bool operator==(const Evaluation& other) const
442 {
443 assert(size() == other.size());
444
445 for (int idx = 0; idx < length_(); ++idx) {
446 if (data_[idx] != other.data_[idx]) {
447 return false;
448 }
449 }
450 return true;
451 }
452
453 bool operator!=(const Evaluation& other) const
454 { return !operator==(other); }
455
456 template <class RhsValueType>
457 bool operator!=(const RhsValueType& other) const
458 { return !operator==(other); }
459
460 template <class RhsValueType>
461 bool operator>(RhsValueType other) const
462 { return value() > other; }
463
464 bool operator>(const Evaluation& other) const
465 {
466 assert(size() == other.size());
467
468 return value() > other.value();
469 }
470
471 template <class RhsValueType>
472 bool operator<(RhsValueType other) const
473 { return value() < other; }
474
475 bool operator<(const Evaluation& other) const
476 {
477 assert(size() == other.size());
478
479 return value() < other.value();
480 }
481
482 template <class RhsValueType>
483 bool operator>=(RhsValueType other) const
484 { return value() >= other; }
485
486 bool operator>=(const Evaluation& other) const
487 {
488 assert(size() == other.size());
489
490 return value() >= other.value();
491 }
492
493 template <class RhsValueType>
494 bool operator<=(RhsValueType other) const
495 { return value() <= other; }
496
497 bool operator<=(const Evaluation& other) const
498 {
499 assert(size() == other.size());
500
501 return value() <= other.value();
502 }
503
504 // return value of variable
505 const ValueType& value() const
506 { return data_[valuepos_()]; }
507
508 // set value of variable
509 template <class RhsValueType>
510 void setValue(const RhsValueType& val)
511 { data_[valuepos_()] = val; }
512
513 // return varIdx'th derivative
514 const ValueType& derivative(int varIdx) const
515 {
516 assert(0 <= varIdx && varIdx < size());
517
518 return data_[dstart_() + varIdx];
519 }
520
521 // set derivative at position varIdx
522 void setDerivative(int varIdx, const ValueType& derVal)
523 {
524 assert(0 <= varIdx && varIdx < size());
525
526 data_[dstart_() + varIdx] = derVal;
527 }
528
529 template<class Serializer>
530 void serializeOp(Serializer& serializer)
531 {
532 serializer(data_);
533 }
534
535private:
536 std::array<ValueT, 3> data_;
537};
538
539} // namespace DenseAd
540} // namespace Opm
541
542#endif // OPM_DENSEAD_EVALUATION2_HPP
Some templates to wrap the valgrind client request macros.
constexpr int valuepos_() const
position index for value
Definition Evaluation2.hpp:68
ValueT ValueType
field type
Definition Evaluation2.hpp:55
constexpr int dstart_() const
start index for derivatives
Definition Evaluation2.hpp:71
constexpr int size() const
number of derivatives
Definition Evaluation2.hpp:58
Evaluation()
default constructor
Definition Evaluation2.hpp:89
void checkDefined_() const
instruct valgrind to check that the value and all derivatives of the Evaluation object are well-defin...
Definition Evaluation2.hpp:79
constexpr int dend_() const
end+1 index for derivatives
Definition Evaluation2.hpp:74
constexpr int length_() const
length of internal data vector
Definition Evaluation2.hpp:63
Evaluation(const Evaluation &other)=default
copy other function evaluation
Represents a function evaluation and its derivatives w.r.t.
Definition Evaluation.hpp:57
Evaluation()
default constructor
Definition Evaluation.hpp:98
ValueT ValueType
field type
Definition Evaluation.hpp:64
void checkDefined_() const
instruct valgrind to check that the value and all derivatives of the Evaluation object are well-defin...
Definition Evaluation.hpp:88
static const int numVars
the template argument which specifies the number of derivatives (-1 == "DynamicSize" means runtime de...
Definition Evaluation.hpp:61
constexpr int size() const
number of derivatives
Definition Evaluation.hpp:67
constexpr int valuepos_() const
position index for value
Definition Evaluation.hpp:77
constexpr int length_() const
length of internal data vector
Definition Evaluation.hpp:72
constexpr int dstart_() const
start index for derivatives
Definition Evaluation.hpp:80
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition Exceptions.hpp:30