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/LinearInterpolation.h"
26 : #include "neml2/tensors/functions/diff.h"
27 : #include "neml2/tensors/Scalar.h"
28 : #include "neml2/tensors/Vec.h"
29 : #include "neml2/tensors/SR2.h"
30 : #include "neml2/tensors/indexing.h"
31 :
32 : namespace neml2
33 : {
34 : template <typename T>
35 : OptionSet
36 6 : LinearInterpolation<T>::expected_options()
37 : {
38 6 : OptionSet options = Interpolation<T>::expected_options();
39 6 : options.doc() += " This object performs a _linear interpolation_.";
40 :
41 12 : options.set<bool>("define_second_derivatives") = true;
42 :
43 12 : options.set<TensorName<Scalar>>("abscissa");
44 12 : options.set("abscissa").doc() = "Scalar defining the abscissa values of the interpolant";
45 :
46 12 : options.set_input("argument");
47 6 : options.set("argument").doc() = "Argument used to query the interpolant";
48 :
49 6 : return options;
50 0 : }
51 :
52 : template <typename T>
53 14 : LinearInterpolation<T>::LinearInterpolation(const OptionSet & options)
54 : : Interpolation<T>(options),
55 42 : _X(this->template declare_parameter<Scalar>("X", "abscissa")),
56 28 : _x(this->template declare_input_variable<Scalar>("argument"))
57 : {
58 14 : }
59 :
60 : template <typename T>
61 : void
62 33 : LinearInterpolation<T>::set_value(bool out, bool dout_din, bool d2out_din2)
63 : {
64 66 : const auto slope =
65 66 : diff(this->_Y, 1, this->_Y.batch_dim() - 1) / diff(this->_X, 1, this->_X.batch_dim() - 1);
66 132 : const auto X0 = this->_X.batch_index({indexing::Ellipsis, indexing::Slice(indexing::None, -1)})
67 : .batch_expand_as(slope);
68 132 : const auto X1 = this->_X.batch_index({indexing::Ellipsis, indexing::Slice(1, indexing::None)})
69 : .batch_expand_as(slope);
70 132 : const auto Y0 = this->_Y.batch_index({indexing::Ellipsis, indexing::Slice(indexing::None, -1)})
71 : .batch_expand_as(slope);
72 :
73 33 : const auto x = Scalar(this->_x);
74 33 : const auto loc =
75 : Scalar(at::logical_and(at::gt(x.batch_unsqueeze(-1), X0), at::le(x.batch_unsqueeze(-1), X1)));
76 33 : const auto si = mask<T>(slope, loc);
77 :
78 33 : if (out)
79 : {
80 15 : const auto X0i = mask<Scalar>(X0, loc);
81 15 : const auto Y0i = mask<T>(Y0, loc);
82 15 : this->_p = Y0i + si * (x - X0i);
83 15 : }
84 :
85 33 : if (dout_din)
86 13 : if (this->_x.is_dependent())
87 13 : this->_p.d(this->_x) = si;
88 :
89 : if (d2out_din2)
90 : {
91 : // zero
92 : }
93 231 : }
94 :
95 : #define REGISTER(T) \
96 : using T##LinearInterpolation = LinearInterpolation<T>; \
97 : register_NEML2_object(T##LinearInterpolation); \
98 : template class LinearInterpolation<T>
99 : REGISTER(Scalar);
100 : REGISTER(Vec);
101 : REGISTER(SR2);
102 : } // namespace neml2
|