NEML2 1.4.0
Loading...
Searching...
No Matches
Model Class Referenceabstract

The base class for all constitutive models. More...

Detailed Description

The base class for all constitutive models.

A model maps some input to output. The forward operator (and its derivative) is defined in the method set_value. All concrete models must provide the implementation of the forward operator by overriding the set_value method.

#include <Model.h>

Inheritance diagram for Model:

Public Member Functions

 Model (const OptionSet &options)
 Construct a new Model object.
 
virtual void diagnose (std::vector< Diagnosis > &) const override
 Check for common problems.
 
void diagnose_nl_sys (std::vector< Diagnosis > &diagnoses) const
 Additional diagnostics for a nonlinear system.
 
virtual bool is_nonlinear_system () const
 Whether this model defines one or more nonlinear equations to be solved.
 
std::tuple< const Tensor &, const Tensor &, const Tensor &, const Tensor &, const Tensor & > get_system_matrices () const
 Get assembled system.
 
virtual void reinit (const Tensor &tensor, int deriv_order)
 Allocate storage and setup views for all the variables of this model and recursively all of the sub-models. See the other overload for detailed description.
 
virtual void reinit (TensorShapeRef batch_shape={}, int deriv_order=0, const torch::Device &device=default_device(), const torch::Dtype &dtype=default_dtype())
 Allocate storage and setup views for all the variables of this model and recursively all of the sub-models.
 
const std::vector< Model * > & registered_models () const
 The models that may be used during the evaluation of this model.
 
Modelregistered_model (const std::string &name) const
 Get a registered model by its name.
 
virtual std::set< VariableNameconsumed_items () const override
 The variables that this model depends on.
 
virtual std::set< VariableNameprovided_items () const override
 The variables that this model defines as part of its output.
 
virtual bool is_AD_enabled () const override
 Whether AD is enabled.
 
bool using_AD_1st_derivative () const
 Whether this model is using AD to get 1st derivatives.
 
bool using_AD_2nd_derivative () const
 Whether this model is using AD to get 2nd derivatives.
 
void use_AD_derivatives (bool first=true, bool second=true)
 Tell this model to use AD to get derivatives.
 
void prepare ()
 Prepare for evaluation.
 
virtual void value ()
 Evalute the model.
 
virtual void value_and_dvalue ()
 Evalute the model and compute its derivative.
 
virtual void dvalue ()
 Evalute the derivative.
 
virtual void value_and_dvalue_and_d2value ()
 Evalute the model and compute its first and second derivatives.
 
virtual void d2value ()
 Evalute the second derivatives.
 
virtual void dvalue_and_d2value ()
 Evalute the first and second derivatives.
 
virtual LabeledVector value (const LabeledVector &in)
 Convenient shortcut to construct and return the model value.
 
virtual std::tuple< LabeledVector, LabeledMatrixvalue_and_dvalue (const LabeledVector &in)
 Convenient shortcut to construct and return the model value and its derivative.
 
virtual LabeledMatrix dvalue (const LabeledVector &in)
 Convenient shortcut to construct and return the derivative.
 
virtual std::tuple< LabeledVector, LabeledMatrix, LabeledTensor3Dvalue_and_dvalue_and_d2value (const LabeledVector &in)
 Convenient shortcut to construct and return the model's value, first and second derivative.
 
virtual LabeledTensor3D d2value (const LabeledVector &in)
 Convenient shortcut to construct and return the model's second derivative.
 
virtual std::tuple< LabeledMatrix, LabeledTensor3Ddvalue_and_d2value (const LabeledVector &in)
 Convenient shortcut to construct and return the model's first and second derivative.
 
Model options

Storage batch dimension

Size batch_dim () const
 
TensorShapeRef batch_sizes () const
 Storage batch shape.
 
const torch::TensorOptions & options () const
 Storage tensor options.
 
torch::Dtype scalar_type () const
 Storage scalar type.
 
torch::Device device () const
 Storage device.
 
- Public Member Functions inherited from Data
 Data (const OptionSet &options)
 Construct a new Data object.
 
const std::vector< Data * > & registered_data () const
 All the registered data objects.
 
- Public Member Functions inherited from NEML2Object
 NEML2Object (const OptionSet &options)
 Construct a new NEML2Object object.
 
virtual ~NEML2Object ()=default
 
