NEML2 1.4.0
Loading...
Searching...
No Matches
Newton.cxx
1// Copyright 2023, UChicago Argonne, LLC
2// All Rights Reserved
3// Software Name: NEML2 -- the New Engineering material Model Library, version 2
4// By: Argonne National Laboratory
5// OPEN SOURCE LICENSE (MIT)
6//
7// Permission is hereby granted, free of charge, to any person obtaining a copy
8// of this software and associated documentation files (the "Software"), to deal
9// in the Software without restriction, including without limitation the rights
10// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11// copies of the Software, and to permit persons to whom the Software is
12// furnished to do so, subject to the following conditions:
13//
14// The above copyright notice and this permission notice shall be included in
15// all copies or substantial portions of the Software.
16//
17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23// THE SOFTWARE.
24
25#include "neml2/solvers/Newton.h"
26#include <iomanip>
27#include "neml2/misc/math.h"
28
29namespace neml2
30{
31register_NEML2_object(Newton);
32
35{
37 options.doc() = "The standard Newton-Raphson solver which always takes the 'full' Newton step.";
38
39 return options;
40}
41
42Newton::Newton(const OptionSet & options)
43 : NonlinearSolver(options)
44{
45}
46
47std::tuple<bool, size_t>
49{
50 // The initial residual for relative convergence check
51 system.residual();
52 auto nR = system.residual_norm();
53 auto nR0 = nR.clone();
54
55 // Check for initial convergence
56 if (converged(0, nR0, nR0))
57 {
58 // TODO: The final update is only necessary if we use AD
59 system.Jacobian();
61 return {true, 0};
62 }
63
64 // Prepare any solver internal data before the iterative update
66
67 // Continuing iterating until one of:
68 // 1. nR < atol (success)
69 // 2. nR / nR0 < rtol (success)
70 // 3. i > miters (failure)
71 for (size_t i = 1; i < miters; i++)
72 {
73 system.Jacobian();
74 update(system, x);
75 system.residual();
76 nR = system.residual_norm();
77
78 // Check for convergence
79 if (converged(i, nR, nR0))
80 {
81 // TODO: The final update is only necessary if we use AD
82 system.Jacobian();
84 return {true, i};
85 }
86 }
87
88 return {false, miters};
89}
90
91bool
92Newton::converged(size_t itr, const torch::Tensor & nR, const torch::Tensor & nR0) const
93{
94 // LCOV_EXCL_START
95 if (verbose)
96 std::cout << "ITERATION " << std::setw(3) << itr << ", |R| = " << std::scientific
97 << torch::max(nR).item<Real>() << ", |R0| = " << std::scientific
98 << torch::max(nR0).item<Real>() << std::endl;
99 // LCOV_EXCL_STOP
100
101 return torch::all(torch::logical_or(nR < atol, nR / nR0 < rtol)).item<bool>();
102}
103
104void
106{
107 auto dx = solve_direction(system);
108
109 x.variable_data() += system.scale_direction(dx);
110 system.set_solution(x);
111}
112
113void
115{
116 auto dx = solve_direction(system);
117 x += system.scale_direction(dx);
118}
119
122{
123 // Special case when this is a scalar system
124 if (system.residual_view().base_dim() == 0)
125 return -system.residual_view() / system.Jacobian_view();
126
127 return -math::linalg::solve(system.Jacobian_view(), system.residual_view());
128}
129
130} // namespace neml2
Definition BatchTensor.h:32
The wrapper (decorator) for cross-referencing unresolved values at parse time.
Definition CrossRef.h:52
The nonlinear solver solves a nonlinear system of equations.
Definition Newton.h:39
virtual void final_update(NonlinearSystem &system, BatchTensor &x)
Do a final update to track AD function graph.
Definition Newton.cxx:114
virtual std::tuple< bool, size_t > solve(NonlinearSystem &system, BatchTensor &x) override
Solve the given nonlinear system.
Definition Newton.cxx:48
virtual bool converged(size_t itr, const torch::Tensor &nR, const torch::Tensor &nR0) const
Check for convergence. The current iteration is said to be converged if the residual norm is below th...
Definition Newton.cxx:92
virtual void prepare(const NonlinearSystem &, const BatchTensor &)
Prepare solver internal data before the iterative update.
Definition Newton.h:49
static OptionSet expected_options()
Definition Newton.cxx:34
Newton(const OptionSet &options)
Definition Newton.cxx:42
virtual void update(NonlinearSystem &system, BatchTensor &x)
Update trial solution.
Definition Newton.cxx:105
virtual BatchTensor solve_direction(const NonlinearSystem &system)
Find the current update direction.
Definition Newton.cxx:121
The nonlinear solver solves a nonlinear system of equations.
Definition NonlinearSolver.h:37
unsigned int miters
Maximum number of iterations.
Definition NonlinearSolver.h:64
Real rtol
Relative tolerance.
Definition NonlinearSolver.h:62
static OptionSet expected_options()
Definition NonlinearSolver.cxx:30
Real atol
Absolute tolerance.
Definition NonlinearSolver.h:60
Definition of a nonlinear system of equations.
Definition NonlinearSystem.h:37
A custom map-like data structure. The keys are strings, and the values can be nonhomogeneously typed.
Definition OptionSet.h:59
const std::string & doc() const
A readonly reference to the option set's docstring.
Definition OptionSet.h:91
const bool verbose
Whether to print additional (debugging) information during the solve.
Definition Solver.h:49
BatchTensor solve(const BatchTensor &A, const BatchTensor &B)
Solve the linear system A X = B.
Definition math.cxx:331
Definition CrossRef.cxx:32