NEML2 1.4.0
Loading...
Searching...
No Matches
ParameterStore.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 "neml2/base/NEML2Object.h"
28#include "neml2/base/OptionSet.h"
29#include "neml2/base/Storage.h"
30#include "neml2/tensors/TensorValue.h"
31
32// The following are not directly used by ParameterStore itself.
33// We put them here so that derived classes can add expected options of these types.
34#include "neml2/base/CrossRef.h"
35#include "neml2/base/EnumSelection.h"
36
37namespace neml2
38{
39// Forward decl
40class VariableBase;
41class Model;
42
45{
46public:
47 ParameterStore(const OptionSet & options, NEML2Object * object);
48
52 {
53 return const_cast<ParameterStore *>(this)->named_parameters();
54 }
57
59 void set_parameter(const std::string &, const Tensor &);
60
62 void set_parameters(const std::map<std::string, Tensor> &);
63
65 TensorValueBase & get_parameter(const std::string & name);
66
68 const TensorValueBase & get_parameter(const std::string & name) const;
69
71 bool has_nl_param() const { return !_nl_params.empty(); }
72
79 const VariableBase * nl_param(const std::string &) const;
80
82 virtual std::map<std::string, const VariableBase *>
83 named_nonlinear_parameters(bool recursive = false) const;
84
86 virtual std::map<std::string, Model *>
88
89protected:
95 virtual void send_parameters_to(const torch::TensorOptions & options);
96
110 template <typename T, typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
111 const T & declare_parameter(const std::string & name, const T & rawval);
112
128 template <typename T, typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
129 const T & declare_parameter(const std::string & name,
130 const std::string & input_option_name,
131 bool allow_nonlinear = false);
132
134 std::map<std::string, const VariableBase *> _nl_params;
135
137 std::map<std::string, Model *> _nl_param_models;
138
139private:
140 NEML2Object * _object;
141
147 const OptionSet _options;
148
151};
152
153template <typename T, typename>
154const T &
155ParameterStore::declare_parameter(const std::string & name, const T & rawval)
156{
157 if (_object->host() != _object)
158 return _object->host<ParameterStore>()->declare_parameter(
159 _object->name() + parameter_name_separator() + name, rawval);
160
162
163 // If the parameter already exists, get it
164 if (_param_values.has_key(name))
165 base_ptr = &get_parameter(name);
166 // If the parameter doesn't exist, create it
167 else
168 {
169 auto val = std::make_unique<TensorValue<T>>(rawval);
170 base_ptr = _param_values.set_pointer(name, std::move(val));
171 }
172
173 auto ptr = dynamic_cast<TensorValue<T> *>(base_ptr);
174 neml_assert(ptr, "Internal error: Failed to cast parameter to a concrete type.");
175 return ptr->value();
176}
177
178} // namespace neml2
The wrapper (decorator) for cross-referencing unresolved values at parse time.
Definition CrossRef.h:56
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
A custom map-like data structure. The keys are strings, and the values can be nonhomogeneously typed.
Definition OptionSet.h:100
Interface for object which can store parameters.
Definition ParameterStore.h:45
std::map< std::string, const VariableBase * > _nl_params
Map from nonlinear parameter names to their corresponding variable views.
Definition ParameterStore.h:134
virtual void send_parameters_to(const torch::TensorOptions &options)
Send parameters to options.
Definition ParameterStore.cxx:39
void set_parameter(const std::string &, const Tensor &)
}@
Definition ParameterStore.cxx:48
ParameterStore(const OptionSet &options, NEML2Object *object)
Definition ParameterStore.cxx:32
std::map< std::string, Model * > _nl_param_models
Map from nonlinear parameter names to models which evaluate them.
Definition ParameterStore.h:137
void set_parameters(const std::map< std::string, Tensor > &)
Set values for parameters.
Definition ParameterStore.cxx:57
bool has_nl_param() const
Whether this parameter store has any nonlinear parameter.
Definition ParameterStore.h:71
TensorValueBase & get_parameter(const std::string &name)
Get a writable reference of a parameter.
Definition ParameterStore.cxx:64
const Storage< std::string, TensorValueBase > & named_parameters() const
Definition ParameterStore.h:51
const T & declare_parameter(const std::string &name, const T &rawval)
Declare a parameter.
Definition ParameterStore.h:155
virtual std::map< std::string, Model * > named_nonlinear_parameter_models(bool recursive=false) const
Get all nonlinear parameters' models.
Definition ParameterStore.cxx:117
virtual std::map< std::string, const VariableBase * > named_nonlinear_parameters(bool recursive=false) const
Get all nonlinear parameters.
Definition ParameterStore.cxx:94
const VariableBase * nl_param(const std::string &) const
Query the existence of a nonlinear parameter.
Definition ParameterStore.cxx:80
The base class to allow us to set up a polymorphic container of Tensors. The concrete definitions wil...
Definition TensorValue.h:36
Definition Tensor.h:32
Definition Variable.h:42
Definition CrossRef.cxx:30
std::string & parameter_name_separator()
Default nested parameter name separator.
Definition types.cxx:95
void neml_assert(bool assertion, Args &&... args)
Definition error.h:64