const OptionSetinput_options () const
 
const std::string & name () const
 A readonly reference to the object's name.
 
const std::string & type () const
 A readonly reference to the object's type.
 
const std::string & path () const
 A readonly reference to the object's path.
 
const std::string & doc () const
 A readonly reference to the object's docstring.
 
template<typename T = NEML2Object>
const T * host () const
 Get a readonly pointer to the host.
 
template<typename T = NEML2Object>
T * host ()
 Get a writable pointer to the host.
 
- Public Member Functions inherited from BufferStore
 BufferStore (const OptionSet &options, NEML2Object *object)
 
const Storage< std::string, TensorValueBase > & named_buffers () const
 
Storage< std::string, TensorValueBase > & named_buffers ()
 
template<typename T , typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
TensorValue< T > & get_buffer (const std::string &name)
 }@
 
- Public Member Functions inherited from ParameterStore
 ParameterStore (const OptionSet &options, NEML2Object *object)
 
const Storage< std::string, TensorValueBase > & named_parameters () const
 
Storage< std::string, TensorValueBase > & named_parameters ()
 
void set_parameter (const std::string &, const Tensor &)
 }@
 
void set_parameters (const std::map< std::string, Tensor > &)
 Set values for parameters.
 
TensorValueBaseget_parameter (const std::string &name)
 Get a writable reference of a parameter.
 
const TensorValueBaseget_parameter (const std::string &name) const
 Get a read-only reference of a parameter.
 
bool has_nl_param () const
 Whether this parameter store has any nonlinear parameter.
 
const VariableBasenl_param (const std::string &) const
 Query the existence of a nonlinear parameter.
 
virtual std::map< std::string, const VariableBase * > named_nonlinear_parameters (bool recursive=false) const
 Get all nonlinear parameters.
 
virtual std::map< std::string, Model * > named_nonlinear_parameter_models (bool recursive=false) const
 Get all nonlinear parameters' models.
 
- Public Member Functions inherited from VariableStore
 VariableStore (const OptionSet &options, Model *object)
 
LabeledAxisdeclare_axis (const std::string &name)
 
virtual void setup_layout ()
 Setup the layouts of all the registered axes.
 
VariableBaseinput_variable (const VariableName &)
 Get the view of an input variable.
 
VariableBaseoutput_variable (const VariableName &)
 Get the view of an output variable.
 
TensorType input_type (const VariableName &) const
 Get the variable type of an input variable.
 
TensorType output_type (const VariableName &) const
 Get the variable type of an output variable.
 
template<typename T = Tensor>
Variable< T > & get_input_variable (const VariableName &name)
 
template<typename T = Tensor>
const Variable< T > & get_input_variable (const VariableName &name) const
 
template<typename T = Tensor>
const Variable< T > & get_output_variable (const VariableName &name)
 
template<typename T = Tensor>
const Variable< T > & get_output_variable (const VariableName &name) const
 
LabeledAxisinput_axis ()
 
const LabeledAxisinput_axis () const
 
LabeledAxisoutput_axis ()
 
const LabeledAxisoutput_axis () const
 
Storage< VariableName, VariableBase > & input_variables ()
 
const Storage< VariableName, VariableBase > & input_variables () const
 
Storage< VariableName, VariableBase > & output_variables ()
 
const Storage< VariableName, VariableBase > & output_variables () const
 
LabeledVectorinput_storage ()
 
const LabeledVectorinput_storage () const
 
LabeledVectoroutput_storage ()
 
const LabeledVectoroutput_storage () const
 
LabeledMatrixderivative_storage ()
 
const LabeledMatrixderivative_storage () const
 
LabeledTensor3Dsecond_derivative_storage ()
 
const LabeledTensor3Dsecond_derivative_storage () const
 
- Public Member Functions inherited from NonlinearSystem
 NonlinearSystem (const OptionSet &options)
 
virtual void init_scaling (const bool verbose=false)
 Compute algebraic Jacobian-based automatic scaling following https://cs.stanford.edu/people/paulliu/files/cs517-project.pdf.
 
Tensor scale_residual (const Tensor &r) const
 Apply scaling to the residual.
 
Tensor scale_Jacobian (const Tensor &J) const
 Apply scaling to the Jacobian.
 
