27#include "neml2/models/LabeledAxisAccessor.h"
28#include "neml2/base/DependencyResolver.h"
29#include "neml2/tensors/Tensor.h"
30#include "neml2/models/map_types.h"
172 void request_AD(
const std::vector<const VariableBase *> &
us);
178 void request_AD(
const std::vector<const VariableBase *> &
u1s,
179 const std::vector<const VariableBase *> &
u2s);
191 virtual void clear();
232 template <
typename T2 = T,
typename =
typename std::enable_if_t<!std::is_same_v<Tensor, T2>>>
241 template <
typename T2 = T,
typename =
typename std::enable_if_t<std::is_same_v<Tensor, T2>>>
258 Model *
owner =
nullptr)
const override;
260 void ref(
const VariableBase & var,
bool ref_is_mutable =
false)
override;
266 void zero(
const torch::TensorOptions &
options)
override;
285 operator T()
const {
return value(); }
288 template <
typename T2 = T,
typename =
typename std::enable_if_t<!std::is_same_v<T2, Tensor>>>
294 void clear()
override;
320 : _base_sizes(base_sizes),
347#define FWD_VARIABLE_BINARY_OP(op) \
348 template <typename T1, \
350 typename = typename std::enable_if_t<std::is_base_of_v<VariableBase, T1> || \
351 std::is_base_of_v<VariableBase, T2>>> \
352 auto op(const T1 & a, const T2 & b) \
354 if constexpr (std::is_base_of_v<VariableBase, T1> && std::is_base_of_v<VariableBase, T2>) \
355 return op(a.value(), b.value()); \
357 if constexpr (std::is_base_of_v<VariableBase, T1> && !std::is_base_of_v<VariableBase, T2>) \
358 return op(a.value(), b); \
360 if constexpr (!std::is_base_of_v<VariableBase, T1> && std::is_base_of_v<VariableBase, T2>) \
361 return op(a, b.value()); \
364FWD_VARIABLE_BINARY_OP(
operator+);
365FWD_VARIABLE_BINARY_OP(
operator-);
366FWD_VARIABLE_BINARY_OP(
operator*);
367FWD_VARIABLE_BINARY_OP(
operator/);
The wrapper (decorator) for cross-referencing unresolved values at parse time.
Definition CrossRef.h:54
Definition Variable.h:311
Derivative()
Definition Variable.h:313
void operator=(const Tensor &val)
Definition Variable.cxx:421
Derivative(TensorShapeRef base_sizes, Tensor *deriv)
Definition Variable.h:319
The accessor containing all the information needed to access an item in a LabeledAxis.
Definition LabeledAxisAccessor.h:58
The base class for all constitutive models.
Definition Model.h:51
neml2::Tensor base_flatten() const
Flatten base dimensions.
Definition TensorBaseImpl.h:413
Base class of variable.
Definition Variable.h:47
VariableBase(VariableBase &&)=delete
TraceableTensorShape batch_sizes() const
Return the batch shape.
Definition Variable.h:109
virtual ~VariableBase()=default
bool is_old_force() const
Definition Variable.cxx:71
Model *const _owner
The model which declared this variable.
Definition Variable.h:203
virtual void zero(const torch::TensorOptions &options)=0
Set the variable value to zero.
bool requires_grad() const
Check if this variable is part of the AD function graph.
Definition Variable.h:155
bool is_parameter() const
Definition Variable.cxx:83
bool is_state() const
Definition Variable.cxx:53
const Model & owner() const
Definition Variable.cxx:39
void apply_second_order_chain_rule(const DependencyResolver< Model, VariableName > &)
Apply second order chain rule.
Definition Variable.cxx:182
Derivative d(const VariableBase &var)
Wrapper for assigning partial derivative.
Definition Variable.cxx:101
virtual const VariableBase * ref() const =0
Get the referencing variable (returns this if this is a storing variable)
torch::Dtype scalar_type() const
Scalar type.
Definition Variable.h:91
Size base_storage() const
Base storage of the variable.
Definition Variable.h:121
bool batched() const
Whether the tensor is batched.
Definition Variable.h:101
Size batch_dim() const
Return the number of batch dimensions.
Definition Variable.h:103
void request_AD(const VariableBase &u)
Definition Variable.cxx:128
Size dim() const
Number of tensor dimensions.
Definition Variable.h:95
TensorShapeRef sizes() const
Tensor shape.
Definition Variable.h:97
Size base_size(Size dim) const
Return the size of a base axis.
Definition Variable.h:117
TensorShapeRef list_sizes() const
Return the list shape.
Definition Variable.h:111
DerivMap & second_derivatives()
Definition Variable.h:188
Size list_dim() const
Return the number of list dimensions.
Definition Variable.h:105
torch::Device device() const
Device.
Definition Variable.h:93
virtual Tensor get() const =0
Get the variable value (with flattened base dimensions, i.e., for assembly purposes)
virtual void ref(const VariableBase &other, bool ref_is_mutable=false)=0
Reference another variable.
torch::TensorOptions options() const
Definition Variable.h:89
virtual void set(const Tensor &val)=0
Set the variable value.
virtual std::unique_ptr< VariableBase > clone(const VariableName &name={}, Model *owner=nullptr) const =0
Clone this variable.
bool is_solve_dependent() const
Definition Variable.cxx:89
VariableBase(const VariableBase &)=delete
bool is_residual() const
Definition Variable.cxx:77
const ValueMap & derivatives() const
Partial derivatives.
Definition Variable.h:183
ValueMap & derivatives()
Definition Variable.h:184
TraceableSize batch_size(Size dim) const
Return the size of a batch axis.
Definition Variable.h:115
VariableBase & operator=(VariableBase &&)=delete
const VariableName & name() const
Name of this variable.
Definition Variable.h:60
virtual void operator=(const Tensor &val)=0
Assignment operator.
bool is_dependent() const
Check if the derivative with respect to this variable should be evaluated.
Definition Variable.cxx:95
VariableBase & operator=(const VariableBase &)=delete
const VariableName _name
Name of the variable.
Definition Variable.h:200
virtual TensorShapeRef base_sizes() const =0
Return the base shape.
const DerivMap & second_derivatives() const
Partial second derivatives.
Definition Variable.h:187
Size size(Size dim) const
Size of a dimension.
Definition Variable.h:99
Size list_size(Size dim) const
Return the size of a list axis.
Definition Variable.h:119
virtual bool owning() const =0
Check if this is an owning variable.
virtual void clear()
Clear the variable value and derivatives.
Definition Variable.cxx:163
Size base_dim() const
Return the number of base dimensions.
Definition Variable.h:107
bool is_force() const
Definition Variable.cxx:65
virtual TensorType type() const =0
Variable tensor type.
void apply_chain_rule(const DependencyResolver< Model, VariableName > &)
Apply first order chain rule.
Definition Variable.cxx:171
virtual Tensor tensor() const =0
Get the variable value.
Size assembly_storage() const
Assembly storage of the variable.
Definition Variable.h:123
bool is_old_state() const
Definition Variable.cxx:59
virtual void requires_grad_(bool req=true)=0
Mark this variable as a leaf variable in tracing function graph for AD.
Concrete definition of a variable.
Definition Variable.h:230
Tensor get() const override
Get the variable value (with flattened base dimensions, i.e., for assembly purposes)
Definition Variable.h:270
void zero(const torch::TensorOptions &options) override
Set the variable value to zero.
Definition Variable.cxx:321
const Variable< T > * _ref
The variable referenced by this (nullptr if this is a storing variable)
Definition Variable.h:301
Tensor tensor() const override
Get the variable value.
Definition Variable.cxx:359
void requires_grad_(bool req=true) override
Mark this variable as a leaf variable in tracing function graph for AD.
Definition Variable.cxx:373
Variable(VariableName name_in, Model *owner, TensorShapeRef list_shape, TensorShapeRef base_shape)
Definition Variable.h:242
bool _ref_is_mutable
Whether mutating the referenced variable is allowed.
Definition Variable.h:304
void operator=(const Tensor &val) override
Assignment operator.
Definition Variable.cxx:384
const VariableBase * ref() const override
Get the referencing variable (returns this if this is a storing variable)
Definition Variable.h:262
T operator-() const
Negation.
Definition Variable.h:282
const TensorShape _base_sizes
Base shape of the variable.
Definition Variable.h:298
void set(const Tensor &val) override
Set the variable value.
Definition Variable.cxx:342
const T & value() const
Variable value.
Definition Variable.h:279
TensorType type() const override
Variable tensor type.
Definition Variable.cxx:270
bool owning() const override
Check if this is an owning variable.
Definition Variable.h:264
std::unique_ptr< VariableBase > clone(const VariableName &name={}, Model *owner=nullptr) const override
Clone this variable.
Definition Variable.cxx:277
T _value
Variable value (undefined if this is a referencing variable)
Definition Variable.h:307
Variable(VariableName name_in, Model *owner, TensorShapeRef list_shape)
Definition Variable.h:233
TensorShapeRef base_sizes() const override
Return the base shape.
Definition Variable.h:255
void clear() override
Clear the variable value and derivatives.
Definition Variable.cxx:400
Size storage_size(TensorShapeRef shape)
The flattened storage size of a tensor with given shape.
Definition utils.cxx:55
Definition CrossRef.cxx:31
std::map< LabeledAxisAccessor, Tensor > ValueMap
Definition map_types.h:34
std::map< LabeledAxisAccessor, ValueMap > DerivMap
Definition map_types.h:35
torch::SmallVector< Size > TensorShape
Definition types.h:34
int64_t Size
Definition types.h:33
TensorType
Definition tensors.h:59
torch::IntArrayRef TensorShapeRef
Definition types.h:35
Traceable size.
Definition types.h:52
Traceable tensor shape.
Definition types.h:81