NEML2 1.4.0
Loading...
Searching...
No Matches
Model.h
1// Copyright 2023, 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/base/DependencyDefinition.h"
28
29#include "neml2/models/Data.h"
30#include "neml2/models/ParameterStore.h"
31#include "neml2/models/VariableStore.h"
32#include "neml2/solvers/NonlinearSystem.h"
33
34#include "neml2/tensors/LabeledVector.h"
35#include "neml2/tensors/LabeledMatrix.h"
36#include "neml2/tensors/LabeledTensor3D.h"
37
38namespace neml2
39{
47class Model : public std::enable_shared_from_this<Model>,
48 public Data,
49 public ParameterStore,
50 public VariableStore,
51 public NonlinearSystem,
52 public DependencyDefinition<VariableName>
53{
54public:
56
62 Model(const OptionSet & options);
63
72 virtual std::vector<Diagnosis> preflight() const;
73
75 virtual bool is_nonlinear_system() const { return _nonlinear_system; }
76
92 virtual void reinit(TorchShapeRef batch_shape,
93 int deriv_order = 0,
94 const torch::Device & device = torch::kCPU,
95 const torch::Dtype & dtype = NEML2_DTYPE);
96
101 virtual void reinit(const BatchTensor & tensor, int deriv_order);
102
104 bool requires_grad() const { return _deriv_order >= 1; }
105
107 bool requires_2nd_grad() const { return _deriv_order >= 2; }
108
110 TorchSize batch_dim() const { return _batch_sizes.size(); }
111
113 TorchShapeRef batch_sizes() const { return _batch_sizes; }
114
116 const torch::TensorOptions & options() const { return _options; }
117
119 const std::vector<Model *> & registered_models() const { return _registered_models; }
120
122 Model * registered_model(const std::string & name) const;
123
125 virtual const std::set<VariableName> consumed_items() const override;
126
128 virtual const std::set<VariableName> provided_items() const override;
129
140 virtual void check_AD_limitation() const;
141
143 void input_requires_grad_(bool req = true);
144
146 bool using_AD_1st_derivative() const { return _AD_1st_deriv; }
147
149 bool using_AD_2nd_derivative() const { return _AD_2nd_deriv; }
150
152 void use_AD_derivatives(bool first = true, bool second = true);
153
155 virtual void set_input(const LabeledVector & in);
156
158 virtual LabeledVector get_output();
159
162
165
167 virtual LabeledVector value(const LabeledVector & in);
168
170 virtual std::tuple<LabeledVector, LabeledMatrix> value_and_dvalue(const LabeledVector & in);
171
173 virtual std::tuple<LabeledVector, LabeledMatrix, LabeledTensor3D>
175
176 virtual void value();
177 virtual void value_and_dvalue();
178 virtual void value_and_dvalue_and_d2value();
179
188 static enum Stage { SOLVING, UPDATING } stage;
189
191 friend class ParameterStore;
192
194 friend class ComposedModel;
195
196protected:
202 virtual void setup() override;
203
205
207 virtual void allocate_variables(int deriv_order, bool options_changed);
208
210 virtual void setup_input_views() override;
211 virtual void setup_submodel_input_views();
212
214 virtual void setup_output_views() override;
215 virtual void setup_submodel_output_views();
216
218 virtual void reinit_input_views() override;
219
221 virtual void reinit_output_views(bool out, bool dout_din = true, bool d2out_din2 = true) override;
222
224 virtual void detach_and_zero(bool out, bool dout_din = true, bool d2out_din2 = true) override;
225
227 virtual void set_solution(const BatchTensor & x) override;
228
230 virtual void set_value(bool out, bool dout_din, bool d2out_din2) = 0;
231
233
234 virtual void cache(TorchShapeRef batch_shape) override;
235
237 virtual void cache(const torch::TensorOptions & options);
238
251 template <typename T, typename = typename std::enable_if_t<std::is_base_of_v<Model, T>>>
252 T & register_model(const std::string & name,
253 int extra_deriv_order = 0,
254 bool nonlinear = false,
255 bool merge_input = true)
256 {
258 extra_opts.set<NEML2Object *>("_host") = host();
259 extra_opts.set<int>("_extra_derivative_order") = extra_deriv_order;
260 extra_opts.set<bool>("_nonlinear_system") = nonlinear;
261
262 auto model = Factory::get_object_ptr<Model>("Models", name, extra_opts, /*force_create=*/true);
263
264 if (merge_input)
265 for (auto && [name, var] : model->input_views())
266 declare_input_variable(var.base_storage(), name);
267
268 _registered_models.push_back(model.get());
269 return *(std::dynamic_pointer_cast<T>(model));
270 }
271
272 virtual void assemble(bool residual, bool Jacobian) override;
273
275 std::vector<Model *> _registered_models;
276
279
282
283private:
285 void extract_derivatives(bool retain_graph, bool create_graph, bool allow_unused);
286
288 void extract_second_derivatives(bool retain_graph, bool create_graph, bool allow_unused);
289
291 TorchShape _batch_sizes;
292
294 torch::TensorOptions _options;
295
306 int _deriv_order;
307
315 const int _extra_deriv_order;
316
318 bool _nonlinear_system;
319};
320} // namespace neml2
Definition BatchTensor.h:32
Definition ComposedModel.h:33
The wrapper (decorator) for cross-referencing unresolved values at parse time.
Definition CrossRef.h:52
Definition Data.h:36
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:257
virtual LabeledMatrix get_doutput_dinput()
Definition Model.cxx:335
virtual void setup_submodel_output_views()
Definition Model.cxx:216
virtual void assemble(bool residual, bool Jacobian) override
Compute the residual and Jacobian.
Definition Model.cxx:445
void input_requires_grad_(bool req=true)
Set requires_grad for the input variables.
Definition Model.cxx:301
void use_AD_derivatives(bool first=true, bool second=true)
Tell this model to use AD to get derivatives.
Definition Model.cxx:308
virtual LabeledVector get_output()
Definition Model.cxx:329
bool requires_grad() const
Whether derivative has been requested for this model.
Definition Model.h:104
virtual LabeledTensor3D get_d2output_dinput2()
Definition Model.cxx:341
TorchShapeRef batch_sizes() const
This model's batch shape.
Definition Model.h:113
virtual void value_and_dvalue_and_d2value()
Definition Model.cxx:396
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:277
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:197
virtual void allocate_variables(int deriv_order, bool options_changed)
Call VariableStore::allocate_variables recursively on all submodels.
Definition Model.cxx:158
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:267
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:242
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:190
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:122
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:294
virtual void set_input(const LabeledVector &in)
Set in to be the input of this model.
Definition Model.cxx:316
virtual void setup() override
Setup this model.
Definition Model.cxx:99
virtual void value()
Definition Model.cxx:374
virtual void reinit_input_views() override
Call VariableStore::reinit_input_views recursively on all submodels.
Definition Model.cxx:230
Stage
Definition Model.h:188
@ SOLVING
Definition Model.h:188
@ UPDATING
Definition Model.h:188
static OptionSet expected_options()
Definition Model.cxx:32
virtual void value_and_dvalue()
Definition Model.cxx:380
virtual void setup_output_views() override
Call VariableStore::setup_output_views recursively on all submodels.
Definition Model.cxx:209
virtual std::vector< Diagnosis > preflight() const
Check for common problems.
Definition Model.cxx:69
virtual const std::set< VariableName > consumed_items() const override
The variables that this model depends on.
Definition Model.cxx:433
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:439
static enum neml2::Model::Stage stage
Definition Model.cxx:29
Model(const OptionSet &options)
Construct a new Model object.
Definition Model.cxx:53
Model * registered_model(const std::string &name) const
Get a registered model by its name.
Definition Model.cxx:422
The base class of all "manufacturable" objects in the NEML2 library.
Definition NEML2Object.h:39
const std::string & name() const
A readonly reference to the object's name.
Definition NEML2Object.h:66
const T * host() const
Get a readonly pointer to the host.
Definition NEML2Object.h:91
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:34
torch::IntArrayRef TorchShapeRef
Definition types.h:35