My Project
Loading...
Searching...
No Matches
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#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, 5>
48{
49public:
52 static const int numVars = 5;
53
55 typedef ValueT ValueType;
56
58 constexpr int size() const
59 { return 5; };
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 data_[3] = 0.0;
133 data_[4] = 0.0;
134 data_[5] = 0.0;
135 }
136
137 // create an uninitialized Evaluation object that is compatible with the
138 // argument, but not initialized
139 //
140 // This basically boils down to the copy constructor without copying
141 // anything. If the number of derivatives is known at compile time, this
142 // is equivalent to creating an uninitialized object using the default
143 // constructor, while for dynamic evaluations, it creates an Evaluation
144 // object which exhibits the same number of derivatives as the argument.
145 static Evaluation createBlank(const Evaluation&)
146 { return Evaluation(); }
147
148 // create an Evaluation with value and all the derivatives to be zero
149 static Evaluation createConstantZero(const Evaluation&)
150 { return Evaluation(0.); }
151
152 // create an Evaluation with value to be one and all the derivatives to be zero
153 static Evaluation createConstantOne(const Evaluation&)
154 { return Evaluation(1.); }
155
156 // create a function evaluation for a "naked" depending variable (i.e., f(x) = x)
157 template <class RhsValueType>
158 static Evaluation createVariable(const RhsValueType& value, int varPos)
159 {
160 // copy function value and set all derivatives to 0, except for the variable
161 // which is represented by the value (which is set to 1.0)
162 return Evaluation(value, varPos);
163 }
164
165 template <class RhsValueType>
166 static Evaluation createVariable(int nVars, const RhsValueType& value, int varPos)
167 {
168 if (nVars != 5)
169 throw std::logic_error("This statically-sized evaluation can only represent objects"
170 " with 5 derivatives");
171
172 // copy function value and set all derivatives to 0, except for the variable
173 // which is represented by the value (which is set to 1.0)
174 return Evaluation(nVars, value, varPos);
175 }
176
177 template <class RhsValueType>
178 static Evaluation createVariable(const Evaluation&, const RhsValueType& value, int varPos)
179 {
180 // copy function value and set all derivatives to 0, except for the variable
181 // which is represented by the value (which is set to 1.0)
182 return Evaluation(value, varPos);
183 }
184
185
186 // "evaluate" a constant function (i.e. a function that does not depend on the set of
187 // relevant variables, f(x) = c).
188 template <class RhsValueType>
189 static Evaluation createConstant(int nVars, const RhsValueType& value)
190 {
191 if (nVars != 5)
192 throw std::logic_error("This statically-sized evaluation can only represent objects"
193 " with 5 derivatives");
194 return Evaluation(value);
195 }
196
197 // "evaluate" a constant function (i.e. a function that does not depend on the set of
198 // relevant variables, f(x) = c).
199 template <class RhsValueType>
200 static Evaluation createConstant(const RhsValueType& value)
201 {
202 return Evaluation(value);
203 }
204
205 // "evaluate" a constant function (i.e. a function that does not depend on the set of
206 // relevant variables, f(x) = c).
207 template <class RhsValueType>
208 static Evaluation createConstant(const Evaluation&, const RhsValueType& value)
209 {
210 return Evaluation(value);
211 }
212
213 // copy all derivatives from other
214 void copyDerivatives(const Evaluation& other)
215 {
216 assert(size() == other.size());
217
218 data_[1] = other.data_[1];
219 data_[2] = other.data_[2];
220 data_[3] = other.data_[3];
221 data_[4] = other.data_[4];
222 data_[5] = other.data_[5];
223 }
224
225
226 // add value and derivatives from other to this values and derivatives
227 Evaluation& operator+=(const Evaluation& other)
228 {
229 assert(size() == other.size());
230
231 data_[0] += other.data_[0];
232 data_[1] += other.data_[1];
233 data_[2] += other.data_[2];
234 data_[3] += other.data_[3];
235 data_[4] += other.data_[4];
236 data_[5] += other.data_[5];
237
238 return *this;
239 }
240
241 // add value from other to this values
242 template <class RhsValueType>
243 Evaluation& operator+=(const RhsValueType& other)
244 {
245 // value is added, derivatives stay the same
246 data_[valuepos_()] += other;
247
248 return *this;
249 }
250
251 // subtract other's value and derivatives from this values
252 Evaluation& operator-=(const Evaluation& other)
253 {
254 assert(size() == other.size());
255
256 data_[0] -= other.data_[0];
257 data_[1] -= other.data_[1];
258 data_[2] -= other.data_[2];
259 data_[3] -= other.data_[3];
260 data_[4] -= other.data_[4];
261 data_[5] -= other.data_[5];
262
263 return *this;
264 }
265
266 // subtract other's value from this values
267 template <class RhsValueType>
268 Evaluation& operator-=(const RhsValueType& other)
269 {
270 // for constants, values are subtracted, derivatives stay the same
271 data_[valuepos_()] -= other;
272
273 return *this;
274 }
275
276 // multiply values and apply chain rule to derivatives: (u*v)' = (v'u + u'v)
277 Evaluation& operator*=(const Evaluation& other)
278 {
279 assert(size() == other.size());
280
281 // while the values are multiplied, the derivatives follow the product rule,
282 // i.e., (u*v)' = (v'u + u'v).
283 const ValueType u = this->value();
284 const ValueType v = other.value();
285
286 // value
287 data_[valuepos_()] *= v ;
288
289 // derivatives
290 data_[1] = data_[1] * v + other.data_[1] * u;
291 data_[2] = data_[2] * v + other.data_[2] * u;
292 data_[3] = data_[3] * v + other.data_[3] * u;
293 data_[4] = data_[4] * v + other.data_[4] * u;
294 data_[5] = data_[5] * v + other.data_[5] * u;
295
296 return *this;
297 }
298
299 // m(c*u)' = c*u'
300 template <class RhsValueType>
301 Evaluation& operator*=(const RhsValueType& other)
302 {
303 data_[0] *= other;
304 data_[1] *= other;
305 data_[2] *= other;
306 data_[3] *= other;
307 data_[4] *= other;
308 data_[5] *= other;
309
310 return *this;
311 }
312
313 // m(u*v)' = (vu' - uv')/v^2
314 Evaluation& operator/=(const Evaluation& other)
315 {
316 assert(size() == other.size());
317
318 // values are divided, derivatives follow the rule for division, i.e., (u/v)' = (v'u -
319 // u'v)/v^2.
320 ValueType& u = data_[valuepos_()];
321 const ValueType& v = other.value();
322 data_[1] = (v*data_[1] - u*other.data_[1])/(v*v);
323 data_[2] = (v*data_[2] - u*other.data_[2])/(v*v);
324 data_[3] = (v*data_[3] - u*other.data_[3])/(v*v);
325 data_[4] = (v*data_[4] - u*other.data_[4])/(v*v);
326 data_[5] = (v*data_[5] - u*other.data_[5])/(v*v);
327 u /= v;
328
329 return *this;
330 }
331
332 // divide value and derivatives by value of other
333 template <class RhsValueType>
334 Evaluation& operator/=(const RhsValueType& other)
335 {
336 const ValueType tmp = 1.0/other;
337
338 data_[0] *= tmp;
339 data_[1] *= tmp;
340 data_[2] *= tmp;
341 data_[3] *= tmp;
342 data_[4] *= tmp;
343 data_[5] *= tmp;
344
345 return *this;
346 }
347
348 // add two evaluation objects
349 Evaluation operator+(const Evaluation& other) const
350 {
351 assert(size() == other.size());
352
353 Evaluation result(*this);
354
355 result += other;
356
357 return result;
358 }
359
360 // add constant to this object
361 template <class RhsValueType>
362 Evaluation operator+(const RhsValueType& other) const
363 {
364 Evaluation result(*this);
365
366 result += other;
367
368 return result;
369 }
370
371 // subtract two evaluation objects
372 Evaluation operator-(const Evaluation& other) const
373 {
374 assert(size() == other.size());
375
376 Evaluation result(*this);
377
378 result -= other;
379
380 return result;
381 }
382
383 // subtract constant from evaluation object
384 template <class RhsValueType>
385 Evaluation operator-(const RhsValueType& other) const
386 {
387 Evaluation result(*this);
388
389 result -= other;
390
391 return result;
392 }
393
394 // negation (unary minus) operator
395 Evaluation operator-() const
396 {
397 Evaluation result;
398
399 // set value and derivatives to negative
400 result.data_[0] = - data_[0];
401 result.data_[1] = - data_[1];
402 result.data_[2] = - data_[2];
403 result.data_[3] = - data_[3];
404 result.data_[4] = - data_[4];
405 result.data_[5] = - data_[5];
406
407 return result;
408 }
409
410 Evaluation operator*(const Evaluation& other) const
411 {
412 assert(size() == other.size());
413
414 Evaluation result(*this);
415
416 result *= other;
417
418 return result;
419 }
420
421 template <class RhsValueType>
422 Evaluation operator*(const RhsValueType& other) const
423 {
424 Evaluation result(*this);
425
426 result *= other;
427
428 return result;
429 }
430
431 Evaluation operator/(const Evaluation& other) const
432 {
433 assert(size() == other.size());
434
435 Evaluation result(*this);
436
437 result /= other;
438
439 return result;
440 }
441
442 template <class RhsValueType>
443 Evaluation operator/(const RhsValueType& other) const
444 {
445 Evaluation result(*this);
446
447 result /= other;
448
449 return result;
450 }
451
452 template <class RhsValueType>
453 Evaluation& operator=(const RhsValueType& other)
454 {
455 setValue( other );
456 clearDerivatives();
457
458 return *this;
459 }
460
461 // copy assignment from evaluation
462 Evaluation& operator=(const Evaluation& other) = default;
463
464 template <class RhsValueType>
465 bool operator==(const RhsValueType& other) const
466 { return value() == other; }
467
468 bool operator==(const Evaluation& other) const
469 {
470 assert(size() == other.size());
471
472 for (int idx = 0; idx < length_(); ++idx) {
473 if (data_[idx] != other.data_[idx]) {
474 return false;
475 }
476 }
477 return true;
478 }
479
480 bool operator!=(const Evaluation& other) const
481 { return !operator==(other); }
482
483 template <class RhsValueType>
484 bool operator!=(const RhsValueType& other) const
485 { return !operator==(other); }
486
487 template <class RhsValueType>
488 bool operator>(RhsValueType other) const
489 { return value() > other; }
490
491 bool operator>(const Evaluation& other) const
492 {
493 assert(size() == other.size());
494
495 return value() > other.value();
496 }
497
498 template <class RhsValueType>
499 bool operator<(RhsValueType other) const
500 { return value() < other; }
501
502 bool operator<(const Evaluation& other) const
503 {
504 assert(size() == other.size());
505
506 return value() < other.value();
507 }
508
509 template <class RhsValueType>
510 bool operator>=(RhsValueType other) const
511 { return value() >= other; }
512
513 bool operator>=(const Evaluation& other) const
514 {
515 assert(size() == other.size());
516
517 return value() >= other.value();
518 }
519
520 template <class RhsValueType>
521 bool operator<=(RhsValueType other) const
522 { return value() <= other; }
523
524 bool operator<=(const Evaluation& other) const
525 {
526 assert(size() == other.size());
527
528 return value() <= other.value();
529 }
530
531 // return value of variable
532 const ValueType& value() const
533 { return data_[valuepos_()]; }
534
535 // set value of variable
536 template <class RhsValueType>
537 void setValue(const RhsValueType& val)
538 { data_[valuepos_()] = val; }
539
540 // return varIdx'th derivative
541 const ValueType& derivative(int varIdx) const
542 {
543 assert(0 <= varIdx && varIdx < size());
544
545 return data_[dstart_() + varIdx];
546 }
547
548 // set derivative at position varIdx
549 void setDerivative(int varIdx, const ValueType& derVal)
550 {
551 assert(0 <= varIdx && varIdx < size());
552
553 data_[dstart_() + varIdx] = derVal;
554 }
555
556 template<class Serializer>
557 void serializeOp(Serializer& serializer)
558 {
559 serializer(data_);
560 }
561
562private:
563 std::array<ValueT, 6> data_;
564};
565
566} // namespace DenseAd
567} // namespace Opm
568
569#endif // OPM_DENSEAD_EVALUATION5_HPP
Some templates to wrap the valgrind client request macros.
ValueT ValueType
field type
Definition Evaluation5.hpp:55
constexpr int dstart_() const
start index for derivatives
Definition Evaluation5.hpp:71
void checkDefined_() const
instruct valgrind to check that the value and all derivatives of the Evaluation object are well-defin...
Definition Evaluation5.hpp:79
Evaluation()
default constructor
Definition Evaluation5.hpp:89
Evaluation(const Evaluation &other)=default
copy other function evaluation
constexpr int dend_() const
end+1 index for derivatives
Definition Evaluation5.hpp:74
constexpr int valuepos_() const
position index for value
Definition Evaluation5.hpp:68
constexpr int size() const
number of derivatives
Definition Evaluation5.hpp:58
constexpr int length_() const
length of internal data vector
Definition Evaluation5.hpp:63
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