25#include "neml2/solvers/NonlinearSystem.h"
26#include "neml2/misc/math.h"
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.";
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.";
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.";
56 options.
set(
"automatic_scaling").suppressed() =
true;
57 options.
set(
"automatic_scaling_tol").suppressed() =
true;
58 options.
set(
"automatic_scaling_miter").suppressed() =
true;
64 options.
set(
"automatic_scaling").suppressed() =
false;
65 options.
set(
"automatic_scaling_tol").suppressed() =
false;
66 options.
set(
"automatic_scaling_miter").suppressed() =
false;
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)
86 using namespace torch::indexing;
96 std::cout <<
"Before automatic scaling cond(J) = " << std::scientific
97 << torch::max(torch::linalg_cond(
Jp)).item<
Real>() << std::endl;
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>();
105 std::cout <<
"ITERATION " <<
itr <<
", ROW ILLNESS = " << std::scientific <<
rR
106 <<
", COL ILLNESS = " << std::scientific <<
rC << std::endl;
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;
125 std::cout <<
" After automatic scaling cond(J) = " << std::scientific
126 << torch::max(torch::linalg_cond(
Jp)).item<
Real>() << std::endl;
134 _autoscale ?
"Automatic scaling is requested but scaling matrices have not been initialized."
135 :
"Automatic scaling is not requested but scaling matrices were initialized.");
144 _autoscale ?
"Automatic scaling is requested but scaling matrices have not been initialized."
145 :
"Automatic scaling is not requested but scaling matrices were initialized.");
155 _autoscale ?
"Automatic scaling is requested but scaling matrices have not been initialized."
156 :
"Automatic scaling is not requested but scaling matrices were initialized.");
200std::tuple<BatchTensor, BatchTensor>
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