NEML2 1.4.0
Loading...
Searching...
No Matches
NewtonWithLineSearch.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/NewtonWithLineSearch.h"
26#include <iomanip>
27#include "neml2/misc/math.h"
28
29namespace neml2
30{
31register_NEML2_object(NewtonWithLineSearch);
32
35{
37 options.doc() = "The Newton-Raphson solver with line search.";
38
39 options.set<unsigned int>("max_linesearch_iterations") = 10;
40 options.set("max_linesearch_iterations").doc() =
41 "Maximum allowable linesearch iterations. No error is produced upon reaching the maximum "
42 "number of iterations, and the scale factor in the last iteration is used to scale the step.";
43
44 options.set<Real>("linesearch_cutback") = 2.0;
45 options.set("linesearch_cutback").doc() = "Linesearch cut-back factor when the current scale "
46 "factor cannot sufficiently reduce the residual.";
47
48 options.set<Real>("linesearch_stopping_criteria") = 1.0e-3;
49 options.set("linesearch_stopping_criteria").doc() =
50 "The lineseach tolerance slightly relaxing the definition of residual decrease";
51
52 return options;
53}
54
56 : Newton(options),
57 _linesearch_miter(options.get<unsigned int>("max_linesearch_iterations")),
58 _linesearch_sigma(options.get<Real>("linesearch_cutback")),
59 _linesearch_c(options.get<Real>("linesearch_stopping_criteria"))
60{
61}
62
63void
65{
67
69 x.variable_data() += system.scale_direction(_alpha * dx);
70 system.set_solution(x);
71}
72
73void
75 const BatchTensor & x,
76 const BatchTensor & dx)
77{
78 _alpha = Scalar::ones(x.batch_sizes(), x.options());
79
80 const auto & R = system.residual_view();
81 auto R0 = R.clone();
82 auto nR02 = math::bvv(R0, R0);
83
84 for (size_t i = 1; i < _linesearch_miter; i++)
85 {
86 system.set_solution(x + system.scale_direction(_alpha * dx));
87 system.residual();
88 auto nR2 = math::bvv(R, R);
89 auto crit = nR02 + 2.0 * _linesearch_c * _alpha * math::bvv(R0, dx);
90 if (verbose)
91 std::cout << " LS ITERATION " << std::setw(3) << i << ", alpha = " << std::scientific
92 << torch::min(_alpha).item<Real>() << ", |R| = " << std::scientific
93 << torch::max(torch::sqrt(nR2)).item<Real>() << ", |Rc| = " << std::scientific
94 << torch::min(torch::sqrt(crit)).item<Real>() << std::endl;
95
96 auto stop = torch::logical_or(nR2 <= crit, nR2 <= std::pow(atol, 2));
97
98 if (torch::all(stop).item<bool>())
99 break;
100
101 _alpha.batch_index_put({torch::logical_not(stop)},
102 _alpha.batch_index({torch::logical_not(stop)}) / _linesearch_sigma);
103 }
104}
105
106} // namespace neml2
Derived batch_index(TorchSlice indices) const
Get a batch.
Definition BatchTensorBase.cxx:184
void batch_index_put(TorchSlice indices, const torch::Tensor &other)
Set a index sliced on the batch dimensions to a value.
Definition BatchTensorBase.cxx:202
Definition BatchTensor.h:32
The wrapper (decorator) for cross-referencing unresolved values at parse time.
Definition CrossRef.h:52
static Scalar ones(const torch::TensorOptions &options=default_tensor_options())
Unbatched unit tensor.
Definition FixedDimTensor.h:161
The nonlinear solver solves a nonlinear system of equations.
Definition NewtonWithLineSearch.h:38
NewtonWithLineSearch(const OptionSet &options)
Definition NewtonWithLineSearch.cxx:55
virtual void linesearch(NonlinearSystem &system, const BatchTensor &x, const BatchTensor &dx)
Perform Armijo linesearch.
Definition NewtonWithLineSearch.cxx:74
Scalar _alpha
The line search parameter.
Definition NewtonWithLineSearch.h:61
Real _linesearch_c
Stopping criteria for linesearch.
Definition NewtonWithLineSearch.h:58
Real _linesearch_sigma
Decrease factor for linesearch.
Definition NewtonWithLineSearch.h:55
virtual void update(NonlinearSystem &system, BatchTensor &x) override
Update trial solution.
Definition NewtonWithLineSearch.cxx:64
static OptionSet expected_options()
Definition NewtonWithLineSearch.cxx:34
unsigned int _linesearch_miter
Linesearch maximum iterations.
Definition NewtonWithLineSearch.h:52
The nonlinear solver solves a nonlinear system of equations.
Definition Newton.h:39
static OptionSet expected_options()
Definition Newton.cxx:34
virtual BatchTensor solve_direction(const NonlinearSystem &system)
Find the current update direction.
Definition Newton.cxx:121
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
T & set(const std::string &)
Definition OptionSet.h:436
const bool verbose
Whether to print additional (debugging) information during the solve.
Definition Solver.h:49
BatchTensor bvv(const BatchTensor &a, const BatchTensor &b)
Batched vector-vector (dot) product.
Definition BatchTensor.cxx:137
Definition CrossRef.cxx:32