NEML2 1.4.0
Loading...
Searching...
No Matches
ParameterStore.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/NEML2Object.h"
28#include "neml2/base/OptionSet.h"
29#include "neml2/base/Storage.h"
30#include "neml2/tensors/TensorValue.h"
31
32namespace neml2
33{
34class VariableBase;
35
38{
39public:
40 ParameterStore(const OptionSet & options, NEML2Object * object);
41
45 {
46 return const_cast<ParameterStore *>(this)->named_parameters();
47 }
50
52 template <typename T,
53 typename = typename std::enable_if_t<std::is_base_of_v<BatchTensorBase<T>, T>>>
54 T & get_parameter(const std::string & name);
55
57 bool has_nl_param() const { return !_nl_params.empty(); }
58
60 const std::map<std::string, const VariableBase *> & nl_params() const { return _nl_params; }
61
68 const VariableBase * nl_param(const std::string &) const;
69
70protected:
76 virtual void send_parameters_to(const torch::TensorOptions & options);
77
91 template <typename T,
92 typename = typename std::enable_if_t<std::is_base_of_v<BatchTensorBase<T>, T>>>
93 const T & declare_parameter(const std::string & name, const T & rawval);
94
109 template <typename T,
110 typename = typename std::enable_if_t<std::is_base_of_v<BatchTensorBase<T>, T>>>
111 const T & declare_parameter(const std::string & name, const std::string & input_option_name);
112
113private:
114 NEML2Object * _object;
115
121 const OptionSet _options;
122
125
127 std::map<std::string, const VariableBase *> _nl_params;
128};
129
130template <typename T, typename>
131T &
132ParameterStore::get_parameter(const std::string & name)
133{
134 neml_assert(_object->host() == _object, "This method should only be called on the host model.");
135
136 auto base_ptr = _param_values.query_value(name);
137 neml_assert(base_ptr, "Parameter named ", name, " does not exist.");
138 auto ptr = dynamic_cast<TensorValue<T> *>(base_ptr);
139 neml_assert_dbg(ptr, "Internal error: Failed to cast parameter to a concrete type.");
140 return ptr->value();
141}
142
143template <typename T, typename>
144const T &
145ParameterStore::declare_parameter(const std::string & name, const T & rawval)
146{
147 if (_object->host() != _object)
148 return _object->host<ParameterStore>()->declare_parameter(_object->name() + "." + name, rawval);
149
150 // If the parameter already exists, return its reference
151 if (_param_values.has_key(name))
152 return get_parameter<T>(name);
153
154 auto val = std::make_unique<TensorValue<T>>(rawval);
155 auto base_ptr = _param_values.set_pointer(name, std::move(val));
156 auto ptr = dynamic_cast<TensorValue<T> *>(base_ptr);
157 neml_assert(ptr, "Internal error: Failed to cast parameter to a concrete type.");
158 return ptr->value();
159}
160
161} // namespace neml2
The wrapper (decorator) for cross-referencing unresolved values at parse time.
Definition CrossRef.h:52
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:59
Interface for object which can store parameters.
Definition ParameterStore.h:38
virtual void send_parameters_to(const torch::TensorOptions &options)
Send parameters to options.
Definition ParameterStore.cxx:47
ParameterStore(const OptionSet &options, NEML2Object *object)
Definition ParameterStore.cxx:32
const std::map< std::string, const VariableBase * > & nl_params() const
Get all nonlinear parameters.
Definition ParameterStore.h:60
bool has_nl_param() const
Whether this parameter store has any nonlinear parameter.
Definition ParameterStore.h:57
const Storage< std::string, TensorValueBase > & named_parameters() const
Definition ParameterStore.h:44
const T & declare_parameter(const std::string &name, const T &rawval)
Declare a parameter.
Definition ParameterStore.h:145
T & get_parameter(const std::string &name)
}@
Definition ParameterStore.h:132
const VariableBase * nl_param(const std::string &) const
Query the existence of a nonlinear parameter.
Definition ParameterStore.cxx:56
Definition Variable.h:41
Definition CrossRef.cxx:32
void neml_assert_dbg(bool assertion, Args &&... args)
Definition error.h:85
void neml_assert(bool assertion, Args &&... args)
Definition error.h:73