Tensor scale_direction (const Tensor &p) const
 Remove scaling from the search direction, i.e. \( J^{-1} r \).
 
virtual Tensor solution () const
 Get the solution vector.
 
Tensor residual (const Tensor &x)
 Convenient shortcut to set the current solution, assemble and return the system residual.
 
void residual ()
 Convenient shortcut to assemble and return the system residual.
 
Tensor Jacobian (const Tensor &x)
 Convenient shortcut to set the current solution, assemble and return the system Jacobian.
 
void Jacobian ()
 Convenient shortcut to assemble and return the system Jacobian.
 
std::tuple< Tensor, Tensorresidual_and_Jacobian (const Tensor &x)
 Convenient shortcut to set the current solution, assemble and return the system residual and Jacobian.
 
void residual_and_Jacobian ()
 Convenient shortcut to assemble and return the system residual and Jacobian.
 
const Tensorget_residual () const
 
const Tensorget_Jacobian () const
 
Tensor residual_norm () const
 The residual norm.
 
virtual bool is_AD_disabled () const final
 Whether AD is disabled.
 
- Public Member Functions inherited from DiagnosticsInterface
 DiagnosticsInterface (NEML2Object *object)
 
template<typename... Args>
void diagnostic_assert (std::vector< Diagnosis > &diagnoses, bool assertion, Args &&... args) const
 
void diagnostic_assert_state (std::vector< Diagnosis > &diagnoses, const VariableBase &v) const
 
void diagnostic_assert_old_state (std::vector< Diagnosis > &diagnoses, const VariableBase &v) const
 
void diagnostic_assert_force (std::vector< Diagnosis > &diagnoses, const VariableBase &v) const
 
void diagnostic_assert_old_force (std::vector< Diagnosis > &diagnoses, const VariableBase &v) const
 
void diagnostic_assert_residual (std::vector< Diagnosis > &diagnoses, const VariableBase &v) const
 
void diagnostic_check_input_variable (std::vector< Diagnosis > &diagnoses, const VariableBase &v) const
 
void diagnostic_check_output_variable (std::vector< Diagnosis > &diagnoses, const VariableBase &v) const
 

Static Public Member Functions

static OptionSet expected_options ()
 
- Static Public Member Functions inherited from Data
static OptionSet expected_options ()
 
- Static Public Member Functions inherited from NEML2Object
static OptionSet expected_options ()
 
- Static Public Member Functions inherited from NonlinearSystem
static OptionSet expected_options ()
 
static void disable_automatic_scaling (OptionSet &options)
 
static void enable_automatic_scaling (OptionSet &options)
 

Protected Member Functions

virtual void setup () override
 Setup this model.
 
virtual void check_AD_limitation () const
 
void check_inplace_dbg ()
 Check for potential in-place operation.
 
virtual void check_input (const LabeledVector &in) const
 Check if the input has valid shape.
 
virtual LabeledVector get_output ()
 
virtual LabeledMatrix get_doutput_dinput ()
 
virtual LabeledTensor3D get_d2output_dinput2 ()
 
template<typename T , typename = typename std::enable_if_t<std::is_base_of_v<Model, T>>>
T & register_model (const std::string &name, int extra_deriv_order=0, bool nonlinear=false, bool merge_input=true)
 Register a model that the current model may use during its evaluation.
 
Model reinitialization and setup
virtual void reinit (bool in, bool out)
 Allocate storage and setup views for all the variables of this model and recursively all of the sub-models. See the other overload for detailed description.
 
virtual void cache (TensorShapeRef batch_shape, int deriv_order, const torch::Device &device, const torch::Dtype &dtype)
 
bool requires_grad () const
 Whether derivative has been requested for this model.
 
bool requires_2nd_grad () const
 Whether 2nd derivative has been requested for this model.
 
virtual void allocate_variables (bool in, bool out)
 Call VariableStore::allocate_variables recursively on all submodels.
 
virtual void setup_input_views (VariableStore *host=nullptr) override
 Call VariableStore::setup_input_views recursively on all submodels.
 
virtual void setup_submodel_input_views (VariableStore *host)
 
virtual void setup_output_views ()
 
virtual void setup_submodel_output_views ()
 
virtual void setup_nonlinear_system ()
 
virtual void cache (TensorShapeRef batch_shape)
 Cache the variable's batch shape.
 
