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