My Project
Loading...
Searching...
No Matches
Evaluation7.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_EVALUATION7_HPP
32#define OPM_DENSEAD_EVALUATION7_HPP
33
34#ifndef NDEBUG
36#endif
37
38#include <array>
39#include <cassert>
40#include <iosfwd>
41#include <stdexcept>
42
43namespace Opm {
44namespace DenseAd {
45
46template <class ValueT>
47class Evaluation<ValueT, 7>
48{
49public:
52 static const int numVars = 7;
53
55 typedef ValueT ValueType;
56
58 constexpr int size() const
59 { return 7; };
60
61protected:
63 constexpr int length_() const
64 { return size() + 1; }
65
66
68 constexpr int valuepos_() const
69 { return 0; }
71 constexpr int dstart_() const
72 { return 1; }
74 constexpr int dend_() const
75 { return length_(); }
76
79 void checkDefined_() const
80 {
81#ifndef NDEBUG
82 for (const auto& v: data_)
83 Valgrind::CheckDefined(v);
84#endif
85 }
86
87public:
89 Evaluation() : data_()
90 {}
91
93 Evaluation(const Evaluation& other) = default;
94
95
96 // create an evaluation which represents a constant function
97 //
98 // i.e., f(x) = c. this implies an evaluation with the given value and all
99 // derivatives being zero.
100 template <class RhsValueType>
101 Evaluation(const RhsValueType& c)
102 {
103 setValue(c);
104 clearDerivatives();
105
107 }
108
109 // create an evaluation which represents a constant function
110 //
111 // i.e., f(x) = c. this implies an evaluation with the given value and all
112 // derivatives being zero.
113 template <class RhsValueType>
114 Evaluation(const RhsValueType& c, int varPos)
115 {
116 // The variable position must be in represented by the given variable descriptor
117 assert(0 <= varPos && varPos < size());
118
119 setValue( c );
120 clearDerivatives();
121
122 data_[varPos + dstart_()] = 1.0;
123
125 }
126
127 // set all derivatives to zero
128 void clearDerivatives()
129 {
130 data_[1] = 0.0;
131 data_[2] = 0.0;
132 data_[3] = 0.0;
133 data_[4] = 0.0;
134 data_[5] = 0.0;
135 data_[6] = 0.0;
136 data_[7] = 0.0;
137 }
138
139 // create an uninitialized Evaluation object that is compatible with the
140 // argument, but not initialized
141 //
142 // This basically boils down to the copy constructor without copying
143 // anything. If the number of derivatives is known at compile time, this
144 // is equivalent to creating an uninitialized object using the default
145 // constructor, while for dynamic evaluations, it creates an Evaluation
146 // object which exhibits the same number of derivatives as the argument.
147 static Evaluation createBlank(const Evaluation&)
148 { return Evaluation(); }
149
150 // create an Evaluation with value and all the derivatives to be zero
151 static Evaluation createConstantZero(const Evaluation&)
152 { return Evaluation(0.); }
153
154 // create an Evaluation with value to be one and all the derivatives to be zero
155 static Evaluation createConstantOne(const Evaluation&)
156 { return Evaluation(1.); }
157
158 // create a function evaluation for a "naked" depending variable (i.e., f(x) = x)
159 template <class RhsValueType>
160 static Evaluation createVariable(const RhsValueType& value, int varPos)
161 {
162 // copy function value and set all derivatives to 0, except for the variable
163 // which is represented by the value (which is set to 1.0)
164 return Evaluation(value, varPos);
165 }
166
167 template <class RhsValueType>
168 static Evaluation createVariable(int nVars, const RhsValueType& value, int varPos)
169 {
170 if (nVars != 7)
171 throw std::logic_error("This statically-sized evaluation can only represent objects"
172 " with 7 derivatives");
173
174 // copy function value and set all derivatives to 0, except for the variable
175 // which is represented by the value (which is set to 1.0)
176 return Evaluation(nVars, value, varPos);
177 }
178
179 template <class RhsValueType>
180 static Evaluation createVariable(const Evaluation&, const RhsValueType& value, int varPos)
181 {
182 // copy function value and set all derivatives to 0, except for the variable
183 // which is represented by the value (which is set to 1.0)
184 return Evaluation(value, varPos);
185 }
186
187
188 // "evaluate" a constant function (i.e. a function that does not depend on the set of
189 // relevant variables, f(x) = c).
190 template <class RhsValueType>
191 static Evaluation createConstant(int nVars, const RhsValueType& value)
192 {
193 if (nVars != 7)
194 throw std::logic_error("This statically-sized evaluation can only represent objects"
195 " with 7 derivatives");
196 return Evaluation(value);
197 }
198
199 // "evaluate" a constant function (i.e. a function that does not depend on the set of
200 // relevant variables, f(x) = c).
201 template <class RhsValueType>
202 static Evaluation createConstant(const RhsValueType& value)
203 {
204 return Evaluation(value);
205 }
206
207 // "evaluate" a constant function (i.e. a function that does not depend on the set of
208 // relevant variables, f(x) = c).
209 template <class RhsValueType>
210 static Evaluation createConstant(const Evaluation&, const RhsValueType& value)
211 {
212 return Evaluation(value);
213 }
214
215 // copy all derivatives from other
216 void copyDerivatives(const Evaluation& other)
217 {
218 assert(size() == other.size());
219
220 data_[1] = other.data_[1];
221 data_[2] = other.data_[2];
222 data_[3] = other.data_[3];
223 data_[4] = other.data_[4];
224 data_[5] = other.data_[5];
225 data_[6] = other.data_[6];
226 data_[7] = other.data_[7];
227 }
228
229
230 // add value and derivatives from other to this values and derivatives
231 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 data_[7] += other.data_[7];
243
244 return *this;
245 }
246
247 // add value from other to this values
248 template <class RhsValueType>
249 Evaluation& operator+=(const RhsValueType& other)
250 {
251 // value is added, derivatives stay the same
252 data_[valuepos_()] += other;
253
254 return *this;
255 }
256
257 // subtract other's value and derivatives from this values
258 Evaluation& operator-=(const Evaluation& other)
259 {
260 assert(size() == other.size());
261
262 data_[0] -= other.data_[0];
263 data_[1] -= other.data_[1];
264 data_[2] -= other.data_[2];
265 data_[3] -= other.data_[3];
266 data_[4] -= other.data_[4];
267 data_[5] -= other.data_[5];
268 data_[6] -= other.data_[6];
269 data_[7] -= other.data_[7];
270
271 return *this;
272 }
273
274 // subtract other's value from this values
275 template <class RhsValueType>
276 Evaluation& operator-=(const RhsValueType& other)
277 {
278 // for constants, values are subtracted, derivatives stay the same
279 data_[valuepos_()] -= other;
280
281 return *this;
282 }
283
284 // multiply values and apply chain rule to derivatives: (u*v)' = (v'u + u'v)
285 Evaluation& operator*=(const Evaluation& other)
286 {
287 assert(size() == other.size());
288
289 // while the values are multiplied, the derivatives follow the product rule,
290 // i.e., (u*v)' = (v'u + u'v).
291 const ValueType u = this->value();
292 const ValueType v = other.value();
293
294 // value
295 data_[valuepos_()] *= v ;
296
297 // derivatives
298 data_[1] = data_[1] * v + other.data_[1] * u;
299 data_[2] = data_[2] * v + other.data_[2] * u;
300 data_[3] = data_[3] * v + other.data_[3] * u;
301 data_[4] = data_[4] * v + other.data_[4] * u;
302 data_[5] = data_[5] * v + other.data_[5] * u;
303 data_[6] = data_[6] * v + other.data_[6] * u;
304 data_[7] = data_[7] * v + other.data_[7] * u;
305
306 return *this;
307 }
308
309 // m(c*u)' = c*u'
310 template <class RhsValueType>
311 Evaluation& operator*=(const RhsValueType& other)
312 {
313 data_[0] *= other;
314 data_[1] *= other;
315 data_[2] *= other;
316 data_[3] *= other;
317 data_[4] *= other;
318 data_[5] *= other;
319 data_[6] *= other;
320 data_[7] *= other;
321
322 return *this;
323 }
324
325 // m(u*v)' = (vu' - uv')/v^2
326 Evaluation& operator/=(const Evaluation& other)
327 {
328 assert(size() == other.size());
329
330 // values are divided, derivatives follow the rule for division, i.e., (u/v)' = (v'u -
331 // u'v)/v^2.
332 ValueType& u = data_[valuepos_()];
333 const ValueType& v = other.value();
334 data_[1] = (v*data_[1] - u*other.data_[1])/(v*v);
335 data_[2] = (v*data_[2] - u*other.data_[2])/(v*v);
336 data_[3] = (v*data_[3] - u*other.data_[3])/(v*v);
337 data_[4] = (v*data_[4] - u*other.data_[4])/(v*v);
338 data_[5] = (v*data_[5] - u*other.data_[5])/(v*v);
339 data_[6] = (v*data_[6] - u*other.data_[6])/(v*v);
340 data_[7] = (v*data_[7] - u*other.data_[7])/(v*v);
341 u /= v;
342
343 return *this;
344 }
345
346 // divide value and derivatives by value of other
347 template <class RhsValueType>
348 Evaluation& operator/=(const RhsValueType& other)
349 {
350 const ValueType tmp = 1.0/other;
351
352 data_[0] *= tmp;
353 data_[1] *= tmp;
354 data_[2] *= tmp;
355 data_[3] *= tmp;
356 data_[4] *= tmp;
357 data_[5] *= tmp;
358 data_[6] *= tmp;
359 data_[7] *= tmp;
360
361 return *this;
362 }
363
364 // add two evaluation objects
365 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 // add constant to this object
377 template <class RhsValueType>
378 Evaluation operator+(const RhsValueType& other) const
379 {
380 Evaluation result(*this);
381
382 result += other;
383
384 return result;
385 }
386
387 // subtract two evaluation objects
388 Evaluation operator-(const Evaluation& other) const
389 {
390 assert(size() == other.size());
391
392 Evaluation result(*this);
393
394 result -= other;
395
396 return result;
397 }
398
399 // subtract constant from evaluation object
400 template <class RhsValueType>
401 Evaluation operator-(const RhsValueType& other) const
402 {
403 Evaluation result(*this);
404
405 result -= other;
406
407 return result;
408 }
409
410 // negation (unary minus) operator
411 Evaluation operator-() const
412 {
413 Evaluation result;
414
415 // set value and derivatives to negative
416 result.data_[0] = - data_[0];
417 result.data_[1] = - data_[1];
418 result.data_[2] = - data_[2];
419 result.data_[3] = - data_[3];
420 result.data_[4] = - data_[4];
421 result.data_[5] = - data_[5];
422 result.data_[6] = - data_[6];
423 result.data_[7] = - data_[7];
424
425 return result;
426 }
427
428 Evaluation operator*(const Evaluation& other) const
429 {
430 assert(size() == other.size());
431
432 Evaluation result(*this);
433
434 result *= other;
435
436 return result;
437 }
438
439 template <class RhsValueType>
440 Evaluation operator*(const RhsValueType& other) const
441 {
442 Evaluation result(*this);
443
444 result *= other;
445
446 return result;
447 }
448
449 Evaluation operator/(const Evaluation& other) const
450 {
451 assert(size() == other.size());
452
453 Evaluation result(*this);
454
455 result /= other;
456
457 return result;
458 }
459
460 template <class RhsValueType>
461 Evaluation operator/(const RhsValueType& other) const
462 {
463 Evaluation result(*this);
464
465 result /= other;
466
467 return result;
468 }
469
470 template <class RhsValueType>
471 Evaluation& operator=(const RhsValueType& other)
472 {
473 setValue( other );
474 clearDerivatives();
475
476 return *this;
477 }
478
479 // copy assignment from evaluation
480 Evaluation& operator=(const Evaluation& other) = default;
481
482 template <class RhsValueType>
483 bool operator==(const RhsValueType& other) const
484 { return value() == other; }
485
486 bool operator==(const Evaluation& other) const
487 {
488 assert(size() == other.size());
489
490 for (int idx = 0; idx < length_(); ++idx) {
491 if (data_[idx] != other.data_[idx]) {
492 return false;
493 }
494 }
495 return true;
496 }
497
498 bool operator!=(const Evaluation& other) const
499 { return !operator==(other); }
500
501 template <class RhsValueType>
502 bool operator!=(const RhsValueType& other) const
503 { return !operator==(other); }
504
505 template <class RhsValueType>
506 bool operator>(RhsValueType other) const
507 { return value() > other; }
508
509 bool operator>(const Evaluation& other) const
510 {
511 assert(size() == other.size());
512
513 return value() > other.value();
514 }
515
516 template <class RhsValueType>
517 bool operator<(RhsValueType other) const
518 { return value() < other; }
519
520 bool operator<(const Evaluation& other) const
521 {
522 assert(size() == other.size());
523
524 return value() < other.value();
525 }
526
527 template <class RhsValueType>
528 bool operator>=(RhsValueType other) const
529 { return value() >= other; }
530
531 bool operator>=(const Evaluation& other) const
532 {
533 assert(size() == other.size());
534
535 return value() >= other.value();
536 }
537
538 template <class RhsValueType>
539 bool operator<=(RhsValueType other) const
540 { return value() <= other; }
541
542 bool operator<=(const Evaluation& other) const
543 {
544 assert(size() == other.size());
545
546 return value() <= other.value();
547 }
548
549 // return value of variable
550 const ValueType& value() const
551 { return data_[valuepos_()]; }
552
553 // set value of variable
554 template <class RhsValueType>
555 void setValue(const RhsValueType& val)
556 { data_[valuepos_()] = val; }
557
558 // return varIdx'th derivative
559 const ValueType& derivative(int varIdx) const
560 {
561 assert(0 <= varIdx && varIdx < size());
562
563 return data_[dstart_() + varIdx];
564 }
565
566 // set derivative at position varIdx
567 void setDerivative(int varIdx, const ValueType& derVal)
568 {
569 assert(0 <= varIdx && varIdx < size());
570
571 data_[dstart_() + varIdx] = derVal;
572 }
573
574 template<class Serializer>
575 void serializeOp(Serializer& serializer)
576 {
577 serializer(data_);
578 }
579
580private:
581 std::array<ValueT, 8> data_;
582};
583
584} // namespace DenseAd
585} // namespace Opm
586
587#endif // OPM_DENSEAD_EVALUATION7_HPP
Some templates to wrap the valgrind client request macros.
constexpr int size() const
number of derivatives
Definition Evaluation7.hpp:58
Evaluation(const Evaluation &other)=default
copy other function evaluation
Evaluation()
default constructor
Definition Evaluation7.hpp:89
constexpr int length_() const
length of internal data vector
Definition Evaluation7.hpp:63
ValueT ValueType
field type
Definition Evaluation7.hpp:55
constexpr int valuepos_() const
position index for value
Definition Evaluation7.hpp:68
void checkDefined_() const
instruct valgrind to check that the value and all derivatives of the Evaluation object are well-defin...
Definition Evaluation7.hpp:79
constexpr int dend_() const
end+1 index for derivatives
Definition Evaluation7.hpp:74
constexpr int dstart_() const
start index for derivatives
Definition Evaluation7.hpp:71
Represents a function evaluation and its derivatives w.r.t.
Definition Evaluation.hpp:57
Evaluation()
default constructor
Definition Evaluation.hpp:98
ValueT ValueType
field type
Definition Evaluation.hpp:64
void checkDefined_() const
instruct valgrind to check that the value and all derivatives of the Evaluation object are well-defin...
Definition Evaluation.hpp:88
static const int numVars
the template argument which specifies the number of derivatives (-1 == "DynamicSize" means runtime de...
Definition Evaluation.hpp:61
constexpr int size() const
number of derivatives
Definition Evaluation.hpp:67
constexpr int valuepos_() const
position index for value
Definition Evaluation.hpp:77
constexpr int length_() const
length of internal data vector
Definition Evaluation.hpp:72
constexpr int dstart_() const
start index for derivatives
Definition Evaluation.hpp:80
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition Exceptions.hpp:30