virtual void allocate_variables (TensorShapeRef batch_shape, const torch::TensorOptions &options, bool in, bool out, bool dout_din, bool d2out_din2)
 Allocate variable storages given the batch shape and tensor options.
 
virtual void setup_output_views (bool out, bool dout_din, bool d2out_din2)
 Call VariableStore::setup_output_views recursively on all submodels.
 
Model (pre-)evaluation

Call VariableStore::zero recursively on all submodels

virtual void zero ()
 
virtual void set_input (const LabeledVector &in)
 Set in to be the input of this model.
 
virtual void set_solution (const Tensor &x) override
 Set x as the current solution of the nonlinear system.
 
virtual void set_value (bool out, bool dout_din, bool d2out_din2)=0
 The map between input -> output, and optionally its derivatives.
 
virtual void assemble (bool residual, bool Jacobian) override
 Compute the residual and Jacobian.
 
virtual void zero (bool dout_din, bool d2out_din2)
 Zero out derivative and second derivative storage.
 
- Protected Member Functions inherited from Data
template<typename T , typename = typename std::enable_if_t<std::is_base_of_v<Data, T>>>
T & register_data (const std::string &name)
 
- Protected Member Functions inherited from BufferStore
virtual void send_buffers_to (const torch::TensorOptions &options)
 Send all buffers to options.
 
template<typename T , typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
const T & declare_buffer (const std::string &name, const T &rawval)
 Declare a buffer.
 
template<typename T , typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
const T & declare_buffer (const std::string &name, const std::string &input_option_name)
 Declare a buffer.
 
- Protected Member Functions inherited from ParameterStore
virtual void send_parameters_to (const torch::TensorOptions &options)
 Send parameters to options.
 
template<typename T , typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
const T & declare_parameter (const std::string &name, const T &rawval)
 Declare a parameter.
 
template<typename T , typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
const T & declare_parameter (const std::string &name, const std::string &input_option_name, bool allow_nonlinear=false)
 Declare a parameter.
 
- Protected Member Functions inherited from VariableStore
template<typename T , typename... S>
const Variable< T > & declare_input_variable (S &&... name)
 Declare an input variable.
 
template<typename... S>
const Variable< Tensor > & declare_input_variable (Size sz, TensorType t, S &&... name)
 Declare an input variable (with unknown base shape at compile time)
 
template<typename T , typename... S>
const Variable< Tensor > & declare_input_variable_list (Size list_size, S &&... name)
 Declare an input variable that is a list of tensors of fixed size.
 
template<typename T , typename... S>
Variable< T > & declare_output_variable (S &&... name)
 Declare an output variable.
 
template<typename... S>
Variable< Tensor > & declare_output_variable (Size sz, TensorType t, S &&... name)
 Declare an input variable (with unknown base shape at compile time)
 
template<typename T , typename... S>
Variable< Tensor > & declare_output_variable_list (Size list_size, S &&... name)
 Declare an output variable that is a list of tensors of fixed size.
 
template<typename T >
VariableName declare_variable (LabeledAxis &axis, const VariableName &var) const
 Declare an item recursively on an axis.
 
VariableName declare_variable (LabeledAxis &axis, const VariableName &var, Size sz) const
 Declare an item (with known storage size) recursively on an axis.
 
VariableName declare_subaxis (LabeledAxis &axis, const VariableName &subaxis) const
 Declare a subaxis recursively on an axis.
 

Protected Attributes

std::vector< Model * > _registered_models
 Models this model may use during its evaluation.
 
- Protected Attributes inherited from Data
std::vector< Data * > _registered_data
 Registered Data objects.
 
- Protected Attributes inherited from ParameterStore
std::map< std::string, const VariableBase * > _nl_params
 Map from nonlinear parameter names to their corresponding variable views.
 
std::map< std::string, Model * > _nl_param_models
 Map from nonlinear parameter names to models which evaluate them.
 
- Protected Attributes inherited from NonlinearSystem
Size _ndof
 Number of degrees of freedom.
 
Tensor _solution
 View for the solution of this nonlinear system.
 
Tensor _residual
 View for the residual of this nonlinear system.
 
Tensor _Jacobian
 View for the Jacobian of this nonlinear system.
 
Tensor _scaled_residual
 
Tensor _scaled_Jacobian
 
