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