|
NEML2 2.1.0
|
Base class of variable. More...
Base class of variable.
Specific implementations are defined by the derived class Variable<T> where we rely on polymorphism so that we can store different types of variables in the same container.
#include <VariableBase.h>

Public Types | |
| using | DerivTuple = std::tuple<Derivative<1>, const VariableBase *> |
| using | DerivContainer = std::vector<DerivTuple> |
| using | SecDerivTuple = std::tuple<Derivative<2>, const VariableBase *, const VariableBase *> |
| using | SecDerivContainer = std::vector<SecDerivTuple> |
Public Member Functions | |
| VariableBase ()=default | |
| VariableBase (const VariableBase &)=delete | |
| VariableBase (VariableBase &&)=delete | |
| VariableBase & | operator= (const VariableBase &)=delete |
| VariableBase & | operator= (VariableBase &&)=delete |
| virtual | ~VariableBase () |
| VariableBase (VariableName name_in, Model *owner, TensorShapeRef base_shape) | |
| The canonical constructor. | |
| const VariableName & | name () const |
| Name of this variable. | |
| virtual TensorType | type () const =0 |
| Variable tensor type. | |
| virtual std::unique_ptr< VariableBase > | clone (const VariableName &name={}, Model *owner=nullptr) const =0 |
| Clone this variable. | |
| virtual void | ref (VariableBase &other)=0 |
| Reference another variable. | |
| virtual const VariableBase * | ref () const =0 |
| Get the referencing variable (returns this if this is a storing variable). | |
| virtual VariableBase * | ref ()=0 |
| virtual const VariableBase * | direct_ref () const =0 |
| Get the direct referencing variable (returns nullptr if this is a storing variable). | |
| virtual VariableBase * | direct_ref ()=0 |
| virtual bool | owning () const =0 |
| Check if this is an owning variable. | |
| bool | is_mutable () const |
| Whether this variable is mutable when it is referenced by another variable. | |
| void | set_mutable (bool m) |
| Allow/disable mutation of this variable when it is referenced by another variable. | |
| Tensor | zeros (const TensorOptions &options) const |
| Make zeros tensor with the shape of this variable. | |
| virtual void | zero (const TensorOptions &options)=0 |
| Set the variable value to zero. | |
| virtual Tensor | tensor () const =0 |
| Get the variable value cast to Tensor. | |
| bool | requires_grad () const |
| Check if this variable is part of the AD function graph. | |
| virtual void | requires_grad_ (bool req=true)=0 |
| Mark this variable as a leaf variable in tracing function graph for AD. | |
| virtual void | assign (const Tensor &val, std::optional< TracerPrivilege > key=std::nullopt)=0 |
| Assignment operator (with TracerPrivilege). | |
| virtual void | operator= (const Tensor &val)=0 |
| Assignment operator. | |
| bool | has_derivative (const VariableName &vname) const |
| Whether the variable has non-zero derivative with respect to another variable. | |
| bool | has_derivative (const VariableName &v1name, const VariableName &v2name) const |
| Whether the variable has non-zero second derivative with respect to another variable. | |
| Derivative< 1 > & | d (const VariableBase &arg, std::size_t deriv_intrsc_intmd_dim=0, std::size_t var_intrsc_intmd_dim=0, std::size_t arg_intrsc_intmd_dim=0) |
| Wrapper for assigning partial derivative. | |
| const Derivative< 1 > & | d (const VariableBase &arg) const |
| Derivative< 2 > & | d2 (const VariableBase &arg1, const VariableBase &arg2, std::size_t deriv_intrsc_intmd_dim=0, std::size_t var_intrsc_intmd_dim=0, std::size_t arg1_intrsc_intmd_dim=0, std::size_t arg2_intrsc_intmd_dim=0) |
| Wrapper for assigning second partial derivative. | |
| const Derivative< 2 > & | d2 (const VariableBase &arg1, const VariableBase &arg2) const |
| const DerivContainer & | derivatives () const |
| Partial derivatives. | |
| DerivContainer & | derivatives () |
| const SecDerivContainer & | second_derivatives () const |
| Partial second derivatives. | |
| SecDerivContainer & | second_derivatives () |
| virtual void | clear () |
| Clear the variable value and derivatives. | |
| void | clear_derivatives () |
| Clear only the derivatives. | |
| const Model & | owner () const |
| Model & | owner () |
Subaxis | |
| bool | is_state () const |
| bool | is_old_state () const |
| bool | is_force () const |
| bool | is_old_force () const |
| bool | is_residual () const |
| bool | is_parameter () const |
| bool | is_solve_dependent () const |
| bool | is_dependent () const |
| Check if the derivative with respect to this variable should be evaluated. | |
Tensor information | |
| virtual bool | defined () const =0 |
| virtual TensorOptions | options () const =0 |
| Tensor options. | |
| virtual Dtype | scalar_type () const =0 |
| Scalar type. | |
| virtual Device | device () const =0 |
| Device. | |
| Size | dim () const |
| Size | batch_dim () const |
| Size | base_dim () const |
| Size | dynamic_dim () const |
| Size | static_dim () const |
| Size | intmd_dim () const |
| TensorShapeRef | sizes () const |
| TraceableTensorShape | batch_sizes () const |
| TensorShapeRef | base_sizes () const |
| virtual const TraceableTensorShape & | dynamic_sizes () const =0 |
| TensorShapeRef | static_sizes () const |
| TensorShapeRef | intmd_sizes () const |
| Size | size (Size i) const |
| TraceableSize | batch_size (Size i) const |
| Size | base_size (Size i) const |
| const TraceableSize & | dynamic_size (Size i) const |
| Size | static_size (Size i) const |
| Size | intmd_size (Size i) const |
| void | request_AD (const VariableBase &u) |
| void | request_AD (const std::vector< const VariableBase * > &us) |
| void | request_AD (const VariableBase &u1, const VariableBase &u2) |
| void | request_AD (const std::vector< const VariableBase * > &u1s, const std::vector< const VariableBase * > &u2s) |
| bool | is_leaf (const DependencyResolver< Model, VariableName > &) const |
| const VariableBase & | provider (const DependencyResolver< Model, VariableName > &) const |
| Get the provider in the dependency graph. | |
| const DerivContainer & | total_derivatives (const DependencyResolver< Model, VariableName > &) const |
| Get total derivatives with respect to leaf variables. | |
| const SecDerivContainer & | total_second_derivatives (const DependencyResolver< Model, VariableName > &) const |
| Get total second derivatives with respect to leaf variables. | |
| void | clear_chain_rule_cache (const DependencyResolver< Model, VariableName > &) const |
| Clear chain rule cache. | |
Protected Attributes | |
| const VariableName | _name = {} |
| Name of the variable. | |
| Model *const | _owner = nullptr |
| The model which declared this variable. | |
| TensorShape | _cached_intmd_sizes = {} |
| const TensorShape | _base_sizes = {} |
| Base shape of the variable. | |
| bool | _mutable = false |
| When referenced by another variable, whether to allow the referencing variable to mutate my value. | |
| using DerivContainer = std::vector<DerivTuple> |
| using DerivTuple = std::tuple<Derivative<1>, const VariableBase *> |
| using SecDerivContainer = std::vector<SecDerivTuple> |
| using SecDerivTuple = std::tuple<Derivative<2>, const VariableBase *, const VariableBase *> |
|
default |
|
delete |
|
delete |
|
virtual |
| VariableBase | ( | VariableName | name_in, |
| Model * | owner, | ||
| TensorShapeRef | base_shape ) |
|
pure virtual |
Assignment operator (with TracerPrivilege).
Implemented in Variable< T >, Variable< neml2::R2 >, Variable< neml2::Rot >, Variable< neml2::Scalar >, Variable< neml2::SR2 >, Variable< neml2::SSR4 >, and Variable< neml2::WR2 >.
| Size base_dim | ( | ) | const |
| TensorShapeRef base_sizes | ( | ) | const |
| Size batch_dim | ( | ) | const |
| TraceableSize batch_size | ( | Size | i | ) | const |
| TraceableTensorShape batch_sizes | ( | ) | const |
|
virtual |
Clear the variable value and derivatives.
Reimplemented in Variable< T >, Variable< neml2::R2 >, Variable< neml2::Rot >, Variable< neml2::Scalar >, Variable< neml2::SR2 >, Variable< neml2::SSR4 >, and Variable< neml2::WR2 >.
| void clear_chain_rule_cache | ( | const DependencyResolver< Model, VariableName > & | ) | const |
Clear chain rule cache.
| void clear_derivatives | ( | ) |
Clear only the derivatives.
|
pure virtual |
Clone this variable.
Implemented in Variable< T >, Variable< neml2::R2 >, Variable< neml2::Rot >, Variable< neml2::Scalar >, Variable< neml2::SR2 >, Variable< neml2::SSR4 >, and Variable< neml2::WR2 >.
| const Derivative< 1 > & d | ( | const VariableBase & | arg | ) | const |
| Derivative< 1 > & d | ( | const VariableBase & | arg, |
| std::size_t | deriv_intrsc_intmd_dim = 0, | ||
| std::size_t | var_intrsc_intmd_dim = 0, | ||
| std::size_t | arg_intrsc_intmd_dim = 0 ) |
Wrapper for assigning partial derivative.
| const Derivative< 2 > & d2 | ( | const VariableBase & | arg1, |
| const VariableBase & | arg2 ) const |
| Derivative< 2 > & d2 | ( | const VariableBase & | arg1, |
| const VariableBase & | arg2, | ||
| std::size_t | deriv_intrsc_intmd_dim = 0, | ||
| std::size_t | var_intrsc_intmd_dim = 0, | ||
| std::size_t | arg1_intrsc_intmd_dim = 0, | ||
| std::size_t | arg2_intrsc_intmd_dim = 0 ) |
Wrapper for assigning second partial derivative.
|
pure virtual |
Defined
Implemented in Variable< T >, Variable< neml2::R2 >, Variable< neml2::Rot >, Variable< neml2::Scalar >, Variable< neml2::SR2 >, Variable< neml2::SSR4 >, and Variable< neml2::WR2 >.
|
inline |
|
inline |
Partial derivatives.
|
pure virtual |
| Size dim | ( | ) | const |
|
pure virtual |
Get the direct referencing variable (returns nullptr if this is a storing variable).
Implemented in Variable< T >, Variable< neml2::R2 >, Variable< neml2::Rot >, Variable< neml2::Scalar >, Variable< neml2::SR2 >, Variable< neml2::SSR4 >, and Variable< neml2::WR2 >.
|
pure virtual |
Implemented in Variable< T >.
| Size dynamic_dim | ( | ) | const |
| const TraceableSize & dynamic_size | ( | Size | i | ) | const |
|
pure virtual |
| bool has_derivative | ( | const VariableName & | v1name, |
| const VariableName & | v2name ) const |
Whether the variable has non-zero second derivative with respect to another variable.
| bool has_derivative | ( | const VariableName & | vname | ) | const |
Whether the variable has non-zero derivative with respect to another variable.
| Size intmd_dim | ( | ) | const |
| TensorShapeRef intmd_sizes | ( | ) | const |
| bool is_dependent | ( | ) | const |
Check if the derivative with respect to this variable should be evaluated.
| bool is_force | ( | ) | const |
| bool is_leaf | ( | const DependencyResolver< Model, VariableName > & | ) | const |
Whether this variable is a leaf variable in the dependency graph
| bool is_mutable | ( | ) | const |
Whether this variable is mutable when it is referenced by another variable.
| bool is_old_force | ( | ) | const |
| bool is_old_state | ( | ) | const |
| bool is_parameter | ( | ) | const |
| bool is_residual | ( | ) | const |
| bool is_solve_dependent | ( | ) | const |
| bool is_state | ( | ) | const |
|
inline |
Name of this variable.
|
pure virtual |
Assignment operator.
Implemented in Variable< T >, Variable< neml2::R2 >, Variable< neml2::Rot >, Variable< neml2::Scalar >, Variable< neml2::SR2 >, Variable< neml2::SSR4 >, and Variable< neml2::WR2 >.
|
delete |
|
delete |
|
pure virtual |
Tensor options.
Implemented in Variable< T >, Variable< neml2::R2 >, Variable< neml2::Rot >, Variable< neml2::Scalar >, Variable< neml2::SR2 >, Variable< neml2::SSR4 >, and Variable< neml2::WR2 >.
| Model & owner | ( | ) |
|
pure virtual |
Check if this is an owning variable.
Implemented in Variable< T >, Variable< neml2::R2 >, Variable< neml2::Rot >, Variable< neml2::Scalar >, Variable< neml2::SR2 >, Variable< neml2::SSR4 >, and Variable< neml2::WR2 >.
| const VariableBase & provider | ( | const DependencyResolver< Model, VariableName > & | ) | const |
Get the provider in the dependency graph.
|
pure virtual |
Get the referencing variable (returns this if this is a storing variable).
Implemented in Variable< T >.
|
pure virtual |
Implemented in Variable< T >.
|
pure virtual |
Reference another variable.
Implemented in Variable< T >, Variable< neml2::R2 >, Variable< neml2::Rot >, Variable< neml2::Scalar >, Variable< neml2::SR2 >, Variable< neml2::SSR4 >, and Variable< neml2::WR2 >.
| void request_AD | ( | const std::vector< const VariableBase * > & | u1s, |
| const std::vector< const VariableBase * > & | u2s ) |
| void request_AD | ( | const std::vector< const VariableBase * > & | us | ) |
| void request_AD | ( | const VariableBase & | u | ) |
Request to use AD to calculate the derivative of this variable with respect to another variable
| void request_AD | ( | const VariableBase & | u1, |
| const VariableBase & | u2 ) |
Request to use AD to calculate the second derivative of this variable with respect to two other variables
| bool requires_grad | ( | ) | const |
Check if this variable is part of the AD function graph.
|
pure virtual |
Mark this variable as a leaf variable in tracing function graph for AD.
Implemented in Variable< T >, Variable< neml2::R2 >, Variable< neml2::Rot >, Variable< neml2::Scalar >, Variable< neml2::SR2 >, Variable< neml2::SSR4 >, and Variable< neml2::WR2 >.
|
pure virtual |
Scalar type.
Implemented in Variable< T >, Variable< neml2::R2 >, Variable< neml2::Rot >, Variable< neml2::Scalar >, Variable< neml2::SR2 >, Variable< neml2::SSR4 >, and Variable< neml2::WR2 >.
|
inline |
|
inline |
Partial second derivatives.
| void set_mutable | ( | bool | m | ) |
Allow/disable mutation of this variable when it is referenced by another variable.
| TensorShapeRef sizes | ( | ) | const |
| Size static_dim | ( | ) | const |
| TensorShapeRef static_sizes | ( | ) | const |
|
pure virtual |
Get the variable value cast to Tensor.
Implemented in Variable< T >, Variable< neml2::R2 >, Variable< neml2::Rot >, Variable< neml2::Scalar >, Variable< neml2::SR2 >, Variable< neml2::SSR4 >, and Variable< neml2::WR2 >.
| const DerivContainer & total_derivatives | ( | const DependencyResolver< Model, VariableName > & | ) | const |
Get total derivatives with respect to leaf variables.
| const SecDerivContainer & total_second_derivatives | ( | const DependencyResolver< Model, VariableName > & | ) | const |
Get total second derivatives with respect to leaf variables.
|
pure virtual |
Variable tensor type.
Implemented in Variable< T >, Variable< neml2::R2 >, Variable< neml2::Rot >, Variable< neml2::Scalar >, Variable< neml2::SR2 >, Variable< neml2::SSR4 >, and Variable< neml2::WR2 >.
|
pure virtual |
Set the variable value to zero.
Implemented in Variable< T >, Variable< neml2::R2 >, Variable< neml2::Rot >, Variable< neml2::Scalar >, Variable< neml2::SR2 >, Variable< neml2::SSR4 >, and Variable< neml2::WR2 >.
| Tensor zeros | ( | const TensorOptions & | options | ) | const |
Make zeros tensor with the shape of this variable.
|
protected |
Base shape of the variable.
|
protected |
Cached intermediate shape that this variable last saw
|
protected |
When referenced by another variable, whether to allow the referencing variable to mutate my value.
|
protected |
Name of the variable.
|
protected |
The model which declared this variable.