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