Cantera  4.0.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(span<const double> x, span<double> step, SteadyStateSystem& r,
42 int loglevel);
43
44 /**
45 * Return the factor by which the undamped Newton step 'step0'
46 * must be multiplied in order to keep all solution components in
47 * all domains between their specified lower and upper bounds.
48 */
49 double boundStep(span<const double> x0, span<const double> step0,
50 const SteadyStateSystem& r, int loglevel);
51
52 /**
53 * Performs a damped Newton step to solve the system of nonlinear equations.
54 *
55 * On entry, `step0` must contain an undamped Newton step for the solution `x0`.
56 * This method attempts to find a damping coefficient `alpha_k` such that the next
57 * undamped step would have a norm smaller than that of `step0`. If successful,
58 * the new solution after taking the damped step is returned in `x1`, and the
59 * undamped step at `x1` is returned in `step1`.
60 *
61 * This uses the method outlined in Kee et al. @cite kee2003.
62 *
63 * The system of equations can be written in the form:
64 * @f[
65 * F(x) = 0
66 * @f]
67 *
68 * Where @f$ F @f$ is the system of nonlinear equations and @f$ x @f$ is the
69 * solution vector.
70 *
71 * For the damped Newton method we are solving:
72 *
73 * @f[
74 * x_{k+1} - x_k = \Delta x_k = -\alpha_k J^{-1}(x_k) F(x_k)
75 * @f]
76 *
77 * Where @f$ J @f$ is the Jacobian matrix of @f$ F @f$ with respect to @f$ x @f$,
78 * and @f$ \alpha_k @f$ is the damping factor, and @f$ \Delta x_k @f$ is the Newton
79 * step at @f$ x_k @f$, sometimes called the correction vector. In the equations
80 * here, @f$ k @f$ is just an iteration variable.
81 *
82 * In this method, the Jacobian does not update, even when the solution vector is
83 * evaluated at different points.
84 *
85 * The general algorithm is described below.
86 *
87 * We want to solve the equation:
88 * @f[
89 * x_{k+1} = x_k + \alpha_k \Delta x_k
90 * @f]
91 *
92 * Pick @f$ \alpha_k @f$ such that
93 * @f$ \Vert \Delta x_{k+1} \Vert < \Vert \Delta x_k \Vert @f$
94 * where @f$ \Delta x_k = J^{-1}(x_k) F(x_k) @f$, and
95 * @f$ \Delta x_{k+1} = J^{-1}(x_{k}) F(x_{k+1}) @f$.
96 *
97 * @param[in] x0 initial solution about which a Newton step will be taken
98 * @param[in] step0 initial undamped Newton step
99 * @param[out] x1 solution after taking the damped Newton step
100 * @param[out] step1 Newton step after taking the damped Newton step
101 * @param[out] s1 norm of the subsequent Newton step after taking the damped Newton
102 * step
103 * @param[in] r domain object, used for evaluating residuals over all domains
104 * @param[in] loglevel controls amount of printed diagnostics
105 * @param[in] writetitle controls if logging title is printed
106 *
107 * @returns
108 * - `1` a damping coefficient was found and the solution converges.
109 * - `0` a damping coefficient was found, but the solution has not converged yet.
110 * - `-2` no suitable damping coefficient was found within the maximum iterations.
111 * - `-3` the current solution `x0` is too close to the solution bounds and the
112 * step would exceed the bounds on one or more components.
113 * - `-4` Steady-state Jacobian factorization failed.
114 * - `-5` Error taking Newton step
115 *
116 * @since Starting in %Cantera 3.2, the Jacobian is accessed via the
117 * SteadyStateSystem object.
118 */
119 int dampStep(span<const double> x0, span<const double> step0,
120 span<double> x1, span<double> step1, double& s1,
121 SteadyStateSystem& r, int loglevel, bool writetitle);
122
123 /**
124 * Find the solution to F(x) = 0 by damped Newton iteration. On entry, x0
125 * contains an initial estimate of the solution. On successful return, x1
126 * contains the converged solution. If failure occurs, x1 will contain the
127 * value of x0 i.e. no change in solution.
128 *
129 * The convergence criteria is when the 2-norm of the Newton step is less than one.
130 *
131 * @returns
132 * - `1` a converged solution was found.
133 * - `-2` no suitable damping coefficient was found within the maximum iterations.
134 * - `-3` the current solution `x0` is too close to the solution bounds and the
135 * step would exceed the bounds on one or more components.
136 *
137 * @since Starting in %Cantera 3.2, the Jacobian is accessed via the
138 * SteadyStateSystem object.
139 */
140 int solve(span<const double> x0, span<double> x1, SteadyStateSystem& r,
141 int loglevel);
142
143 //! Set options.
144 //! @param maxJacAge Maximum number of steps that can be taken before requiring
145 //! a Jacobian update
146 void setOptions(int maxJacAge = 5) {
147 m_maxAge = maxJacAge;
148 }
149
150 //! Change the problem size.
151 void resize(size_t points);
152
153protected:
154 //! Work array holding the system state after the last successful step. Size #m_n.
155 vector<double> m_x;
156
157 //! Work array holding the undamped Newton step or the system residual. Size #m_n.
158 vector<double> m_stp;
159
160 //! Work array holding the damped Newton step. Size #m_n.
161 vector<double> m_stp1;
162
163 //! Maximum allowable Jacobian age before it is recomputed.
164 int m_maxAge = 5;
165
166 //! Factor by which the damping coefficient is reduced in each iteration
167 double m_dampFactor = sqrt(2.0);
168
169 //! Maximum number of damping iterations
170 size_t m_maxDampIter = 7;
171
172 //! number of variables
173 size_t m_n;
174
175 //! Elapsed CPU time spent computing the Jacobian.
176 double m_elapsed = 0.0;
177};
178}
179
180#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.
int dampStep(span< const double > x0, span< const double > step0, span< double > x1, span< double > step1, double &s1, SteadyStateSystem &r, int loglevel, bool writetitle)
Performs a damped Newton step to solve the system of nonlinear equations.
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 m_maxAge
Maximum allowable Jacobian age before it is recomputed.
void setOptions(int maxJacAge=5)
Set options.
double boundStep(span< const double > x0, span< 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...
size_t m_n
number of variables
void step(span< const double > x, span< double > step, SteadyStateSystem &r, int loglevel)
Compute the undamped Newton step.
int solve(span< const double > x0, span< double > x1, SteadyStateSystem &r, int loglevel)
Find the solution to F(x) = 0 by damped Newton iteration.
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