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