NEML2 1.4.0
Loading...
Searching...
No Matches
TrustRegionSubProblem.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/TrustRegionSubProblem.h"
26#include "neml2/misc/math.h"
27
28namespace neml2
29{
34
35void
37{
38 _batch_sizes = delta.batch_sizes().vec();
39 _options = delta.options();
40
44
45 _R = system.residual_view().clone();
46 _J = system.Jacobian_view().clone();
47 _delta = delta.clone();
48
49 _JJ = math::bmm(_J.base_transpose(0, 1), _J);
50 _JR = math::bmv(_J.base_transpose(0, 1), _R);
51}
52
53void
54TrustRegionSubProblem::assemble(bool residual, bool Jacobian)
55{
56 auto s = Scalar(_solution);
58 auto np = math::sqrt(math::bvv(p, p));
59
60 if (residual)
61 _residual = 1.0 / np - 1.0 / math::sqrt(2.0 * _delta);
62
63 if (Jacobian)
65}
66
69{
70 return math::linalg::solve(_JJ + s * BatchTensor::identity(v.base_sizes()[0], _options), v);
71}
72
78} // namespace neml2
BatchTensor base_transpose(TorchSize d1, TorchSize d2) const
Transpose two base dimensions.
Definition BatchTensorBase.cxx:299
Definition BatchTensor.h:32
static BatchTensor identity(TorchSize n, const torch::TensorOptions &options=default_tensor_options())
Unbatched identity tensor.
Definition BatchTensor.cxx:91
The wrapper (decorator) for cross-referencing unresolved values at parse time.
Definition CrossRef.h:52
static Scalar empty(const torch::TensorOptions &options=default_tensor_options())
Unbatched empty tensor.
Definition FixedDimTensor.h:129
static Scalar zeros(const torch::TensorOptions &options=default_tensor_options())
Unbatched zero tensor.
Definition FixedDimTensor.h:145
Definition of a nonlinear system of equations.
Definition NonlinearSystem.h:37
BatchTensor _Jacobian
View for the Jacobian of this nonlinear system.
Definition NonlinearSystem.h:108
void residual()
Convenient shortcut to assemble and return the system residual.
Definition NonlinearSystem.cxx:175
BatchTensor _solution
View for the solution of this nonlinear system.
Definition NonlinearSystem.h:102
BatchTensor _residual
View for the residual of this nonlinear system.
Definition NonlinearSystem.h:105
void Jacobian()
Convenient shortcut to assemble and return the system Jacobian.
Definition NonlinearSystem.cxx:192
A custom map-like data structure. The keys are strings, and the values can be nonhomogeneously typed.
Definition OptionSet.h:59
The (logical) scalar.
Definition Scalar.h:38
virtual void assemble(bool residual, bool Jacobian) override
Compute the residual and Jacobian.
Definition TrustRegionSubProblem.cxx:54
torch::TensorOptions _options
Definition TrustRegionSubProblem.h:53
BatchTensor preconditioned_solve(const Scalar &s, const BatchTensor &v) const
Definition TrustRegionSubProblem.cxx:68
TrustRegionSubProblem(const OptionSet &options)
Definition TrustRegionSubProblem.cxx:30
TorchShape _batch_sizes
Definition TrustRegionSubProblem.h:51
virtual void reinit(const NonlinearSystem &system, const Scalar &delta)
Definition TrustRegionSubProblem.cxx:36
BatchTensor preconditioned_direction(const Scalar &s) const
Definition TrustRegionSubProblem.cxx:74
BatchTensor solve(const BatchTensor &A, const BatchTensor &B)
Solve the linear system A X = B.
Definition math.cxx:340
BatchTensor bvv(const BatchTensor &a, const BatchTensor &b)
Batched vector-vector (dot) product.
Definition BatchTensor.cxx:137
BatchTensor bmv(const BatchTensor &a, const BatchTensor &v)
Batched matrix-vector product.
Definition BatchTensor.cxx:122
BatchTensor bmm(const BatchTensor &a, const BatchTensor &b)
Batched matrix-matrix product.
Definition BatchTensor.cxx:107
Derived sqrt(const Derived &a)
Definition BatchTensorBase.h:439
Derived pow(const Derived &a, const Real &n)
Definition BatchTensorBase.h:332
Definition CrossRef.cxx:32