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