Cantera  2.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ResidEval.h
Go to the documentation of this file.
1 /**
2  * @file ResidEval.h
3  */
4 
5 // Copyright 2006 California Institute of Technology
6 
7 #ifndef CT_RESIDEVAL_H
8 #define CT_RESIDEVAL_H
9 
10 #include "cantera/base/ct_defs.h"
12 #include "cantera/base/utilities.h"
13 
14 #include <cstdio>
15 
16 namespace Cantera
17 {
18 
19 const int c_NONE = 0;
20 const int c_GE_ZERO = 1;
21 const int c_GT_ZERO = 2;
22 const int c_LE_ZERO = -1;
23 const int c_LT_ZERO = -2;
24 
25 /**
26  * Virtual base class for DAE residual function evaluators.
27  * Classes derived from ResidEval evaluate the residual function
28  * \f[
29  * \vec{F}(t,\vec{y}, \vec{y^\prime})
30  * \f]
31  * The DAE solver attempts to find a solution y(t) such that F = 0.
32  * @ingroup DAE_Group
33  */
34 class ResidEval
35 {
36 public:
37  ResidEval() {}
38  virtual ~ResidEval() {}
39 
40  /**
41  * Constrain solution component k. Possible values for
42  * 'flag' are:
43  * - c_NONE no constraint
44  * - c_GE_ZERO >= 0
45  * - c_GT_ZERO > 0
46  * - c_LE_ZERO <= 0
47  * - c_LT_ZERO < 0
48  */
49  virtual void constrain(const int k, const int flag) {
50  m_constrain[k] = flag;
51  }
52  int constraint(const int k) const {
53  return getValue(m_constrain, k, c_NONE);
54  }
55 
56  //! Initialization function
57  virtual void initSizes() {
58  int neq = nEquations();
59  m_alg.resize(neq, 0);
60  }
61 
62  /**
63  * Specify that solution component k is purely algebraic -
64  * that is, the derivative of this component does not appear
65  * in the residual function.
66  */
67  virtual void setAlgebraic(const int k) {
68  if ((int) m_alg.size() < (k+1)) {
69  initSizes();
70  }
71  m_alg[k] = 1;
72  }
73 
74  virtual bool isAlgebraic(const int k) {
75  return (m_alg[k] == 1);
76  }
77 
78  /**
79  * Evaluate the residual function. Called by the integrator.
80  * @param t time. (input)
81  * @param y solution vector. (input)
82  * @param ydot rate of change of solution vector. (input)
83  * @param r residual vector (output)
84  */
85  virtual int eval(const doublereal t, const doublereal* const y,
86  const doublereal* const ydot,
87  doublereal* const r) {
88  throw CanteraError("ResidEval::eval()", "base class called");
89  }
90 
91  virtual int evalSS(const doublereal t, const doublereal* const y,
92  doublereal* const r) {
93  return eval(t, y, 0, r);
94  }
95 
96  virtual int evalSimpleTD(const doublereal t, const doublereal* const y,
97  const doublereal* const yold, doublereal deltaT,
98  doublereal* const r) {
99  int nn = nEquations();
100  vector_fp ydot(nn);
101  for (int i = 0; i < nn; i++) {
102  ydot[i] = (y[i] - yold[i]) / deltaT;
103  }
104  return eval(t, y, DATA_PTR(ydot), r);
105  }
106 
107  //! Fill in the initial conditions
108  /*!
109  * Values for both the solution and the value of ydot may be provided.
110  *
111  * @param[in] t0 Time
112  * @param[out] y Solution vector
113  * @param[out] ydot Rate of change of solution vector.
114  *
115  * @return Returns a flag to indicate that operation is successful.
116  * 1 Means a successful operation
117  * -0 or neg value Means an unsuccessful operation
118  */
119  virtual int getInitialConditions(const doublereal t0, doublereal* const y,
120  doublereal* const ydot) {
121  initSizes();
122  throw CanteraError("ResidEval::GetInitialConditions()", "base class called");
123  return 1;
124  }
125 
126  //! Return the number of equations in the equation system
127  virtual int nEquations() const = 0;
128 
129 
130  //! Write out to a file or to standard output the current solution
131  /*!
132  * ievent is a description of the event that caused this
133  * function to be called.
134  */
135  virtual void writeSolution(int ievent, const double time,
136  const double deltaT,
137  const int time_step_num,
138  const double* y, const double* ydot) {
139  int k;
140  printf("ResidEval::writeSolution\n");
141  printf(" Time = %g, ievent = %d, deltaT = %g\n", time, ievent, deltaT);
142  if (ydot) {
143  printf(" k y[] ydot[]\n");
144  for (k = 0; k < nEquations(); k++) {
145  printf("%d %g %g\n", k, y[k], ydot[k]);
146  }
147  } else {
148  printf(" k y[]\n");
149  for (k = 0; k < nEquations(); k++) {
150  printf("%d %g \n", k, y[k]);
151  }
152  }
153  }
154 
155  //! Return the number of parameters in the calculation
156  /*!
157  * This is the number of parameters in the sensitivity calculation. We have
158  * set this to zero and have included it for later expansion
159  */
160  int nparams() const {
161  return 0;
162  }
163 
164 protected:
165 
166  //! Mapping vector that stores whether a degree of freedom is a DAE or not
167  /*!
168  * The first index is the equation number. The second index is 1 if it is a DAE,
169  * and zero if it is not.
170  */
171  std::vector<int> m_alg;
172  std::map<int, int> m_constrain;
173 
174 private:
175 
176 };
177 
178 }
179 
180 #endif
virtual void initSizes()
Initialization function.
Definition: ResidEval.h:57
Various templated functions that carry out common vector operations (see Templated Utility Functions)...
This file contains definitions of terms that are used in internal routines and are unlikely to need m...
virtual void writeSolution(int ievent, const double time, const double deltaT, const int time_step_num, const double *y, const double *ydot)
Write out to a file or to standard output the current solution.
Definition: ResidEval.h:135
virtual int getInitialConditions(const doublereal t0, doublereal *const y, doublereal *const ydot)
Fill in the initial conditions.
Definition: ResidEval.h:119
Virtual base class for DAE residual function evaluators.
Definition: ResidEval.h:34
int nparams() const
Return the number of parameters in the calculation.
Definition: ResidEval.h:160
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:99
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
virtual int nEquations() const =0
Return the number of equations in the equation system.
std::vector< double > vector_fp
Turn on the use of stl vectors for the basic array type within cantera Vector of doubles.
Definition: ct_defs.h:157
virtual void constrain(const int k, const int flag)
Constrain solution component k.
Definition: ResidEval.h:49
virtual void setAlgebraic(const int k)
Specify that solution component k is purely algebraic - that is, the derivative of this component doe...
Definition: ResidEval.h:67
#define DATA_PTR(vec)
Creates a pointer to the start of the raw data for a vector.
Definition: ct_defs.h:36
virtual int eval(const doublereal t, const doublereal *const y, const doublereal *const ydot, doublereal *const r)
Evaluate the residual function.
Definition: ResidEval.h:85
std::vector< int > m_alg
Mapping vector that stores whether a degree of freedom is a DAE or not.
Definition: ResidEval.h:171
Definitions for the classes that are thrown when Cantera experiences an error condition (also contain...