Cantera  2.5.1
ReactorNet.h
Go to the documentation of this file.
1 //! @file ReactorNet.h
2 
3 // This file is part of Cantera. See License.txt in the top-level directory or
4 // at https://cantera.org/license.txt for license and copyright information.
5 
6 #ifndef CT_REACTORNET_H
7 #define CT_REACTORNET_H
8 
9 #include "Reactor.h"
12 #include "cantera/base/Array.h"
13 
14 namespace Cantera
15 {
16 
17 //! A class representing a network of connected reactors.
18 /*!
19  * This class is used to integrate the time-dependent governing equations for
20  * a network of reactors (Reactor, ConstPressureReactor) connected by various
21  * means, e.g. Wall, MassFlowController, Valve, PressureController.
22  */
23 class ReactorNet : public FuncEval
24 {
25 public:
26  ReactorNet();
27  virtual ~ReactorNet() {};
28  ReactorNet(const ReactorNet&) = delete;
29  ReactorNet& operator=(const ReactorNet&) = delete;
30 
31  //! @name Methods to set up a simulation.
32  //@{
33 
34  //! Set initial time. Default = 0.0 s. Restarts integration from this time
35  //! using the current mixture state as the initial condition.
36  void setInitialTime(double time);
37 
38  //! Get the maximum time step.
39  double maxTimeStep() {
40  return m_maxstep;
41  }
42 
43  //! Set the maximum time step.
44  void setMaxTimeStep(double maxstep);
45 
46  //! Set the maximum number of error test failures permitted by the CVODES
47  //! integrator in a single time step.
48  void setMaxErrTestFails(int nmax);
49 
50  //! Set the relative and absolute tolerances for the integrator.
51  void setTolerances(double rtol, double atol);
52 
53  //! Set the relative and absolute tolerances for integrating the
54  //! sensitivity equations.
55  void setSensitivityTolerances(double rtol, double atol);
56 
57  //! Current value of the simulation time.
58  doublereal time() {
59  return m_time;
60  }
61 
62  //! Relative tolerance.
63  doublereal rtol() {
64  return m_rtol;
65  }
66 
67  //! Absolute integration tolerance
68  doublereal atol() {
69  return m_atols;
70  }
71 
72  //! Relative sensitivity tolerance
73  doublereal rtolSensitivity() const {
74  return m_rtolsens;
75  }
76 
77  //! Absolute sensitivity tolerance
78  doublereal atolSensitivity() const {
79  return m_atolsens;
80  }
81 
82  /**
83  * Advance the state of all reactors in time. Take as many internal
84  * timesteps as necessary to reach *time*.
85  * @param time Time to advance to (s).
86  */
87  void advance(doublereal time);
88 
89  /**
90  * Advance the state of all reactors in time. Take as many internal
91  * timesteps as necessary towards *time*. If *applylimit* is true,
92  * the advance step will be automatically reduced if needed to
93  * stay within limits (set by setAdvanceLimit).
94  * Returns the time at the end of integration.
95  * @param time Time to advance to (s).
96  * @param applylimit Limit advance step (boolean).
97  */
98  double advance(double time, bool applylimit);
99 
100  //! Advance the state of all reactors in time.
101  double step();
102 
103  //@}
104 
105  //! Add the reactor *r* to this reactor network.
106  void addReactor(Reactor& r);
107 
108  //! Return a reference to the *n*-th reactor in this network. The reactor
109  //! indices are determined by the order in which the reactors were added
110  //! to the reactor network.
111  Reactor& reactor(int n) {
112  return *m_reactors[n];
113  }
114 
115  //! Returns `true` if verbose logging output is enabled.
116  bool verbose() const {
117  return m_verbose;
118  }
119 
120  //! Enable or disable verbose logging while setting up and integrating the
121  //! reactor network.
122  void setVerbose(bool v = true) {
123  m_verbose = v;
124  suppressErrors(!m_verbose);
125  }
126 
127  //! Return a reference to the integrator.
129  return *m_integ;
130  }
131 
132  //! Update the state of all the reactors in the network to correspond to
133  //! the values in the solution vector *y*.
134  void updateState(doublereal* y);
135 
136  //! Return the sensitivity of the *k*-th solution component with respect to
137  //! the *p*-th sensitivity parameter.
138  /*!
139  * The sensitivity coefficient \f$ S_{ki} \f$ of solution variable \f$ y_k
140  * \f$ with respect to sensitivity parameter \f$ p_i \f$ is defined as:
141  *
142  * \f[ S_{ki} = \frac{1}{y_k} \frac{\partial y_k}{\partial p_i} \f]
143  *
144  * For reaction sensitivities, the parameter is a multiplier on the forward
145  * rate constant (and implicitly on the reverse rate constant for
146  * reversible reactions) which has a nominal value of 1.0, and the
147  * sensitivity is nondimensional.
148  *
149  * For species enthalpy sensitivities, the parameter is a perturbation to
150  * the molar enthalpy of formation, such that the dimensions of the
151  * sensitivity are kmol/J.
152  */
153  double sensitivity(size_t k, size_t p);
154 
155  //! Return the sensitivity of the component named *component* with respect to
156  //! the *p*-th sensitivity parameter.
157  //! @copydetails ReactorNet::sensitivity(size_t, size_t)
158  double sensitivity(const std::string& component, size_t p, int reactor=0) {
159  size_t k = globalComponentIndex(component, reactor);
160  return sensitivity(k, p);
161  }
162 
163  //! Evaluate the Jacobian matrix for the reactor network.
164  /*!
165  * @param[in] t Time at which to evaluate the Jacobian
166  * @param[in] y Global state vector at time *t*
167  * @param[out] ydot Time derivative of the state vector evaluated at *t*.
168  * @param[in] p sensitivity parameter vector (unused?)
169  * @param[out] j Jacobian matrix, size neq() by neq().
170  */
171  void evalJacobian(doublereal t, doublereal* y,
172  doublereal* ydot, doublereal* p, Array2D* j);
173 
174  // overloaded methods of class FuncEval
175  virtual size_t neq() {
176  return m_nv;
177  }
178  virtual void eval(doublereal t, doublereal* y,
179  doublereal* ydot, doublereal* p);
180 
181  virtual void getState(doublereal* y);
182 
183  //! Return k-th derivative at the current time
184  virtual void getDerivative(int k, double* dky);
185 
186  virtual size_t nparams() {
187  return m_sens_params.size();
188  }
189 
190  //! Return the index corresponding to the component named *component* in the
191  //! reactor with index *reactor* in the global state vector for the
192  //! reactor network.
193  size_t globalComponentIndex(const std::string& component, size_t reactor=0);
194 
195  //! Return the name of the i-th component of the global state vector. The
196  //! name returned includes both the name of the reactor and the specific
197  //! component, e.g. `'reactor1: CH4'`.
198  std::string componentName(size_t i) const;
199 
200  //! Used by Reactor and Wall objects to register the addition of
201  //! sensitivity parameters so that the ReactorNet can keep track of the
202  //! order in which sensitivity parameters are added.
203  //! @param name A name describing the parameter, e.g. the reaction string
204  //! @param value The nominal value of the parameter
205  //! @param scale A scaling factor to be applied to the sensitivity
206  //! coefficient
207  //! @returns the index of this parameter in the vector of sensitivity
208  //! parameters (global across all reactors)
209  size_t registerSensitivityParameter(const std::string& name, double value,
210  double scale);
211 
212  //! The name of the p-th sensitivity parameter added to this ReactorNet.
213  const std::string& sensitivityParameterName(size_t p) {
214  return m_paramNames.at(p);
215  }
216 
217  //! Initialize the reactor network. Called automatically the first time
218  //! advance or step is called.
219  void initialize();
220 
221  //! Reinitialize the integrator. Used to solve a new problem (different
222  //! initial conditions) but with the same configuration of the reactor
223  //! network. Can be called manually, or automatically after calling
224  //! setInitialTime or modifying a reactor's contents.
225  void reinitialize();
226 
227  //! Called to trigger integrator reinitialization before further
228  //! integration.
229  void setNeedsReinit() {
230  m_integrator_init = false;
231  }
232 
233  //! Set the maximum number of internal integration time-steps the
234  //! integrator will take before reaching the next output time
235  //! @param nmax The maximum number of steps, setting this value
236  //! to zero disables this option.
237  virtual void setMaxSteps(int nmax) {
238  m_integ->setMaxSteps(nmax);
239  }
240 
241  //! Returns the maximum number of internal integration time-steps the
242  //! integrator will take before reaching the next output time
243  //!
244  virtual int maxSteps() {
245  return m_integ->maxSteps();
246  }
247 
248  //! Set absolute step size limits during advance
249  void setAdvanceLimits(const double* limits);
250 
251  //! Check whether ReactorNet object uses advance limits
252  bool hasAdvanceLimits();
253 
254  //! Retrieve absolute step size limits during advance
255  bool getAdvanceLimits(double* limits);
256 
257 protected:
258 
259  //! Estimate a future state based on current derivatives.
260  //! The function is intended for internal use by ReactorNet::advance
261  //! and deliberately not exposed in external interfaces.
262  virtual void getEstimate(double time, int k, double* yest);
263 
264  //! Returns the order used for last solution step of the ODE integrator
265  //! The function is intended for internal use by ReactorNet::advance
266  //! and deliberately not exposed in external interfaces.
267  virtual int lastOrder() {
268  return m_integ->lastOrder();
269  }
270 
271  std::vector<Reactor*> m_reactors;
272  std::unique_ptr<Integrator> m_integ;
273  doublereal m_time;
274  bool m_init;
275  bool m_integrator_init; //!< True if integrator initialization is current
276  size_t m_nv;
277 
278  //! m_start[n] is the starting point in the state vector for reactor n
279  std::vector<size_t> m_start;
280 
281  vector_fp m_atol;
282  doublereal m_rtol, m_rtolsens;
283  doublereal m_atols, m_atolsens;
284 
285  //! Maximum integrator internal timestep. Default of 0.0 means infinity.
286  doublereal m_maxstep;
287 
288  int m_maxErrTestFails;
289  bool m_verbose;
290 
291  //! Names corresponding to each sensitivity parameter
292  std::vector<std::string> m_paramNames;
293 
294  vector_fp m_ydot;
295  vector_fp m_yest;
296  vector_fp m_advancelimits;
297 };
298 }
299 
300 #endif
Header file for class Cantera::Array2D.
A class for 2D arrays stored in column-major (Fortran-compatible) form.
Definition: Array.h:32
Virtual base class for ODE right-hand-side function evaluators.
Definition: FuncEval.h:27
vector_fp m_sens_params
Values for the problem parameters for which sensitivities are computed This is the array which is per...
Definition: FuncEval.h:81
bool suppressErrors() const
Get current state of error suppression.
Definition: FuncEval.h:71
Abstract base class for ODE system integrators.
Definition: Integrator.h:53
A class representing a network of connected reactors.
Definition: ReactorNet.h:24
virtual size_t nparams()
Number of sensitivity parameters.
Definition: ReactorNet.h:186
void updateState(doublereal *y)
Update the state of all the reactors in the network to correspond to the values in the solution vecto...
Definition: ReactorNet.cpp:283
double step()
Advance the state of all reactors in time.
Definition: ReactorNet.cpp:195
std::vector< size_t > m_start
m_start[n] is the starting point in the state vector for reactor n
Definition: ReactorNet.h:279
virtual void setMaxSteps(int nmax)
Set the maximum number of internal integration time-steps the integrator will take before reaching th...
Definition: ReactorNet.h:237
virtual int maxSteps()
Returns the maximum number of internal integration time-steps the integrator will take before reachin...
Definition: ReactorNet.h:244
void initialize()
Initialize the reactor network.
Definition: ReactorNet.cpp:73
void advance(doublereal time)
Advance the state of all reactors in time.
Definition: ReactorNet.cpp:128
bool getAdvanceLimits(double *limits)
Retrieve absolute step size limits during advance.
Definition: ReactorNet.cpp:325
std::vector< std::string > m_paramNames
Names corresponding to each sensitivity parameter.
Definition: ReactorNet.h:292
void setNeedsReinit()
Called to trigger integrator reinitialization before further integration.
Definition: ReactorNet.h:229
virtual int lastOrder()
Returns the order used for last solution step of the ODE integrator The function is intended for inte...
Definition: ReactorNet.h:267
doublereal rtol()
Relative tolerance.
Definition: ReactorNet.h:63
doublereal m_maxstep
Maximum integrator internal timestep. Default of 0.0 means infinity.
Definition: ReactorNet.h:286
std::string componentName(size_t i) const
Return the name of the i-th component of the global state vector.
Definition: ReactorNet.cpp:342
double maxTimeStep()
Get the maximum time step.
Definition: ReactorNet.h:39
void addReactor(Reactor &r)
Add the reactor r to this reactor network.
Definition: ReactorNet.cpp:227
virtual void getState(doublereal *y)
Fill in the vector y with the current state of the system.
Definition: ReactorNet.cpp:291
virtual size_t neq()
Number of equations.
Definition: ReactorNet.h:175
void setInitialTime(double time)
Set initial time.
Definition: ReactorNet.cpp:33
virtual void getDerivative(int k, double *dky)
Return k-th derivative at the current time.
Definition: ReactorNet.cpp:298
void setMaxErrTestFails(int nmax)
Set the maximum number of error test failures permitted by the CVODES integrator in a single time ste...
Definition: ReactorNet.cpp:45
size_t globalComponentIndex(const std::string &component, size_t reactor=0)
Return the index corresponding to the component named component in the reactor with index reactor in ...
Definition: ReactorNet.cpp:334
doublereal atolSensitivity() const
Absolute sensitivity tolerance.
Definition: ReactorNet.h:78
void setSensitivityTolerances(double rtol, double atol)
Set the relative and absolute tolerances for integrating the sensitivity equations.
Definition: ReactorNet.cpp:62
Reactor & reactor(int n)
Return a reference to the n-th reactor in this network.
Definition: ReactorNet.h:111
doublereal rtolSensitivity() const
Relative sensitivity tolerance.
Definition: ReactorNet.h:73
double sensitivity(size_t k, size_t p)
Return the sensitivity of the k-th solution component with respect to the p-th sensitivity parameter.
Definition: ReactorNet.cpp:244
bool hasAdvanceLimits()
Check whether ReactorNet object uses advance limits.
Definition: ReactorNet.cpp:316
void setAdvanceLimits(const double *limits)
Set absolute step size limits during advance.
Definition: ReactorNet.cpp:306
void evalJacobian(doublereal t, doublereal *y, doublereal *ydot, doublereal *p, Array2D *j)
Evaluate the Jacobian matrix for the reactor network.
Definition: ReactorNet.cpp:260
doublereal time()
Current value of the simulation time.
Definition: ReactorNet.h:58
bool verbose() const
Returns true if verbose logging output is enabled.
Definition: ReactorNet.h:116
const std::string & sensitivityParameterName(size_t p)
The name of the p-th sensitivity parameter added to this ReactorNet.
Definition: ReactorNet.h:213
double sensitivity(const std::string &component, size_t p, int reactor=0)
Return the sensitivity of the component named component with respect to the p-th sensitivity paramete...
Definition: ReactorNet.h:158
void setMaxTimeStep(double maxstep)
Set the maximum time step.
Definition: ReactorNet.cpp:39
Integrator & integrator()
Return a reference to the integrator.
Definition: ReactorNet.h:128
size_t registerSensitivityParameter(const std::string &name, double value, double scale)
Used by Reactor and Wall objects to register the addition of sensitivity parameters so that the React...
Definition: ReactorNet.cpp:354
bool m_integrator_init
True if integrator initialization is current.
Definition: ReactorNet.h:275
void reinitialize()
Reinitialize the integrator.
Definition: ReactorNet.cpp:117
void setVerbose(bool v=true)
Enable or disable verbose logging while setting up and integrating the reactor network.
Definition: ReactorNet.h:122
virtual void eval(doublereal t, doublereal *y, doublereal *ydot, doublereal *p)
Evaluate the right-hand-side function.
Definition: ReactorNet.cpp:233
doublereal atol()
Absolute integration tolerance.
Definition: ReactorNet.h:68
void setTolerances(double rtol, double atol)
Set the relative and absolute tolerances for the integrator.
Definition: ReactorNet.cpp:51
virtual void getEstimate(double time, int k, double *yest)
Estimate a future state based on current derivatives.
Definition: ReactorNet.cpp:207
Class Reactor is a general-purpose class for stirred reactors.
Definition: Reactor.h:38
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:180
Namespace for the Cantera kernel.
Definition: AnyMap.cpp:264
void scale(InputIter begin, InputIter end, OutputIter out, S scale_factor)
Multiply elements of an array by a scale factor.
Definition: utilities.h:135