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