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