NEML2 1.4.0
Loading...
Searching...
No Matches
SumModel.cxx
1// Copyright 2023, 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/SumModel.h"
26#include "neml2/tensors/SSR4.h"
27
28namespace neml2
29{
30register_NEML2_object(ScalarSumModel);
31register_NEML2_object(SR2SumModel);
32
33template <typename T>
36{
37 // This is the only way of getting tensor type in a static method like this...
38 // Trim 6 chars to remove 'neml2::'
39 auto tensor_type = utils::demangle(typeid(T).name()).substr(7);
40
42 options.doc() = "Calculate linear combination of multiple " + tensor_type +
43 " tensors as \\f$ u = c_i v_i \\f$ (Einstein summation assumed), where \\f$ c_i "
44 "\\f$ are the coefficients, and \\f$ v_i \\f$ are the variables to be summed.";
45
46 options.set<std::vector<VariableName>>("from_var");
47 options.set("from_var").doc() = tensor_type + " tensors to be summed";
48
49 options.set<VariableName>("to_var");
50 options.set("to_var").doc() = "The sum";
51
52 options.set<std::vector<CrossRef<Scalar>>>("coefficients") = {};
53 options.set("coefficients").doc() = "Weights associated with each variable";
54
55 return options;
56}
57
58template <typename T>
60 : Model(options),
61 _to(declare_output_variable<T>("to_var"))
62{
63 for (auto fv : options.get<std::vector<VariableName>>("from_var"))
65
66 // The number of coefficients can be 0, 1, or N
67 // - 0: The _coefs vector will be filled with ones
68 // - 1: The _coefs vector will be filled with _coefs[0]
69 // - N: N must be equal to the length of _from
70 const auto coefs_in = options.get<std::vector<CrossRef<Scalar>>>("coefficients");
71 const auto N = _from.size();
72 if (coefs_in.size() == 0)
73 _coefs = std::vector<const Scalar *>(
75 else if (coefs_in.size() == 1)
76 _coefs = std::vector<const Scalar *>(N, &declare_parameter("c", Scalar(coefs_in[0])));
77 else
78 {
79 neml_assert(coefs_in.size() == N,
80 "Number of coefficients must be 0, 1, or N, where N is the number of 'from_var'.");
81 _coefs.resize(N);
82 for (size_t i = 0; i < N; i++)
84 }
85}
86
87template <typename T>
88void
90{
91 const auto N = _from.size();
92
93 if (out)
94 {
95 auto sum = T::zeros(_to.batch_sizes(), options());
96 for (size_t i = 0; i < N; i++)
97 sum += (*_coefs[i]) * (*_from[i]);
98 _to = sum;
99 }
100
101 if (dout_din)
102 for (size_t i = 0; i < N; i++)
103 _to.d(*_from[i]) = (*_coefs[i]) * T::identity_map(options());
104
105 if (d2out_din2)
106 {
107 // zero
108 }
109}
110
111template class SumModel<Scalar>;
112template class SumModel<SR2>;
113} // namespace neml2
The wrapper (decorator) for cross-referencing unresolved values at parse time.
Definition CrossRef.h:52
CrossRef()=default
The accessor containing all the information needed to access an item in a LabeledAxis.
Definition LabeledAxisAccessor.h:44
The base class for all constitutive models.
Definition Model.h:53
const torch::TensorOptions & options() const
This model's tensor options.
Definition Model.h:116
static OptionSet expected_options()
Definition Model.cxx:32
A custom map-like data structure. The keys are strings, and the values can be nonhomogeneously typed.
Definition OptionSet.h:59
const std::string & doc() const
A readonly reference to the option set's docstring.
Definition OptionSet.h:91
T & set(const std::string &)
Definition OptionSet.h:436
const T & declare_parameter(const std::string &name, const T &rawval)
Declare a parameter.
Definition ParameterStore.h:145
The (logical) scalar.
Definition Scalar.h:38
std::vector< const Scalar * > _coefs
Scaling coefficient for each term.
Definition SumModel.h:49
std::vector< const Variable< T > * > _from
The input variables (to be summed)
Definition SumModel.h:46
static OptionSet expected_options()
Definition SumModel.cxx:35
SumModel(const OptionSet &options)
Definition SumModel.cxx:59
void set_value(bool out, bool dout_din, bool d2out_din2) override
The map between input -> output, and optionally its derivatives.
Definition SumModel.cxx:89
std::string stringify(const T &t)
Definition utils.h:302
std::string demangle(const char *name)
Definition parser_utils.cxx:46
Definition CrossRef.cxx:32
const torch::TensorOptions default_tensor_options()
Definition types.cxx:30
void neml_assert(bool assertion, Args &&... args)
Definition error.h:73