17 static double calc_damping(
double* x,
double* dx,
size_t dim,
int*);
23 m_objects(surfChemPtr->getObjects()),
26 for (
size_t n = 0; n <
m_objects.size(); n++) {
31 "InterfaceKinetics object has no surface phase");
50 size_t tsp =
m_objects[n]->nTotalSpecies();
72 for (
size_t k = 0; k < nsp; k++, kindexSP++) {
79 size_t dim1 = std::max<size_t>(1,
m_neq);
90 double PGas,
double reltol,
double abstol)
92 double EXTRA_ACCURACY = 0.001;
94 EXTRA_ACCURACY *= 0.001;
99 double label_factor = 1.0;
102 double deltaT = 1.0E-10;
105 double t_real = 0.0, update_norm = 1.0E6;
106 bool do_time =
false, not_converged =
true;
107 m_ioflag = std::min(m_ioflag, 1);
131 print_header(m_ioflag, ifunc, time_scale,
true, reltol, abstol);
136 not_converged =
false;
141 while (not_converged && iter < iter_max) {
161 &label_t, &label_t_old, &label_factor, m_ioflag);
164 }
else if (tmp > 2.0*inv_t) {
172 tmp = t_real + 1.0/inv_t;
173 if (tmp > time_scale) {
174 inv_t = 1.0/(time_scale - t_real);
213 for (
size_t irow = 0; irow <
m_neq; irow++) {
216 for (
size_t irow = 0; irow <
m_neq; irow++) {
222 t_real += damp/inv_t;
226 printIteration(m_ioflag, damp, label_d, label_t, inv_t, t_real, iter,
227 update_norm, resid_norm, do_time);
231 not_converged = (t_real < time_scale);
234 if (t_real > time_scale ||
235 (resid_norm < 1.0e-7 &&
236 update_norm*time_scale/t_real < EXTRA_ACCURACY)) {
240 not_converged = ((update_norm > EXTRA_ACCURACY) ||
241 (resid_norm > EXTRA_ACCURACY));
248 if (not_converged && m_ioflag) {
249 writelog(
"#$#$#$# Error in solveSP $#$#$#$ \n");
250 writelogf(
"Newton iter on surface species did not converge, "
251 "update_norm = %e \n", update_norm);
261 printIteration(m_ioflag, damp, label_d, label_t, inv_t, t_real, iter,
262 update_norm, resid_norm, do_time,
true);
266 if (update_norm > 1.0) {
278 for (
size_t k = 0; k < X.size(); k++) {
297 for (
size_t iph = 0; iph < kin->
nPhases(); iph++) {
307 double Clarge = CSolnSP[kindexSP];
311 if (CSolnSP[kindexSP] > Clarge) {
312 Clarge = CSolnSP[kindexSP];
320 const bool do_time,
const double deltaT)
323 double lenScale = 1.0E-9;
339 size_t kins = kindexSP;
341 for (k = 0; k < nsp; k++, kindexSP++) {
343 (CSoln[kindexSP] - CSolnOld[kindexSP]) / deltaT
349 resid[kspecial] = sd;
350 for (k = 0; k < nsp; k++) {
351 resid[kspecial] -= CSoln[kins + k];
359 size_t kins = kindexSP;
361 for (k = 0; k < nsp; k++, kindexSP++) {
366 resid[kspecial] = sd;
367 for (k = 0; k < nsp; k++) {
368 resid[kspecial] -= CSoln[kins + k];
379 for (k = 0; k < nsp; k++) {
385 for (k = 0; k < nsp; k++) {
386 resid[kindexSP] -= CSoln[kindexSP + k];
389 for (k = 1; k < nsp; k++) {
391 resid[kindexSP + k] = XBlk[k] * grRate
394 resid[kindexSP + k] = XBlk[k] * grRate;
401 for (k = 1; k < nsp; k++) {
402 resid[kindexSP + k] = grRate * (XBlk[k] - 1.0/nsp);
406 for (k = 1; k < nsp; k++) {
407 resid[kindexSP + k] +=
409 (CSoln[kindexSP + k]- CSolnOld[kindexSP + k]);
419 const double CSolnOld[],
const bool do_time,
422 size_t kColIndex = 0;
424 fun_eval(resid, CSoln, CSolnOld, do_time, deltaT);
429 for (
size_t kCol = 0; kCol < nsp; kCol++) {
430 double cSave = CSoln[kColIndex];
431 double dc = std::max(1.0E-10 * sd, fabs(cSave) * 1.0E-7);
432 CSoln[kColIndex] += dc;
434 for (
size_t i = 0; i <
m_neq; i++) {
435 jac(i, kColIndex) = (
m_numEqn2[i] - resid[i])/dc;
437 CSoln[kColIndex] = cSave;
446 for (
size_t kCol = 0; kCol < nsp; kCol++) {
447 double cSave = CSoln[kColIndex];
448 double dc = std::max(1.0E-10 * sd, fabs(cSave) * 1.0E-7);
449 CSoln[kColIndex] += dc;
451 for (
size_t i = 0; i <
m_neq; i++) {
452 jac(i, kColIndex) = (
m_numEqn2[i] - resid[i])/dc;
454 CSoln[kColIndex] = cSave;
472 static double calc_damping(
double x[],
double dxneg[],
size_t dim,
int* label)
474 const double APPROACH = 0.80;
476 static double damp_old = 1.0;
479 for (
size_t i = 0; i < dim; i++) {
481 double xnew = x[i] - damp * dxneg[i];
486 double xtop = 1.0 - 0.1*fabs(1.0-x[i]);
487 double xbot = fabs(x[i]*0.1) - 1.0e-16;
489 damp = - APPROACH * (1.0 - x[i]) / dxneg[i];
491 }
else if (xnew < xbot) {
492 damp = APPROACH * x[i] / dxneg[i];
494 }
else if (xnew > 3.0*std::max(x[i], 1.0E-10)) {
495 damp = - 2.0 * std::max(x[i], 1.0E-10) / dxneg[i];
499 damp = std::max(damp, 1e-2);
503 if (damp > damp_old*3) {
524 for (
size_t i = 0; i < dim; i++) {
525 norm += pow(dx[i] / wtX[i], 2);
527 return sqrt(norm/dim);
531 const Array2D& Jac,
const double CSoln[],
532 const double abstol,
const double reltol)
540 wtSpecies[kindex] = abstol * sd + reltol * fabs(CSoln[kindex]);
547 wtSpecies[kindex] = abstol * sd + reltol * fabs(CSoln[kindex]);
555 for (
size_t k = 0; k <
m_neq; k++) {
557 for (
size_t jcol = 0; jcol <
m_neq; jcol++) {
558 wtResid[k] += fabs(Jac(k,jcol) * wtSpecies[jcol]);
564 int* label_old,
double* label_factor,
int ioflag)
566 double inv_timeScale = 1.0E-10;
577 netProdRateSolnSP[kindexSP] =
m_numEqn1[k];
578 double tmp = std::max(XMolSolnSP[kindexSP], 1.0e-10);
580 tmp = fabs(netProdRateSolnSP[kindexSP]/ tmp);
581 if (netProdRateSolnSP[kindexSP]> 0.0) {
584 if (tmp > inv_timeScale) {
586 *label = int(kindexSP);
593 if (*label == *label_old) {
594 *label_factor *= 1.5;
599 return inv_timeScale / *label_factor;
603 int damping,
double reltol,
double abstol)
606 writelog(
"\n================================ SOLVESP CALL SETUP "
607 "========================================\n");
609 writelog(
"\n SOLVESP Called with Initialization turned on\n");
610 writelogf(
" Time scale input = %9.3e\n", time_scale);
612 writelog(
"\n SOLVESP Called to calculate steady state residual\n");
613 writelog(
" from a good initial guess\n");
615 writelog(
"\n SOLVESP Called to calculate steady state Jacobian\n");
616 writelog(
" from a good initial guess\n");
618 writelog(
"\n SOLVESP Called to integrate surface in time\n");
619 writelogf(
" for a total of %9.3e sec\n", time_scale);
622 "Unknown ifunc flag = {}", ifunc);
626 writelog(
" The composition of the Bulk Phases will be calculated\n");
628 writelog(
" Bulk Phases have fixed compositions\n");
640 writelogf(
" Reltol = %9.3e, Abstol = %9.3e\n", reltol, abstol);
644 writelog(
"\n\n\t Iter Time Del_t Damp DelX "
645 " Resid Name-Time Name-Damp\n");
646 writelog(
"\t -----------------------------------------------"
647 "------------------------------------\n");
652 int label_t,
double inv_t,
double t_real,
653 size_t iter,
double update_norm,
654 double resid_norm,
bool do_time,
bool final)
663 writelogf(
"%9.4e %9.4e ", t_real, 1.0/inv_t);
665 writeline(
' ', 22,
false);
670 writeline(
' ', 11,
false);
672 writelogf(
"%9.4e %9.4e", update_norm, resid_norm);
678 writeline(
' ', 16,
false);
Declarations for the implicit integration of surface site density equations (see Kinetics Managers an...
Header for a simple thermodynamics model of a surface phase derived from ThermoPhase,...
A class for 2D arrays stored in column-major (Fortran-compatible) form.
Base class for exceptions thrown by Cantera classes.
A class for full (non-sparse) matrices with Fortran-compatible data storage, which adds matrix operat...
void resize(size_t n, size_t m, double v=0.0) override
Resize the matrix.
Advances the surface coverages of the associated set of SurfacePhase objects in time.
A kinetics manager for heterogeneous reaction mechanisms.
size_t nPhases() const
The number of phases participating in the reaction mechanism.
size_t kineticsSpeciesIndex(size_t k, size_t n) const
The location of species k of phase n in species arrays.
virtual void getNetProductionRates(double *wdot)
Species net production rates [kmol/m^3/s or kmol/m^2/s].
ThermoPhase & thermo(size_t n=0)
This method returns a reference to the nth ThermoPhase object defined in this kinetics mechanism.
virtual double molarDensity() const
Molar density (kmol/m^3).
size_t nSpecies() const
Returns the number of species in the phase.
void getMoleFractions(double *const x) const
Get the species mole fraction vector.
A simple thermodynamic model for a surface phase, assuming an ideal solution model.
vector< size_t > m_numBulkSpecies
Vector of number of species in the m_numBulkPhases phases.
vector< double > m_wtSpecies
Weights for the species concentrations norm calculation.
size_t m_numTotSurfSpecies
Total number of surface species in all surface phases.
void evalSurfLarge(const double *CSolnSP)
Update the vector that keeps track of the largest species in each surface phase.
void updateState(const double *cSurfSpec)
Update the surface states of the surface phases.
vector< size_t > m_indexKinObjSurfPhase
Mapping between the surface phases and the InterfaceKinetics objects.
vector< size_t > m_kinObjIndex
Index between the equation index and the index of the InterfaceKinetics object.
solveSP(ImplicitSurfChem *surfChemPtr, int bulkFunc=BULK_ETCH)
Constructor for the object.
vector< double > m_resid
Residual for the surface problem.
vector< ThermoPhase * > m_bulkPhasePtrs
Vector of bulk phase pointers, length is equal to m_numBulkPhases.
vector< size_t > m_spSurfLarge
Vector containing the indices of the largest species in each surface phase.
vector< double > m_netProductionRatesSave
Temporary vector with length equal to max m_maxTotSpecies.
double calc_t(double netProdRateSolnSP[], double XMolSolnSP[], int *label, int *label_old, double *label_factor, int ioflag)
Calculate a conservative delta T to use in a pseudo-steady state algorithm.
int m_bulkFunc
This variable determines how the bulk phases are to be handled.
vector< double > m_wtResid
Weights for the residual norm calculation. length MAX(1, m_neq)
vector< InterfaceKinetics * > & m_objects
Vector of interface kinetics objects.
void fun_eval(double *resid, const double *CSolnSP, const double *CSolnOldSP, const bool do_time, const double deltaT)
Main Function evaluation.
vector< double > m_CSolnSave
Temporary vector with length equal to max m_maxTotSpecies.
void updateMFSolnSP(double *XMolSolnSP)
Update mole fraction vector consisting of unknowns in surface problem.
vector< double > m_CSolnSPOld
Saved solution vector at the old time step. length MAX(1, m_neq)
void printIteration(int ioflag, double damp, int label_d, int label_t, double inv_t, double t_real, size_t iter, double update_norm, double resid_norm, bool do_time, bool final=false)
Printing routine that gets called after every iteration.
vector< double > m_CSolnSP
Solution vector. length MAX(1, m_neq)
vector< size_t > m_kinSpecIndex
Index between the equation index and the position in the kinetic species array for the appropriate ki...
size_t m_numTotBulkSpeciesSS
Total number of species in all bulk phases.
vector< double > m_numEqn1
Temporary vector with length equal to max m_maxTotSpecies.
vector< double > m_CSolnSPInit
Saved initial solution vector. length MAX(1, m_neq)
vector< double > m_XMolKinSpecies
Vector of mole fractions. length m_maxTotSpecies.
vector< double > m_numEqn2
Temporary vector with length equal to max m_maxTotSpecies.
vector< size_t > m_eqnIndexStartSolnPhase
Index of the start of the unknowns for each solution phase.
vector< size_t > m_nSpeciesSurfPhase
Vector of length number of surface phases containing the number of surface species in each phase.
size_t m_numSurfPhases
Number of surface phases in the surface problem.
void print_header(int ioflag, int ifunc, double time_scale, int damping, double reltol, double abstol)
Printing routine that optionally gets called at the start of every invocation.
void updateMFKinSpecies(double *XMolKinSp, int isp)
Update the mole fraction vector for a specific kinetic species vector corresponding to one InterfaceK...
DenseMatrix m_Jac
Jacobian.
size_t m_numBulkPhasesSS
Total number of volumetric condensed phases included in the steady state problem handled by this rout...
size_t m_neq
Total number of equations to solve in the implicit problem.
vector< SurfPhase * > m_ptrsSurfPhase
Vector of surface phase pointers.
size_t m_maxTotSpecies
Maximum number of species in any single kinetics operator -> also maxed wrt the total # of solution s...
int solveSurfProb(int ifunc, double time_scale, double TKelvin, double PGas, double reltol, double abstol)
Main routine that actually calculates the pseudo steady state of the surface problem.
void calcWeights(double wtSpecies[], double wtResid[], const Array2D &Jac, const double CSolnSP[], const double abstol, const double reltol)
Calculate the solution and residual weights.
void resjac_eval(DenseMatrix &jac, double *resid, double *CSolnSP, const double *CSolnSPOld, const bool do_time, const double deltaT)
Main routine that calculates the current residual and Jacobian.
void writelogf(const char *fmt, const Args &... args)
Write a formatted message to the screen.
void writelog(const string &fmt, const Args &... args)
Write a formatted message to the screen.
const int BULK_ETCH
Etching of a bulk phase is to be expected.
const int BULK_DEPOSITION
Deposition of a bulk phase is to be expected.
const int SFLUX_TRANSIENT
The transient calculation is performed here for an amount of time specified by "time_scale".
const int SFLUX_RESIDUAL
Need to solve the surface problem in order to calculate the surface fluxes of gas-phase species.
const int SFLUX_JACOBIAN
Calculation of the surface problem is due to the need for a numerical Jacobian for the gas-problem.
const int SFLUX_INITIALIZE
This assumes that the initial guess supplied to the routine is far from the correct one.
Namespace for the Cantera kernel.
static double calcWeightedNorm(const double[], const double dx[], size_t)
This function calculates the norm of an update, dx[], based on the weighted values of x.
int solve(DenseMatrix &A, double *b, size_t nrhs, size_t ldb)
Solve Ax = b. Array b is overwritten on exit with x.
Header file for implicit surface problem solver (see Chemical Kinetics and class solveSP).