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