My Project
Loading...
Searching...
No Matches
DynamicEvaluation.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*/
32#ifndef OPM_DENSEAD_EVALUATION_DYNAMIC_HPP
33#define OPM_DENSEAD_EVALUATION_DYNAMIC_HPP
34
35#ifndef NDEBUG
37#endif
39
40#include <cassert>
41#include <iosfwd>
42#include <stdexcept>
43
44#include <opm/common/utility/gpuDecorators.hpp>
45
46namespace Opm {
47namespace DenseAd {
48
53template <class ValueT, unsigned staticSize>
54class Evaluation<ValueT, DynamicSize, staticSize>
55{
56public:
59 static const int numVars = DynamicSize;
60
62 typedef ValueT ValueType;
63
65 OPM_HOST_DEVICE int size() const
66 { return data_.size() - 1; }
67
68protected:
70 OPM_HOST_DEVICE int length_() const
71 { return data_.size(); }
72
73
75 OPM_HOST_DEVICE constexpr int valuepos_() const
76 { return 0; }
78 OPM_HOST_DEVICE constexpr int dstart_() const
79 { return 1; }
81 OPM_HOST_DEVICE int dend_() const
82 { return length_(); }
83
86 OPM_HOST_DEVICE constexpr void checkDefined_() const
87 {
88#ifndef NDEBUG
89 for (int i = dstart_(); i < dend_(); ++i)
90 Valgrind::CheckDefined(data_[i]);
91#endif
92 }
93
94public:
96 OPM_HOST_DEVICE Evaluation() : data_()
97 {}
98
100 Evaluation(const Evaluation& other) = default;
101
104 OPM_HOST_DEVICE Evaluation(Evaluation&& other)
105 : data_(std::move(other.data_))
106 { }
107
109 OPM_HOST_DEVICE Evaluation& operator=(Evaluation&& other)
110 {
111 data_ = std::move(other.data_);
112 return *this;
113 }
114
115 // create a "blank" dynamic evaluation
116 OPM_HOST_DEVICE explicit Evaluation(int numDerivatives)
117 : data_(1 + numDerivatives)
118 {}
119
120 // create a dynamic evaluation which represents a constant function
121 //
122 // i.e., f(x) = c. this implies an evaluation with the given value and all
123 // derivatives being zero.
124 template <class RhsValueType>
125 OPM_HOST_DEVICE Evaluation(int numDerivatives, const RhsValueType& c)
126 : data_(1 + numDerivatives, 0.0)
127 {
128 //clearDerivatives();
129 setValue(c);
130
132 }
133
134 // create an evaluation which represents a constant function
135 //
136 // i.e., f(x) = c. this implies an evaluation with the given value and all
137 // derivatives being zero.
138 template <class RhsValueType>
139 OPM_HOST_DEVICE Evaluation(int nVars, const RhsValueType& c, int varPos)
140 : data_(1 + nVars, 0.0)
141 {
142 // The variable position must be in represented by the given variable descriptor
143 assert(0 <= varPos && varPos < size());
144
145 setValue(c);
146
147 data_[varPos + dstart_()] = 1.0;
148
150 }
151
152 // set all derivatives to zero
153 OPM_HOST_DEVICE constexpr void clearDerivatives()
154 {
155 for (int i = dstart_(); i < dend_(); ++i)
156 data_[i] = 0.0;
157 }
158
159 // create an uninitialized Evaluation object that is compatible with the
160 // argument, but not initialized
161 //
162 // This basically boils down to the copy constructor without copying
163 // anything. If the number of derivatives is known at compile time, this
164 // is equivalent to creating an uninitialized object using the default
165 // constructor, while for dynamic evaluations, it creates an Evaluation
166 // object which exhibits the same number of derivatives as the argument.
167 OPM_HOST_DEVICE static Evaluation createBlank(const Evaluation& x)
168 { return Evaluation(x.size()); }
169
170 // create an Evaluation with value and all the derivatives to be zero
171 OPM_HOST_DEVICE static Evaluation createConstantZero(const Evaluation& x)
172 { return Evaluation(x.size(), 0.0); }
173
174 // create an Evaluation with value to be one and all the derivatives to be zero
175 OPM_HOST_DEVICE static Evaluation createConstantOne(const Evaluation& x)
176 { return Evaluation(x.size(), 1.); }
177
178 // create a function evaluation for a "naked" depending variable (i.e., f(x) = x)
179 template <class RhsValueType>
180 OPM_HOST_DEVICE static Evaluation createVariable(const RhsValueType&, int)
181 {
182 throw std::logic_error("Dynamically sized evaluations require that the number of "
183 "derivatives is specified when creating an evaluation");
184 }
185
186 template <class RhsValueType>
187 OPM_HOST_DEVICE static Evaluation createVariable(int nVars, const RhsValueType& value, int varPos)
188 {
189 // copy function value and set all derivatives to 0, except for the variable
190 // which is represented by the value (which is set to 1.0)
191 return Evaluation(nVars, value, varPos);
192 }
193
194 template <class RhsValueType>
195 OPM_HOST_DEVICE static Evaluation createVariable(const Evaluation& x, const RhsValueType& value, int varPos)
196 {
197 // copy function value and set all derivatives to 0, except for the variable
198 // which is represented by the value (which is set to 1.0)
199 return Evaluation(x.size(), value, varPos);
200 }
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(int nVars, const RhsValueType& value)
207 {
208 return Evaluation(nVars, 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 RhsValueType&)
215 {
216 throw std::logic_error("Dynamically-sized evaluation objects require to specify the number of derivatives.");
217 }
218
219 // "evaluate" a constant function (i.e. a function that does not depend on the set of
220 // relevant variables, f(x) = c).
221 template <class RhsValueType>
222 OPM_HOST_DEVICE static Evaluation createConstant(const Evaluation& x, const RhsValueType& value)
223 {
224 return Evaluation(x.size(), value);
225 }
226
227 // copy all derivatives from other
228 OPM_HOST_DEVICE void copyDerivatives(const Evaluation& other)
229 {
230 assert(size() == other.size());
231
232 for (int i = dstart_(); i < dend_(); ++i)
233 data_[i] = other.data_[i];
234 }
235
236
237 // add value and derivatives from other to this values and derivatives
238 OPM_HOST_DEVICE Evaluation& operator+=(const Evaluation& other)
239 {
240 assert(size() == other.size());
241
242 for (int i = 0; i < length_(); ++i)
243 data_[i] += other.data_[i];
244
245 return *this;
246 }
247
248 // add value from other to this values
249 template <class RhsValueType>
250 OPM_HOST_DEVICE Evaluation& operator+=(const RhsValueType& other)
251 {
252 // value is added, derivatives stay the same
253 data_[valuepos_()] += other;
254
255 return *this;
256 }
257
258 // subtract other's value and derivatives from this values
259 OPM_HOST_DEVICE Evaluation& operator-=(const Evaluation& other)
260 {
261 assert(size() == other.size());
262
263 for (int i = 0; i < length_(); ++i)
264 data_[i] -= other.data_[i];
265
266 return *this;
267 }
268
269 // subtract other's value from this values
270 template <class RhsValueType>
271 OPM_HOST_DEVICE Evaluation& operator-=(const RhsValueType& other)
272 {
273 // for constants, values are subtracted, derivatives stay the same
274 data_[valuepos_()] -= other;
275
276 return *this;
277 }
278
279 // multiply values and apply chain rule to derivatives: (u*v)' = (v'u + u'v)
280 OPM_HOST_DEVICE Evaluation& operator*=(const Evaluation& other)
281 {
282 assert(size() == other.size());
283
284 // while the values are multiplied, the derivatives follow the product rule,
285 // i.e., (u*v)' = (v'u + u'v).
286 const ValueType u = this->value();
287 const ValueType v = other.value();
288
289 // value
290 data_[valuepos_()] *= v ;
291
292 // derivatives
293 for (int i = dstart_(); i < dend_(); ++i)
294 data_[i] = data_[i] * v + other.data_[i] * u;
295
296 return *this;
297 }
298
299 // m(c*u)' = c*u'
300 template <class RhsValueType>
301 OPM_HOST_DEVICE Evaluation& operator*=(const RhsValueType& other)
302 {
303 for (int i = 0; i < length_(); ++i)
304 data_[i] *= other;
305
306 return *this;
307 }
308
309 // m(u*v)' = (vu' - uv')/v^2
310 OPM_HOST_DEVICE Evaluation& operator/=(const Evaluation& other)
311 {
312 assert(size() == other.size());
313
314 // values are divided, derivatives follow the rule for division, i.e., (u/v)' = (v'u -
315 // u'v)/v^2.
316 ValueType& u = data_[valuepos_()];
317 const ValueType& v = other.value();
318 for (int idx = dstart_(); idx < dend_(); ++idx) {
319 const ValueType& uPrime = data_[idx];
320 const ValueType& vPrime = other.data_[idx];
321
322 data_[idx] = (v*uPrime - u*vPrime)/(v*v);
323 }
324 u /= v;
325
326 return *this;
327 }
328
329 // divide value and derivatives by value of other
330 template <class RhsValueType>
331 OPM_HOST_DEVICE Evaluation& operator/=(const RhsValueType& other)
332 {
333 const ValueType tmp = 1.0/other;
334
335 for (int i = 0; i < length_(); ++i)
336 data_[i] *= tmp;
337
338 return *this;
339 }
340
341 // add 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 // add constant to this 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 // subtract two evaluation objects
365 OPM_HOST_DEVICE Evaluation operator-(const Evaluation& other) const
366 {
367 assert(size() == other.size());
368
369 Evaluation result(*this);
370
371 result -= other;
372
373 return result;
374 }
375
376 // subtract constant from evaluation object
377 template <class RhsValueType>
378 OPM_HOST_DEVICE Evaluation operator-(const RhsValueType& other) const
379 {
380 Evaluation result(*this);
381
382 result -= other;
383
384 return result;
385 }
386
387 // negation (unary minus) operator
388 OPM_HOST_DEVICE Evaluation operator-() const
389 {
390 Evaluation result(*this);
391
392 // set value and derivatives to negative
393 for (int i = 0; i < length_(); ++i)
394 result.data_[i] = - data_[i];
395
396 return result;
397 }
398
399 OPM_HOST_DEVICE Evaluation operator*(const Evaluation& other) const
400 {
401 assert(size() == other.size());
402
403 Evaluation result(*this);
404
405 result *= other;
406
407 return result;
408 }
409
410 template <class RhsValueType>
411 OPM_HOST_DEVICE Evaluation operator*(const RhsValueType& other) const
412 {
413 Evaluation result(*this);
414
415 result *= other;
416
417 return result;
418 }
419
420 OPM_HOST_DEVICE Evaluation operator/(const Evaluation& other) const
421 {
422 assert(size() == other.size());
423
424 Evaluation result(*this);
425
426 result /= other;
427
428 return result;
429 }
430
431 template <class RhsValueType>
432 OPM_HOST_DEVICE Evaluation operator/(const RhsValueType& other) const
433 {
434 Evaluation result(*this);
435
436 result /= other;
437
438 return result;
439 }
440
441 template <class RhsValueType>
442 OPM_HOST_DEVICE Evaluation& operator=(const RhsValueType& other)
443 {
444 setValue( other );
445 clearDerivatives();
446
447 return *this;
448 }
449
450 // copy assignment from evaluation
451 Evaluation& operator=(const Evaluation& other) = default;
452
453 template <class RhsValueType>
454 OPM_HOST_DEVICE bool operator==(const 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 for (int idx = 0; idx < length_(); ++idx) {
462 if (data_[idx] != other.data_[idx]) {
463 return false;
464 }
465 }
466 return true;
467 }
468
469 OPM_HOST_DEVICE bool operator!=(const Evaluation& other) const
470 { return !operator==(other); }
471
472 template <class RhsValueType>
473 OPM_HOST_DEVICE bool operator!=(const RhsValueType& other) const
474 { return !operator==(other); }
475
476 template <class RhsValueType>
477 OPM_HOST_DEVICE bool operator>(RhsValueType other) const
478 { return value() > other; }
479
480 OPM_HOST_DEVICE bool operator>(const Evaluation& other) const
481 {
482 assert(size() == other.size());
483
484 return value() > other.value();
485 }
486
487 template <class RhsValueType>
488 OPM_HOST_DEVICE bool operator<(RhsValueType other) const
489 { return value() < other; }
490
491 OPM_HOST_DEVICE bool operator<(const Evaluation& other) const
492 {
493 assert(size() == other.size());
494
495 return value() < other.value();
496 }
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 // return value of variable
521 OPM_HOST_DEVICE const ValueType& value() const
522 { return data_[valuepos_()]; }
523
524 // set value of variable
525 template <class RhsValueType>
526 OPM_HOST_DEVICE constexpr void setValue(const RhsValueType& val)
527 { data_[valuepos_()] = val; }
528
529 // return varIdx'th derivative
530 OPM_HOST_DEVICE const ValueType& derivative(int varIdx) const
531 {
532 assert(0 <= varIdx && varIdx < size());
533
534 return data_[dstart_() + varIdx];
535 }
536
537 // set derivative at position varIdx
538 OPM_HOST_DEVICE void setDerivative(int varIdx, const ValueType& derVal)
539 {
540 assert(0 <= varIdx && varIdx < size());
541
542 data_[dstart_() + varIdx] = derVal;
543 }
544
545 template<class Serializer>
546 OPM_HOST_DEVICE void serializeOp(Serializer& serializer)
547 {
548 serializer(data_);
549 }
550
551private:
552 FastSmallVector<ValueT, staticSize> data_;
553};
554
555template <class Scalar, unsigned staticSize = 0>
556using DynamicEvaluation = Evaluation<Scalar, DynamicSize, staticSize>;
557
558} // namespace DenseAd
559
560template <class Scalar, unsigned staticSize>
561OPM_HOST_DEVICE DenseAd::Evaluation<Scalar, -1, staticSize> constant(int numDerivatives, const Scalar& value)
562{ return DenseAd::Evaluation<Scalar, -1, staticSize>::createConstant(numDerivatives, value); }
563
564template <class Scalar, unsigned staticSize>
565OPM_HOST_DEVICE DenseAd::Evaluation<Scalar, -1, staticSize> variable(int numDerivatives, const Scalar& value, unsigned idx)
566{ return DenseAd::Evaluation<Scalar, -1, staticSize>::createVariable(numDerivatives, value, idx); }
567
568} // namespace Opm
569
570#endif // OPM_DENSEAD_EVALUATION_DYNAMIC_HPP
An implementation of vector/array based on small object optimization.
Some templates to wrap the valgrind client request macros.
OPM_HOST_DEVICE int dend_() const
end+1 index for derivatives
Definition DynamicEvaluation.hpp:81
Evaluation(const Evaluation &other)=default
copy other function evaluation
OPM_HOST_DEVICE Evaluation()
default constructor
Definition DynamicEvaluation.hpp:96
OPM_HOST_DEVICE Evaluation & operator=(Evaluation &&other)
move assignment
Definition DynamicEvaluation.hpp:109
OPM_HOST_DEVICE constexpr int valuepos_() const
position index for value
Definition DynamicEvaluation.hpp:75
OPM_HOST_DEVICE constexpr int dstart_() const
start index for derivatives
Definition DynamicEvaluation.hpp:78
OPM_HOST_DEVICE int size() const
number of derivatives
Definition DynamicEvaluation.hpp:65
OPM_HOST_DEVICE int length_() const
length of internal data vector
Definition DynamicEvaluation.hpp:70
ValueT ValueType
field type
Definition DynamicEvaluation.hpp:62
OPM_HOST_DEVICE Evaluation(Evaluation &&other)
move other function evaluation (this only makes sense for dynamically allocated Evaluations)
Definition DynamicEvaluation.hpp:104
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 DynamicEvaluation.hpp:86
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
OPM_HOST_DEVICE constexpr int dend_() const
end+1 index for derivatives
Definition Evaluation.hpp:85
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition Exceptions.hpp:30