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