const bool _autoscale
 If true, do automatic scaling.
 
const Real _autoscale_tol
 Tolerance for convergence check of the iterative automatic scaling algorithm.
 
const unsigned int _autoscale_miter
 Maximum number of iterations allowed for the iterative automatic scaling algorithm.
 
bool _scaling_matrices_initialized
 Flag to indicate whether scaling matrices have been computed.
 
Tensor _row_scaling
 Row scaling "matrix" – since it's a batched diagonal matrix, we are only storing its diagonals.
 
Tensor _col_scaling
 Column scaling "matrix" – since it's a batched diagonal matrix, we are only storing its diagonals.
 

Friends

class ParameterStore
 Declaration of nonlinear parameters may require manipulation of input.
 
class ComposedModel
 ComposedModel's set_value need to call submodel's set_value.
 

Constructor & Destructor Documentation

◆ Model()

Model ( const OptionSet & options)

Construct a new Model object.

Parameters
optionsThe options extracted from the input file

Member Function Documentation

◆ allocate_variables() [1/2]

void allocate_variables ( bool in,
bool out )
protectedvirtual

Call VariableStore::allocate_variables recursively on all submodels.

Reimplemented in ComposedModel.

◆ allocate_variables() [2/2]

void allocate_variables ( TensorShapeRef batch_shape,
const torch::TensorOptions & options,
bool in,
bool out,
bool dout_din,
bool d2out_din2 )
protectedvirtual

Allocate variable storages given the batch shape and tensor options.

Parameters
batch_shapeBatch shape of the allocated tensors
optionsTensor options of the allocated tensors
inWhether to allocate tensor storage for input
outWhether to allocate tensor storage for output
dout_dinWhether to allocate tensor storage for the first derivatives
d2out_din2Whether to allocate tensor storage for the second derivatives

Reimplemented from VariableStore.

◆ assemble()

void assemble ( bool residual,
bool Jacobian )
overrideprotectedvirtual

Compute the residual and Jacobian.

Parameters
residualWhether residual is requested
JacobianWhether Jacobian is requested

Implements NonlinearSystem.

◆ batch_dim()

Size batch_dim ( ) const
inline

◆ batch_sizes()

TensorShapeRef batch_sizes ( ) const
inline

Storage batch shape.

◆ cache() [1/2]

void cache ( TensorShapeRef batch_shape)
protectedvirtual

Cache the variable's batch shape.

Reimplemented from VariableStore.

◆ cache() [2/2]

void cache ( TensorShapeRef batch_shape,
int deriv_order,
const torch::Device & device,
const torch::Dtype & dtype )
protectedvirtual

◆ check_AD_limitation()

void check_AD_limitation ( ) const
protectedvirtual

Validate the currently requested AD settings.

true true okay, just slow
true false error, this is a weird case
false true okay
false false great, everything handcoded
The wrapper (decorator) for cross-referencing unresolved values at parse time.
Definition CrossRef.h:56

Reimplemented in ComposedModel, and ImplicitUpdate.

◆ check_inplace_dbg()

void check_inplace_dbg ( )
protected

Check for potential in-place operation.

◆ check_input()

void check_input ( const LabeledVector & in) const
protectedvirtual

Check if the input has valid shape.

◆ consumed_items()

std::set< VariableName > consumed_items ( ) const
overridevirtual

The variables that this model depends on.

Implements DependencyDefinition< VariableName >.

◆ d2value() [1/2]

void d2value ( )
virtual

Evalute the second derivatives.

◆ d2value() [2/2]

LabeledTensor3D d2value ( const LabeledVector & in)
virtual

Convenient shortcut to construct and return the model's second derivative.

◆ device()

torch::Device device ( ) const
inline

Storage device.

◆ diagnose()

void diagnose ( std::vector< Diagnosis > & diagnoses) const
overridevirtual

Check for common problems.

This method serves as the entry point for diagnosing common problems in object setup. The idea behind this method is that while some errors could be detected at construction time, i.e., when the object's constructor is called, it doesn't hinder other objects' creation. We therefore would like to defer the detection of errors until after all objects have been created, collect all errors at once, and present the user with a complete understanding of all errors encountered.

Note, however, if an error could interfere with other objects' creation, it should be raised right away inside the constructor, instead of inside this method.

