Base class for representing a system of differential-algebraic equations and solving for its steady-state response. More...
#include <SteadyStateSystem.h>
Base class for representing a system of differential-algebraic equations and solving for its steady-state response.
Definition at line 23 of file SteadyStateSystem.h.
Public Member Functions | |
SteadyStateSystem (const SteadyStateSystem &)=delete | |
SteadyStateSystem & | operator= (const SteadyStateSystem &)=delete |
virtual void | eval (double *x, double *r, double rdt=-1.0, int count=1)=0 |
Evaluate the residual function. | |
virtual void | evalJacobian (double *x0)=0 |
Evaluates the Jacobian at x0 using finite differences. | |
virtual double | weightedNorm (const double *step) const =0 |
Compute the weighted norm of step . | |
int | solve (double *x0, double *x1, int loglevel) |
Solve \( F(x) = 0 \), where \( F(x) \) is the residual function. | |
void | setInitialGuess (const double *x) |
Set the initial guess. Should be called before solve(). | |
void | solve (int loglevel=0) |
Solve the steady-state problem, taking internal timesteps as necessary until the Newton solver can converge for the steady problem. | |
void | getState (double *x) const |
Get the converged steady-state solution after calling solve(). | |
double | ssnorm (double *x, double *r) |
Steady-state max norm (infinity norm) of the residual evaluated using solution x. | |
size_t | size () const |
Total solution vector length;. | |
virtual void | resize () |
Call to set the size of internal data structures after first defining the system or if the problem size changes, for example after grid refinement. | |
size_t | bandwidth () const |
Jacobian bandwidth. | |
virtual string | componentName (size_t i) const |
Get the name of the i-th component of the state vector. | |
virtual pair< string, string > | componentTableHeader () const |
Get header lines describing the column names included in a component label. | |
virtual string | componentTableLabel (size_t i) const |
Get elements of the component name, aligned with the column headings given by componentTableHeader(). | |
virtual double | upperBound (size_t i) const =0 |
Get the upper bound for global component i in the state vector. | |
virtual double | lowerBound (size_t i) const =0 |
Get the lower bound for global component i in the state vector. | |
MultiNewton & | newton () |
Return a reference to the Newton iterator. | |
void | setLinearSolver (shared_ptr< SystemJacobian > solver) |
Set the linear solver used to hold the Jacobian matrix and solve linear systems as part of each Newton iteration. | |
shared_ptr< SystemJacobian > | linearSolver () const |
Get the the linear solver being used to hold the Jacobian matrix and solve linear systems as part of each Newton iteration. | |
double | rdt () const |
Reciprocal of the time step. | |
bool | transient () const |
True if transient mode. | |
bool | steady () const |
True if steady mode. | |
virtual void | initTimeInteg (double dt, double *x) |
Prepare for time stepping beginning with solution x and timestep dt. | |
virtual void | setSteadyMode () |
Prepare to solve the steady-state problem. | |
vector< int > & | transientMask () |
Access the vector indicating which equations contain a transient term. | |
double | timeStep (int nsteps, double dt, double *x, double *r, int loglevel) |
Take time steps using Backward Euler. | |
virtual void | resetBadValues (double *x) |
Reset values such as negative species concentrations. | |
void | setJacAge (int ss_age, int ts_age=-1) |
Set the maximum number of steps that can be taken using the same Jacobian before it must be re-evaluated. | |
void | setInterrupt (Func1 *interrupt) |
Set a function that will be called every time eval is called. | |
void | setTimeStepCallback (Func1 *callback) |
Set a function that will be called after each successful timestep. | |
void | setJacobianPerturbation (double relative, double absolute, double threshold) |
Configure perturbations used to evaluate finite difference Jacobian. | |
virtual void | writeDebugInfo (const string &header_suffix, const string &message, int loglevel, int attempt_counter) |
Write solver debugging based on the specified log level. | |
virtual void | clearDebugFile () |
Delete debug output file that may be created by writeDebugInfo() when solving with high loglevel . | |
Options | |
void | setTimeStep (double stepsize, size_t n, const int *tsteps) |
Set the number of time steps to try when the steady Newton solver is unsuccessful. | |
void | setMinTimeStep (double tmin) |
Set the minimum time step allowed during time stepping. | |
void | setMaxTimeStep (double tmax) |
Set the maximum time step allowed during time stepping. | |
void | setTimeStepFactor (double tfactor) |
Sets a factor by which the time step is reduced if the time stepping fails. | |
void | setMaxTimeStepCount (int nmax) |
Set the maximum number of timeteps allowed before successful steady-state solve. | |
int | maxTimeStepCount () const |
Get the maximum number of timeteps allowed before successful steady-state solve. | |
Protected Member Functions | |
void | evalSSJacobian (double *x, double *rsd) |
Evaluate the steady-state Jacobian, accessible via linearSolver() | |
Protected Attributes | |
vector< int > | m_steps = { 10 } |
Array of number of steps to take after each unsuccessful steady-state solve before re-attempting the steady-state solution. | |
double | m_tstep = 1.0e-5 |
Initial timestep. | |
double | m_tmin = 1e-16 |
Minimum timestep size. | |
double | m_tmax = 1e+08 |
Maximum timestep size. | |
double | m_tfactor = 0.5 |
Factor time step is multiplied by if time stepping fails ( < 1 ) | |
shared_ptr< vector< double > > | m_state |
Solution vector. | |
vector< double > | m_xnew |
Work array used to hold the residual or the new solution. | |
vector< double > | m_xlast_ts |
State vector after the last successful set of time steps. | |
unique_ptr< MultiNewton > | m_newt |
Newton iterator. | |
double | m_rdt = 0.0 |
Reciprocal of time step. | |
shared_ptr< SystemJacobian > | m_jac |
Jacobian evaluator. | |
bool | m_jac_ok = false |
If true , Jacobian is current. | |
size_t | m_bw = 0 |
Jacobian bandwidth. | |
size_t | m_size = 0 |
Solution vector size | |
vector< double > | m_work1 |
Work arrays used during Jacobian evaluation. | |
vector< double > | m_work2 |
vector< int > | m_mask |
Transient mask. | |
int | m_ss_jac_age = 20 |
Maximum age of the Jacobian in steady-state mode. | |
int | m_ts_jac_age = 20 |
Maximum age of the Jacobian in time-stepping mode. | |
int | m_attempt_counter = 0 |
Counter used to manage the number of states stored in the debug log file generated by writeDebugInfo() | |
int | m_max_history = 10 |
Constant that determines the maximum number of states stored in the debug log file generated by writeDebugInfo() | |
Func1 * | m_interrupt = nullptr |
Function called at the start of every call to eval. | |
Func1 * | m_time_step_callback = nullptr |
User-supplied function called after each successful timestep. | |
int | m_nsteps = 0 |
Number of time steps taken in the current call to solve() | |
int | m_nsteps_max = 500 |
Maximum number of timesteps allowed per call to solve() | |
double | m_jacobianThreshold = 0.0 |
Threshold for ignoring small elements in Jacobian. | |
double | m_jacobianRelPerturb = 1e-5 |
Relative perturbation of each component in finite difference Jacobian. | |
double | m_jacobianAbsPerturb = 1e-10 |
Absolute perturbation of each component in finite difference Jacobian. | |
Definition at line 15 of file SteadyStateSystem.cpp.
|
virtual |
Definition at line 21 of file SteadyStateSystem.cpp.
|
pure virtual |
Evaluate the residual function.
[in] | x | State vector |
[out] | r | On return, contains the residual vector |
rdt | Reciprocal of the time step. if omitted, then the internally stored value (accessible using the rdt() method) is used. | |
count | Set to zero to omit this call from the statistics |
Implemented in OneDim, Sim1D, and SteadyReactorSolver.
|
pure virtual |
Evaluates the Jacobian at x0 using finite differences.
The Jacobian is computed by perturbing each component of x0
, evaluating the residual function, and then estimating the partial derivatives numerically using finite differences to determine the corresponding column of the Jacobian.
x0 | State vector at which to evaluate the Jacobian |
Implemented in OneDim, and SteadyReactorSolver.
|
pure virtual |
Compute the weighted norm of step
.
The purpose is to measure the "size" of the step vector \( \Delta x \) in a way that takes into account the relative importance or scale of different solution components. Each component of the step vector is normalized by a weight that depends on both the current magnitude of the solution vector and specified tolerances. This makes the norm dimensionless and scaled appropriately, avoiding issues where some components dominate due to differences in their scales. See OneDim::weightedNorm() for a representative implementation.
Implemented in OneDim, and SteadyReactorSolver.
int solve | ( | double * | x0, |
double * | x1, | ||
int | loglevel | ||
) |
Solve \( F(x) = 0 \), where \( F(x) \) is the residual function.
[in] | x0 | Starting estimate of solution. |
[out] | x1 | Final solution satisfying \( F(x1) = 0 \). |
loglevel | Controls amount of diagnostic output. |
Definition at line 37 of file SteadyStateSystem.cpp.
void setInitialGuess | ( | const double * | x | ) |
Set the initial guess. Should be called before solve().
Definition at line 25 of file SteadyStateSystem.cpp.
void solve | ( | int | loglevel = 0 | ) |
Solve the steady-state problem, taking internal timesteps as necessary until the Newton solver can converge for the steady problem.
loglevel | Controls amount of diagnostic output. |
Definition at line 48 of file SteadyStateSystem.cpp.
void getState | ( | double * | x | ) | const |
Get the converged steady-state solution after calling solve().
Definition at line 32 of file SteadyStateSystem.cpp.
double ssnorm | ( | double * | x, |
double * | r | ||
) |
Steady-state max norm (infinity norm) of the residual evaluated using solution x.
On return, array r contains the steady-state residual values.
Definition at line 216 of file SteadyStateSystem.cpp.
|
inline |
Total solution vector length;.
Definition at line 91 of file SteadyStateSystem.h.
|
virtual |
Call to set the size of internal data structures after first defining the system or if the problem size changes, for example after grid refinement.
The m_size variable should be updated before calling this method
Reimplemented in OneDim, and Sim1D.
Definition at line 235 of file SteadyStateSystem.cpp.
|
inline |
Jacobian bandwidth.
Definition at line 102 of file SteadyStateSystem.h.
|
inlinevirtual |
Get the name of the i-th component of the state vector.
Reimplemented in OneDim, and SteadyReactorSolver.
Definition at line 107 of file SteadyStateSystem.h.
|
inlinevirtual |
Get header lines describing the column names included in a component label.
Headings should be aligned with items formatted in the output of componentTableLabel(). Two lines are allowed. If only one is needed, the first line should be blank.
Reimplemented in OneDim.
Definition at line 113 of file SteadyStateSystem.h.
|
inlinevirtual |
Get elements of the component name, aligned with the column headings given by componentTableHeader().
Reimplemented in OneDim.
Definition at line 119 of file SteadyStateSystem.h.
|
pure virtual |
Get the upper bound for global component i in the state vector.
Implemented in OneDim, and SteadyReactorSolver.
|
pure virtual |
Get the lower bound for global component i in the state vector.
Implemented in OneDim, and SteadyReactorSolver.
MultiNewton & newton | ( | ) |
Return a reference to the Newton iterator.
Definition at line 300 of file SteadyStateSystem.cpp.
void setLinearSolver | ( | shared_ptr< SystemJacobian > | solver | ) |
Set the linear solver used to hold the Jacobian matrix and solve linear systems as part of each Newton iteration.
Definition at line 251 of file SteadyStateSystem.cpp.
|
inline |
Get the the linear solver being used to hold the Jacobian matrix and solve linear systems as part of each Newton iteration.
Definition at line 136 of file SteadyStateSystem.h.
|
inline |
Reciprocal of the time step.
Definition at line 139 of file SteadyStateSystem.h.
|
inline |
True if transient mode.
Definition at line 144 of file SteadyStateSystem.h.
|
inline |
True if steady mode.
Definition at line 149 of file SteadyStateSystem.h.
|
virtual |
Prepare for time stepping beginning with solution x and timestep dt.
Reimplemented in OneDim, and SteadyReactorSolver.
Definition at line 269 of file SteadyStateSystem.cpp.
|
virtual |
Prepare to solve the steady-state problem.
After invoking this method, subsequent calls to solve() will solve the steady-state problem. Sets the reciprocal of the time step to zero, and, if it was previously non-zero, signals that a new Jacobian will be needed.
Reimplemented in OneDim.
Definition at line 280 of file SteadyStateSystem.cpp.
|
inline |
Access the vector indicating which equations contain a transient term.
Elements are 1 for equations with a transient terms and 0 otherwise.
Definition at line 164 of file SteadyStateSystem.h.
double timeStep | ( | int | nsteps, |
double | dt, | ||
double * | x, | ||
double * | r, | ||
int | loglevel | ||
) |
Take time steps using Backward Euler.
nsteps | number of steps |
dt | initial step size |
x | current solution vector |
r | solution vector after time stepping |
loglevel | controls amount of printed diagnostics |
Definition at line 122 of file SteadyStateSystem.cpp.
|
inlinevirtual |
Reset values such as negative species concentrations.
Reimplemented in OneDim, and SteadyReactorSolver.
Definition at line 179 of file SteadyStateSystem.h.
void setTimeStep | ( | double | stepsize, |
size_t | n, | ||
const int * | tsteps | ||
) |
Set the number of time steps to try when the steady Newton solver is unsuccessful.
stepsize | Initial time step size [s] |
n | Length of tsteps array |
tsteps | A sequence of time step counts to take after subsequent failures of the steady-state solver. The last value in tsteps will be used again after further unsuccessful solution attempts. |
Definition at line 226 of file SteadyStateSystem.cpp.
|
inline |
Set the minimum time step allowed during time stepping.
Definition at line 194 of file SteadyStateSystem.h.
|
inline |
Set the maximum time step allowed during time stepping.
Definition at line 199 of file SteadyStateSystem.h.
|
inline |
Sets a factor by which the time step is reduced if the time stepping fails.
The default value is 0.5.
tfactor | factor time step is multiplied by if time stepping fails |
Definition at line 207 of file SteadyStateSystem.h.
|
inline |
Set the maximum number of timeteps allowed before successful steady-state solve.
Definition at line 212 of file SteadyStateSystem.h.
|
inline |
Get the maximum number of timeteps allowed before successful steady-state solve.
Definition at line 217 of file SteadyStateSystem.h.
void setJacAge | ( | int | ss_age, |
int | ts_age = -1 |
||
) |
Set the maximum number of steps that can be taken using the same Jacobian before it must be re-evaluated.
ss_age | Age limit during steady-state mode |
ts_age | Age limit during time stepping mode. If not specified, the steady-state age is also used during time stepping. |
Definition at line 290 of file SteadyStateSystem.cpp.
|
inline |
Set a function that will be called every time eval is called.
Can be used to provide keyboard interrupt support in the high-level language interfaces.
Definition at line 232 of file SteadyStateSystem.h.
|
inline |
Set a function that will be called after each successful timestep.
The function will be called with the size of the timestep as the argument. Intended to be used for observing solver progress for debugging purposes.
Definition at line 240 of file SteadyStateSystem.h.
|
inline |
Configure perturbations used to evaluate finite difference Jacobian.
relative | Relative perturbation (multiplied by the absolute value of each component). Default 1.0e-5 . |
absolute | Absolute perturbation (independent of component value). Default 1.0e-10 . |
threshold | Threshold below which to exclude elements from the Jacobian Default 0.0 . |
Definition at line 251 of file SteadyStateSystem.h.
|
inlinevirtual |
Write solver debugging based on the specified log level.
Reimplemented in Sim1D, and SteadyReactorSolver.
Definition at line 260 of file SteadyStateSystem.h.
|
inlinevirtual |
Delete debug output file that may be created by writeDebugInfo() when solving with high loglevel
.
Reimplemented in Sim1D.
Definition at line 265 of file SteadyStateSystem.h.
|
protected |
Evaluate the steady-state Jacobian, accessible via linearSolver()
Definition at line 260 of file SteadyStateSystem.cpp.
|
protected |
Array of number of steps to take after each unsuccessful steady-state solve before re-attempting the steady-state solution.
For subsequent time stepping calls, the final value is reused. See setTimeStep().
Definition at line 276 of file SteadyStateSystem.h.
|
protected |
Initial timestep.
Definition at line 278 of file SteadyStateSystem.h.
|
protected |
Minimum timestep size.
Definition at line 279 of file SteadyStateSystem.h.
|
protected |
Maximum timestep size.
Definition at line 280 of file SteadyStateSystem.h.
|
protected |
Factor time step is multiplied by if time stepping fails ( < 1 )
Definition at line 283 of file SteadyStateSystem.h.
|
protected |
Solution vector.
Definition at line 285 of file SteadyStateSystem.h.
|
protected |
Work array used to hold the residual or the new solution.
Definition at line 288 of file SteadyStateSystem.h.
|
protected |
State vector after the last successful set of time steps.
Definition at line 291 of file SteadyStateSystem.h.
|
protected |
Newton iterator.
Definition at line 293 of file SteadyStateSystem.h.
|
protected |
Reciprocal of time step.
Definition at line 294 of file SteadyStateSystem.h.
|
protected |
Jacobian evaluator.
Definition at line 296 of file SteadyStateSystem.h.
|
protected |
If true
, Jacobian is current.
Definition at line 297 of file SteadyStateSystem.h.
|
protected |
Jacobian bandwidth.
Definition at line 299 of file SteadyStateSystem.h.
|
protected |
Solution vector size
Definition at line 300 of file SteadyStateSystem.h.
|
protected |
Work arrays used during Jacobian evaluation.
Definition at line 303 of file SteadyStateSystem.h.
|
protected |
Definition at line 303 of file SteadyStateSystem.h.
|
protected |
|
protected |
Maximum age of the Jacobian in steady-state mode.
Definition at line 306 of file SteadyStateSystem.h.
|
protected |
Maximum age of the Jacobian in time-stepping mode.
Definition at line 307 of file SteadyStateSystem.h.
|
protected |
Counter used to manage the number of states stored in the debug log file generated by writeDebugInfo()
Definition at line 311 of file SteadyStateSystem.h.
|
protected |
Constant that determines the maximum number of states stored in the debug log file generated by writeDebugInfo()
Definition at line 315 of file SteadyStateSystem.h.
|
protected |
Function called at the start of every call to eval.
Definition at line 318 of file SteadyStateSystem.h.
|
protected |
User-supplied function called after each successful timestep.
Definition at line 321 of file SteadyStateSystem.h.
|
protected |
Number of time steps taken in the current call to solve()
Definition at line 323 of file SteadyStateSystem.h.
|
protected |
Maximum number of timesteps allowed per call to solve()
Definition at line 324 of file SteadyStateSystem.h.
|
protected |
Threshold for ignoring small elements in Jacobian.
Definition at line 327 of file SteadyStateSystem.h.
|
protected |
Relative perturbation of each component in finite difference Jacobian.
Definition at line 329 of file SteadyStateSystem.h.
|
protected |
Absolute perturbation of each component in finite difference Jacobian.
Definition at line 331 of file SteadyStateSystem.h.