Line data Source code
1 : // Copyright 2024, 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/tensors/functions/bmm.h"
27 : #include "neml2/tensors/functions/bmv.h"
28 : #include "neml2/tensors/functions/bvv.h"
29 : #include "neml2/tensors/functions/sqrt.h"
30 : #include "neml2/tensors/functions/pow.h"
31 : #include "neml2/tensors/functions/linalg/solve.h"
32 :
33 : namespace neml2
34 : {
35 3 : TrustRegionSubProblem::TrustRegionSubProblem(const OptionSet & options)
36 3 : : NonlinearSystem(options)
37 : {
38 3 : }
39 :
40 : void
41 18 : TrustRegionSubProblem::reinit(const NonlinearSystem::Res<true> & r,
42 : const NonlinearSystem::Jac<true> & J,
43 : const Scalar & delta)
44 : {
45 18 : _delta = delta;
46 18 : _JJ = bmm(J.base_transpose(0, 1), J);
47 18 : _Jr = bmv(J.base_transpose(0, 1), r);
48 18 : }
49 :
50 : void
51 69 : TrustRegionSubProblem::set_guess(const NonlinearSystem::Sol<false> & x)
52 : {
53 69 : _s = Scalar(x);
54 69 : }
55 :
56 : void
57 120 : TrustRegionSubProblem::assemble(NonlinearSystem::Res<false> * residual,
58 : NonlinearSystem::Jac<false> * Jacobian)
59 : {
60 120 : auto p = -preconditioned_direction(_s);
61 120 : auto np = sqrt(bvv(p, p));
62 :
63 120 : if (residual)
64 69 : *residual = NonlinearSystem::Res<false>(1.0 / np - 1.0 / sqrt(2.0 * _delta));
65 :
66 120 : if (Jacobian)
67 : *Jacobian =
68 51 : NonlinearSystem::Jac<false>(1.0 / pow(np, 3.0) * bvv(p, preconditioned_solve(_s, p)));
69 120 : }
70 :
71 : Tensor
72 189 : TrustRegionSubProblem::preconditioned_solve(const Scalar & s, const Tensor & v) const
73 : {
74 189 : return linalg::solve(_JJ + s * Tensor::identity(v.base_sizes()[0], v.options()), v);
75 : }
76 :
77 : Tensor
78 138 : TrustRegionSubProblem::preconditioned_direction(const Scalar & s) const
79 : {
80 138 : return preconditioned_solve(s, _Jr);
81 : }
82 : } // namespace neml2
|