15SteadyStateSystem::SteadyStateSystem()
17 m_state = make_shared<vector<double>>();
18 m_newt = make_unique<MultiNewton>(1);
21SteadyStateSystem::~SteadyStateSystem()
40 "To be removed after Cantera 3.2. Use setInitialGuess, solve(int), and "
62 debuglog(
"\nAttempt Newton solution of steady-state problem.", loglevel);
73 writelog(
"\nSteady Jacobian factorization failed:"
93 debuglog(
"\nNewton steady-state solve failed.\n", loglevel);
98 writelog(
"\nAttempt {} timesteps.", nsteps);
108 writelog(
"\nFinal timestep info: dt= {:<10.4g} log(ss)= {:<10.4g}\n", dt,
117 dt = std::min(dt,
m_tmax);
128 int successiveFailures = 0;
132 writelog(
"\n============================");
133 writelog(
"\n{:<5s} {:<11s} {:<7s}\n",
"step",
"dt (s)",
"log(ss)");
134 writelog(
"============================");
139 writelog(
"\n{:<5d} {:<6.4e} {:>7.4f}", n, dt, log10(ss));
140 }
else if (loglevel > 1) {
142 writelog(
"\nTimestep ({}) dt= {:<11.4e} log(ss)= {:<7.4f}", n, dt, log10(ss));
148 int j0 =
m_jac->nEvals();
151 int status =
newton().
solve(x, r, *
this, loglevel);
157 writelog(
"\nTimestep ({}) succeeded", n);
159 successiveFailures = 0;
164 if (
m_jac->nEvals() == j0) {
170 dt = std::min(dt,
m_tmax);
173 "Took maximum number of timesteps allowed ({}) without "
179 successiveFailures++;
182 }
else if (loglevel > 1) {
183 writelog(
"\nTimestep ({}) failed", n);
185 if (successiveFailures > 2) {
186 debuglog(
"--> Resetting negative species concentrations", loglevel);
188 successiveFailures = 0;
190 debuglog(
"--> Reducing timestep", loglevel);
193 string err_msg = fmt::format(
194 "Time integration failed. Minimum timestep ({}) reached.",
m_tmin);
204 writelog(
"\n{:<5d} {:<6.4e} {:>7.4f}", n, dt, log10(ss));
205 writelog(
"\n============================");
206 }
else if (loglevel > 1) {
208 writelog(
"\nTimestep ({}) dt= {:<11.4e} log10(ss)= {:<7.4f}\n", n, dt, log10(ss));
220 for (
size_t i = 0; i <
m_size; i++) {
221 ss = std::max(fabs(r[i]),ss);
230 for (
size_t i = 0; i < n; i++) {
243 "Jacobian evaluator must be instantiated before calling resize()");
262 double rdt_save =
m_rdt;
271 double rdt_old =
m_rdt;
Base class for exceptions thrown by Cantera classes.
virtual string getMessage() const
Method overridden by derived classes to format the error message.
virtual string getMethod() const
Get the name of the method that threw the exception.
virtual double eval(double t) const
Evaluate the function.
Newton iterator for multi-domain, one-dimensional problems.
int solve(double *x0, double *x1, SteadyStateSystem &r, int loglevel)
Find the solution to F(x) = 0 by damped Newton iteration.
void setOptions(int maxJacAge=5)
Set options.
int solve(double *x0, double *x1, int loglevel)
Solve , where is the residual function.
int m_nsteps
Number of time steps taken in the current call to solve()
size_t m_size
Solution vector size
int m_nsteps_max
Maximum number of timesteps allowed per call to solve()
virtual void resize()
Call to set the size of internal data structures after first defining the system or if the problem si...
vector< double > m_xnew
Work array used to hold the residual or the new solution.
unique_ptr< MultiNewton > m_newt
Newton iterator.
virtual void resetBadValues(double *x)
Reset values such as negative species concentrations.
size_t size() const
Total solution vector length;.
double ssnorm(double *x, double *r)
Steady-state max norm (infinity norm) of the residual evaluated using solution x.
virtual void initTimeInteg(double dt, double *x)
Prepare for time stepping beginning with solution x and timestep dt.
size_t bandwidth() const
Jacobian bandwidth.
double m_rdt
Reciprocal of time step.
shared_ptr< SystemJacobian > m_jac
Jacobian evaluator.
shared_ptr< vector< double > > m_state
Solution vector.
vector< int > m_mask
Transient mask.
vector< int > m_steps
Array of number of steps to take after each unsuccessful steady-state solve before re-attempting the ...
double m_tfactor
Factor time step is multiplied by if time stepping fails ( < 1 )
bool m_jac_ok
If true, Jacobian is current.
double m_tstep
Initial timestep.
int m_ts_jac_age
Maximum age of the Jacobian in time-stepping mode.
virtual void eval(double *x, double *r, double rdt=-1.0, int count=1)=0
Evaluate the residual function.
double timeStep(int nsteps, double dt, double *x, double *r, int loglevel)
Take time steps using Backward Euler.
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-evalua...
void evalSSJacobian(double *x, double *rsd)
Evaluate the steady-state Jacobian, accessible via linearSolver()
virtual void writeDebugInfo(const string &header_suffix, const string &message, int loglevel, int attempt_counter)
Write solver debugging based on the specified log level.
double m_tmin
Minimum timestep size.
double m_tmax
Maximum timestep size.
int m_ss_jac_age
Maximum age of the Jacobian in steady-state mode.
virtual void setSteadyMode()
Prepare to solve the steady-state problem.
virtual void clearDebugFile()
Delete debug output file that may be created by writeDebugInfo() when solving with high loglevel.
int m_attempt_counter
Counter used to manage the number of states stored in the debug log file generated by writeDebugInfo(...
void setLinearSolver(shared_ptr< SystemJacobian > solver)
Set the linear solver used to hold the Jacobian matrix and solve linear systems as part of each Newto...
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.
int m_max_history
Constant that determines the maximum number of states stored in the debug log file generated by write...
void setInitialGuess(const double *x)
Set the initial guess. Should be called before solve().
void getState(double *x) const
Get the converged steady-state solution after calling solve().
Func1 * m_time_step_callback
User-supplied function called after each successful timestep.
virtual void evalJacobian(double *x0)=0
Evaluates the Jacobian at x0 using finite differences.
vector< double > m_xlast_ts
State vector after the last successful set of time steps.
MultiNewton & newton()
Return a reference to the Newton iterator.
void debuglog(const string &msg, int loglevel)
Write a message to the log only if loglevel > 0.
void writelog(const string &fmt, const Args &... args)
Write a formatted message to the screen.
Namespace for the Cantera kernel.
const double Tiny
Small number to compare differences of mole fractions against.
void warn_deprecated(const string &source, const AnyBase &node, const string &message)
A deprecation warning for syntax in an input file.