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/models/BackwardEulerTimeIntegration.h"
26 :
27 : namespace neml2
28 : {
29 : template <typename T>
30 : OptionSet
31 8 : BackwardEulerTimeIntegration<T>::expected_options()
32 : {
33 8 : OptionSet options = Model::expected_options();
34 8 : options.doc() =
35 : "Define the backward Euler time integration residual \\f$ r = s - s_n - (t - t_n) \\dot{s} "
36 : "\\f$, where \\f$s\\f$ is the variable being integrated, \\f$\\dot{s}\\f$ is the variable "
37 : "rate, and \\f$t\\f$ is time. Subscripts \\f$n\\f$ denote quantities from the previous time "
38 : "step.";
39 :
40 8 : NonlinearSystem::enable_automatic_scaling(options);
41 :
42 16 : options.set_input("variable");
43 16 : options.set("variable").doc() = "Variable being integrated";
44 :
45 16 : options.set_input("rate");
46 8 : options.set("rate").doc() = "Variable rate";
47 :
48 24 : options.set_input("time") = VariableName(FORCES, "t");
49 8 : options.set("time").doc() = "Time";
50 :
51 8 : return options;
52 0 : }
53 :
54 : template <typename T>
55 25 : BackwardEulerTimeIntegration<T>::BackwardEulerTimeIntegration(const OptionSet & options)
56 : : Model(options),
57 25 : _s(declare_input_variable<T>("variable")),
58 25 : _sn(declare_input_variable<T>(_s.name().old())),
59 50 : _ds_dt(options.get<VariableName>("rate").empty()
60 98 : ? declare_input_variable<T>(_s.name().with_suffix("_rate"))
61 25 : : declare_input_variable<T>("rate")),
62 25 : _t(declare_input_variable<Scalar>("time")),
63 25 : _tn(declare_input_variable<Scalar>(_t.name().old())),
64 50 : _r(declare_output_variable<T>(_s.name().remount(RESIDUAL)))
65 : {
66 25 : }
67 :
68 : template <typename T>
69 : void
70 9 : BackwardEulerTimeIntegration<T>::diagnose() const
71 : {
72 9 : Model::diagnose();
73 9 : diagnostic_assert_state(_s);
74 9 : diagnostic_assert_state(_ds_dt);
75 9 : diagnostic_assert_force(_t);
76 9 : }
77 :
78 : template <typename T>
79 : void
80 49 : BackwardEulerTimeIntegration<T>::set_value(bool out, bool dout_din, bool /*d2out_din2*/)
81 : {
82 49 : if (out)
83 48 : _r = _s - _sn - _ds_dt * (_t - _tn);
84 :
85 49 : if (dout_din)
86 : {
87 28 : auto I = T::identity_map(_s.options());
88 :
89 28 : _r.d(_s) = I;
90 28 : _r.d(_ds_dt) = -I * (_t - _tn);
91 :
92 28 : if (currently_solving_nonlinear_system())
93 15 : return;
94 :
95 13 : _r.d(_sn) = -I;
96 13 : _r.d(_t) = -_ds_dt;
97 13 : _r.d(_tn) = _ds_dt;
98 28 : }
99 : }
100 :
101 : #define REGISTER(T) \
102 : using T##BackwardEulerTimeIntegration = BackwardEulerTimeIntegration<T>; \
103 : register_NEML2_object(T##BackwardEulerTimeIntegration); \
104 : template class BackwardEulerTimeIntegration<T>
105 : REGISTER(Scalar);
106 : REGISTER(Vec);
107 : REGISTER(SR2);
108 : REGISTER(R2);
109 : } // namespace neml2
|