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