Cantera  2.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
utilities.h
Go to the documentation of this file.
1 /**
2  * @file utilities.h
3  * Various templated functions that carry out common vector
4  * operations (see \ref utils).
5  */
6 
7 // Copyright 2001 California Institute of Technology
8 /**
9  * @defgroup utils Templated Utility Functions
10  *
11  * These are templates to perform various simple operations on arrays.
12  * Note that the compiler will inline these, so using them carries no
13  * performance penalty.
14  */
15 
16 #ifndef CT_UTILITIES_H
17 #define CT_UTILITIES_H
18 
19 #include "ct_defs.h"
20 #include "global.h"
21 #include <stdexcept>
22 
23 #include <numeric>
24 
25 namespace Cantera
26 {
27 //! Unary operator to multiply the argument by a constant.
28 /*!
29  * The form of this operator is designed for use by std::transform.
30  * @see @ref scale().
31  */
32 template<class T> struct timesConstant : public std::unary_function<T, double> {
33  //! Constructor
34  /*!
35  * @param c Constant of templated type T
36  * that will be stored internally within the object
37  * and used in the multiplication operation
38  */
39  timesConstant(T c) : m_c(c) {}
40 
41  //! Parenthesis operator returning a double
42  /*!
43  * @param x Variable of templated type T that will be
44  * used in the multiplication operator
45  *
46  * @return Returns a value of type double from the internal
47  * multiplication
48  */
49  double operator()(T x) {
50  return m_c * x;
51  }
52 
53  //! Stored constant value of time T
54  T m_c;
55 };
56 
57 //! Templated Inner product of two vectors of length 4.
58 /*!
59  * If either \a x
60  * or \a y has length greater than 4, only the first 4 elements
61  * will be used.
62  *
63  * @param x first reference to the templated class V
64  * @param y second reference to the templated class V
65  * @return
66  * This class returns a hard-coded type, doublereal.
67  */
68 template<class V>
69 inline doublereal dot4(const V& x, const V& y)
70 {
71  return x[0]*y[0] + x[1]*y[1] + x[2]*y[2] + x[3]*y[3];
72 }
73 
74 
75 //! Templated Inner product of two vectors of length 5
76 /*!
77  * If either \a x
78  * or \a y has length greater than 4, only the first 4 elements
79  * will be used.
80  *
81  * @param x first reference to the templated class V
82  * @param y second reference to the templated class V
83  * @return
84  * This class returns a hard-coded type, doublereal.
85  */
86 template<class V>
87 inline doublereal dot5(const V& x, const V& y)
88 {
89  return x[0]*y[0] + x[1]*y[1] + x[2]*y[2] + x[3]*y[3] +
90  x[4]*y[4];
91 }
92 
93 //! Templated Inner product of two vectors of length 6
94 /*!
95  * If either \a x
96  * or \a y has length greater than 4, only the first 4 elements
97  * will be used.
98  *
99  * @param x first reference to the templated class V
100  * @param y second reference to the templated class V
101  * @return
102  * This class returns a hard-coded type, doublereal.
103  * @deprecated Unused. To be removed after Cantera 2.2.
104  */
105 template<class V>
106 inline doublereal dot6(const V& x, const V& y)
107 {
108  warn_deprecated("dot6", "To be removed after Cantera 2.2.");
109  return x[0]*y[0] + x[1]*y[1] + x[2]*y[2] + x[3]*y[3] +
110  x[4]*y[4] + x[5]*y[5];
111 }
112 
113 //! Function that calculates a templated inner product.
114 /*!
115  * This inner product is templated twice. The output variable is hard coded
116  * to return a doublereal.
117  *
118  * template<class InputIter, class InputIter2>
119  *
120  * @code
121  * double x[8], y[8];
122  * doublereal dsum = dot<double *,double *>(x, &x+7, y);
123  * @endcode
124  *
125  * @param x_begin Iterator pointing to the beginning, belonging to the
126  * iterator class InputIter.
127  * @param x_end Iterator pointing to the end, belonging to the
128  * iterator class InputIter.
129  * @param y_begin Iterator pointing to the beginning of y, belonging to the
130  * iterator class InputIter2.
131  * @return
132  * The return is hard-coded to return a double.
133  */
134 template<class InputIter, class InputIter2>
135 inline doublereal dot(InputIter x_begin, InputIter x_end,
136  InputIter2 y_begin)
137 {
138  return inner_product(x_begin, x_end, y_begin, 0.0);
139 }
140 
141 //! Multiply elements of an array by a scale factor.
142 /*!
143  * \code
144  * vector_fp in(8, 1.0), out(8);
145  * scale(in.begin(), in.end(), out.begin(), factor);
146  * \endcode
147  *
148  * @param begin Iterator pointing to the beginning, belonging to the
149  * iterator class InputIter.
150  * @param end Iterator pointing to the end, belonging to the
151  * iterator class InputIter.
152  * @param out Iterator pointing to the beginning of out, belonging to the
153  * iterator class OutputIter. This is the output variable
154  * for this routine.
155  * @param scale_factor input scale factor belonging to the class S.
156  */
157 template<class InputIter, class OutputIter, class S>
158 inline void scale(InputIter begin, InputIter end,
159  OutputIter out, S scale_factor)
160 {
161  std::transform(begin, end, out, timesConstant<S>(scale_factor));
162 }
163 
164 /*!
165  * Multiply elements of an array, y, by a scale factor, f and add the
166  * result to an existing array, x. This is essentially a templated daxpy_
167  * operation.
168  *
169  * The template arguments are: template<class InputIter,
170  * class OutputIter, class S>
171  *
172  * Simple Code Example of the functionality;
173  * @code
174  * double x[10], y[10], f;
175  * for (i = 0; i < n; i++) {
176  * y[i] += f * x[i]
177  * }
178  * @endcode
179  * Example of the function call to implement the simple code example
180  * @code
181  * double x[10], y[10], f;
182  * increment_scale(x, x+10, y, f);
183  * @endcode
184  *
185  * It is templated with three parameters. The first template
186  * is the iterator, InputIter, which controls access to y[].
187  * The second template is the iterator OutputIter, which controls
188  * access to y[]. The third iterator is S, which is f.
189  *
190  * @param begin InputIter Iterator for beginning of y[]
191  * @param end inputIter Iterator for end of y[]
192  * @param out OutputIter Iterator for beginning of x[]
193  * @param scale_factor Scale Factor to multiply y[i] by
194  * @deprecated Unused. To be removed after Cantera 2.2.
195  */
196 template<class InputIter, class OutputIter, class S>
197 inline void increment_scale(InputIter begin, InputIter end,
198  OutputIter out, S scale_factor)
199 {
200  warn_deprecated("increment_scale",
201  "To be removed after Cantera 2.2.");
202  for (; begin != end; ++begin, ++out) {
203  *out += scale_factor * *begin;
204  }
205 }
206 
207 
208 //! Multiply each entry in x by the corresponding entry in y.
209 /*!
210  * The template arguments are: template<class InputIter, class OutputIter>
211  *
212  * Simple code Equivalent:
213  * \code
214  * double x[10], y[10]
215  * for (n = 0; n < 10; n++) {
216  * x[n] *= y[n];
217  * }
218  * \endcode
219  * Example of function call usage to implement the simple code example:
220  * \code
221  * double x[10], y[10]
222  * multiply_each(x, x+10, y);
223  * \endcode
224  *
225  * @param x_begin Iterator pointing to the beginning of the vector x, belonging to the
226  * iterator class InputIter.
227  * @param x_end Iterator pointing to the end of the vector x, belonging to the
228  * iterator class InputIter. The difference between end and begin
229  * determines the loop length
230  * @param y_begin Iterator pointing to the beginning of the vector y, belonging to the
231  * iterator class outputIter.
232  */
233 template<class InputIter, class OutputIter>
234 inline void multiply_each(OutputIter x_begin, OutputIter x_end,
235  InputIter y_begin)
236 {
237  for (; x_begin != x_end; ++x_begin, ++y_begin) {
238  *x_begin *= *y_begin;
239  }
240 }
241 
242 //! Invoke method 'resize' with argument \a m for a sequence of objects (templated version)
243 /*!
244  * The template arguments are: template<class InputIter>
245  *
246  * Simple code Equivalent:
247  * \code
248  * vector<vector<double> *> VV;
249  * for (n = 0; n < 20; n++) {
250  * vector<double> *vp = VV[n];
251  * vp->resize(m);
252  * }
253  * \endcode
254  * Example of function call usage to implement the simple code example:
255  * \code
256  * vector<vector<double> *> VV;
257  * resize_each(m, &VV[0], &VV[20]);
258  * \endcode
259  *
260  * @param m Integer specifying the size that each object should be resized to.
261  * @param begin Iterator pointing to the beginning of the sequence of object, belonging to the
262  * iterator class InputIter.
263  * @param end Iterator pointing to the end of the sequence of objects, belonging to the
264  * iterator class InputIter. The difference between end and begin
265  * determines the loop length
266  *
267  * @note This is currently unused.
268  * @deprecated Unused. To be removed after Cantera 2.2.
269  */
270 template<class InputIter>
271 inline void resize_each(int m, InputIter begin, InputIter end)
272 {
273  warn_deprecated("resize_each",
274  "To be removed after Cantera 2.2.");
275  for (; begin != end; ++begin) {
276  begin->resize(m);
277  }
278 }
279 
280 //! The maximum absolute value (templated version)
281 /*!
282  * The template arguments are: template<class InputIter>
283  *
284  * Simple code Equivalent:
285  * \code
286  * double x[10] amax = 0.0;
287  * for (int n = 0; n < 10; n++) {
288  * if (fabs(x[n]) > amax) amax = fabs(x[10]);
289  * }
290  * return amax;
291  * \endcode
292  * Example of function call usage to implement the simple code example:
293  * \code
294  * double x[10]
295  * double amax = absmax(x, x+10);
296  * \endcode
297  *
298  * @param begin Iterator pointing to the beginning of the x vector, belonging to the
299  * iterator class InputIter.
300  * @param end Iterator pointing to the end of the x vector, belonging to the
301  * iterator class InputIter. The difference between end and begin
302  * determines the loop length
303  */
304 template<class InputIter>
305 inline doublereal absmax(InputIter begin, InputIter end)
306 {
307  doublereal amax = 0.0;
308  for (; begin != end; ++begin) {
309  amax = std::max(fabs(*begin), amax);
310  }
311  return amax;
312 }
313 
314 //! Normalize the values in a sequence, such that they sum to 1.0 (templated version)
315 /*!
316  * The template arguments are: template<class InputIter, class OutputIter>
317  *
318  * Simple Equivalent:
319  * \code
320  * double x[10], y[10], sum = 0.0;
321  * for (int n = 0; n < 10; n++) {
322  * sum += x[10];
323  * }
324  * for (int n = 0; n < 10; n++) {
325  * y[n] = x[n]/sum;
326  * }
327  * \endcode
328  * Example of function call usage:
329  * \code
330  * double x[10], y[10];
331  * normalize(x, x+10, y);
332  * \endcode
333  *
334  * @param begin Iterator pointing to the beginning of the x vector, belonging to the
335  * iterator class InputIter.
336  * @param end Iterator pointing to the end of the x vector, belonging to the
337  * iterator class InputIter. The difference between end and begin
338  * determines the loop length
339  * @param out Iterator pointing to the beginning of the output vector, belonging to the
340  * iterator class OutputIter.
341  */
342 template<class InputIter, class OutputIter>
343 inline void normalize(InputIter begin, InputIter end,
344  OutputIter out)
345 {
346  doublereal sum = accumulate(begin, end, 0.0);
347  for (; begin != end; ++begin, ++out) {
348  *out = *begin/sum;
349  }
350 }
351 
352 //! Templated divide of each element of \a x by the corresponding element of \a y.
353 /*!
354  * The template arguments are: template<class InputIter, class OutputIter>
355  *
356  * Simple Equivalent:
357  * \code
358  * double x[10], y[10];
359  * for (n = 0; n < 10; n++) {
360  * x[n] /= y[n];
361  * }
362  * \endcode
363  * Example of code usage:
364  * \code
365  * double x[10], y[10];
366  * divide_each(x, x+10, y);
367  * \endcode
368  *
369  * @param x_begin Iterator pointing to the beginning of the x vector, belonging to the
370  * iterator class OutputIter.
371  * @param x_end Iterator pointing to the end of the x vector, belonging to the
372  * iterator class OutputIter. The difference between end and begin
373  * determines the number of inner iterations.
374  * @param y_begin Iterator pointing to the beginning of the yvector, belonging to the
375  * iterator class InputIter.
376  */
377 template<class InputIter, class OutputIter>
378 inline void divide_each(OutputIter x_begin, OutputIter x_end,
379  InputIter y_begin)
380 {
381  for (; x_begin != x_end; ++x_begin, ++y_begin) {
382  *x_begin /= *y_begin;
383  }
384 }
385 
386 //! Increment each entry in \a x by the corresponding entry in \a y.
387 /*!
388  * The template arguments are: template<class InputIter, class OutputIter>
389  *
390  * @param x_begin Iterator pointing to the beginning of the x vector, belonging to the
391  * iterator class OutputIter.
392  * @param x_end Iterator pointing to the end of the x vector, belonging to the
393  * iterator class OutputIter. The difference between end and begin
394  * determines the number of inner iterations.
395  * @param y_begin Iterator pointing to the beginning of the yvector, belonging to the
396  * iterator class InputIter.
397  */
398 template<class InputIter, class OutputIter>
399 inline void sum_each(OutputIter x_begin, OutputIter x_end,
400  InputIter y_begin)
401 {
402  for (; x_begin != x_end; ++x_begin, ++y_begin) {
403  *x_begin += *y_begin;
404  }
405 }
406 
407 //! Copies a contiguous range in a sequence to indexed
408 //! positions in another sequence.
409 /*!
410  * The template arguments are: template<class InputIter, class OutputIter, class IndexIter>
411  *
412  * Example:
413  *
414  * \code
415  * vector<double> x(3), y(20), ;
416  * vector<int> index(3);
417  * index[0] = 9;
418  * index[1] = 2;
419  * index[3] = 16;
420  * scatter_copy(x.begin(), x.end(), y.begin(), index.begin());
421  * \endcode
422  *
423  * This routine is templated 3 times.
424  * InputIter is an iterator for the source vector
425  * OutputIter is an iterator for the destination vector
426  * IndexIter is an iterator for the index into the destination vector.
427  *
428  * @param begin Iterator pointing to the beginning of the source vector, belonging to the
429  * iterator class InputIter.
430  * @param end Iterator pointing to the end of the source vector, belonging to the
431  * iterator class InputIter. The difference between end and begin
432  * determines the number of inner iterations.
433  * @param result Iterator pointing to the beginning of the output vector, belonging to the
434  * iterator class outputIter.
435  * @param index Iterator pointing to the beginning of the index vector, belonging to the
436  * iterator class IndexIter.
437  */
438 template<class InputIter, class OutputIter, class IndexIter>
439 inline void scatter_copy(InputIter begin, InputIter end,
440  OutputIter result, IndexIter index)
441 {
442  for (; begin != end; ++begin, ++index) {
443  *(result + *index) = *begin;
444  }
445 }
446 
447 
448 //! Multiply selected elements in an array by a contiguous
449 //! sequence of multipliers.
450 /*!
451  * The template arguments are: template<class InputIter, class RandAccessIter, class IndexIter>
452  *
453  * Example:
454  * \code
455  * double multipliers[] = {8.9, -2.0, 5.6};
456  * int index[] = {7, 4, 13};
457  * vector_fp data(20);
458  * ...
459  * // Multiply elements 7, 4, and 13 in data by multipliers[0], multipliers[1],and multipliers[2],
460  * // respectively
461  * scatter_mult(multipliers, multipliers + 3, data.begin(), index);
462  * \endcode
463  *
464  * @param mult_begin Iterator pointing to the beginning of the multiplier vector, belonging to the
465  * iterator class InputIter.
466  * @param mult_end Iterator pointing to the end of the multiplier vector, belonging to the
467  * iterator class InputIter. The difference between end and begin
468  * determines the number of inner iterations.
469  * @param data Iterator pointing to the beginning of the output vector, belonging to the
470  * iterator class RandAccessIter, that will be selectively multiplied.
471  * @param index Iterator pointing to the beginning of the index vector, belonging to the
472  * iterator class IndexIter.
473  */
474 template<class InputIter, class RandAccessIter, class IndexIter>
475 inline void scatter_mult(InputIter mult_begin, InputIter mult_end,
476  RandAccessIter data, IndexIter index)
477 {
478  for (; mult_begin != mult_end; ++mult_begin, ++index) {
479  *(data + *index) *= *mult_begin;
480  }
481 }
482 
483 
484 //! Divide selected elements in an array by a contiguous sequence of divisors.
485 /*!
486  * The template arguments are: template<class InputIter, class OutputIter, class IndexIter>
487  *
488  * Example:
489  * \code
490  * double divisors[] = {8.9, -2.0, 5.6};
491  * int index[] = {7, 4, 13};
492  * vector_fp data(20);
493  * ...
494  * // divide elements 7, 4, and 13 in data by divisors[7] divisors[4], and divisors[13]
495  * // respectively
496  * scatter_divide(divisors, divisors + 3, data.begin(), index);
497  * \endcode
498  *
499  * @param begin Iterator pointing to the beginning of the source vector, belonging to the
500  * iterator class InputIter.
501  * @param end Iterator pointing to the end of the source vector, belonging to the
502  * iterator class InputIter. The difference between end and begin
503  * determines the number of inner iterations.
504  * @param result Iterator pointing to the beginning of the output vector, belonging to the
505  * iterator class outputIter.
506  * @param index Iterator pointing to the beginning of the index vector, belonging to the
507  * iterator class IndexIter.
508  * @deprecated Unused. To be removed after Cantera 2.2.
509  */
510 template<class InputIter, class OutputIter, class IndexIter>
511 inline void scatter_divide(InputIter begin, InputIter end,
512  OutputIter result, IndexIter index)
513 {
514  warn_deprecated("scatter_divide",
515  "To be removed after Cantera 2.2.");
516  for (; begin != end; ++begin, ++index) {
517  *(result + *index) /= *begin;
518  }
519 }
520 
521 //! Compute \f[ \sum_k x_k \log x_k. \f].
522 /*!
523  * The template arguments are: template<class InputIter>
524  *
525  * A small number (1.0E-20) is added before taking the log. This templated
526  * class does the indicated sun. The template must be an iterator.
527  *
528  * @param begin Iterator pointing to the beginning, belonging to the
529  * iterator class InputIter.
530  * @param end Iterator pointing to the end, belonging to the
531  * iterator class InputIter.
532  * @return
533  * The return from this class is a double.
534  */
535 template<class InputIter>
536 inline doublereal sum_xlogx(InputIter begin, InputIter end)
537 {
538  doublereal sum = 0.0;
539  for (; begin != end; ++begin) {
540  sum += (*begin) * std::log(*begin + Tiny);
541  }
542  return sum;
543 }
544 
545 //! Compute \f[ \sum_k x_k \log Q_k. \f].
546 /*!
547  * The template arguments are: template<class InputIter1, class InputIter2>
548  *
549  * This class is templated twice. The first template, InputIter1
550  * is the iterator that points to $x_k$. The second iterator
551  * InputIter2, point to $Q_k$.
552  * A small number (1.0E-20) is added before taking the log.
553  *
554  * @param begin Iterator pointing to the beginning, belonging to the
555  * iterator class InputIter1.
556  * @param end Iterator pointing to the end, belonging to the
557  * iterator class InputIter1.
558  * @param Q_begin Iterator pointing to the beginning of Q_k, belonging to the
559  * iterator class InputIter2.
560  * @return
561  * The return from this class is hard coded to a doublereal.
562  */
563 template<class InputIter1, class InputIter2>
564 inline doublereal sum_xlogQ(InputIter1 begin, InputIter1 end,
565  InputIter2 Q_begin)
566 {
567  doublereal sum = 0.0;
568  for (; begin != end; ++begin, ++Q_begin) {
569  sum += (*begin) * std::log(*Q_begin + Tiny);
570  }
571  return sum;
572 }
573 
574 //! Scale a templated vector by a constant factor.
575 /*!
576  * The template arguments are: template<class OutputIter>
577  *
578  * This function is essentially a wrapper around the stl
579  * function %scale(). The function is has one template
580  * parameter, OutputIter. OutputIter is a templated iterator
581  * that points to the vector to be scaled.
582  *
583  * @param N Length of the vector
584  * @param alpha scale factor - double
585  * @param x Templated Iterator to the start of the vector
586  * to be scaled.
587  * @deprecated Unused. To be removed after Cantera 2.2.
588  */
589 template<class OutputIter>
590 inline void scale(int N, double alpha, OutputIter x)
591 {
592  warn_deprecated("scale(int N, double alpha, OutputIter x)",
593  "To be removed after Cantera 2.2.");
594  scale(x, x+N, x, alpha);
595 }
596 
597 
598 //! Templated evaluation of a polynomial of order 6
599 /*!
600  * @param x Value of the independent variable - First template parameter
601  * @param c Pointer to the polynomial - Second template parameter
602  */
603 template<class D, class R>
604 R poly6(D x, R* c)
605 {
606  return ((((((c[6]*x + c[5])*x + c[4])*x + c[3])*x +
607  c[2])*x + c[1])*x + c[0]);
608 }
609 
610 //! Templated evaluation of a polynomial of order 8
611 /*!
612  * @param x Value of the independent variable - First template parameter
613  * @param c Pointer to the polynomial - Second template parameter
614  */
615 template<class D, class R>
616 R poly8(D x, R* c)
617 {
618  return ((((((((c[8]*x + c[7])*x + c[6])*x + c[5])*x + c[4])*x + c[3])*x +
619  c[2])*x + c[1])*x + c[0]);
620 }
621 
622 //! Templated evaluation of a polynomial of order 10
623 /*!
624  * @param x Value of the independent variable - First template parameter
625  * @param c Pointer to the polynomial - Second template parameter
626  * @deprecated Unused. To be removed after Cantera 2.2.
627  */
628 template<class D, class R>
629 R poly10(D x, R* c)
630 {
631  warn_deprecated("poly10",
632  "To be removed after Cantera 2.2.");
633  return ((((((((((c[10]*x + c[9])*x + c[8])*x + c[7])*x
634  + c[6])*x + c[5])*x + c[4])*x + c[3])*x
635  + c[2])*x + c[1])*x + c[0]);
636 }
637 
638 //! Templated evaluation of a polynomial of order 5
639 /*!
640  * @param x Value of the independent variable - First template parameter
641  * @param c Pointer to the polynomial - Second template parameter
642  */
643 template<class D, class R>
644 R poly5(D x, R* c)
645 {
646  return (((((c[5]*x + c[4])*x + c[3])*x +
647  c[2])*x + c[1])*x + c[0]);
648 }
649 
650 //! Evaluates a polynomial of order 4.
651 /*!
652  * @param x Value of the independent variable.
653  * @param c Pointer to the polynomial coefficient array.
654  */
655 template<class D, class R>
656 R poly4(D x, R* c)
657 {
658  return ((((c[4]*x + c[3])*x +
659  c[2])*x + c[1])*x + c[0]);
660 }
661 
662 //! Templated evaluation of a polynomial of order 3
663 /*!
664  * @param x Value of the independent variable - First template parameter
665  * @param c Pointer to the polynomial - Second template parameter
666  */
667 template<class D, class R>
668 R poly3(D x, R* c)
669 {
670  return (((c[3]*x + c[2])*x + c[1])*x + c[0]);
671 }
672 
673 //! Templated deep copy of a std vector of pointers
674 /*!
675  * Performs a deep copy of a std vectors of pointers to an object. This template assumes that
676  * that the templated object has a functioning copy constructor.
677  * It also assumes that pointers are zero when they are not malloced.
678  *
679  * @param fromVec Vector of pointers to a templated class. This will be
680  * deep-copied to the other vector
681  * @param toVec Vector of pointers to a templated class. This will be
682  * overwritten and on return will be a copy of the fromVec
683  */
684 template<class D>
685 void deepStdVectorPointerCopy(const std::vector<D*> &fromVec, std::vector<D*> &toVec)
686 {
687  size_t is = toVec.size();
688  for (size_t i = 0; i < is; i++) {
689  delete toVec[i];
690  }
691  is = fromVec.size();
692  toVec.resize(is);
693  for (size_t i = 0; i < is; i++) {
694  if (fromVec[i]) {
695  toVec[i] = new D(*(fromVec[i]));
696  } else {
697  toVec[i] = 0;
698  }
699  }
700 }
701 
702 //@}
703 
704 //! Check to see that a number is finite (not NaN, +Inf or -Inf)
705 void checkFinite(const double tmp);
706 
707 //! Const accessor for a value in a std::map.
708 /*!
709  * This is a const alternative to operator[]. Roughly equivalent to the 'at'
710  * member function introduced in C++11. Throws std::out_of_range if the key
711  * does not exist.
712  */
713 template <class T, class U>
714 const U& getValue(const std::map<T, U>& m, const T& key) {
715  typename std::map<T,U>::const_iterator iter = m.find(key);
716  if (iter == m.end()) {
717  throw std::out_of_range("std::map: key not found");
718  } else {
719  return iter->second;
720  }
721 }
722 
723 //! Const accessor for a value in a std::map.
724 /*
725  * Similar to the two-argument version of getValue, but returns *default_val*
726  * if the key is not found instead of throwing an exception.
727  */
728 template <class T, class U>
729 const U& getValue(const std::map<T, U>& m, const T& key, const U& default_val) {
730  typename std::map<T,U>::const_iterator iter = m.find(key);
731  return (iter == m.end()) ? default_val : iter->second;
732 }
733 
734 }
735 
736 #endif
doublereal dot6(const V &x, const V &y)
Templated Inner product of two vectors of length 6.
Definition: utilities.h:106
R poly3(D x, R *c)
Templated evaluation of a polynomial of order 3.
Definition: utilities.h:668
void divide_each(OutputIter x_begin, OutputIter x_end, InputIter y_begin)
Templated divide of each element of x by the corresponding element of y.
Definition: utilities.h:378
timesConstant(T c)
Constructor.
Definition: utilities.h:39
void checkFinite(const double tmp)
Check to see that a number is finite (not NaN, +Inf or -Inf)
Definition: checkFinite.cpp:43
doublereal dot4(const V &x, const V &y)
Templated Inner product of two vectors of length 4.
Definition: utilities.h:69
R poly5(D x, R *c)
Templated evaluation of a polynomial of order 5.
Definition: utilities.h:644
R poly10(D x, R *c)
Templated evaluation of a polynomial of order 10.
Definition: utilities.h:629
This file contains definitions of terms that are used in internal routines and are unlikely to need m...
void warn_deprecated(const std::string &method, const std::string &extra)
Print a warning indicating that method is deprecated.
Definition: global.cpp:78
This file contains definitions for utility functions and text for modules, inputfiles, logs, textlogs, (see Input File Handling, Diagnostic Output, and Writing messages to the screen).
Unary operator to multiply the argument by a constant.
Definition: utilities.h:32
double operator()(T x)
Parenthesis operator returning a double.
Definition: utilities.h:49
void multiply_each(OutputIter x_begin, OutputIter x_end, InputIter y_begin)
Multiply each entry in x by the corresponding entry in y.
Definition: utilities.h:234
void resize_each(int m, InputIter begin, InputIter end)
Invoke method 'resize' with argument m for a sequence of objects (templated version) ...
Definition: utilities.h:271
R poly4(D x, R *c)
Evaluates a polynomial of order 4.
Definition: utilities.h:656
void increment_scale(InputIter begin, InputIter end, OutputIter out, S scale_factor)
Definition: utilities.h:197
R poly8(D x, R *c)
Templated evaluation of a polynomial of order 8.
Definition: utilities.h:616
void normalize(InputIter begin, InputIter end, OutputIter out)
Normalize the values in a sequence, such that they sum to 1.0 (templated version) ...
Definition: utilities.h:343
R poly6(D x, R *c)
Templated evaluation of a polynomial of order 6.
Definition: utilities.h:604
void scatter_mult(InputIter mult_begin, InputIter mult_end, RandAccessIter data, IndexIter index)
Multiply selected elements in an array by a contiguous sequence of multipliers.
Definition: utilities.h:475
doublereal absmax(InputIter begin, InputIter end)
The maximum absolute value (templated version)
Definition: utilities.h:305
doublereal sum_xlogQ(InputIter1 begin, InputIter1 end, InputIter2 Q_begin)
Compute .
Definition: utilities.h:564
const U & getValue(const std::map< T, U > &m, const T &key)
Const accessor for a value in a std::map.
Definition: utilities.h:714
doublereal sum_xlogx(InputIter begin, InputIter end)
Compute .
Definition: utilities.h:536
doublereal dot(InputIter x_begin, InputIter x_end, InputIter2 y_begin)
Function that calculates a templated inner product.
Definition: utilities.h:135
void sum_each(OutputIter x_begin, OutputIter x_end, InputIter y_begin)
Increment each entry in x by the corresponding entry in y.
Definition: utilities.h:399
doublereal dot5(const V &x, const V &y)
Templated Inner product of two vectors of length 5.
Definition: utilities.h:87
void deepStdVectorPointerCopy(const std::vector< D * > &fromVec, std::vector< D * > &toVec)
Templated deep copy of a std vector of pointers.
Definition: utilities.h:685
void scale(InputIter begin, InputIter end, OutputIter out, S scale_factor)
Multiply elements of an array by a scale factor.
Definition: utilities.h:158
const doublereal Tiny
Small number to compare differences of mole fractions against.
Definition: ct_defs.h:142
void scatter_copy(InputIter begin, InputIter end, OutputIter result, IndexIter index)
Copies a contiguous range in a sequence to indexed positions in another sequence. ...
Definition: utilities.h:439
T m_c
Stored constant value of time T.
Definition: utilities.h:54
void scatter_divide(InputIter begin, InputIter end, OutputIter result, IndexIter index)
Divide selected elements in an array by a contiguous sequence of divisors.
Definition: utilities.h:511