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