Cantera  2.0
DAE_Solver.h
Go to the documentation of this file.
1 /**
2  * @file DAE_Solver.h
3  * Header file for class DAE_Solver
4  */
5 
6 // Copyright 2006 California Institute of Technology
7 
8 #ifndef CT_DAE_Solver_H
9 #define CT_DAE_Solver_H
10 
11 #include <vector>
12 
13 #include "cantera/base/ct_defs.h"
14 #include "ResidJacEval.h"
15 #include "cantera/base/global.h"
16 
17 namespace Cantera
18 {
19 
20 #define DAE_DEVEL
21 #ifdef DAE_DEVEL
22 
23 class Jacobian
24 {
25 public:
26  Jacobian() {}
27  virtual ~Jacobian() {}
28  virtual bool supplied() {
29  return false;
30  }
31  virtual bool isBanded() {
32  return false;
33  }
34  virtual int lowerBandWidth() {
35  return 0;
36  }
37  virtual int upperBandWidth() {
38  return 0;
39  }
40 };
41 
42 class BandedJacobian : public Jacobian
43 {
44 public:
45  BandedJacobian(int ml, int mu) {
46  m_ml = ml;
47  m_mu = mu;
48  }
49  virtual bool supplied() {
50  return false;
51  }
52  virtual bool isBanded() {
53  return true;
54  }
55  virtual int lowerBandWidth() {
56  return m_ml;
57  }
58  virtual int upperBandWidth() {
59  return m_mu;
60  }
61 protected:
62  int m_ml, m_mu;
63 };
64 
65 const int cDirect = 0;
66 const int cKrylov = 1;
67 
68 
69 
70 /**
71  * Wrapper for DAE solvers
72  */
74 {
75 public:
76 
78  m_resid(f),
79  m_neq(f.nEquations()),
80  m_time(0.0) {
81  }
82 
83  virtual ~DAE_Solver() {}
84 
85  /**
86  * Set error tolerances. This version specifies a scalar
87  * relative tolerance, and a vector absolute tolerance.
88  */
89  virtual void setTolerances(doublereal reltol,
90  doublereal* abstol) {
91  warn("setTolerances");
92  }
93 
94  /**
95  * Set error tolerances. This version specifies a scalar
96  * relative tolerance, and a scalar absolute tolerance.
97  */
98  virtual void setTolerances(doublereal reltol, doublereal abstol) {
99  warn("setTolerances");
100  }
101 
102  /**
103  * Specify a Jacobian evaluator. If this method is not called,
104  * the Jacobian will be computed by finite difference.
105  */
106  void setJacobian(Jacobian& jac) {
107  warn("setJacobian");
108  }
109 
110  virtual void setLinearSolverType(int solverType) {
111  warn("setLinearSolverType");
112  }
113 
114  virtual void setDenseLinearSolver() {
115  warn("setDenseLinearSolver");
116  }
117 
118  virtual void setBandedLinearSolver(int m_upper, int m_lower) {
119  warn("setBandedLinearSolver");
120  }
121  virtual void setMaxStepSize(doublereal dtmax) {
122  warn("setMaxStepSize");
123  }
124  virtual void setMaxOrder(int n) {
125  warn("setMaxOrder");
126  }
127  virtual void setMaxNumSteps(int n) {
128  warn("setMaxNumSteps");
129  }
130  virtual void setInitialStepSize(doublereal h0) {
131  warn("setInitialStepSize");
132  }
133  virtual void setStopTime(doublereal tstop) {
134  warn("setStopTime");
135  }
136  virtual void setMaxErrTestFailures(int n) {
137  warn("setMaxErrTestFailures");
138  }
139  virtual void setMaxNonlinIterations(int n) {
140  warn("setMaxNonlinIterations");
141  }
142  virtual void setMaxNonlinConvFailures(int n) {
143  warn("setMaxNonlinConvFailures");
144  }
145  virtual void inclAlgebraicInErrorTest(bool yesno) {
146  warn("inclAlgebraicInErrorTest");
147  }
148 
149  /**
150  * This method may be called if the initial conditions do not
151  * satisfy the residual equation F = 0. Given the derivatives
152  * of all variables, this method computes the initial y
153  * values.
154  */
155  virtual void correctInitial_Y_given_Yp(doublereal* y, doublereal* yp,
156  doublereal tout) {
157  warn("correctInitial_Y_given_Yp");
158  }
159 
160  /**
161  * This method may be called if the initial conditions do not
162  * satisfy the residual equation F = 0. Given the initial
163  * values of all differential variables, it computes the
164  * initial values of all algebraic variables and the initial
165  * derivatives of all differential variables.
166  */
167  virtual void correctInitial_YaYp_given_Yd(doublereal* y, doublereal* yp,
168  doublereal tout) {
169  warn("correctInitial_YaYp_given_Yd");
170  }
171 
172  /**
173  * Solve the system of equations up to time tout.
174  */
175  virtual int solve(doublereal tout) {
176  warn("solve");
177  return 0;
178  }
179 
180  /**
181  * Take one internal step.
182  */
183  virtual doublereal step(doublereal tout) {
184  warn("step");
185  return 0;
186  }
187 
188  /// Number of equations.
189  int nEquations() const {
190  return m_resid.nEquations();
191  }
192 
193  /**
194  * initialize. Base class method does nothing.
195  */
196  virtual void init(doublereal t0) {}
197 
198  /**
199  * Set a solver-specific input parameter.
200  */
201  virtual void setInputParameter(int flag, doublereal value) {
202  warn("setInputParameter");
203  }
204 
205  /**
206  * Get the value of a solver-specific output parameter.
207  */
208  virtual doublereal getOutputParameter(int flag) const {
209  warn("getOutputParameter");
210  return 0.0;
211  }
212 
213  /// the current value of solution component k.
214  virtual doublereal solution(int k) const {
215  warn("solution");
216  return 0.0;
217  }
218 
219  virtual const doublereal* solutionVector() const {
220  warn("solutionVector");
221  return &m_dummy;
222  }
223 
224  /// the current value of the derivative of solution component k.
225  virtual doublereal derivative(int k) const {
226  warn("derivative");
227  return 0.0;
228  }
229 
230  virtual const doublereal* derivativeVector() const {
231  warn("derivativeVector");
232  return &m_dummy;
233  }
234 
235 protected:
236 
237  doublereal m_dummy;
238 
239  ResidJacEval& m_resid;
240 
241  //! Number of total equations in the system
242  integer m_neq;
243  doublereal m_time;
244 
245 
246 private:
247  void warn(std::string msg) const {
248  writelog(">>>> Warning: method "+msg+" of base class "
249  +"DAE_Solver called. Nothing done.\n");
250  }
251 };
252 
253 
254 //! Factor method for choosing a DAE solver
255 /*!
256  *
257  * @param itype String identifying the type
258  * (IDA is the only option)
259  * @param f Residual function to be solved by the DAE algorithm
260  *
261  * @return Returns a point to the instantiated DAE_Solver object
262  */
263 DAE_Solver* newDAE_Solver(std::string itype, ResidJacEval& f);
264 
265 #endif
266 
267 }
268 
269 #endif