18 span<shared_ptr<ReactorBase>> reactors,
23 if (reactors.empty()) {
25 "Surface requires at least one adjacent reactor.");
27 vector<shared_ptr<Solution>>
adjacent;
28 for (
auto R : reactors) {
30 m_reactors.push_back(R);
39 m_surf = std::dynamic_pointer_cast<SurfPhase>(
m_solution->thermo());
42 "Solution object must have a SurfPhase object as the thermo manager.");
45 if (!soln->kinetics() ) {
46 throw CanteraError(
"ReactorSurface::ReactorSurface",
47 "Solution object must have kinetics manager.");
48 }
else if (!std::dynamic_pointer_cast<InterfaceKinetics>(soln->kinetics())) {
49 throw CanteraError(
"ReactorSurface::ReactorSurface",
50 "Kinetics manager must be an InterfaceKinetics object.");
52 m_kinetics = std::dynamic_pointer_cast<InterfaceKinetics>(
m_solution->kinetics());
53 m_thermo = m_surf.get();
55 m_sdot.resize(m_kinetics->nTotalSpecies(), 0.0);
70 m_surf->setCoveragesNoNorm(cov);
75 m_surf->setCoveragesByName(cov);
80 m_surf->setCoveragesByName(cov);
85 m_surf->getCoverages(cov);
90 m_surf->getCoverages(y);
106 m_surf->setCoveragesNoNorm(y);
108 m_kinetics->getNetProductionRates(
m_sdot);
113 size_t nsp = m_surf->nSpecies();
114 double rs0 = 1.0 / m_surf->siteDensity();
116 for (
size_t k = 1; k < nsp; k++) {
117 RHS[k] =
m_sdot[k] * rs0 * m_surf->size(k);
126 vector<double> cov(
m_nsp);
127 m_surf->getCoverages(cov);
129 for (
size_t k = 0; k <
m_nsp; k++) {
137 if (params.empty()) {
140 for (
auto& p : m_sensParams) {
141 if (p.type == SensParameterType::reaction) {
142 p.value = m_kinetics->multiplier(p.local);
143 m_kinetics->setMultiplier(p.local, p.value*params[p.global]);
144 }
else if (p.type == SensParameterType::enthalpy) {
149 m_kinetics->invalidateCache();
154 if (params.empty()) {
157 for (
auto& p : m_sensParams) {
158 if (p.type == SensParameterType::reaction) {
159 m_kinetics->setMultiplier(p.local, p.value);
160 }
else if (p.type == SensParameterType::enthalpy) {
165 m_kinetics->invalidateCache();
170 if (rxn >= m_kinetics->nReactions()) {
171 throw CanteraError(
"ReactorSurface::addSensitivityReaction",
172 "Reaction number out of range ({})", rxn);
174 size_t p = m_reactors[0]->network().registerSensitivityParameter(
175 m_kinetics->reaction(rxn)->equation(), 1.0, 1.0);
176 m_sensParams.emplace_back(
182 return m_surf->speciesIndex(nm);
187 return m_surf->speciesName(k);
202 for (
size_t k = 0; k <
m_nsp; k++) {
203 y[k] = std::max(y[k], 0.0);
205 m_surf->setCoverages(y);
206 m_surf->getCoverages(y);
215 m_f_energy.resize(m_kinetics->nTotalSpecies(), 0.0);
216 m_f_species.resize(m_kinetics->nTotalSpecies(), 0.0);
217 m_kin2net.resize(m_kinetics->nTotalSpecies(), -1);
220 for (
size_t k = 0; k <
m_nsp; k++) {
223 for (
auto R : m_reactors) {
224 size_t nsp = R->phase()->thermo()->nSpecies();
225 size_t k0 = m_kinetics->speciesOffset(*R->phase()->thermo());
226 int offset =
static_cast<int>(R->offset() + R->speciesOffset());
227 for (
size_t k = 0; k < nsp; k++) {
236 m_surf->getCoverages(y);
237 double totalSites = m_surf->siteDensity() * m_area;
238 for (
size_t k = 0; k <
m_nsp; k++) {
239 y[k] *= totalSites / m_surf->size(k);
245 vector<double> y(
neq());
247 double totalMoles = std::accumulate(y.begin(), y.end(), 0.0);
248 if (totalMoles <= 0.0) {
251 std::fill(atol.begin(), atol.end(), baseAtol * totalMoles);
256 std::copy(y.begin(), y.end(),
m_cov_tmp.begin());
257 double totalSites = m_surf->siteDensity() * m_area;
258 for (
size_t k = 0; k <
m_nsp; k++) {
259 m_cov_tmp[k] *= m_surf->size(k) / totalSites;
263 m_kinetics->getNetProductionRates(
m_sdot);
268 for (
size_t k = 0; k <
m_nsp; k++) {
269 RHS[k] =
m_sdot[k] * m_area;
276 double cov_sum = 0.0;
277 for (
size_t k = 0; k <
m_nsp; k++) {
280 RHS[0] = 1.0 - cov_sum;
295 for (
size_t k = 0; k <
m_nsp; k++) {
296 y[k] = std::max(y[k], 0.0);
302 auto sdot_ddC = m_kinetics->netProductionRates_ddCi();
303 for (
auto R : m_reactors) {
305 size_t nsp_R = R->phase()->thermo()->nSpecies();
306 size_t k0 = m_kinetics->speciesOffset(*R->phase()->thermo());
307 R->getJacobianScalingFactors(f_species, span<double>(&
m_f_energy[k0], nsp_R));
311 for (
int k = 0; k < sdot_ddC.outerSize(); k++) {
316 for (Eigen::SparseMatrix<double>::InnerIterator it(sdot_ddC, k); it; ++it) {
318 if (row == -1 || it.value() == 0.0) {
322 trips.emplace_back(row, col, it.value() *
m_f_species[k]);
324 trips.emplace_back(R->
offset(), col,
334 span<shared_ptr<ReactorBase>> reactors,
349 return 2.0 * sqrt(
Pi * m_reactors[0]->
area());
353 span<const double> ydot, span<double> residual)
355 size_t nsp = m_surf->nSpecies();
357 for (
size_t k = 1; k < nsp; k++) {
361 residual[0] = sum - 1.0;
377 std::fill(constraints.begin(), constraints.end(), 1.0);
Base class for kinetics managers and also contains the kineticsmgr module documentation (see Kinetics...
Header file for class ReactorSurface.
Header for a simple thermodynamics model of a surface phase derived from ThermoPhase,...
Base class for exceptions thrown by Cantera classes.
FlowReactorSurface(shared_ptr< Solution > soln, span< shared_ptr< ReactorBase > > reactors, bool clone, const string &name="(none)")
Create a new ReactorSurface.
double area() const override
Surface area per unit length [m].
double m_ss_rtol
steady-state relative tolerance, used to determine initial surface coverages
int m_max_ss_error_fails
maximum number of steady-state integrator error test failures
void getConstraints(span< double > constraints) override
Given a vector of length neq(), mark which variables should be considered algebraic constraints.
int m_max_ss_steps
maximum number of steady-state coverage integrator-steps
void evalDae(double t, span< const double > y, span< const double > ydot, span< double > residual) override
Evaluate the reactor governing equations.
double m_ss_atol
steady-state absolute tolerance, used to determine initial surface coverages
void getStateDae(span< double > y, span< double > ydot) override
Get the current state and derivative vector of the reactor for a DAE solver.
double upperBound(size_t k) const override
Get the upper bound on the k-th component of the local state vector.
void resetBadValues(span< double > y) override
Reset physically or mathematically problematic values, such as negative species concentrations.
vector< double > m_f_energy
Temporary storage for d(energy)/d(moles) scaling factors.
void eval(double t, span< double > LHS, span< double > RHS) override
Evaluate the reactor governing equations.
vector< Eigen::SparseMatrix< double >::StorageIndex > m_kin2net
Mapping from InterfaceKinetics species index to ReactorNet state vector index.
void evalSteady(double t, span< double > LHS, span< double > RHS) override
Evaluate the governing equations with modifications for the steady-state solver.
vector< double > m_f_species
Temporary storage for d(moles)/d(moles) scaling factor.
vector< ReactorBase * > m_kin2reactor
Mapping from InterfaceKinetics species index to the corresponding reactor.
void getJacobianElements(vector< Eigen::Triplet< double > > &trips) override
Get Jacobian elements for this reactor within the full reactor network.
double lowerBound(size_t k) const override
Get the lower bound on the k-th component of the local state vector.
void initialize(double t0=0.0) override
Initialize the reactor.
void updateState(span< const double > y) override
Set the state of the reactor to correspond to the state vector y.
vector< double > m_cov_tmp
Temporary storage for coverages.
void updateDefaultTolerances(span< double > atol, double baseAtol) override
Update default absolute tolerances based on this reactor's state variables.
void getState(span< double > y) override
Get the current state of the reactor.
Base class for reactor objects.
shared_ptr< Solution > m_solution
Composite thermo/kinetics/transport handler.
double pressure() const
Returns the current pressure (Pa) of the reactor.
size_t neq()
Number of equations (state variables) for this reactor.
size_t m_nv
Number of state variables for this reactor.
double temperature() const
Returns the current temperature (K) of the reactor's contents.
size_t m_offset
Offset into global ReactorNet state vector.
vector< double > m_sdot
species production rates on surfaces
size_t m_nsp
Number of homogeneous species in the mixture.
size_t offset() const
Get the starting offset for this reactor's state variables within the global state vector of the Reac...
A surface where reactions can occur that is in contact with the bulk fluid of a Reactor.
double upperBound(size_t k) const override
Get the upper bound on the k-th component of the local state vector.
void resetBadValues(span< double > y) override
Reset physically or mathematically problematic values, such as negative species concentrations.
void eval(double t, span< double > LHS, span< double > RHS) override
Evaluate the reactor governing equations.
double area() const override
Returns the surface area [m²].
void evalSteady(double t, span< double > LHS, span< double > RHS) override
Evaluate the governing equations with modifications for the steady-state solver.
ReactorSurface(shared_ptr< Solution > soln, span< shared_ptr< ReactorBase > > reactors, bool clone, const string &name="(none)")
Create a new ReactorSurface.
void setArea(double a) override
Set the surface area [m²].
size_t componentIndex(const string &nm) const override
Return the index in the solution vector for this reactor of the component named nm.
void setCoverages(span< const double > cov)
Set the surface coverages.
void resetSensitivity(span< const double > params) override
Reset the reaction rate multipliers.
void applySensitivity(span< const double > params) override
Set reaction rate multipliers based on the sensitivity variables in params.
vector< size_t > initializeSteady() override
Initialize the reactor before solving a steady-state problem.
void addSensitivityReaction(size_t rxn) override
Add a sensitivity parameter associated with the reaction number rxn
double lowerBound(size_t k) const override
Get the lower bound on the k-th component of the local state vector.
string componentName(size_t k) override
Return the name of the solution component with index i.
shared_ptr< ReactorBase > adjacent(size_t n)
Access an adjacent Reactor or Reservoir.
void getCoverages(span< double > cov) const
Get the surface coverages.
void initialize(double t0=0.0) override
Initialize the reactor.
void updateState(span< const double > y) override
Set the state of the reactor to correspond to the state vector y.
void getState(span< double > y) override
Get the current state of the reactor.
virtual void setState_TP(double t, double p)
Set the temperature (K) and pressure (Pa)
virtual void modifyOneHf298SS(const size_t k, const double Hf298New)
Modify the value of the 298 K Heat of Formation of one species in the phase (J kmol-1)
void invalidateCache() override
Invalidate any cached values which are normally updated only when a change in state is detected.
virtual void resetHf298(const size_t k=npos)
Restore the original heat of formation of one or more species.
Namespace for the Cantera kernel.
const double Tiny
Small number to compare differences of mole fractions against.
offset
Offsets of solution components in the 1D solution array.
const double BigNumber
largest number to compare to inf.
map< string, double > Composition
Map from string names to doubles.