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