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