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