NEML2 2.0.0
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
Variable.h
1// Copyright 2024, 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 <memory>
28
29#include "neml2/models/map_types_fwd.h"
30#include "neml2/base/LabeledAxisAccessor.h"
31#include "neml2/misc/types.h"
32
33namespace neml2
34{
35// Forward declarations
36class Model;
37class Derivative;
38enum class TensorType : int8_t;
39template <typename, typename>
41struct TraceableSize;
43
52{
53public:
54 VariableBase() = default;
55
56 VariableBase(const VariableBase &) = delete;
58 VariableBase & operator=(const VariableBase &) = delete;
60 virtual ~VariableBase() = default;
61
62 VariableBase(VariableName name_in, Model * owner, TensorShapeRef list_shape);
63
65 const VariableName & name() const { return _name; }
66
69 const Model & owner() const;
70 Model & owner();
72
74 virtual TensorType type() const = 0;
75
78 bool is_state() const;
79 bool is_old_state() const;
80 bool is_force() const;
81 bool is_old_force() const;
82 bool is_residual() const;
83 bool is_parameter() const;
84 bool is_solve_dependent() const;
86 // Note that the check depends on whether we are currently solving nonlinear system
87 bool is_dependent() const;
89
91 // These methods mirror TensorBase
94 TensorOptions options() const;
96 Dtype scalar_type() const;
98 Device device() const;
100 Size dim() const;
102 TensorShapeRef sizes() const;
104 Size size(Size dim) const;
106 bool batched() const;
108 Size batch_dim() const;
110 Size list_dim() const;
112 Size base_dim() const;
118 virtual TensorShapeRef base_sizes() const = 0;
122 Size base_size(Size dim) const;
124 Size list_size(Size dim) const;
126 Size base_storage() const;
128 Size assembly_storage() const;
130
132 virtual std::unique_ptr<VariableBase> clone(const VariableName & name = {},
133 Model * owner = nullptr) const = 0;
134
136 virtual void ref(const VariableBase & other, bool ref_is_mutable = false) = 0;
137
139 virtual const VariableBase * ref() const = 0;
140
142 virtual bool owning() const = 0;
143
145 virtual void zero(const TensorOptions & options) = 0;
146
148 virtual void set(const Tensor & val) = 0;
149
152 virtual void set(const ATensor & val, bool force = false) = 0;
153
155 virtual Tensor get() const = 0;
156
158 virtual Tensor tensor() const = 0;
159
161 bool requires_grad() const;
162
164 virtual void requires_grad_(bool req = true) = 0;
165
167 virtual void operator=(const Tensor & val) = 0;
168
170 Derivative d(const VariableBase & var);
171
173 Derivative d(const VariableBase & var1, const VariableBase & var2);
174
177 void request_AD(const VariableBase & u);
178 void request_AD(const std::vector<const VariableBase *> & us);
180
183 void request_AD(const VariableBase & u1, const VariableBase & u2);
184 void request_AD(const std::vector<const VariableBase *> & u1s,
185 const std::vector<const VariableBase *> & u2s);
187
189 const ValueMap & derivatives() const { return _derivs; }
190 ValueMap & derivatives() { return _derivs; }
191
193 const DerivMap & second_derivatives() const { return _sec_derivs; }
194 DerivMap & second_derivatives() { return _sec_derivs; }
195
197 virtual void clear();
198
201
204
206 const VariableName _name = {};
207
209 Model * const _owner = nullptr;
210
211private:
212 ValueMap total_derivatives(const DependencyResolver<Model, VariableName> & dep,
213 Model * model,
214 const VariableName & yvar) const;
215
216 DerivMap total_second_derivatives(const DependencyResolver<Model, VariableName> & dep,
217 Model * model,
218 const VariableName & yvar) const;
219
221 const TensorShape _list_sizes = {};
222
224 ValueMap _derivs;
225
227 DerivMap _sec_derivs;
228};
229
234template <typename T>
235class Variable : public VariableBase
236{
237public:
238 template <typename T2 = T, typename = typename std::enable_if_t<!std::is_same_v<Tensor, T2>>>
240 : VariableBase(std::move(name_in), owner, list_shape),
241 _base_sizes(T::const_base_sizes),
242 _ref(nullptr),
243 _ref_is_mutable(false)
244 {
245 }
246
247 template <typename T2 = T, typename = typename std::enable_if_t<std::is_same_v<Tensor, T2>>>
249 Model * owner,
250 TensorShapeRef list_shape,
251 TensorShapeRef base_shape)
252 : VariableBase(std::move(name_in), owner, list_shape),
253 _base_sizes(base_shape),
254 _ref(nullptr),
255 _ref_is_mutable(false)
256 {
257 }
258
259 TensorType type() const override;
260
261 TensorShapeRef base_sizes() const override { return _base_sizes; }
262
263 std::unique_ptr<VariableBase> clone(const VariableName & name = {},
264 Model * owner = nullptr) const override;
265
266 void ref(const VariableBase & var, bool ref_is_mutable = false) override;
267
268 const VariableBase * ref() const override { return _ref ? _ref->ref() : this; }
269
270 bool owning() const override { return !_ref; }
271
272 void zero(const TensorOptions & options) override;
273
274 void set(const Tensor & val) override;
275
276 void set(const ATensor & val, bool force = false) override;
277
278 Tensor get() const override;
279
280 Tensor tensor() const override;
281
282 void requires_grad_(bool req = true) override;
283
284 void operator=(const Tensor & val) override;
285
287 const T & value() const { return owning() ? _value : _ref->value(); }
288
290 T operator-() const { return -value(); }
291
293 operator T() const { return value(); }
294
295 void clear() override;
296
297protected:
300
303
306
309};
310
312{
313public:
315 : _base_sizes({}),
316 _deriv(nullptr)
317 {
318 }
319
320 Derivative(TensorShapeRef base_sizes, Tensor * deriv)
321 : _base_sizes(base_sizes),
322 _deriv(deriv)
323 {
324 }
325
326 Derivative & operator=(const Tensor & val);
327
328 template <typename T>
330 {
331 return operator=(var.value());
332 }
333
334private:
336 const TensorShape _base_sizes;
337
339 Tensor * const _deriv;
340};
341
342// Everything below is just for convenience: We just forward operations to the the variable values
343// so that we can do
344//
345// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
346// var4 = (var1 - var2) * var3
347// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
348//
349// instead of the (ugly?) expression below
350//
351// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
352// var4 = (var1.v - var2.v) * var3.v
353// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
354#define FWD_VARIABLE_BINARY_OP(op) \
355 template <typename T1, \
356 typename T2, \
357 typename = typename std::enable_if_t<std::is_base_of_v<VariableBase, T1> || \
358 std::is_base_of_v<VariableBase, T2>>> \
359 auto op(const T1 & a, const T2 & b) \
360 { \
361 if constexpr (std::is_base_of_v<VariableBase, T1> && std::is_base_of_v<VariableBase, T2>) \
362 return op(a.value(), b.value()); \
363 \
364 if constexpr (std::is_base_of_v<VariableBase, T1> && !std::is_base_of_v<VariableBase, T2>) \
365 return op(a.value(), b); \
366 \
367 if constexpr (!std::is_base_of_v<VariableBase, T1> && std::is_base_of_v<VariableBase, T2>) \
368 return op(a, b.value()); \
369 } \
370 static_assert(true)
371FWD_VARIABLE_BINARY_OP(operator+);
372FWD_VARIABLE_BINARY_OP(operator-);
373FWD_VARIABLE_BINARY_OP(operator*);
374FWD_VARIABLE_BINARY_OP(operator/);
375}
The DependencyResolver identifies and resolves the dependencies among a set of objects derived from D...
Definition DependencyResolver.h:46
Definition Variable.h:312
Derivative()
Definition Variable.h:314
Derivative & operator=(const Tensor &val)
Definition Variable.cxx:588
Derivative & operator=(const Variable< T > &var)
Definition Variable.h:329
Derivative(TensorShapeRef base_sizes, Tensor *deriv)
Definition Variable.h:320
The base class for all constitutive models.
Definition Model.h:97
Definition Tensor.h:46
virtual void set(const ATensor &val, bool force=false)=0
VariableBase(VariableBase &&)=delete
Device device() const
Device.
Definition Variable.cxx:119
TraceableTensorShape batch_sizes() const
Return the batch shape.
Definition Variable.cxx:167
virtual ~VariableBase()=default
bool is_old_force() const
Definition Variable.cxx:77
Model *const _owner
The model which declared this variable.
Definition Variable.h:209
bool requires_grad() const
Check if this variable is part of the AD function graph.
Definition Variable.cxx:209
bool is_parameter() const
Definition Variable.cxx:89
bool is_state() const
Definition Variable.cxx:59
const Model & owner() const
Definition Variable.cxx:45
void apply_second_order_chain_rule(const DependencyResolver< Model, VariableName > &)
Apply second order chain rule.
Definition Variable.cxx:296
Derivative d(const VariableBase &var)
Wrapper for assigning partial derivative.
Definition Variable.cxx:215
virtual const VariableBase * ref() const =0
Get the referencing variable (returns this if this is a storing variable)
Dtype scalar_type() const
Scalar type.
Definition Variable.cxx:113
Size base_storage() const
Base storage of the variable.
Definition Variable.cxx:197
bool batched() const
Whether the tensor is batched.
Definition Variable.cxx:143
Size batch_dim() const
Return the number of batch dimensions.
Definition Variable.cxx:149
void request_AD(const VariableBase &u)
Definition Variable.cxx:242
Size dim() const
Number of tensor dimensions.
Definition Variable.cxx:125
VariableBase()=default
TensorShapeRef sizes() const
Tensor shape.
Definition Variable.cxx:131
Size base_size(Size dim) const
Return the size of a base axis.
Definition Variable.cxx:185
TensorShapeRef list_sizes() const
Return the list shape.
Definition Variable.cxx:173
DerivMap & second_derivatives()
Definition Variable.h:194
Size list_dim() const
Return the number of list dimensions.
Definition Variable.cxx:155
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.
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:95
virtual void zero(const TensorOptions &options)=0
Set the variable value to zero.
VariableBase(const VariableBase &)=delete
bool is_residual() const
Definition Variable.cxx:83
const ValueMap & derivatives() const
Partial derivatives.
Definition Variable.h:189
ValueMap & derivatives()
Definition Variable.h:190
TraceableSize batch_size(Size dim) const
Return the size of a batch axis.
Definition Variable.cxx:179
VariableBase & operator=(VariableBase &&)=delete
const VariableName & name() const
Name of this variable.
Definition Variable.h:65
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:101
VariableBase & operator=(const VariableBase &)=delete
const VariableName _name
Name of the variable.
Definition Variable.h:206
virtual TensorShapeRef base_sizes() const =0
Return the base shape.
const DerivMap & second_derivatives() const
Partial second derivatives.
Definition Variable.h:193
Size size(Size dim) const
Size of a dimension.
Definition Variable.cxx:137
Size list_size(Size dim) const
Return the size of a list axis.
Definition Variable.cxx:191
virtual bool owning() const =0
Check if this is an owning variable.
virtual void clear()
Clear the variable value and derivatives.
Definition Variable.cxx:277
Size base_dim() const
Return the number of base dimensions.
Definition Variable.cxx:161
bool is_force() const
Definition Variable.cxx:71
virtual TensorType type() const =0
Variable tensor type.
void apply_chain_rule(const DependencyResolver< Model, VariableName > &)
Apply first order chain rule.
Definition Variable.cxx:285
virtual Tensor tensor() const =0
Get the variable value.
Size assembly_storage() const
Assembly storage of the variable.
Definition Variable.cxx:203
TensorOptions options() const
Definition Variable.cxx:107
bool is_old_state() const
Definition Variable.cxx:65
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:236
Tensor get() const override
Get the variable value (with flattened base dimensions, i.e., for assembly purposes)
Definition Variable.cxx:509
const Variable< T > * _ref
The variable referenced by this (nullptr if this is a storing variable)
Definition Variable.h:302
Tensor tensor() const override
Get the variable value.
Definition Variable.cxx:516
void requires_grad_(bool req=true) override
Mark this variable as a leaf variable in tracing function graph for AD.
Definition Variable.cxx:530
Variable(VariableName name_in, Model *owner, TensorShapeRef list_shape, TensorShapeRef base_shape)
Definition Variable.h:248
bool _ref_is_mutable
Whether mutating the referenced variable is allowed.
Definition Variable.h:305
void operator=(const Tensor &val) override
Assignment operator.
Definition Variable.cxx:541
const VariableBase * ref() const override
Get the referencing variable (returns this if this is a storing variable)
Definition Variable.h:268
void zero(const TensorOptions &options) override
Set the variable value to zero.
Definition Variable.cxx:435
T operator-() const
Negation.
Definition Variable.h:290
const TensorShape _base_sizes
Base shape of the variable.
Definition Variable.h:299
void set(const Tensor &val) override
Set the variable value.
Definition Variable.cxx:461
const T & value() const
Variable value.
Definition Variable.h:287
TensorType type() const override
Variable tensor type.
Definition Variable.cxx:384
bool owning() const override
Check if this is an owning variable.
Definition Variable.h:270
std::unique_ptr< VariableBase > clone(const VariableName &name={}, Model *owner=nullptr) const override
Clone this variable.
Definition Variable.cxx:391
T _value
Variable value (undefined if this is a referencing variable)
Definition Variable.h:308
Variable(VariableName name_in, Model *owner, TensorShapeRef list_shape)
Definition Variable.h:239
TensorShapeRef base_sizes() const override
Return the base shape.
Definition Variable.h:261
void clear() override
Clear the variable value and derivatives.
Definition Variable.cxx:562
Definition DiagnosticsInterface.cxx:30
c10::Device Device
Definition types.h:66
c10::SmallVector< Size, 8 > TensorShape
Definition types.h:71
std::map< LabeledAxisAccessor, Tensor > ValueMap
Definition map_types_fwd.h:33
at::Tensor ATensor
Definition types.h:42
std::map< LabeledAxisAccessor, ValueMap > DerivMap
Definition map_types_fwd.h:34
std::string name(ElasticConstant p)
Definition ElasticityConverter.cxx:30
int64_t Size
Definition types.h:69
TensorType
Definition tensors.h:61
LabeledAxisAccessor VariableName
Definition LabeledAxisAccessor.h:185
c10::TensorOptions TensorOptions
Definition types.h:63
c10::ArrayRef< Size > TensorShapeRef
Definition types.h:72
c10::ScalarType Dtype
Definition types.h:64
Traceable size.
Definition TraceableSize.h:40
Traceable tensor shape.
Definition TraceableTensorShape.h:38