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