Parameters
diagnosesA vector of exceptions of type Diagnosis for each of the detected problem.

Implements DiagnosticsInterface.

Reimplemented in BackwardEulerTimeIntegration< T >, ForwardEulerTimeIntegration< T >, ImplicitUpdate, VariableRate< T >, WR2ExplicitExponentialTimeIntegration, and WR2ImplicitExponentialTimeIntegration.

◆ diagnose_nl_sys()

void diagnose_nl_sys ( std::vector< Diagnosis > & diagnoses) const

Additional diagnostics for a nonlinear system.

◆ dvalue() [1/2]

void dvalue ( )
virtual

Evalute the derivative.

◆ dvalue() [2/2]

LabeledMatrix dvalue ( const LabeledVector & in)
virtual

Convenient shortcut to construct and return the derivative.

◆ dvalue_and_d2value() [1/2]

void dvalue_and_d2value ( )
virtual

Evalute the first and second derivatives.

◆ dvalue_and_d2value() [2/2]

std::tuple< LabeledMatrix, LabeledTensor3D > dvalue_and_d2value ( const LabeledVector & in)
virtual

Convenient shortcut to construct and return the model's first and second derivative.

◆ expected_options()

OptionSet expected_options ( )
static

◆ get_d2output_dinput2()

LabeledTensor3D get_d2output_dinput2 ( )
protectedvirtual
Returns
the second derivative of the output w.r.t. the input of this model

◆ get_doutput_dinput()

LabeledMatrix get_doutput_dinput ( )
protectedvirtual
Returns
the derivative of the output w.r.t. the input of this model

◆ get_output()

LabeledVector get_output ( )
protectedvirtual
Returns
the output of this model

◆ get_system_matrices()

std::tuple< const Tensor &, const Tensor &, const Tensor &, const Tensor &, const Tensor & > get_system_matrices ( ) const

Get assembled system.

◆ is_AD_enabled()

virtual bool is_AD_enabled ( ) const
inlineoverridevirtual

Whether AD is enabled.

Reimplemented from NonlinearSystem.

◆ is_nonlinear_system()

virtual bool is_nonlinear_system ( ) const
inlinevirtual

Whether this model defines one or more nonlinear equations to be solved.

◆ options()

const torch::TensorOptions & options ( ) const
inline

Storage tensor options.

◆ prepare()

void prepare ( )

Prepare for evaluation.

◆ provided_items()

std::set< VariableName > provided_items ( ) const
overridevirtual

The variables that this model defines as part of its output.

Implements DependencyDefinition< VariableName >.

◆ register_model()

template<typename T , typename = typename std::enable_if_t<std::is_base_of_v<Model, T>>>
T & register_model ( const std::string & name,
int extra_deriv_order = 0,
bool nonlinear = false,
bool merge_input = true )
inlineprotected

Register a model that the current model may use during its evaluation.

If merge_input is set to true, this model will also consume the consumed variables of model, which will affect dependency resolution inside a ComposedModel.

Parameters
nameThe model to register
extra_deriv_orderThe additional derivative order required for the registered-submodel
nonlinearSet to true if the registered model defines a nonlinear system to be solved
merge_inputWhether to merge the input of the registered model into this model's input.

◆ registered_model()

Model * registered_model ( const std::string & name) const

Get a registered model by its name.

◆ registered_models()

const std::vector< Model * > & registered_models ( ) const
inline

The models that may be used during the evaluation of this model.

◆ reinit() [1/3]

void reinit ( bool in,
bool out )
protectedvirtual

Allocate storage and setup views for all the variables of this model and recursively all of the sub-models. See the other overload for detailed description.

◆ reinit() [2/3]

void reinit ( const Tensor & tensor,
int deriv_order )
virtual

Allocate storage and setup views for all the variables of this model and recursively all of the sub-models. See the other overload for detailed description.

◆ reinit() [3/3]

void reinit ( TensorShapeRef batch_shape = {},
int deriv_order = 0,
const torch::Device & device = default_device(),
const torch::Dtype & dtype = default_dtype() )
virtual

Allocate storage and setup views for all the variables of this model and recursively all of the sub-models.

This method must be called before any call to the forward operators, e.g., value, dvalue, value_and_dvalue, etc.

IMPORTANT: If the batch shape of this model changes, this method must be called again to re-allocate the storage and views.

