NEML2 1.4.0
Loading...
Searching...
No Matches
Normality.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/solid_mechanics/Normality.h"
26
27namespace neml2
28{
29register_NEML2_object(Normality);
30
33{
35 options.doc() = "Store the first derivatives of a scalar-valued function in given variables, "
36 "i.e. \\f$ u_i = \\dfrac{f(\\boldsymbol{v})}{v_i} \\f$.";
37
38 options.set<std::string>("model");
39 options.set("model").doc() = "The model which evaluates the scalar-valued function";
40
41 options.set<VariableName>("function");
42 options.set("function").doc() = "Function to take derivative";
43
44 options.set<std::vector<VariableName>>("from");
45 options.set("from").doc() = "Function arguments to take derivatives w.r.t.";
46
47 options.set<std::vector<VariableName>>("to");
48 options.set("to").doc() = "Variables to store the first derivatives";
49
50 return options;
51}
52
54 : Model(options),
55 _model(register_model<Model>(options.get<std::string>("model"), /*extra_deriv_order=*/1)),
56 _f(options.get<VariableName>("function"))
57{
58 // Set up the conjugate pairs
59 const auto from = options.get<std::vector<VariableName>>("from");
60 const auto to = options.get<std::vector<VariableName>>("to");
61 neml_assert(from.size() == to.size(),
62 "The conjugate pairs should have a one-to-one correspondance. ",
63 from.size(),
64 " variables are being mapped to ",
65 to.size(),
66 " variables.");
67 for (size_t i = 0; i < from.size(); i++)
68 {
71 }
72}
73
74void
76{
77 neml_assert_dbg(!d2out_din2, "Normality doesn't implement second derivatives.");
78
79 // All we do here is simply mapping the derivatives.
80 // However, let's consider all the cases to make it as efficient as possible.
81 if (out && !dout_din)
83 else
85
86 for (auto && [ivar, var] : _conjugate_pairs)
87 {
88 if (out)
89 (*var) = _model.derivative_storage()(_f, ivar);
90
91 if (dout_din)
92 for (auto && [jvar, j] : input_views())
94 }
95}
96} // namespace neml2
The wrapper (decorator) for cross-referencing unresolved values at parse time.
Definition CrossRef.h:52
The accessor containing all the information needed to access an item in a LabeledAxis.
Definition LabeledAxisAccessor.h:44
TorchSize storage_size() const
Get the (total) storage size of this axis.
Definition LabeledAxis.h:142
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
virtual std::tuple< LabeledVector, LabeledMatrix, LabeledTensor3D > value_and_dvalue_and_d2value(const LabeledVector &in)
Convenient shortcut to construct and return the model's value, first and second derivative.
Definition Model.cxx:365
virtual std::tuple< LabeledVector, LabeledMatrix > value_and_dvalue(const LabeledVector &in)
Convenient shortcut to construct and return the model value and its derivative.
Definition Model.cxx:356
static OptionSet expected_options()
Definition Model.cxx:32
Definition Normality.h:32
Model & _model
The model which evaluates the potential function.
Definition Normality.h:43
const VariableName _f
The potential function.
Definition Normality.h:46
Normality(const OptionSet &options)
Definition Normality.cxx:53
static OptionSet expected_options()
Definition Normality.cxx:32
std::map< VariableName, Variable< BatchTensor > * > _conjugate_pairs
Definition Normality.h:48
void set_value(bool out, bool dout_din, bool d2out_din2) override
The flow direction.
Definition Normality.cxx:75
A custom map-like data structure. The keys are strings, and the values can be nonhomogeneously typed.
Definition OptionSet.h:59
LabeledMatrix & derivative_storage()
Definition VariableStore.h:127
Variable< T > & declare_output_variable(S &&... name)
Declare an output variable.
Definition VariableStore.h:205
LabeledAxis & output_axis()
Definition VariableStore.h:97
Storage< VariableName, VariableBase > & input_views()
Definition VariableStore.h:103
LabeledTensor3D & second_derivative_storage()
Definition VariableStore.h:133
LabeledAxis & input_axis()
Definition VariableStore.h:91
Definition CrossRef.cxx:32
void neml_assert_dbg(bool assertion, Args &&... args)
Definition error.h:85
void neml_assert(bool assertion, Args &&... args)
Definition error.h:73