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