Cantera  3.3.0a1
Loading...
Searching...
No Matches
MultiNewton.h
Go to the documentation of this file.
1//! @file MultiNewton.h
2
3// This file is part of Cantera. See License.txt in the top-level directory or
4// at https://cantera.org/license.txt for license and copyright information.
5
6#ifndef CT_MULTINEWTON_H
7#define CT_MULTINEWTON_H
8
10
11namespace Cantera
12{
13
14//! @defgroup onedUtilsGroup Utilities
15//! Utility classes and functions for one-dimensional problems.
16//! @ingroup onedGroup
17
18/**
19 * Newton iterator for multi-domain, one-dimensional problems.
20 * Used by class OneDim.
21 * @ingroup onedUtilsGroup
22 */
24{
25public:
26 //! Constructor
27 //! @param sz Number of variables in the system
28 MultiNewton(int sz);
29 virtual ~MultiNewton() {};
30 MultiNewton(const MultiNewton&) = delete;
31 MultiNewton& operator=(const MultiNewton&) = delete;
32
33 //! Get the number of variables in the system.
34 size_t size() {
35 return m_n;
36 }
37
38 //! Compute the undamped Newton step. The residual function is evaluated
39 //! at `x`, but the Jacobian is not recomputed.
40 //! @since Starting in %Cantera 3.2, the Jacobian is accessed via the OneDim object.
41 void step(double* x, double* step, SteadyStateSystem& r, int loglevel);
42
43 /**
44 * Return the factor by which the undamped Newton step 'step0'
45 * must be multiplied in order to keep all solution components in
46 * all domains between their specified lower and upper bounds.
47 */
48 double boundStep(const double* x0, const double* step0,
49 const SteadyStateSystem& r, int loglevel);
50
51 /**
52 * Performs a damped Newton step to solve the system of nonlinear equations.
53 *
54 * On entry, `step0` must contain an undamped Newton step for the solution `x0`.
55 * This method attempts to find a damping coefficient `alpha_k` such that the next
56 * undamped step would have a norm smaller than that of `step0`. If successful,
57 * the new solution after taking the damped step is returned in `x1`, and the
58 * undamped step at `x1` is returned in `step1`.
59 *
60 * This uses the method outlined in Kee et al. @cite kee2003.
61 *
62 * The system of equations can be written in the form:
63 * @f[
64 * F(x) = 0
65 * @f]
66 *
67 * Where @f$ F @f$ is the system of nonlinear equations and @f$ x @f$ is the
68 * solution vector.
69 *
70 * For the damped Newton method we are solving:
71 *
72 * @f[
73 * x_{k+1} - x_k = \Delta x_k = -\alpha_k J^{-1}(x_k) F(x_k)
74 * @f]
75 *
76 * Where @f$ J @f$ is the Jacobian matrix of @f$ F @f$ with respect to @f$ x @f$,
77 * and @f$ \alpha_k @f$ is the damping factor, and @f$ \Delta x_k @f$ is the Newton
78 * step at @f$ x_k @f$, sometimes called the correction vector. In the equations
79 * here, @f$ k @f$ is just an iteration variable.
80 *
81 * In this method, the Jacobian does not update, even when the solution vector is
82 * evaluated at different points.
83 *
84 * The general algorithm is described below.
85 *
86 * We want to solve the equation:
87 * @f[
88 * x_{k+1} = x_k + \alpha_k \Delta x_k
89 * @f]
90 *
91 * Pick @f$ \alpha_k @f$ such that
92 * @f$ \Vert \Delta x_{k+1} \Vert < \Vert \Delta x_k \Vert @f$
93 * where @f$ \Delta x_k = J^{-1}(x_k) F(x_k) @f$, and
94 * @f$ \Delta x_{k+1} = J^{-1}(x_{k}) F(x_{k+1}) @f$.
95 *
96 * @param[in] x0 initial solution about which a Newton step will be taken
97 * @param[in] step0 initial undamped Newton step
98 * @param[out] x1 solution after taking the damped Newton step
99 * @param[out] step1 Newton step after taking the damped Newton step
100 * @param[out] s1 norm of the subsequent Newton step after taking the damped Newton
101 * step
102 * @param[in] r domain object, used for evaluating residuals over all domains
103 * @param[in] loglevel controls amount of printed diagnostics
104 * @param[in] writetitle controls if logging title is printed
105 *
106 * @returns
107 * - `1` a damping coefficient was found and the solution converges.
108 * - `0` a damping coefficient was found, but the solution has not converged yet.
109 * - `-2` no suitable damping coefficient was found within the maximum iterations.
110 * - `-3` the current solution `x0` is too close to the solution bounds and the
111 * step would exceed the bounds on one or more components.
112 * - `-4` Steady-state Jacobian factorization failed.
113 * - `-5` Error taking Newton step
114 *
115 * @since Starting in %Cantera 3.2, the Jacobian is accessed via the
116 * SteadyStateSystem object.
117 */
118 int dampStep(const double* x0, const double* step0, double* x1, double* step1,
119 double& s1, SteadyStateSystem& r, int loglevel, bool writetitle);
120
121 /**
122 * Find the solution to F(x) = 0 by damped Newton iteration. On entry, x0
123 * contains an initial estimate of the solution. On successful return, x1
124 * contains the converged solution. If failure occurs, x1 will contain the
125 * value of x0 i.e. no change in solution.
126 *
127 * The convergence criteria is when the 2-norm of the Newton step is less than one.
128 *
129 * @returns
130 * - `1` a converged solution was found.
131 * - `-2` no suitable damping coefficient was found within the maximum iterations.
132 * - `-3` the current solution `x0` is too close to the solution bounds and the
133 * step would exceed the bounds on one or more components.
134 *
135 * @since Starting in %Cantera 3.2, the Jacobian is accessed via the
136 * SteadyStateSystem object.
137 */
138 int solve(double* x0, double* x1, SteadyStateSystem& r, int loglevel);
139
140 //! Set options.
141 //! @param maxJacAge Maximum number of steps that can be taken before requiring
142 //! a Jacobian update
143 void setOptions(int maxJacAge = 5) {
144 m_maxAge = maxJacAge;
145 }
146
147 //! Change the problem size.
148 void resize(size_t points);
149
150protected:
151 //! Work array holding the system state after the last successful step. Size #m_n.
152 vector<double> m_x;
153
154 //! Work array holding the undamped Newton step or the system residual. Size #m_n.
155 vector<double> m_stp;
156
157 //! Work array holding the damped Newton step. Size #m_n.
158 vector<double> m_stp1;
159
160 //! Maximum allowable Jacobian age before it is recomputed.
161 int m_maxAge = 5;
162
163 //! Factor by which the damping coefficient is reduced in each iteration
164 double m_dampFactor = sqrt(2.0);
165
166 //! Maximum number of damping iterations
167 size_t m_maxDampIter = 7;
168
169 //! number of variables
170 size_t m_n;
171
172 //! Elapsed CPU time spent computing the Jacobian.
173 double m_elapsed = 0.0;
174};
175}
176
177#endif
Newton iterator for multi-domain, one-dimensional problems.
Definition MultiNewton.h:24
double m_dampFactor
Factor by which the damping coefficient is reduced in each iteration.
size_t size()
Get the number of variables in the system.
Definition MultiNewton.h:34
void resize(size_t points)
Change the problem size.
vector< double > m_x
Work array holding the system state after the last successful step. Size m_n.
double m_elapsed
Elapsed CPU time spent computing the Jacobian.
vector< double > m_stp1
Work array holding the damped Newton step. Size m_n.
vector< double > m_stp
Work array holding the undamped Newton step or the system residual. Size m_n.
size_t m_maxDampIter
Maximum number of damping iterations.
int dampStep(const double *x0, const double *step0, double *x1, double *step1, double &s1, SteadyStateSystem &r, int loglevel, bool writetitle)
Performs a damped Newton step to solve the system of nonlinear equations.
int m_maxAge
Maximum allowable Jacobian age before it is recomputed.
int solve(double *x0, double *x1, SteadyStateSystem &r, int loglevel)
Find the solution to F(x) = 0 by damped Newton iteration.
double boundStep(const double *x0, const double *step0, const SteadyStateSystem &r, int loglevel)
Return the factor by which the undamped Newton step 'step0' must be multiplied in order to keep all s...
void setOptions(int maxJacAge=5)
Set options.
void step(double *x, double *step, SteadyStateSystem &r, int loglevel)
Compute the undamped Newton step.
size_t m_n
number of variables
Base class for representing a system of differential-algebraic equations and solving for its steady-s...
Namespace for the Cantera kernel.
Definition AnyMap.cpp:595