NEML2 2.1.0
Loading...
Searching...
No Matches
Derivative.h
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#pragma once
26
27#include "neml2/misc/types.h"
28#include "neml2/tensors/Tensor.h"
29
30namespace neml2
31{
32// forward declaration
33template <std::size_t N>
34class Derivative;
35
37template <std::size_t N>
38std::string
39derivative_name(const std::string & var_name, const std::array<std::string, N> & arg_names)
40{
41 std::string name = "d";
42 if constexpr (N > 1)
43 name += std::to_string(N);
44 name += "(" + var_name + ")/";
45 for (const auto & arg_name : arg_names)
46 name += "d(" + arg_name + ")";
47 return name;
48}
49
60template <std::size_t N>
61Tensor fullify(const Tensor & t, Size iid, std::array<TensorShape, N> iiss);
62
64template <std::size_t N>
66{
67public:
68 Derivative() = default;
70 const std::array<std::size_t, N + 1> & intrsc_intmd_dims,
71 const std::array<TensorShapeRef, N + 1> & intmd_sizes,
72 const std::array<TensorShapeRef, N + 1> & base_sizes,
73 [[maybe_unused]] std::string var_name = "",
74 [[maybe_unused]] std::array<std::string, N> arg_names = {});
75
77 Derivative(const Derivative<N> & val) = default;
78 Derivative(Derivative<N> && val) = default;
79 ~Derivative() = default;
81
83 std::string name() const;
85 const std::string & var_name() const;
87 const std::string & arg_name(std::size_t i) const;
88
91 Derivative & operator=(const Tensor & val);
92 Derivative & operator=(const Derivative<N> & val) = default;
93 Derivative & operator=(Derivative<N> && val) noexcept = default;
95
101
103 const Tensor & tensor() const;
104
106 void clear();
107
109 bool defined() const;
110
113
121 TensorShapeRef arg_intmd_sizes(std::size_t i) const;
123
126 TensorShapeRef arg_base_sizes(std::size_t i) const;
128
134 Size arg_intrsc_intmd_dim(std::size_t i) const;
135
136 Size base_dim() const;
138 Size arg_base_dim(std::size_t i) const;
140
142 Derivative<N> reinterpret(std::size_t additional_intrsc_intmd_dim) const;
143
150
151private:
152 Tensor try_intmd_expand(const Tensor & val) const;
153
155 Size _intrsc_intmd_dim = 0;
156
158 std::array<Size, N + 1> _intrsc_intmd_dims;
159
161 std::array<TensorShape, N + 1> _intmd_sizes;
162
164 std::array<TensorShape, N + 1> _base_sizes;
165
167 std::string _var_name;
168 std::array<std::string, N> _arg_names;
169
171 Tensor _deriv;
172
175 Size _cached_intmd_dim = 0;
176};
177} // namespace neml2
Derivative wrapper.
Definition Derivative.h:66
Derivative & operator=(const Tensor &val)
Derivative & operator+=(const Derivative< N > &val)
TensorShapeRef arg_intmd_sizes(std::size_t i) const
Derivative(std::size_t intrsc_intmd_dim, const std::array< std::size_t, N+1 > &intrsc_intmd_dims, const std::array< TensorShapeRef, N+1 > &intmd_sizes, const std::array< TensorShapeRef, N+1 > &base_sizes, std::string var_name="", std::array< std::string, N > arg_names={})
std::string name() const
Get the derivative name.
const std::string & var_name() const
Get the variable name.
const Tensor & tensor() const
Get the derivative.
TensorShapeRef intmd_sizes() const
Derivative & operator=(Derivative< N > &&val) noexcept=default
TensorShape base_sizes() const
Size var_base_dim() const
Derivative()=default
TensorShapeRef var_base_sizes() const
const std::string & arg_name(std::size_t i) const
Get the i-th argument name.
bool defined() const
Whether this derivative contains defined value.
Size arg_base_dim(std::size_t i) const
Derivative & operator+=(const Tensor &val)
TensorShapeRef arg_base_sizes(std::size_t i) const
Size intrsc_intmd_dim() const
Derivative & operator=(const Derivative< N > &val)=default
TensorShapeRef var_intrsc_intmd_sizes() const
Derivative< N > reinterpret(std::size_t additional_intrsc_intmd_dim) const
Reinterpret the derivative to add additional intrinsic intermediate dimensions to the variable and ar...
Derivative(Derivative< N > &&val)=default
Size arg_intrsc_intmd_dim(std::size_t i) const
TensorShapeRef var_intmd_sizes() const
~Derivative()=default
Size intmd_dim() const
TensorShapeRef intrsc_intmd_sizes() const
void clear()
Clear the derivative.
Size base_dim() const
Size var_intrsc_intmd_dim() const
Derivative(const Derivative< N > &val)=default
bool is_intrsc_intmd_broadcast() const
Whether this derivative represents a broadcast over intrinsic intermediate dimensions.
TensorShapeRef extrsc_intmd_sizes() const
TensorShapeRef arg_intrsc_intmd_sizes(std::size_t i) const
Tensor fullify() const
Definition Tensor.h:49
Definition DiagnosticsInterface.h:31
c10::SmallVector< Size, 8 > TensorShape
Definition types.h:72
std::string derivative_name(const std::string &var_name, const std::array< std::string, N > &arg_names)
Pretty print derivative names.
Definition Derivative.h:39
std::string name(ElasticConstant p)
Tensor fullify(const Tensor &t, Size iid, std::array< TensorShape, N > iiss)
int64_t Size
Definition types.h:71
c10::ArrayRef< Size > TensorShapeRef
Definition types.h:73