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