NEML2 2.0.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/tensors/Tensor.h"
28
29namespace neml2
30{
31// Forward declaration
32class VariableBase;
33
35template <std::size_t N>
36class Derivative
37{
38public:
39 Derivative() = default;
40 Derivative(const std::array<const VariableBase *, N + 1> & var_and_args, ArrayRef<Size> dep_dims);
41
44 Derivative & operator=(const Tensor & val);
45 Derivative & operator=(const VariableBase & val);
47
49 bool operator==(const Derivative<N> & other) const;
50
52 const Tensor & tensor() const { return _deriv; }
53
55 const Tensor & get() const;
56
58 void set(const Tensor & val);
59
61 const VariableBase * var() const { return _var_and_args[0]; }
62
64 std::array<const VariableBase *, N> args() const;
65
66private:
67 std::array<TensorShapeRef, N + 1> get_intmd_sizes() const;
68 std::array<TensorShapeRef, N + 1> get_base_sizes() const;
69
70 TensorShape total_intmd_sizes() const;
71 TensorShape total_base_sizes() const;
72
74 Tensor broadcast_intmd_dims(const Tensor & val) const;
75
77 const std::array<const VariableBase *, N + 1> _var_and_args = {};
78
80 TensorShape _dep_dims = {};
81
83 Tensor _deriv;
84
86 mutable Tensor _deriv_assembly;
87
89 const std::string _debug_name = "<anonymous>";
90};
91} // namespace neml2
Derivative wrapper.
Definition VariableBase.h:38
const Tensor & tensor() const
Get the derivative.
Definition Derivative.h:52
std::array< const VariableBase *, N > args() const
Get the args.
Definition Derivative.cxx:76
Derivative()=default
bool operator==(const Derivative< N > &other) const
Equality operator (for searching in a container)
Definition Derivative.cxx:324
void set(const Tensor &val)
Set the derivative (given in assembly format)
Definition Derivative.cxx:348
Derivative & operator=(const Tensor &val)
Definition Derivative.cxx:122
const Tensor & get() const
Get the derivative in assembly format.
Definition Derivative.cxx:335
const VariableBase * var() const
Get the variable.
Definition Derivative.h:61
Definition Tensor.h:47
Base class of variable.
Definition VariableBase.h:53
Definition DiagnosticsInterface.cxx:30
c10::SmallVector< Size, 8 > TensorShape
Definition types.h:66
c10::ArrayRef< T > ArrayRef
Definition types.h:59