NEML2 2.0.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#include "neml2/tensors/tensors.h"
37
38namespace neml2
39{
40// Forward decl
41class VariableBase;
42class Model;
43
46{
47public:
48 ParameterStore(OptionSet options, NEML2Object * object);
49
50 ParameterStore(const ParameterStore &) = delete;
54 virtual ~ParameterStore() = default;
55
59 {
60 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
61 return const_cast<ParameterStore *>(this)->named_parameters();
62 }
65
67 void set_parameter(const std::string &, const Tensor &);
68
70 void set_parameters(const std::map<std::string, Tensor> &);
71
73 TensorValueBase & get_parameter(const std::string & name);
74
76 const TensorValueBase & get_parameter(const std::string & name) const;
77
79 bool has_nl_param() const { return !_nl_params.empty(); }
80
87 const VariableBase * nl_param(const std::string &) const;
88
90 virtual std::map<std::string, const VariableBase *>
91 named_nonlinear_parameters(bool recursive = false) const;
92
94 virtual std::map<std::string, Model *>
96
97protected:
103 virtual void send_parameters_to(const torch::TensorOptions & options);
104
118 template <typename T, typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
119 const T & declare_parameter(const std::string & name, const T & rawval);
120
136 template <typename T, typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
137 const T &
138 declare_parameter(const std::string & name, const CrossRef<T> & crossref, bool allow_nonlinear);
139
153 template <typename T, typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
154 const T & declare_parameter(const std::string & name,
155 const std::string & input_option_name,
156 bool allow_nonlinear = false);
157
159 std::map<std::string, const VariableBase *> _nl_params;
160
162 std::map<std::string, Model *> _nl_param_models;
163
164private:
165 NEML2Object * _object;
166
173 const OptionSet _object_options;
174
177};
178
179} // namespace neml2
The wrapper (decorator) for cross-referencing unresolved values at parse time.
Definition CrossRef.h:54
The base class of all "manufacturable" objects in the NEML2 library.
Definition NEML2Object.h:38
A custom map-like data structure. The keys are strings, and the values can be nonhomogeneously typed.
Definition OptionSet.h:85
Interface for object which can store parameters.
Definition ParameterStore.h:46
std::map< std::string, const VariableBase * > _nl_params
Map from nonlinear parameter names to their corresponding variable views.
Definition ParameterStore.h:159
ParameterStore & operator=(const ParameterStore &)=delete
virtual void send_parameters_to(const torch::TensorOptions &options)
Send parameters to options.
Definition ParameterStore.cxx:38
void set_parameter(const std::string &, const Tensor &)
}@
Definition ParameterStore.cxx:45
std::map< std::string, Model * > _nl_param_models
Map from nonlinear parameter names to models which evaluate them.
Definition ParameterStore.h:162
void set_parameters(const std::map< std::string, Tensor > &)
Set values for parameters.
Definition ParameterStore.cxx:54
ParameterStore(OptionSet options, NEML2Object *object)
Definition ParameterStore.cxx:31
ParameterStore & operator=(ParameterStore &&)=delete
ParameterStore(const ParameterStore &)=delete
bool has_nl_param() const
Whether this parameter store has any nonlinear parameter.
Definition ParameterStore.h:79
virtual ~ParameterStore()=default
TensorValueBase & get_parameter(const std::string &name)
Get a writable reference of a parameter.
Definition ParameterStore.cxx:61
const Storage< std::string, TensorValueBase > & named_parameters() const
Definition ParameterStore.h:58
const T & declare_parameter(const std::string &name, const T &rawval)
Declare a parameter.
Definition ParameterStore.cxx:140
virtual std::map< std::string, Model * > named_nonlinear_parameter_models(bool recursive=false) const
Get all nonlinear parameters' models.
Definition ParameterStore.cxx:116
virtual std::map< std::string, const VariableBase * > named_nonlinear_parameters(bool recursive=false) const
Get all nonlinear parameters.
Definition ParameterStore.cxx:93
ParameterStore(ParameterStore &&)=delete
const VariableBase * nl_param(const std::string &) const
Query the existence of a nonlinear parameter.
Definition ParameterStore.cxx:79
The base class to allow us to set up a polymorphic container of Tensors. The concrete definitions wil...
Definition TensorValue.h:39
Definition Tensor.h:47
Base class of variable.
Definition Variable.h:47
Definition CrossRef.cxx:31
std::string name(ElasticConstant p)
Definition ElasticityConverter.cxx:30