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/IncrementToRate.h"
26 : #include "neml2/tensors/Scalar.h"
27 : #include "neml2/tensors/Vec.h"
28 : #include "neml2/tensors/SR2.h"
29 : #include "neml2/tensors/R2.h"
30 : #include "neml2/tensors/SSR4.h"
31 : #include "neml2/tensors/R4.h"
32 :
33 : namespace neml2
34 : {
35 : template <typename T>
36 : OptionSet
37 8 : IncrementToRate<T>::expected_options()
38 : {
39 8 : OptionSet options = Model::expected_options();
40 8 : options.doc() = "Calculate the first order discrete time derivative of a variable as \\f$ "
41 : "\\dot{f} = \\frac{\\Delta f}{t-t_n} \\f$, where \\f$ \\Delta f \\f$ is the "
42 : "variable with the increment, "
43 : "and \\f$ t \\f$ is time.";
44 :
45 16 : options.set_output("rate");
46 16 : options.set("rate").doc() = "The variable's rate of change";
47 :
48 16 : options.set_input("variable");
49 8 : options.set("variable").doc() = "The incremental value";
50 :
51 24 : options.set_input("time") = VariableName(FORCES, "t");
52 8 : options.set("time").doc() = "Time";
53 :
54 8 : return options;
55 0 : }
56 :
57 : template <typename T>
58 4 : IncrementToRate<T>::IncrementToRate(const OptionSet & options)
59 : : Model(options),
60 4 : _dv(declare_input_variable<T>("variable")),
61 4 : _t(declare_input_variable<Scalar>("time")),
62 4 : _tn(declare_input_variable<Scalar>(_t.name().old())),
63 8 : _dv_dt(options.get<VariableName>("rate").empty()
64 8 : ? declare_output_variable<T>(_dv.name().with_suffix("_rate"))
65 8 : : declare_output_variable<T>("rate"))
66 : {
67 4 : }
68 :
69 : template <typename T>
70 : void
71 8 : IncrementToRate<T>::set_value(bool out, bool dout_din, bool /*d2out_din2*/)
72 : {
73 8 : auto dt = _t - _tn;
74 :
75 8 : if (out)
76 4 : _dv_dt = _dv / dt;
77 :
78 8 : if (dout_din)
79 : {
80 4 : if (_dv.is_dependent())
81 4 : _dv_dt.d(_dv) = T::identity_map(_dv.options()) / dt;
82 4 : if (_t.is_dependent())
83 4 : _dv_dt.d(_t) = -_dv / dt / dt;
84 4 : if (_tn.is_dependent())
85 4 : _dv_dt.d(_tn) = _dv / dt / dt;
86 : }
87 8 : }
88 :
89 : #define REGISTER(T) \
90 : using T##IncrementToRate = IncrementToRate<T>; \
91 : register_NEML2_object(T##IncrementToRate); \
92 : template class IncrementToRate<T>
93 : REGISTER(Scalar);
94 : REGISTER(Vec);
95 : REGISTER(SR2);
96 : REGISTER(R2);
97 : } // namespace neml2
|