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/user_tensors/LinspacePrimitiveTensor.h"
26 : #include "neml2/tensors/tensors.h"
27 : #include "neml2/misc/assertions.h"
28 :
29 : namespace neml2
30 : {
31 : template <typename T>
32 : OptionSet
33 44 : LinspacePrimitiveTensor<T>::expected_options()
34 : {
35 : // This is the only way of getting tensor type in a static method like this...
36 : // Trim 6 chars to remove 'neml2::'
37 44 : auto tensor_type = utils::demangle(typeid(T).name()).substr(7);
38 :
39 44 : OptionSet options = UserTensorBase::expected_options();
40 44 : options.doc() = "Construct a " + tensor_type +
41 : " linearly spaced on the batch dimensions. See neml2::TensorBase::linspace "
42 : "for a detailed explanation.";
43 :
44 88 : options.set<TensorName<T>>("start");
45 88 : options.set("start").doc() = "The starting tensor";
46 :
47 88 : options.set<TensorName<T>>("end");
48 88 : options.set("end").doc() = "The ending tensor";
49 :
50 88 : options.set<Size>("nstep");
51 88 : options.set("nstep").doc() = "The number of steps with even spacing along the new dimension";
52 :
53 88 : options.set<Size>("dim") = 0;
54 44 : options.set("dim").doc() = "Where to insert the new dimension";
55 :
56 132 : options.set<TensorShape>("batch_expand") = TensorShape();
57 44 : options.set("batch_expand").doc() = "After construction, perform an additional batch expanding "
58 : "operation into the given batch shape.";
59 :
60 88 : return options;
61 44 : }
62 :
63 : template <typename T>
64 84 : LinspacePrimitiveTensor<T>::LinspacePrimitiveTensor(const OptionSet & options)
65 : : UserTensorBase(options),
66 84 : T(make(options))
67 : {
68 84 : }
69 :
70 : template <typename T>
71 : T
72 84 : LinspacePrimitiveTensor<T>::make(const OptionSet & options) const
73 : {
74 84 : auto * f = this->factory();
75 84 : neml_assert(f, "Internal error: factory != nullptr");
76 :
77 672 : auto t = T::linspace(options.get<TensorName<T>>("start").resolve(f),
78 : options.get<TensorName<T>>("end").resolve(f),
79 : options.get<Size>("nstep"),
80 : options.get<Size>("dim"));
81 :
82 : // Expand if requested
83 84 : auto S = options.get<TensorShape>("batch_expand");
84 84 : if (!S.empty())
85 0 : t = t.batch_expand(S);
86 :
87 168 : return t;
88 84 : }
89 :
90 : #define LINSPACEPRIMITIVETENSOR_REGISTER(T) \
91 : using Linspace##T = LinspacePrimitiveTensor<T>; \
92 : register_NEML2_object_alias(Linspace##T, "Linspace" #T)
93 : FOR_ALL_PRIMITIVETENSOR(LINSPACEPRIMITIVETENSOR_REGISTER);
94 : } // namespace neml2
|