Parameters
batch_shapeBatch shape of the input, output and derivatives
deriv_orderOrder of derivative required for this model
deviceDevice on which the model will be evaluated
dtypeNumber type, e.g., torch::kFloat32, torch::kFloat64, etc

◆ requires_2nd_grad()

bool requires_2nd_grad ( ) const
inlineprotected

Whether 2nd derivative has been requested for this model.

◆ requires_grad()

bool requires_grad ( ) const
inlineprotected

Whether derivative has been requested for this model.

◆ scalar_type()

torch::Dtype scalar_type ( ) const
inline

Storage scalar type.

◆ set_input()

void set_input ( const LabeledVector & in)
protectedvirtual

Set in to be the input of this model.

◆ set_solution()

void set_solution ( const Tensor & x)
overrideprotectedvirtual

Set x as the current solution of the nonlinear system.

Reimplemented from NonlinearSystem.

◆ set_value()

virtual void set_value ( bool out,
bool dout_din,
bool d2out_din2 )
protectedpure virtual

◆ setup()

void setup ( )
overrideprotectedvirtual

Setup this model.

  1. Setup the layout of the input and output axes.
  2. Setup the arguments of each variable.

Reimplemented from NEML2Object.

Reimplemented in ComposedModel.

◆ setup_input_views()

void setup_input_views ( VariableStore * host = nullptr)
overrideprotectedvirtual

Call VariableStore::setup_input_views recursively on all submodels.

Reimplemented from VariableStore.

◆ setup_nonlinear_system()

void setup_nonlinear_system ( )
protectedvirtual

◆ setup_output_views() [1/2]

void setup_output_views ( )
protectedvirtual

Reimplemented in ComposedModel, ImplicitUpdate, and Normality.

◆ setup_output_views() [2/2]

void setup_output_views ( bool out,
bool dout_din,
bool d2out_din2 )
protectedvirtual

Call VariableStore::setup_output_views recursively on all submodels.

Reimplemented from VariableStore.

◆ setup_submodel_input_views()

void setup_submodel_input_views ( VariableStore * host)
protectedvirtual

Reimplemented in ComposedModel.

◆ setup_submodel_output_views()

void setup_submodel_output_views ( )
protectedvirtual

◆ use_AD_derivatives()

void use_AD_derivatives ( bool first = true,
bool second = true )

Tell this model to use AD to get derivatives.

◆ using_AD_1st_derivative()

bool using_AD_1st_derivative ( ) const
inline

Whether this model is using AD to get 1st derivatives.

◆ using_AD_2nd_derivative()

bool using_AD_2nd_derivative ( ) const
inline

Whether this model is using AD to get 2nd derivatives.

◆ value() [1/2]

void value ( )
virtual

Evalute the model.

◆ value() [2/2]

LabeledVector value ( const LabeledVector & in)
virtual

Convenient shortcut to construct and return the model value.

◆ value_and_dvalue() [1/2]

void value_and_dvalue ( )
virtual

Evalute the model and compute its derivative.

◆ value_and_dvalue() [2/2]

std::tuple< LabeledVector, LabeledMatrix > value_and_dvalue ( const LabeledVector & in)
virtual

Convenient shortcut to construct and return the model value and its derivative.

◆ value_and_dvalue_and_d2value() [1/2]

void value_and_dvalue_and_d2value ( )
virtual

Evalute the model and compute its first and second derivatives.

◆ value_and_dvalue_and_d2value() [2/2]

std::tuple< LabeledVector, LabeledMatrix, LabeledTensor3D > value_and_dvalue_and_d2value ( const LabeledVector & in)
virtual

Convenient shortcut to construct and return the model's value, first and second derivative.

◆ zero() [1/2]

void zero ( )
protectedvirtual

◆ zero() [2/2]

void zero ( bool dout_din,
bool d2out_din2 )
protectedvirtual

Zero out derivative and second derivative storage.

Reimplemented from VariableStore.

Friends And Related Symbol Documentation

◆ ComposedModel

ComposedModel's set_value need to call submodel's set_value.

◆ ParameterStore

Declaration of nonlinear parameters may require manipulation of input.

Member Data Documentation

◆ _registered_models

std::vector<Model *> _registered_models
protected

Models this model may use during its evaluation.