25 static doublereal calc_damping(doublereal* x, doublereal* dx,
size_t dim,
int*);
26 static doublereal calcWeightedNorm(
const doublereal [],
const doublereal dx[],
size_t);
33 m_SurfChemPtr(surfChemPtr),
34 m_objects(surfChemPtr->getObjects()),
38 m_numTotSurfSpecies(0),
40 m_numTotBulkSpeciesSS(0),
48 size_t numPossibleSurfPhases =
m_objects.size();
49 for (
size_t n = 0; n < numPossibleSurfPhases; n++) {
52 if (surfPhaseIndex !=
npos) {
58 "InterfaceKinetics object has no surface phase");
64 "Inconsistent ThermoPhase object within "
65 "InterfaceKinetics object");
87 size_t tsp =
m_objects[n]->nTotalSpecies();
106 size_t isp, k, nsp, kstart;
114 for (k = 0; k < nsp; k++, kindexSP++) {
121 size_t dim1 = std::max<size_t>(1,
m_neq);
128 m_Jac.resize(dim1, dim1, 0.0);
132 doublereal PGas, doublereal reltol, doublereal abstol)
134 doublereal EXTRA_ACCURACY = 0.001;
136 EXTRA_ACCURACY *= 0.001;
142 doublereal label_factor = 1.0;
145 doublereal deltaT = 1.0E-10;
146 doublereal damp=1.0, tmp;
149 doublereal resid_norm;
150 doublereal inv_t = 0.0;
151 doublereal t_real = 0.0, update_norm = 1.0E6;
153 bool do_time =
false, not_converged =
true;
154 m_ioflag = std::min(m_ioflag, 1);
172 for (
size_t k = 0; k <nsp; k++) {
184 print_header(m_ioflag, ifunc, time_scale,
true, reltol, abstol);
191 not_converged =
false;
199 while (not_converged && iter < iter_max) {
225 &label_t, &label_t_old, &label_factor, m_ioflag);
228 }
else if (tmp > 2.0*inv_t) {
239 tmp = t_real + 1.0/inv_t;
240 if (tmp > time_scale) {
241 inv_t = 1.0/(time_scale - t_real);
286 printf(
"solveSurfSS: Zero pivot, assuming converged: %g (%d)\n",
289 for (
size_t jcol = 0; jcol <
m_neq; jcol++) {
296 printf(
"solveSurfProb: iter %d t_real %g delta_t %g\n\n",
297 iter,t_real, 1.0/inv_t);
298 printf(
"solveSurfProb: init guess, current concentration,"
300 for (
size_t jcol = 0; jcol <
m_neq; jcol++) {
301 printf(
"\t%s %g %g %g\n",
int2str(jcol).c_str(),
308 t_real += time_scale;
331 for (
size_t irow = 0; irow <
m_neq; irow++) {
334 for (
size_t irow = 0; irow <
m_neq; irow++) {
340 t_real += damp/inv_t;
344 printIteration(m_ioflag, damp, label_d, label_t, inv_t, t_real, iter,
345 update_norm, resid_norm, do_time);
349 not_converged = (t_real < time_scale);
352 if (t_real > time_scale ||
353 (resid_norm < 1.0e-7 &&
354 update_norm*time_scale/t_real < EXTRA_ACCURACY)) {
358 not_converged = ((update_norm > EXTRA_ACCURACY) ||
359 (resid_norm > EXTRA_ACCURACY));
370 printf(
"#$#$#$# Error in solveSP $#$#$#$ \n");
371 printf(
"Newton iter on surface species did not converge, "
372 "update_norm = %e \n", update_norm);
373 printf(
"Continuing anyway\n");
387 printIteration(m_ioflag, damp, label_d, label_t, inv_t, t_real, iter,
388 update_norm, resid_norm, do_time,
true);
394 if (update_norm > 1.0) {
421 for (
size_t iph = 0; iph < nph; iph++) {
433 doublereal Clarge = CSolnSP[kindexSP];
436 for (
size_t k = 1; k < nsp; k++, kindexSP++) {
437 if (CSolnSP[kindexSP] > Clarge) {
438 Clarge = CSolnSP[kindexSP];
446 const doublereal* CSolnOld,
const bool do_time,
447 const doublereal deltaT)
449 size_t isp, nsp, kstart, k, kindexSP, kins, kspecial;
450 doublereal lenScale = 1.0E-9;
476 for (k = 0; k < nsp; k++, kindexSP++) {
478 (CSoln[kindexSP] - CSolnOld[kindexSP]) / deltaT
484 resid[kspecial] = sd;
485 for (k = 0; k < nsp; k++) {
486 resid[kspecial] -= CSoln[kins + k];
498 for (k = 0; k < nsp; k++, kindexSP++) {
503 resid[kspecial] = sd;
504 for (k = 0; k < nsp; k++) {
505 resid[kspecial] -= CSoln[kins + k];
519 for (k = 0; k < nsp; k++) {
525 for (k = 0; k < nsp; k++) {
526 resid[kindexSP] -= CSoln[kindexSP + k];
529 for (k = 1; k < nsp; k++) {
531 resid[kindexSP + k] = XBlk[k] * grRate
534 resid[kindexSP + k] = XBlk[k] * grRate;
540 for (k = 1; k < nsp; k++) {
541 resid[kindexSP + k] = grRate * (XBlk[k] - 1.0/nsp);
545 for (k = 1; k < nsp; k++) {
546 resid[kindexSP + k] +=
548 (CSoln[kindexSP + k]- CSolnOld[kindexSP + k]);
558 doublereal resid[], doublereal CSoln[],
559 const doublereal CSolnOld[],
const bool do_time,
560 const doublereal deltaT)
562 size_t kColIndex = 0, nsp, jsp, i, kCol;
563 doublereal dc, cSave, sd;
567 fun_eval(resid, CSoln, CSolnOld, do_time, deltaT);
574 for (kCol = 0; kCol < nsp; kCol++) {
575 cSave = CSoln[kColIndex];
576 dc = std::max(1.0E-10 * sd, fabs(cSave) * 1.0E-7);
577 CSoln[kColIndex] += dc;
579 for (i = 0; i <
m_neq; i++) {
580 jac(i, kColIndex) = (
m_numEqn2[i] - resid[i])/dc;
582 CSoln[kColIndex] = cSave;
591 for (kCol = 0; kCol < nsp; kCol++) {
592 cSave = CSoln[kColIndex];
593 dc = std::max(1.0E-10 * sd, fabs(cSave) * 1.0E-7);
594 CSoln[kColIndex] += dc;
596 for (i = 0; i <
m_neq; i++) {
597 jac(i, kColIndex) = (
m_numEqn2[i] - resid[i])/dc;
599 CSoln[kColIndex] = cSave;
617 static doublereal calc_damping(doublereal x[], doublereal dxneg[],
size_t dim,
int* label)
619 const doublereal APPROACH = 0.80;
620 doublereal damp = 1.0, xnew, xtop, xbot;
621 static doublereal damp_old = 1.0;
625 for (
size_t i = 0; i < dim; i++) {
631 xnew = x[i] - damp * dxneg[i];
639 xtop = 1.0 - 0.1*fabs(1.0-x[i]);
640 xbot = fabs(x[i]*0.1) - 1.0e-16;
642 damp = - APPROACH * (1.0 - x[i]) / dxneg[i];
644 }
else if (xnew < xbot) {
645 damp = APPROACH * x[i] / dxneg[i];
647 }
else if (xnew > 3.0*std::max(x[i], 1.0E-10)) {
648 damp = - 2.0 * std::max(x[i], 1.0E-10) / dxneg[i];
652 damp = std::max(damp, 1e-2);
658 if (damp > damp_old*3) {
677 static doublereal calcWeightedNorm(
const doublereal wtX[],
const doublereal dx[],
size_t dim)
679 doublereal norm = 0.0;
684 for (
size_t i = 0; i < dim; i++) {
685 tmp = dx[i] / wtX[i];
688 return sqrt(norm/dim);
692 const Array2D& Jac,
const doublereal CSoln[],
693 const doublereal abstol,
const doublereal reltol)
695 size_t k, jcol, kindex, isp, nsp;
705 for (k = 0; k < nsp; k++, kindex++) {
706 wtSpecies[kindex] = abstol * sd + reltol * fabs(CSoln[kindex]);
713 for (k = 0; k < nsp; k++, kindex++) {
714 wtSpecies[kindex] = abstol * sd + reltol * fabs(CSoln[kindex]);
724 for (k = 0; k <
m_neq; k++) {
726 for (jcol = 0; jcol <
m_neq; jcol++) {
727 wtResid[k] += fabs(Jac(k,jcol) * wtSpecies[jcol]);
733 doublereal XMolSolnSP[],
734 int* label,
int* label_old,
735 doublereal* label_factor,
int ioflag)
737 size_t k, isp, nsp, kstart;
738 doublereal inv_timeScale = 1.0E-10;
739 doublereal sden, tmp;
758 for (k = 0; k < nsp; k++, kindexSP++) {
759 size_t kspindex = kstart + k;
760 netProdRateSolnSP[kindexSP] =
m_numEqn1[kspindex];
761 if (XMolSolnSP[kindexSP] <= 1.0E-10) {
764 tmp = XMolSolnSP[kindexSP];
767 tmp = fabs(netProdRateSolnSP[kindexSP]/ tmp);
768 if (netProdRateSolnSP[kindexSP]> 0.0) {
771 if (tmp > inv_timeScale) {
773 *label = int(kindexSP);
782 if (*label == *label_old) {
783 *label_factor *= 1.5;
788 return inv_timeScale / *label_factor;
792 int damping, doublereal reltol, doublereal abstol)
795 printf(
"\n================================ SOLVESP CALL SETUP "
796 "========================================\n");
798 printf(
"\n SOLVESP Called with Initialization turned on\n");
799 printf(
" Time scale input = %9.3e\n", time_scale);
801 printf(
"\n SOLVESP Called to calculate steady state residual\n");
802 printf(
" from a good initial guess\n");
804 printf(
"\n SOLVESP Called to calculate steady state Jacobian\n");
805 printf(
" from a good initial guess\n");
807 printf(
"\n SOLVESP Called to integrate surface in time\n");
808 printf(
" for a total of %9.3e sec\n", time_scale);
811 "Unknown ifunc flag = " +
int2str(ifunc));
815 printf(
" The composition of the Bulk Phases will be calculated\n");
817 printf(
" Bulk Phases have fixed compositions\n");
824 printf(
" Damping is ON \n");
826 printf(
" Damping is OFF \n");
829 printf(
" Reltol = %9.3e, Abstol = %9.3e\n", reltol, abstol);
833 printf(
"\n\n\t Iter Time Del_t Damp DelX "
834 " Resid Name-Time Name-Damp\n");
835 printf(
"\t -----------------------------------------------"
836 "------------------------------------\n");
841 int label_t, doublereal inv_t, doublereal t_real,
842 size_t iter, doublereal update_norm,
843 doublereal resid_norm,
bool do_time,
bool final)
849 printf(
"\tFIN%3s ",
int2str(iter).c_str());
851 printf(
"\t%6s ",
int2str(iter).c_str());
854 printf(
"%9.4e %9.4e ", t_real, 1.0/inv_t);
856 for (i = 0; i < 22; i++) {
860 printf(
"%9.4e ", damp);
862 for (i = 0; i < 11; i++) {
865 printf(
"%9.4e %9.4e", update_norm, resid_norm);
871 printf(
" %-16s", nm.c_str());
873 for (i = 0; i < 16; i++) {
882 printf(
" %-16s", nm.c_str());
885 printf(
" -- success");
void printIteration(int ioflag, doublereal damp, int label_d, int label_t, doublereal inv_t, doublereal t_real, size_t iter, doublereal update_norm, doublereal resid_norm, bool do_time, bool final=false)
Printing routine that gets called after every iteration.
std::string int2str(const int n, const std::string &fmt)
Convert an int to a string using a format converter.
int m_bulkFunc
This variable determines how the bulk phases are to be handled.
Header for a simple thermodynamics model of a surface phase derived from ThermoPhase, assuming an ideal solution model (see Thermodynamic Properties and class SurfPhase).
std::vector< InterfaceKinetics * > & m_objects
Vector of interface kinetics objects.
size_t m_neq
Total number of equations to solve in the implicit problem.
thermo_t & thermo(size_t n=0)
This method returns a reference to the nth ThermoPhase object defined in this kinetics mechanism...
const size_t npos
index returned by functions to indicate "no position"
std::string kineticsSpeciesName(size_t k) const
Return the name of the kth species in the kinetics manager.
vector_fp m_CSolnSPInit
Saved initial solution vector. length MAX(1, m_neq)
size_t m_numBulkPhasesSS
Total number of volumetric condensed phases included in the steady state problem handled by this rout...
void updateMFSolnSP(doublereal *XMolSolnSP)
Update mole fraction vector consisting of unknowns in surface problem.
vector_fp m_CSolnSP
Solution vector. length MAX(1, m_neq)
doublereal molarDensity() const
Molar density (kmol/m^3).
const int BULK_ETCH
Etching of a bulk phase is to be expected.
void getMoleFractions(doublereal *const x) const
Get the species mole fraction vector.
void getConcentrations(doublereal *const c) const
Get the species concentrations (kmol/m^3).
A class for 2D arrays stored in column-major (Fortran-compatible) form.
virtual void getNetProductionRates(doublereal *wdot)
Species net production rates [kmol/m^3/s or kmol/m^2/s].
Base class for a phase with thermodynamic properties.
void evalSurfLarge(const doublereal *CSolnSP)
Update the vector that keeps track of the largest species in each surface phase.
A simple thermodynamic model for a surface phase, assuming an ideal solution model.
size_t surfacePhaseIndex()
This returns the integer index of the phase which has ThermoPhase type cSurf.
size_t m_numTotSurfSpecies
Total number of surface species in all surface phases.
void print_header(int ioflag, int ifunc, doublereal time_scale, int damping, doublereal reltol, doublereal abstol)
Printing routine that optionally gets called at the start of every invocation.
int solveSurfProb(int ifunc, doublereal time_scale, doublereal TKelvin, doublereal PGas, doublereal reltol, doublereal abstol)
Main routine that actually calculates the pseudo steady state of the surface problem.
const int SFLUX_INITIALIZE
This assumes that the initial guess supplied to the routine is far from the correct one...
size_t nPhases() const
The number of phases participating in the reaction mechanism.
std::vector< size_t > m_eqnIndexStartSolnPhase
Index of the start of the unknowns for each solution phase.
A kinetics manager for heterogeneous reaction mechanisms.
vector_fp m_wtResid
Weights for the residual norm calculation. length MAX(1, m_neq)
const int SFLUX_RESIDUAL
Need to solve the surface problem in order to calculate the surface fluxes of gas-phase species...
vector_fp m_XMolKinSpecies
Vector of mole fractions. length m_maxTotSpecies.
int solve(doublereal *b, size_t nrhs=1, size_t ldb=0)
Solves the Ax = b system returning x in the b spot.
doublereal calc_t(doublereal netProdRateSolnSP[], doublereal XMolSolnSP[], int *label, int *label_old, doublereal *label_factor, int ioflag)
Calculate a conservative delta T to use in a pseudo-steady state algorithm.
vector_fp m_numEqn1
Temporary vector with length equal to max m_maxTotSpecies.
const int BULK_DEPOSITION
Deposition of a bulk phase is to be expected.
A class for full (non-sparse) matrices with Fortran-compatible data storage.
const int SFLUX_JACOBIAN
Calculation of the surface problem is due to the need for a numerical Jacobian for the gas-problem...
std::vector< size_t > m_spSurfLarge
Vector containing the indices of the largest species in each surface phase.
vector_fp m_CSolnSave
Temporary vector with length equal to max m_maxTotSpecies.
std::vector< size_t > m_nSpeciesSurfPhase
Vector of length number of surface phases containing the number of surface species in each phase...
int factor()
Factors the A matrix, overwriting A.
void updateMFKinSpecies(doublereal *XMolKinSp, int isp)
Update the mole fraction vector for a specific kinetic species vector corresponding to one InterfaceK...
void resjac_eval(SquareMatrix &jac, doublereal *resid, doublereal *CSolnSP, const doublereal *CSolnSPOld, const bool do_time, const doublereal deltaT)
Main routine that calculates the current residual and Jacobian.
Base class for exceptions thrown by Cantera classes.
vector_fp m_numEqn2
Temporary vector with length equal to max m_maxTotSpecies.
const int SFLUX_TRANSIENT
The transient calculation is performed here for an amount of time specified by "time_scale".
Declarations for the implicit integration of surface site density equations (see Kinetics Managers an...
size_t kineticsSpeciesIndex(size_t k, size_t n) const
The location of species k of phase n in species arrays.
std::vector< SurfPhase * > m_ptrsSurfPhase
Vector of surface phase pointers.
size_t nSpecies() const
Returns the number of species in the phase.
std::vector< size_t > m_kinObjPhaseIDSurfPhase
Phase ID in the InterfaceKinetics object of the surface phase.
vector_fp m_netProductionRatesSave
Temporary vector with length equal to max m_maxTotSpecies.
std::vector< size_t > m_kinObjIndex
Index between the equation index and the index of the InterfaceKinetics object.
Advances the surface coverages of the associated set of SurfacePhase objects in time.
size_t m_numSurfPhases
Number of surface phases in the surface problem.
std::vector< ThermoPhase * > m_bulkPhasePtrs
Vector of bulk phase pointers, length is equal to m_numBulkPhases.
std::vector< size_t > m_numBulkSpecies
Vector of number of species in the m_numBulkPhases phases.
vector_fp m_CSolnSPOld
Saved solution vector at the old time step. length MAX(1, m_neq)
vector_fp m_resid
Residual for the surface problem.
Header file for implicit surface problem solver (see Chemical Kinetics and class solveSP).
#define DATA_PTR(vec)
Creates a pointer to the start of the raw data for a vector.
std::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_maxTotSpecies
Maximum number of species in any single kinetics operator -> also maxed wrt the total # of solution s...
std::vector< size_t > m_indexKinObjSurfPhase
Mapping between the surface phases and the InterfaceKinetics objects.
vector_fp m_wtSpecies
Weights for the species concentrations norm calculation.
SquareMatrix m_Jac
Jacobian.
void fun_eval(doublereal *resid, const doublereal *CSolnSP, const doublereal *CSolnOldSP, const bool do_time, const doublereal deltaT)
Main Function evaluation.
size_t m_numTotBulkSpeciesSS
Total number of species in all bulk phases.
void updateState(const doublereal *cSurfSpec)
Update the surface states of the surface phases.
void calcWeights(doublereal wtSpecies[], doublereal wtResid[], const Array2D &Jac, const doublereal CSolnSP[], const doublereal abstol, const doublereal reltol)
Calculate the solution and residual weights.