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/HermiteSmoothStep.h"
26 : #include "neml2/misc/types.h"
27 : #include "neml2/tensors/functions/clamp.h"
28 : #include "neml2/tensors/assertions.h"
29 :
30 : namespace neml2
31 : {
32 : register_NEML2_object(HermiteSmoothStep);
33 :
34 : OptionSet
35 2 : HermiteSmoothStep::expected_options()
36 : {
37 2 : OptionSet options = Model::expected_options();
38 2 : options.doc() = "The smooth step function defined by Hermite polynomials";
39 :
40 4 : options.set_input("argument");
41 4 : options.set("argument").doc() = "Argument of the smooth step function";
42 :
43 4 : options.set_output("value");
44 4 : options.set("value").doc() = "Value of the smooth step function";
45 :
46 4 : options.set_buffer<TensorName<Scalar>>("lower_bound");
47 4 : options.set("lower_bound").doc() = "Lower bound of the argument";
48 :
49 4 : options.set_buffer<TensorName<Scalar>>("upper_bound");
50 4 : options.set("upper_bound").doc() = "Upper bound of the argument";
51 :
52 4 : options.set<bool>("complement_condition") = false;
53 2 : options.set("complement_condition").doc() = "Whether takes 1 to subtract the function.";
54 :
55 2 : return options;
56 0 : }
57 :
58 2 : HermiteSmoothStep::HermiteSmoothStep(const OptionSet & options)
59 : : Model(options),
60 2 : _x(declare_input_variable<Scalar>("argument")),
61 2 : _y(declare_output_variable<Scalar>("value")),
62 8 : _x0(declare_buffer<Scalar>("lb", "lower_bound")),
63 10 : _x1(declare_buffer<Scalar>("ub", "upper_bound")),
64 4 : _comp_cond(options.get<bool>("complement_condition"))
65 : {
66 2 : }
67 :
68 : void
69 4 : HermiteSmoothStep::set_value(bool out, bool dout_din, bool d2out_din2)
70 : {
71 4 : neml_assert_dbg(!d2out_din2, "Second derivatives not implemented");
72 :
73 4 : const auto eps = machine_precision(_x.scalar_type()).toDouble();
74 4 : const auto x = clamp((_x - _x0) / (_x1 - _x0), eps, 1.0 - eps);
75 :
76 4 : if (out)
77 : {
78 :
79 2 : _y = 3 * x * x - 2 * x * x * x;
80 2 : if (_comp_cond)
81 1 : _y = 1 - (3 * x * x - 2 * x * x * x);
82 : }
83 :
84 4 : if (dout_din)
85 : {
86 2 : _y.d(_x) = 6 * x * (1 - x) / (_x1 - _x0);
87 2 : if (_comp_cond)
88 1 : _y.d(_x) = -(6 * x * (1 - x) / (_x1 - _x0));
89 : }
90 4 : }
91 : } // namespace neml2
|