My Project
Loading...
Searching...
No Matches
Evaluation3.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_EVALUATION3_HPP
32#define OPM_DENSEAD_EVALUATION3_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, 3>
50{
51public:
54 static const int numVars = 3;
55
57 typedef ValueT ValueType;
58
60 OPM_HOST_DEVICE constexpr int size() const
61 { return 3; };
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 }
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 OPM_HOST_DEVICE static Evaluation createBlank(const Evaluation&)
146 { return Evaluation(); }
147
148 // create an Evaluation with value and all the derivatives to be zero
149 OPM_HOST_DEVICE 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 OPM_HOST_DEVICE 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 OPM_HOST_DEVICE 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 OPM_HOST_DEVICE static Evaluation createVariable(int nVars, const RhsValueType& value, int varPos)
167 {
168 if (nVars != 3)
169 throw std::logic_error("This statically-sized evaluation can only represent objects"
170 " with 3 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 OPM_HOST_DEVICE 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 OPM_HOST_DEVICE static Evaluation createConstant(int nVars, const RhsValueType& value)
190 {
191 if (nVars != 3)
192 throw std::logic_error("This statically-sized evaluation can only represent objects"
193 " with 3 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 OPM_HOST_DEVICE 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 OPM_HOST_DEVICE static Evaluation createConstant(const Evaluation&, const RhsValueType& value)
209 {
210 return Evaluation(value);
211 }
212
213 // copy all derivatives from other
214 OPM_HOST_DEVICE 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 }
222
223
224 // add value and derivatives from other to this values and derivatives
225 OPM_HOST_DEVICE Evaluation& operator+=(const Evaluation& other)
226 {
227 assert(size() == other.size());
228
229 data_[0] += other.data_[0];
230 data_[1] += other.data_[1];
231 data_[2] += other.data_[2];
232 data_[3] += other.data_[3];
233
234 return *this;
235 }
236
237 // add value from other to this values
238 template <class RhsValueType>
239 OPM_HOST_DEVICE Evaluation& operator+=(const RhsValueType& other)
240 {
241 // value is added, derivatives stay the same
242 data_[valuepos_()] += other;
243
244 return *this;
245 }
246
247 // subtract other's value and derivatives from this values
248 OPM_HOST_DEVICE Evaluation& operator-=(const Evaluation& other)
249 {
250 assert(size() == other.size());
251
252 data_[0] -= other.data_[0];
253 data_[1] -= other.data_[1];
254 data_[2] -= other.data_[2];
255 data_[3] -= other.data_[3];
256
257 return *this;
258 }
259
260 // subtract other's value from this values
261 template <class RhsValueType>
262 OPM_HOST_DEVICE Evaluation& operator-=(const RhsValueType& other)
263 {
264 // for constants, values are subtracted, derivatives stay the same
265 data_[valuepos_()] -= other;
266
267 return *this;
268 }
269
270 // multiply values and apply chain rule to derivatives: (u*v)' = (v'u + u'v)
271 OPM_HOST_DEVICE Evaluation& operator*=(const Evaluation& other)
272 {
273 assert(size() == other.size());
274
275 // while the values are multiplied, the derivatives follow the product rule,
276 // i.e., (u*v)' = (v'u + u'v).
277 const ValueType u = this->value();
278 const ValueType v = other.value();
279
280 // value
281 data_[valuepos_()] *= v ;
282
283 // derivatives
284 data_[1] = data_[1] * v + other.data_[1] * u;
285 data_[2] = data_[2] * v + other.data_[2] * u;
286 data_[3] = data_[3] * v + other.data_[3] * u;
287
288 return *this;
289 }
290
291 // m(c*u)' = c*u'
292 template <class RhsValueType>
293 OPM_HOST_DEVICE Evaluation& operator*=(const RhsValueType& other)
294 {
295 data_[0] *= other;
296 data_[1] *= other;
297 data_[2] *= other;
298 data_[3] *= other;
299
300 return *this;
301 }
302
303 // m(u*v)' = (vu' - uv')/v^2
304 OPM_HOST_DEVICE Evaluation& operator/=(const Evaluation& other)
305 {
306 assert(size() == other.size());
307
308 // values are divided, derivatives follow the rule for division, i.e., (u/v)' = (v'u -
309 // u'v)/v^2.
310 ValueType& u = data_[valuepos_()];
311 const ValueType& v = other.value();
312 data_[1] = (v*data_[1] - u*other.data_[1])/(v*v);
313 data_[2] = (v*data_[2] - u*other.data_[2])/(v*v);
314 data_[3] = (v*data_[3] - u*other.data_[3])/(v*v);
315 u /= v;
316
317 return *this;
318 }
319
320 // divide value and derivatives by value of other
321 template <class RhsValueType>
322 OPM_HOST_DEVICE Evaluation& operator/=(const RhsValueType& other)
323 {
324 const ValueType tmp = 1.0/other;
325
326 data_[0] *= tmp;
327 data_[1] *= tmp;
328 data_[2] *= tmp;
329 data_[3] *= tmp;
330
331 return *this;
332 }
333
334 // add two evaluation objects
335 OPM_HOST_DEVICE Evaluation operator+(const Evaluation& other) const
336 {
337 assert(size() == other.size());
338
339 Evaluation result(*this);
340
341 result += other;
342
343 return result;
344 }
345
346 // add constant to this object
347 template <class RhsValueType>
348 OPM_HOST_DEVICE Evaluation operator+(const RhsValueType& other) const
349 {
350 Evaluation result(*this);
351
352 result += other;
353
354 return result;
355 }
356
357 // subtract two evaluation objects
358 OPM_HOST_DEVICE Evaluation operator-(const Evaluation& other) const
359 {
360 assert(size() == other.size());
361
362 Evaluation result(*this);
363
364 result -= other;
365
366 return result;
367 }
368
369 // subtract constant from evaluation object
370 template <class RhsValueType>
371 OPM_HOST_DEVICE Evaluation operator-(const RhsValueType& other) const
372 {
373 Evaluation result(*this);
374
375 result -= other;
376
377 return result;
378 }
379
380 // negation (unary minus) operator
381 OPM_HOST_DEVICE Evaluation operator-() const
382 {
383 Evaluation result;
384
385 // set value and derivatives to negative
386 result.data_[0] = - data_[0];
387 result.data_[1] = - data_[1];
388 result.data_[2] = - data_[2];
389 result.data_[3] = - data_[3];
390
391 return result;
392 }
393
394 OPM_HOST_DEVICE Evaluation operator*(const Evaluation& other) const
395 {
396 assert(size() == other.size());
397
398 Evaluation result(*this);
399
400 result *= other;
401
402 return result;
403 }
404
405 template <class RhsValueType>
406 OPM_HOST_DEVICE Evaluation operator*(const RhsValueType& other) const
407 {
408 Evaluation result(*this);
409
410 result *= other;
411
412 return result;
413 }
414
415 OPM_HOST_DEVICE Evaluation operator/(const Evaluation& other) const
416 {
417 assert(size() == other.size());
418
419 Evaluation result(*this);
420
421 result /= other;
422
423 return result;
424 }
425
426 template <class RhsValueType>
427 OPM_HOST_DEVICE Evaluation operator/(const RhsValueType& other) const
428 {
429 Evaluation result(*this);
430
431 result /= other;
432
433 return result;
434 }
435
436 template <class RhsValueType>
437 OPM_HOST_DEVICE Evaluation& operator=(const RhsValueType& other)
438 {
439 setValue( other );
440 clearDerivatives();
441
442 return *this;
443 }
444
445 // copy assignment from evaluation
446 Evaluation& operator=(const Evaluation& other) = default;
447
448 template <class RhsValueType>
449 OPM_HOST_DEVICE bool operator==(const RhsValueType& other) const
450 { return value() == other; }
451
452 OPM_HOST_DEVICE bool operator==(const Evaluation& other) const
453 {
454 assert(size() == other.size());
455
456 for (int idx = 0; idx < length_(); ++idx) {
457 if (data_[idx] != other.data_[idx]) {
458 return false;
459 }
460 }
461 return true;
462 }
463
464 OPM_HOST_DEVICE bool operator!=(const Evaluation& other) const
465 { return !operator==(other); }
466
467 template <class RhsValueType>
468 OPM_HOST_DEVICE bool operator!=(const RhsValueType& other) const
469 { return !operator==(other); }
470
471 template <class RhsValueType>
472 OPM_HOST_DEVICE bool operator>(RhsValueType other) const
473 { return value() > other; }
474
475 OPM_HOST_DEVICE 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 OPM_HOST_DEVICE bool operator<(RhsValueType other) const
484 { return value() < other; }
485
486 OPM_HOST_DEVICE 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 OPM_HOST_DEVICE bool operator>=(RhsValueType other) const
495 { return value() >= other; }
496
497 OPM_HOST_DEVICE bool operator>=(const Evaluation& other) const
498 {
499 assert(size() == other.size());
500
501 return value() >= other.value();
502 }
503
504 template <class RhsValueType>
505 OPM_HOST_DEVICE bool operator<=(RhsValueType other) const
506 { return value() <= other; }
507
508 OPM_HOST_DEVICE bool operator<=(const Evaluation& other) const
509 {
510 assert(size() == other.size());
511
512 return value() <= other.value();
513 }
514
515 // return value of variable
516 OPM_HOST_DEVICE const ValueType& value() const
517 { return data_[valuepos_()]; }
518
519 // set value of variable
520 template <class RhsValueType>
521 OPM_HOST_DEVICE void setValue(const RhsValueType& val)
522 { data_[valuepos_()] = val; }
523
524 // return varIdx'th derivative
525 OPM_HOST_DEVICE const ValueType& derivative(int varIdx) const
526 {
527 assert(0 <= varIdx && varIdx < size());
528
529 return data_[dstart_() + varIdx];
530 }
531
532 // set derivative at position varIdx
533 OPM_HOST_DEVICE void setDerivative(int varIdx, const ValueType& derVal)
534 {
535 assert(0 <= varIdx && varIdx < size());
536
537 data_[dstart_() + varIdx] = derVal;
538 }
539
540 template<class Serializer>
541 OPM_HOST_DEVICE void serializeOp(Serializer& serializer)
542 {
543 serializer(data_);
544 }
545
546private:
547 std::array<ValueT, 4> data_;
548};
549
550} // namespace DenseAd
551} // namespace Opm
552
553#endif // OPM_DENSEAD_EVALUATION3_HPP
Some templates to wrap the valgrind client request macros.
OPM_HOST_DEVICE void checkDefined_() const
instruct valgrind to check that the value and all derivatives of the Evaluation object are well-defin...
Definition Evaluation3.hpp:81
OPM_HOST_DEVICE Evaluation()
default constructor
Definition Evaluation3.hpp:91
OPM_HOST_DEVICE constexpr int dend_() const
end+1 index for derivatives
Definition Evaluation3.hpp:76
Evaluation(const Evaluation &other)=default
copy other function evaluation
ValueT ValueType
field type
Definition Evaluation3.hpp:57
OPM_HOST_DEVICE constexpr int size() const
number of derivatives
Definition Evaluation3.hpp:60
OPM_HOST_DEVICE constexpr int dstart_() const
start index for derivatives
Definition Evaluation3.hpp:73
OPM_HOST_DEVICE constexpr int valuepos_() const
position index for value
Definition Evaluation3.hpp:70
OPM_HOST_DEVICE constexpr int length_() const
length of internal data vector
Definition Evaluation3.hpp:65
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