hips
Loading...
Searching...
No Matches
batchReactor_cvode.cc
Go to the documentation of this file.
2
14
15int rhsf_cvode(sunrealtype t, N_Vector varsCV, N_Vector dvarsdtCV, void *user_data);
16
25
26batchReactor_cvode::batchReactor_cvode(std::shared_ptr<Cantera::Solution> cantSol) {
27
28 gas = cantSol->thermo();
29 kin = cantSol->kinetics();
30
31 nvar = gas->nSpecies();
32
33 // Set absolute and relative tolerances
34 std::vector<double> atol(nvar, 1E-10);
35 double rtol = 1E-4;
36
37 // Initialize the integrator with CVODE
38 integrator = std::make_shared<integrator_cvode>(rhsf_cvode, this, nvar, rtol, atol);
39}
40
49
50void batchReactor_cvode::react(double &h, std::vector<double> &y, const double tRun) {
51
52 // Store fixed enthalpy and pressure
53 h_fixed = h;
54 P_fixed = gas->pressure();
55
56 // Integrate over the given time
57 integrator->integrate(y, tRun);
58}
59
71
72int batchReactor_cvode::rhsf(const double t, const double *vars, double *dvarsdt) {
73
74 // Set mass fractions and state
75 gas->setMassFractions_NoNorm(vars);
76 gas->setState_HP(h_fixed, P_fixed);
77
78 // Calculate density and reaction rates
79 rho = gas->density();
80 temperature = gas->temperature();
81
82 std::vector<double> rr(nvar);
83 kin->getNetProductionRates(&rr[0]);
84 for (size_t k = 0; k < gas->nSpecies(); k++)
85 dvarsdt[k] = rr[k] * gas->molecularWeight(k) / rho;
86
87 return 0;
88}
89
91// CVODE interface; CVODE calls this function, which then calls user_data's rhsf
92
103
104int rhsf_cvode(sunrealtype t, N_Vector varsCV, N_Vector dvarsdtCV, void *user_data) {
105
106 batchReactor_cvode *bRxr = static_cast<batchReactor_cvode *>(user_data); // Cast user_data pointer to batchReactor_cvode pointer
107
108 double *vars = N_VGetArrayPointer_Serial(varsCV); // Get array pointers for variables and derivatives
109
110 double *dvarsdt = N_VGetArrayPointer_Serial(dvarsdtCV); // Call rhsf method of batchReactor_cvode instance
111
112 int rv = bRxr->rhsf(t, vars, dvarsdt);
113
114 return rv;
115}
int rhsf_cvode(sunrealtype t, N_Vector varsCV, N_Vector dvarsdtCV, void *user_data)
Callback function for the right-hand side of the ODE system used by CVODE.
batchReactor_cvode(std::shared_ptr< Cantera::Solution > cantSol)
Constructor for the batchReactor_cvode class.
std::shared_ptr< integrator_cvode > integrator
cvode integrator wrapper
int rhsf(const double t, const double *vars, double *dvarsdt)
Computes the right-hand side of the ODE system.
virtual void react(double &h, std::vector< double > &y, const double tRun)
Simulates a reaction in the batch reactor.
int nvar
number of variables/equations solved
double rho
density during integrate
std::shared_ptr< Cantera::Kinetics > kin
Cantera kinetics object.
double P_fixed
pressure during integrate
std::shared_ptr< Cantera::ThermoPhase > gas
Cantera thermo object.
double h_fixed
adiabatic h during integrate
double temperature
temperature during integrate