27#include "neml2/base/DependencyDefinition.h"
29#include "neml2/models/Data.h"
30#include "neml2/models/ParameterStore.h"
31#include "neml2/models/VariableStore.h"
32#include "neml2/solvers/NonlinearSystem.h"
34#include "neml2/tensors/LabeledVector.h"
35#include "neml2/tensors/LabeledMatrix.h"
36#include "neml2/tensors/LabeledTensor3D.h"
47class Model :
public std::enable_shared_from_this<Model>,
72 virtual std::vector<Diagnosis>
preflight()
const;
94 const torch::Device &
device = torch::kCPU,
95 const torch::Dtype &
dtype = NEML2_DTYPE);
116 const torch::TensorOptions &
options()
const {
return _options; }
125 virtual const std::set<VariableName>
consumed_items()
const override;
128 virtual const std::set<VariableName>
provided_items()
const override;
173 virtual std::tuple<LabeledVector, LabeledMatrix, LabeledTensor3D>
176 virtual void value();
202 virtual void setup()
override;
237 virtual void cache(
const torch::TensorOptions &
options);
251 template <
typename T,
typename =
typename std::enable_if_t<std::is_base_of_v<Model, T>>>
269 return *(std::dynamic_pointer_cast<T>(
model));
294 torch::TensorOptions _options;
315 const int _extra_deriv_order;
318 bool _nonlinear_system;
Definition BatchTensor.h:32
Definition ComposedModel.h:33
The wrapper (decorator) for cross-referencing unresolved values at parse time.
Definition CrossRef.h:52
Definition DependencyDefinition.h:40
A single-batched, logically 2D LabeledTensor.
Definition LabeledMatrix.h:38
A single-batched, logically 3D LabeledTensor.
Definition LabeledTensor3D.h:38
A single-batched, logically 1D LabeledTensor.
Definition LabeledVector.h:38
The base class for all constitutive models.
Definition Model.h:53
virtual void detach_and_zero(bool out, bool dout_din=true, bool d2out_din2=true) override
Call VariableStore::detach_and_zero recursively on all submodels.
Definition Model.cxx:258
virtual LabeledMatrix get_doutput_dinput()
Definition Model.cxx:336
virtual void setup_submodel_output_views()
Definition Model.cxx:217
virtual void assemble(bool residual, bool Jacobian) override
Compute the residual and Jacobian.
Definition Model.cxx:446
void input_requires_grad_(bool req=true)
Set requires_grad for the input variables.
Definition Model.cxx:302
void use_AD_derivatives(bool first=true, bool second=true)
Tell this model to use AD to get derivatives.
Definition Model.cxx:309
virtual LabeledVector get_output()
Definition Model.cxx:330
bool requires_grad() const
Whether derivative has been requested for this model.
Definition Model.h:104
virtual LabeledTensor3D get_d2output_dinput2()
Definition Model.cxx:342
TorchShapeRef batch_sizes() const
This model's batch shape.
Definition Model.h:113
virtual void value_and_dvalue_and_d2value()
Definition Model.cxx:397
const std::vector< Model * > & registered_models() const
The models that may be used during the evaluation of this model.
Definition Model.h:119
std::vector< Model * > _registered_models
Models this model may use during its evaluation.
Definition Model.h:275
bool _AD_2nd_deriv
Whether to use AD to compute 2nd derivatives.
Definition Model.h:281
virtual void cache(TorchShapeRef batch_shape) override
Cache the variable's batch shape.
Definition Model.cxx:278
bool using_AD_1st_derivative() const
Whether this model is using AD to get 1st derivatives.
Definition Model.h:146
virtual void setup_submodel_input_views()
Definition Model.cxx:198
virtual void allocate_variables(int deriv_order, bool options_changed)
Call VariableStore::allocate_variables recursively on all submodels.
Definition Model.cxx:159
bool requires_2nd_grad() const
Whether 2nd derivative has been requested for this model.
Definition Model.h:107
TorchSize batch_dim() const
This model's batch dim.
Definition Model.h:110
virtual void set_value(bool out, bool dout_din, bool d2out_din2)=0
The map between input -> output, and optionally its derivatives.
virtual void set_solution(const BatchTensor &x) override
Set x as the current solution of the nonlinear system.
Definition Model.cxx:268
virtual void reinit_output_views(bool out, bool dout_din=true, bool d2out_din2=true) override
Call VariableStore::reinit_output_views recursively on all submodels.
Definition Model.cxx:243
const torch::TensorOptions & options() const
This model's tensor options.
Definition Model.h:116
virtual void setup_input_views() override
Call VariableStore::setup_input_views recursively on all submodels.
Definition Model.cxx:191
virtual void reinit(TorchShapeRef batch_shape, int deriv_order=0, const torch::Device &device=torch::kCPU, const torch::Dtype &dtype=NEML2_DTYPE)
Allocate storage and setup views for all the variables of this model and recursively all of the sub-m...
Definition Model.cxx:123
bool _AD_1st_deriv
Whether to use AD to compute 1st derivatives.
Definition Model.h:278
virtual bool is_nonlinear_system() const
Whether this model defines one or more nonlinear equations to be solved.
Definition Model.h:75
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.
Definition Model.h:252
virtual void check_AD_limitation() const
Definition Model.cxx:295
virtual void set_input(const LabeledVector &in)
Set in to be the input of this model.
Definition Model.cxx:317
virtual void setup() override
Setup this model.
Definition Model.cxx:100
virtual void value()
Definition Model.cxx:375
virtual void reinit_input_views() override
Call VariableStore::reinit_input_views recursively on all submodels.
Definition Model.cxx:231
Stage
Definition Model.h:188
@ SOLVING
Definition Model.h:188
@ UPDATING
Definition Model.h:188
static OptionSet expected_options()
Definition Model.cxx:33
virtual void value_and_dvalue()
Definition Model.cxx:381
virtual void setup_output_views() override
Call VariableStore::setup_output_views recursively on all submodels.
Definition Model.cxx:210
virtual std::vector< Diagnosis > preflight() const
Check for common problems.
Definition Model.cxx:70
virtual const std::set< VariableName > consumed_items() const override
The variables that this model depends on.
Definition Model.cxx:434
bool using_AD_2nd_derivative() const
Whether this model is using AD to get 2nd derivatives.
Definition Model.h:149
virtual const std::set< VariableName > provided_items() const override
The variables that this model defines as part of its output.
Definition Model.cxx:440
static enum neml2::Model::Stage stage
Definition Model.cxx:30
Model(const OptionSet &options)
Construct a new Model object.
Definition Model.cxx:54
Model * registered_model(const std::string &name) const
Get a registered model by its name.
Definition Model.cxx:423
The base class of all "manufacturable" objects in the NEML2 library.
Definition NEML2Object.h:38
const std::string & name() const
A readonly reference to the object's name.
Definition NEML2Object.h:65
const T * host() const
Get a readonly pointer to the host.
Definition NEML2Object.h:90
Definition of a nonlinear system of equations.
Definition NonlinearSystem.h:37
void residual()
Convenient shortcut to assemble and return the system residual.
Definition NonlinearSystem.cxx:175
void Jacobian()
Convenient shortcut to assemble and return the system Jacobian.
Definition NonlinearSystem.cxx:192
A custom map-like data structure. The keys are strings, and the values can be nonhomogeneously typed.
Definition OptionSet.h:59
Interface for object which can store parameters.
Definition ParameterStore.h:38
Definition VariableStore.h:37
const Variable< T > & declare_input_variable(S &&... name)
Declare an input variable.
Definition VariableStore.h:180
virtual void allocate_variables(TorchShapeRef 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.
Definition VariableStore.cxx:78
virtual void cache(TorchShapeRef batch_shape)
Cache the variable's batch shape.
Definition VariableStore.cxx:69
Definition CrossRef.cxx:32
std::vector< TorchSize > TorchShape
Definition types.h:36
torch::IntArrayRef TorchShapeRef
Definition types.h:37