NEML2 1.4.0
Loading...
Searching...
No Matches
NonlinearSystem.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/NonlinearSystem.h"
26#include "neml2/misc/math.h"
27
28namespace neml2
29{
30OptionSet
32{
33 OptionSet options;
34
35 options.set<bool>("automatic_scaling") = false;
36 options.set("automatic_scaling").doc() =
37 "Whether to perform automatic scaling. See neml2::NonlinearSystem::init_scaling for "
38 "implementation details.";
39
40 options.set<Real>("automatic_scaling_tol") = 0.01;
41 options.set("automatic_scaling_tol").doc() =
42 "Tolerance used in iteratively updating the scaling matrices.";
43
44 options.set<unsigned int>("automatic_scaling_miter") = 20;
45 options.set("automatic_scaling_miter").doc() =
46 "Maximum number of automatic scaling iterations. No error is produced upon reaching the "
47 "maximum number of scaling iterations, and the scaling matrices obtained at the last "
48 "iteration are used to scale the nonlinear system.";
49
50 return options;
51}
52
53void
55{
56 options.set("automatic_scaling").suppressed() = true;
57 options.set("automatic_scaling_tol").suppressed() = true;
58 options.set("automatic_scaling_miter").suppressed() = true;
59}
60
61void
63{
64 options.set("automatic_scaling").suppressed() = false;
65 options.set("automatic_scaling_tol").suppressed() = false;
66 options.set("automatic_scaling_miter").suppressed() = false;
67}
68
70 : _autoscale(options.get<bool>("automatic_scaling")),
71 _autoscale_tol(options.get<Real>("automatic_scaling_tol")),
72 _autoscale_miter(options.get<unsigned int>("automatic_scaling_miter")),
73 _scaling_matrices_initialized(false)
74{
75}
76
77void
79{
80 if (!_autoscale)
81 return;
82
84 return;
85
86 using namespace torch::indexing;
87
88 // First compute the Jacobian
89 assemble(false, true);
90
91 auto Jp = _Jacobian.clone();
94
95 if (verbose)
96 std::cout << "Before automatic scaling cond(J) = " << std::scientific
97 << torch::max(torch::linalg_cond(Jp)).item<Real>() << std::endl;
98
99 for (unsigned int itr = 0; itr < _autoscale_miter; itr++)
100 {
101 // check for convergence
102 auto rR = torch::max(torch::abs(1.0 - 1.0 / torch::sqrt(std::get<0>(Jp.max(-1))))).item<Real>();
103 auto rC = torch::max(torch::abs(1.0 - 1.0 / torch::sqrt(std::get<0>(Jp.max(-2))))).item<Real>();
104 if (verbose)
105 std::cout << "ITERATION " << itr << ", ROW ILLNESS = " << std::scientific << rR
106 << ", COL ILLNESS = " << std::scientific << rC << std::endl;
108 break;
109
110 // scale rows and columns
111 for (TorchSize i = 0; i < _ndof; i++)
112 {
113 auto ar = 1.0 / torch::sqrt(torch::max(Jp.base_index({i})));
114 auto ac = 1.0 / torch::sqrt(torch::max(Jp.base_index({Slice(), i})));
117 Jp.base_index({i}) *= ar;
118 Jp.base_index({Slice(), i}) *= ac;
119 }
120 }
121
123
124 if (verbose)
125 std::cout << " After automatic scaling cond(J) = " << std::scientific
126 << torch::max(torch::linalg_cond(Jp)).item<Real>() << std::endl;
127}
128
131{
134 _autoscale ? "Automatic scaling is requested but scaling matrices have not been initialized."
135 : "Automatic scaling is not requested but scaling matrices were initialized.");
136 return _row_scaling * r;
137}
138
141{
144 _autoscale ? "Automatic scaling is requested but scaling matrices have not been initialized."
145 : "Automatic scaling is not requested but scaling matrices were initialized.");
148}
149
152{
155 _autoscale ? "Automatic scaling is requested but scaling matrices have not been initialized."
156 : "Automatic scaling is not requested but scaling matrices were initialized.");
157 return _autoscale ? _col_scaling * p : p;
158}
159
160void
162{
163 _solution.variable_data().copy_(x);
164}
165
168{
170 residual();
171 return residual_view();
172}
173
174void
182
185{
187 Jacobian();
188 return Jacobian_view();
189}
190
191void
199
200std::tuple<BatchTensor, BatchTensor>
207
208void
219
225} // namespace neml2
Derived clone(torch::MemoryFormat memory_format=torch::MemoryFormat::Contiguous) const
Clone (take ownership)
Definition BatchTensorBase.cxx:317
BatchTensor base_index(const TorchSlice &indices) const
Return an index sliced on the base dimensions.
Definition BatchTensorBase.cxx:193
static BatchTensor ones_like(const BatchTensor &other)
Unit tensor like another, i.e. same batch and base shapes, same tensor options, etc.
Definition BatchTensorBase.cxx:66
Definition BatchTensor.h:32
The wrapper (decorator) for cross-referencing unresolved values at parse time.
Definition CrossRef.h:52
static void disable_automatic_scaling(OptionSet &options)
Definition NonlinearSystem.cxx:54
BatchTensor _scaled_residual
Definition NonlinearSystem.h:110
BatchTensor _Jacobian
View for the Jacobian of this nonlinear system.
Definition NonlinearSystem.h:108
static void enable_automatic_scaling(OptionSet &options)
Definition NonlinearSystem.cxx:62
NonlinearSystem(const OptionSet &options)
Definition NonlinearSystem.cxx:69
virtual void set_solution(const BatchTensor &x)
Set the solution vector.
Definition NonlinearSystem.cxx:161
BatchTensor _col_scaling
Column scaling "matrix" – since it's a batched diagonal matrix, we are only storing its diagonals.
Definition NonlinearSystem.h:130
virtual void init_scaling(const bool verbose=false)
Compute algebraic Jacobian-based automatic scaling following https://cs.stanford.edu/people/paulliu/f...
Definition NonlinearSystem.cxx:78
void residual_and_Jacobian()
Convenient shortcut to assemble and return the system residual and Jacobian.
Definition NonlinearSystem.cxx:209
void residual()
Convenient shortcut to assemble and return the system residual.
Definition NonlinearSystem.cxx:175
const BatchTensor & Jacobian_view() const
Definition NonlinearSystem.h:84
BatchTensor _solution
View for the solution of this nonlinear system.
Definition NonlinearSystem.h:102
BatchTensor scale_direction(const BatchTensor &p) const
Remove scaling from the search direction, i.e. .
Definition NonlinearSystem.cxx:151
TorchSize _ndof
Number of degrees of freedom.
Definition NonlinearSystem.h:99
BatchTensor _residual
View for the residual of this nonlinear system.
Definition NonlinearSystem.h:105
const bool _autoscale
If true, do automatic scaling.
Definition NonlinearSystem.h:115
BatchTensor scale_residual(const BatchTensor &r) const
Apply scaling to the residual.
Definition NonlinearSystem.cxx:130
const BatchTensor & residual_view() const
Definition NonlinearSystem.h:83
BatchTensor _row_scaling
Row scaling "matrix" – since it's a batched diagonal matrix, we are only storing its diagonals.
Definition NonlinearSystem.h:127
const unsigned int _autoscale_miter
Maximum number of iterations allowed for the iterative automatic scaling algorithm.
Definition NonlinearSystem.h:121
BatchTensor _scaled_Jacobian
Definition NonlinearSystem.h:112
BatchTensor residual_norm() const
The residual norm.
Definition NonlinearSystem.cxx:221
void Jacobian()
Convenient shortcut to assemble and return the system Jacobian.
Definition NonlinearSystem.cxx:192
static OptionSet expected_options()
Definition NonlinearSystem.cxx:31
const Real _autoscale_tol
Tolerance for convergence check of the iterative automatic scaling algorithm.
Definition NonlinearSystem.h:118
BatchTensor scale_Jacobian(const BatchTensor &J) const
Apply scaling to the Jacobian.
Definition NonlinearSystem.cxx:140
bool _scaling_matrices_initialized
Flag to indicate whether scaling matrices have been computed.
Definition NonlinearSystem.h:124
virtual void assemble(bool residual, bool Jacobian)=0
Compute the residual and Jacobian.
A custom map-like data structure. The keys are strings, and the values can be nonhomogeneously typed.
Definition OptionSet.h:59
T & set(const std::string &)
Definition OptionSet.h:436
BatchTensor vector_norm(const BatchTensor &v)
Vector norm of a vector. Falls back to math::abs is v is a Scalar.
Definition math.cxx:317
BatchTensor base_diag_embed(const BatchTensor &a, TorchSize offset, TorchSize d1, TorchSize d2)
Definition math.cxx:251
BatchTensor bmm(const BatchTensor &a, const BatchTensor &b)
Batched matrix-matrix product.
Definition BatchTensor.cxx:107
Definition CrossRef.cxx:32
void neml_assert_dbg(bool assertion, Args &&... args)
Definition error.h:85
double Real
Definition types.h:33