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