NEML2 2.0.0
Loading...
Searching...
No Matches
NonlinearSystem.h
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#pragma once
26
27#include "neml2/tensors/Tensor.h"
28#include "neml2/base/OptionSet.h"
29
30namespace neml2
31{
37{
38public:
43 template <bool scaled>
44 struct Res : public Tensor
45 {
46 Res() = default;
47
49 explicit Res(const Tensor & r)
50 : Tensor(r)
51 {
52 }
53
55 explicit Res(const Res<!scaled> & r)
56 : Tensor(r)
57 {
58 }
59 };
60
65 template <bool scaled>
66 struct Jac : public Tensor
67 {
68 Jac() = default;
69
71 explicit Jac(const Tensor & J)
72 : Tensor(J)
73 {
74 }
75
77 explicit Jac(const Jac<!scaled> & J)
78 : Tensor(J)
79 {
80 }
81 };
82
87 template <bool scaled>
88 struct Sol : public Tensor
89 {
90 Sol() = default;
91
93 explicit Sol(const Tensor & u)
94 : Tensor(u)
95 {
96 }
97
99 explicit Sol(const Sol<!scaled> & u)
100 : Tensor(u)
101 {
102 }
103 };
104
110
114
116
147 virtual void init_scaling(const Sol<false> & x, const bool verbose = false);
148
161
167 template <bool scaled>
170 template <bool scaled>
173 template <bool scaled>
176 template <bool scaled>
179 template <bool scaled>
182 template <bool scaled>
184
192 virtual void assemble(Res<false> * r, Jac<false> * J) = 0;
193
196
199
202
205
208
211
212private:
213 void ensure_scaling_matrices_initialized_dbg() const;
214};
215
217// Implementation
219
220template <bool scaled>
223{
225 assemble(&r, nullptr);
226 if constexpr (scaled)
227 return scale(r);
228 else
229 return r;
230}
231
232template <bool scaled>
239
240template <bool scaled>
243{
245 assemble(nullptr, &J);
246 if constexpr (scaled)
247 return scale(J);
248 else
249 return J;
250}
251
252template <bool scaled>
259
260template <bool scaled>
261std::tuple<NonlinearSystem::Res<scaled>, NonlinearSystem::Jac<scaled>>
263{
266 assemble(&r, &J);
267 if constexpr (scaled)
268 return {scale(r), scale(J)};
269 else
270 return {r, J};
271}
272
273template <bool scaled>
274std::tuple<NonlinearSystem::Res<scaled>, NonlinearSystem::Jac<scaled>>
280} // namespace neml2
The wrapper (decorator) for cross-referencing unresolved values at parse time.
Definition CrossRef.h:54
Definition of a nonlinear system of equations.
Definition NonlinearSystem.h:37
Tensor _row_scaling
Row scaling "matrix" – since it's a batched diagonal matrix, we are only storing its diagonals.
Definition NonlinearSystem.h:207
static void disable_automatic_scaling(OptionSet &options)
Definition NonlinearSystem.cxx:54
Res< false > unscale(const Res< true > &r) const
Remove scaling to the residual.
Definition NonlinearSystem.cxx:148
Res< true > scale(const Res< false > &r) const
Apply scaling to the residual.
Definition NonlinearSystem.cxx:138
virtual void assemble(Res< false > *r, Jac< false > *J)=0
Compute the unscaled residual and Jacobian.
static void enable_automatic_scaling(OptionSet &options)
Definition NonlinearSystem.cxx:62
Jac< scaled > Jacobian()
Assemble and return the Jacobian.
Definition NonlinearSystem.h:242
NonlinearSystem(const NonlinearSystem &)=default
NonlinearSystem(NonlinearSystem &&) noexcept=default
Tensor _col_scaling
Column scaling "matrix" – since it's a batched diagonal matrix, we are only storing its diagonals.
Definition NonlinearSystem.h:210
const bool _autoscale
If true, do automatic scaling.
Definition NonlinearSystem.h:195
const unsigned int _autoscale_miter
Maximum number of iterations allowed for the iterative automatic scaling algorithm.
Definition NonlinearSystem.h:201
Res< scaled > residual()
Assemble and return the residual.
Definition NonlinearSystem.h:222
static OptionSet expected_options()
Definition NonlinearSystem.cxx:31
virtual void set_guess(const Sol< true > &x) final
Set the current guess.
Definition NonlinearSystem.cxx:200
const Real _autoscale_tol
Tolerance for convergence check of the iterative automatic scaling algorithm.
Definition NonlinearSystem.h:198
virtual void init_scaling(const Sol< false > &x, const bool verbose=false)
Compute algebraic Jacobian-based automatic scaling following https://cs.stanford.edu/people/paulliu/f...
Definition NonlinearSystem.cxx:78
bool _scaling_matrices_initialized
Flag to indicate whether scaling matrices have been computed.
Definition NonlinearSystem.h:204
std::tuple< Res< scaled >, Jac< scaled > > residual_and_Jacobian()
Assemble and return the residual and Jacobian.
Definition NonlinearSystem.h:262
A custom map-like data structure. The keys are strings, and the values can be nonhomogeneously typed.
Definition OptionSet.h:85
Definition Tensor.h:47
Definition CrossRef.cxx:31
double Real
Definition types.h:31
Definition NonlinearSystem.h:67
Jac(const Jac<!scaled > &J)
Conversion between scaled and unscaled must be explicit.
Definition NonlinearSystem.h:77
Jac(const Tensor &J)
Conversion from Tensor must be explicit.
Definition NonlinearSystem.h:71
Definition NonlinearSystem.h:45
Res(const Tensor &r)
Conversion from Tensor must be explicit.
Definition NonlinearSystem.h:49
Res(const Res<!scaled > &r)
Conversion between scaled and unscaled must be explicit.
Definition NonlinearSystem.h:55
Definition NonlinearSystem.h:89
Sol(const Tensor &u)
Conversion from Tensor must be explicit.
Definition NonlinearSystem.h:93
Sol(const Sol<!scaled > &u)
Conversion between scaled and unscaled must be explicit.
Definition NonlinearSystem.h:99