NEML2 2.0.0
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
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{
39
40// Guard a region where implicit solve is being performed
53
59{
60public:
65 template <bool scaled>
66 struct Res : public Tensor
67 {
68 Res() = default;
69
71 explicit Res(const Tensor & r)
72 : Tensor(r)
73 {
74 }
75
77 explicit Res(const Res<!scaled> & r)
78 : Tensor(r)
79 {
80 }
81 };
82
87 template <bool scaled>
88 struct Jac : public Tensor
89 {
90 Jac() = default;
91
93 explicit Jac(const Tensor & J)
94 : Tensor(J)
95 {
96 }
97
99 explicit Jac(const Jac<!scaled> & J)
100 : Tensor(J)
101 {
102 }
103 };
104
109 template <bool scaled>
110 struct Sol : public Tensor
111 {
112 Sol() = default;
113
115 explicit Sol(const Tensor & u)
116 : Tensor(u)
117 {
118 }
119
121 explicit Sol(const Sol<!scaled> & u)
122 : Tensor(u)
123 {
124 }
125 };
126
128 NonlinearSystem(NonlinearSystem &&) noexcept = default;
129 NonlinearSystem & operator=(const NonlinearSystem &) = delete;
130 NonlinearSystem & operator=(NonlinearSystem &&) = delete;
131 virtual ~NonlinearSystem() = default;
132
134 static void disable_automatic_scaling(OptionSet & options);
135 static void enable_automatic_scaling(OptionSet & options);
136
137 NonlinearSystem(const OptionSet & options);
138
169 virtual void init_scaling(const Sol<false> & x, const bool verbose = false);
170
172 Res<true> scale(const Res<false> & r) const;
174 Res<false> unscale(const Res<true> & r) const;
176 Jac<true> scale(const Jac<false> & J) const;
178 Jac<false> unscale(const Jac<true> & J) const;
180 Sol<true> scale(const Sol<false> & u) const;
182 Sol<false> unscale(const Sol<true> & u) const;
183
185 void set_guess(const Sol<true> & x);
187 virtual void set_guess(const Sol<false> & x) = 0;
189 template <bool scaled>
190 Res<scaled> residual();
192 template <bool scaled>
193 Res<scaled> residual(const Sol<scaled> & x);
195 template <bool scaled>
196 Jac<scaled> Jacobian();
198 template <bool scaled>
199 Jac<scaled> Jacobian(const Sol<scaled> & x);
201 template <bool scaled>
202 std::tuple<Res<scaled>, Jac<scaled>> residual_and_Jacobian();
204 template <bool scaled>
205 std::tuple<Res<scaled>, Jac<scaled>> residual_and_Jacobian(const Sol<scaled> & x);
206
207protected:
214 virtual void assemble(Res<false> * r, Jac<false> * J) = 0;
215
217 const bool _autoscale;
218
221
223 const unsigned int _autoscale_miter;
224
227
230
233
234private:
235 void ensure_scaling_matrices_initialized_dbg() const;
236};
237
239// Implementation
241
242template <bool scaled>
243NonlinearSystem::Res<scaled>
245{
246 Res<false> r;
247 assemble(&r, nullptr);
248 if constexpr (scaled)
249 return scale(r);
250 else
251 return r;
252}
253
254template <bool scaled>
261
262template <bool scaled>
265{
266 Jac<false> J;
267 assemble(nullptr, &J);
268 if constexpr (scaled)
269 return scale(J);
270 else
271 return J;
272}
273
274template <bool scaled>
281
282template <bool scaled>
283std::tuple<NonlinearSystem::Res<scaled>, NonlinearSystem::Jac<scaled>>
285{
286 Res<false> r;
287 Jac<false> J;
288 assemble(&r, &J);
289 if constexpr (scaled)
290 return {scale(r), scale(J)};
291 else
292 return {r, J};
293}
294
295template <bool scaled>
296std::tuple<NonlinearSystem::Res<scaled>, NonlinearSystem::Jac<scaled>>
302} // namespace neml2
Tensor _row_scaling
Row scaling "matrix" – since it's a batched diagonal matrix, we are only storing its diagonals.
Definition NonlinearSystem.h:229
static void disable_automatic_scaling(OptionSet &options)
Definition NonlinearSystem.cxx:76
Res< false > unscale(const Res< true > &r) const
Remove scaling to the residual.
Definition NonlinearSystem.cxx:175
Res< true > scale(const Res< false > &r) const
Apply scaling to the residual.
Definition NonlinearSystem.cxx:165
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:84
Jac< scaled > Jacobian()
Assemble and return the Jacobian.
Definition NonlinearSystem.h:264
NonlinearSystem(const NonlinearSystem &)=default
void set_guess(const Sol< true > &x)
Set the current guess.
Definition NonlinearSystem.cxx:226
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:232
const bool _autoscale
If true, do automatic scaling.
Definition NonlinearSystem.h:217
const unsigned int _autoscale_miter
Maximum number of iterations allowed for the iterative automatic scaling algorithm.
Definition NonlinearSystem.h:223
Res< scaled > residual()
Assemble and return the residual.
Definition NonlinearSystem.h:244
static OptionSet expected_options()
Definition NonlinearSystem.cxx:53
const Real _autoscale_tol
Tolerance for convergence check of the iterative automatic scaling algorithm.
Definition NonlinearSystem.h:220
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:99
bool _scaling_matrices_initialized
Flag to indicate whether scaling matrices have been computed.
Definition NonlinearSystem.h:226
std::tuple< Res< scaled >, Jac< scaled > > residual_and_Jacobian()
Assemble and return the residual and Jacobian.
Definition NonlinearSystem.h:284
A custom map-like data structure. The keys are strings, and the values can be nonhomogeneously typed.
Definition OptionSet.h:52
Definition Tensor.h:46
Tensor()=default
Special member functions.
Definition DiagnosticsInterface.cxx:30
double Real
Definition types.h:68
bool & currently_solving_nonlinear_system()
Definition NonlinearSystem.cxx:35
Definition NonlinearSystem.h:89
Jac(const Jac<!scaled > &J)
Conversion between scaled and unscaled must be explicit.
Definition NonlinearSystem.h:99
Jac(const Tensor &J)
Conversion from Tensor must be explicit.
Definition NonlinearSystem.h:93
Definition NonlinearSystem.h:67
Res(const Tensor &r)
Conversion from Tensor must be explicit.
Definition NonlinearSystem.h:71
Res(const Res<!scaled > &r)
Conversion between scaled and unscaled must be explicit.
Definition NonlinearSystem.h:77
Definition NonlinearSystem.h:111
Sol(const Tensor &u)
Conversion from Tensor must be explicit.
Definition NonlinearSystem.h:115
Sol(const Sol<!scaled > &u)
Conversion between scaled and unscaled must be explicit.
Definition NonlinearSystem.h:121
SolvingNonlinearSystem(const SolvingNonlinearSystem &)=delete
SolvingNonlinearSystem & operator=(SolvingNonlinearSystem &&)=delete
SolvingNonlinearSystem & operator=(const SolvingNonlinearSystem &)=delete
SolvingNonlinearSystem(SolvingNonlinearSystem &&)=delete
~SolvingNonlinearSystem()
Definition NonlinearSystem.cxx:47
const bool prev_bool
Definition NonlinearSystem.h:51
SolvingNonlinearSystem(bool solving=true)
Definition NonlinearSystem.cxx:41