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