17 ReactorNet::ReactorNet() :
18 m_integ(newIntegrator(
"CVODE")),
19 m_time(0.0), m_init(false), m_integrator_init(false),
20 m_nv(0), m_rtol(1.0e-9), m_rtolsens(1.0e-4),
21 m_atols(1.0e-15), m_atolsens(1.0e-6),
22 m_maxstep(0.0), m_maxErrTestFails(0),
30 m_integ->setProblemType(DENSE + NOJAC);
34 void ReactorNet::setInitialTime(
double time)
37 m_integrator_init =
false;
40 void ReactorNet::setMaxTimeStep(
double maxstep)
46 void ReactorNet::setMaxErrTestFails(
int nmax)
48 m_maxErrTestFails = nmax;
52 void ReactorNet::setTolerances(
double rtol,
double atol)
63 void ReactorNet::setSensitivityTolerances(
double rtol,
double atol)
74 void ReactorNet::initialize()
77 debuglog(
"Initializing reactor network.\n", m_verbose);
78 if (m_reactors.empty()) {
80 "no reactors in network!");
83 for (
size_t n = 0; n < m_reactors.size(); n++) {
88 m_start.push_back(m_nv);
91 writelog(
"Reactor {:d}: {:d} variables.\n", n, nv);
94 if (r.
type() == FlowReactorType && m_reactors.size() > 1) {
96 "FlowReactors must be used alone.");
100 m_ydot.resize(m_nv,0.0);
101 m_atol.resize(neq());
102 fill(m_atol.begin(), m_atol.end(), m_atols);
103 m_integ->setTolerances(m_rtol, neq(), m_atol.data());
104 m_integ->setSensitivityTolerances(m_rtolsens, m_atolsens);
105 m_integ->setMaxStepSize(m_maxstep);
106 m_integ->setMaxErrTestFails(m_maxErrTestFails);
108 writelog(
"Number of equations: {:d}\n", neq());
109 writelog(
"Maximum time step: {:14.6g}\n", m_maxstep);
111 m_integ->initialize(m_time, *
this);
112 m_integrator_init =
true;
116 void ReactorNet::reinitialize()
119 debuglog(
"Re-initializing reactor network.\n", m_verbose);
120 m_integ->reinitialize(m_time, *
this);
121 m_integrator_init =
true;
127 void ReactorNet::advance(doublereal time)
131 }
else if (!m_integrator_init) {
134 m_integ->integrate(time);
136 updateState(m_integ->solution());
139 double ReactorNet::step(doublereal time)
143 " is deprecated and will be removed after Cantera 2.3.");
147 }
else if (!m_integrator_init) {
150 m_time = m_integ->step(m_time + 1.0);
151 updateState(m_integ->solution());
158 m_reactors.push_back(&r);
161 void ReactorNet::eval(doublereal t, doublereal* y,
162 doublereal* ydot, doublereal* p)
165 for (
size_t n = 0; n < m_reactors.size(); n++) {
166 m_reactors[n]->evalEqs(t, y + m_start[n], ydot + m_start[n], p);
171 double ReactorNet::sensitivity(
size_t k,
size_t p)
176 if (p >= m_sens_params.size()) {
178 "m_sens_params", p, m_sens_params.size()-1);
180 double denom = m_integ->solution(k);
184 return m_integ->sensitivity(k, p) / denom;
187 void ReactorNet::evalJacobian(doublereal t, doublereal* y,
188 doublereal* ydot, doublereal* p,
Array2D* j)
192 for (
size_t n = 0; n < m_nv; n++) {
195 double dy = m_atol[n] + fabs(ysave)*m_rtol;
200 eval(t, y, m_ydot.data(), p);
203 for (
size_t m = 0; m < m_nv; m++) {
204 j->
value(m,n) = (m_ydot[m] - ydot[m])/dy;
210 void ReactorNet::updateState(doublereal* y)
213 for (
size_t n = 0; n < m_reactors.size(); n++) {
214 m_reactors[n]->updateState(y + m_start[n]);
218 void ReactorNet::getInitialConditions(
double t0,
size_t leny,
double* y)
221 "Use getState instead. To be removed after Cantera 2.3.");
225 void ReactorNet::getState(
double* y)
227 for (
size_t n = 0; n < m_reactors.size(); n++) {
228 m_reactors[n]->getState(y + m_start[n]);
232 size_t ReactorNet::globalComponentIndex(
const string& component,
size_t reactor)
237 return m_start[reactor] + m_reactors[reactor]->componentIndex(component);
240 std::string ReactorNet::componentName(
size_t i)
const 242 for (
auto r : m_reactors) {
244 return r->name() +
": " + r->componentName(i);
249 throw CanteraError(
"ReactorNet::componentName",
"Index out of bounds");
252 size_t ReactorNet::registerSensitivityParameter(
253 const std::string& name,
double value,
double scale)
255 if (m_integrator_init) {
256 throw CanteraError(
"ReactorNet::registerSensitivityParameter",
257 "Sensitivity parameters cannot be added after the" 258 "integrator has been initialized.");
260 m_paramNames.push_back(name);
261 m_sens_params.push_back(value);
262 m_paramScales.push_back(
scale);
263 return m_sens_params.size() - 1;
Backward Differentiation.
void checkFinite(const double tmp)
Check to see that a number is finite (not NaN, +Inf or -Inf)
virtual size_t nSensParams()
Number of sensitivity parameters associated with this reactor (including walls)
Header file for class Wall.
void writelog(const std::string &fmt, const Args &... args)
Write a formatted message to the screen.
void warn_deprecated(const std::string &method, const std::string &extra)
Print a warning indicating that method is deprecated.
virtual void initialize(doublereal t0=0.0)
Initialize the reactor.
A class for 2D arrays stored in column-major (Fortran-compatible) form.
Base class for exceptions thrown by Cantera classes.
doublereal & value(size_t i, size_t j)
Returns a changeable reference to position in the matrix.
void setNetwork(ReactorNet *net)
Set the ReactorNet that this reactor belongs to.
void debuglog(const std::string &msg, int loglevel)
Write a message to the log only if loglevel > 0.
const doublereal SmallNumber
smallest number to compare to zero.
void scale(InputIter begin, InputIter end, OutputIter out, S scale_factor)
Multiply elements of an array by a scale factor.
virtual int type() const
Return a constant indicating the type of this Reactor.
virtual size_t neq()
Number of equations (state variables) for this reactor.
An array index is out of range.
Namespace for the Cantera kernel.
Class Reactor is a general-purpose class for stirred reactors.