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