NEML2 2.0.0
Loading...
Searching...
No Matches
NEML2Object.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/OptionSet.h"
28#include "neml2/base/Factory.h"
29
30// Registry.h is included here because it is needed for the factory pattern, i.e., for the
31// register_NEML2_object macros
32#include "neml2/base/Registry.h"
33
34namespace neml2
35{
36class Settings;
37class Solver;
38class Data;
39class Model;
40class Driver;
41class WorkScheduler;
42
51{
52public:
54
55 NEML2Object() = delete;
57 NEML2Object(const NEML2Object &) = delete;
59 NEML2Object & operator=(const NEML2Object &) = delete;
60 virtual ~NEML2Object() = default;
61
67 NEML2Object(const OptionSet & options);
68
69 const OptionSet & input_options() const { return _input_options; }
70
80 virtual void setup() {}
81
83 const std::string & name() const { return _input_options.name(); }
85 const std::string & type() const { return _input_options.type(); }
87 const std::string & path() const { return _input_options.path(); }
89 const std::string & doc() const { return _input_options.doc(); }
90
92 Factory * factory() const { return _factory; }
93
95 const Settings & settings() const { return *_settings; }
96
98 template <typename T = NEML2Object>
99 const T * host() const;
100
102 template <typename T = NEML2Object>
103 T * host();
104
106 template <typename T>
107 const T & resolve_tensor(const std::string & name);
108
111 template <class T>
112 std::shared_ptr<T> get_object(const std::string & section, const std::string & name);
114 template <class T = Solver>
115 std::shared_ptr<T> get_solver(const std::string & name);
117 template <class T = Data>
118 std::shared_ptr<T> get_data(const std::string & name);
120 template <class T = Model>
121 std::shared_ptr<T> get_model(const std::string & name);
123 template <class T = Driver>
124 std::shared_ptr<T> get_driver(const std::string & name);
126 template <class T = WorkScheduler>
127 std::shared_ptr<T> get_scheduler(const std::string & name);
129
130private:
131 const OptionSet _input_options;
132
139 Factory * _factory;
140
142 const std::shared_ptr<Settings> _settings;
143
145 NEML2Object * _host;
146};
147
148template <typename T>
149const T *
151{
152 auto host_ptr = dynamic_cast<const T *>(_host ? _host : this);
153 if (!host_ptr)
154 throw NEMLException("Internal error: Failed to retrieve host of object " + name());
155 return host_ptr;
156}
157
158template <typename T>
159T *
161{
162 auto host_ptr = dynamic_cast<T *>(_host ? _host : this);
163 if (!host_ptr)
164 throw NEMLException("Internal error: Failed to retrieve host of object " + name());
165 return host_ptr;
166}
167
168template <class T>
169std::shared_ptr<T>
170NEML2Object::get_object(const std::string & section, const std::string & name)
171{
172 auto obj_name = _input_options.contains(name) ? _input_options.get<std::string>(name) : name;
173 if (!_factory)
174 throw NEMLException("Internal error: factory is nullptr for object " + this->name());
175 return _factory->get_object<T>(section, obj_name);
176}
177
178template <class T>
179std::shared_ptr<T>
180NEML2Object::get_solver(const std::string & name)
181{
182 return get_object<T>("Solvers", name);
183}
184
185template <class T>
186std::shared_ptr<T>
187NEML2Object::get_data(const std::string & name)
188{
189 return get_object<T>("Data", name);
190}
191
192template <class T>
193std::shared_ptr<T>
194NEML2Object::get_model(const std::string & name)
195{
196 return get_object<T>("Models", name);
197}
198
199template <class T>
200std::shared_ptr<T>
201NEML2Object::get_driver(const std::string & name)
202{
203 return get_object<T>("Drivers", name);
204}
205
206template <class T>
207std::shared_ptr<T>
209{
210 return get_object<T>("Schedulers", name);
211}
212} // namespace neml2
Definition Factory.h:65
std::shared_ptr< T > get_object(const std::string &section, const std::string &name, const OptionSet &additional_options=OptionSet(), bool force_create=true)
Retrive an object pointer under the given section with the given object name.
Definition Factory.h:154
The base class of all "manufacturable" objects in the NEML2 library.
Definition NEML2Object.h:51
NEML2Object(const NEML2Object &)=delete
std::shared_ptr< T > get_driver(const std::string &name)
Get a driver from the factory.
Definition NEML2Object.h:201
const T & resolve_tensor(const std::string &name)
Resolve a TensorName to a Tensor.
Definition NEML2Object.cxx:59
const std::string & name() const
A readonly reference to the object's name.
Definition NEML2Object.h:83
const T * host() const
Get a readonly pointer to the host.
Definition NEML2Object.h:150
std::shared_ptr< T > get_data(const std::string &name)
Get a data from the factory.
Definition NEML2Object.h:187
Factory * factory() const
Get the factory that created this object.
Definition NEML2Object.h:92
virtual void setup()
Setup this object.
Definition NEML2Object.h:80
const OptionSet & input_options() const
Definition NEML2Object.h:69
const std::string & type() const
A readonly reference to the object's type.
Definition NEML2Object.h:85
std::shared_ptr< T > get_object(const std::string &section, const std::string &name)
Definition NEML2Object.h:170
NEML2Object(NEML2Object &&)=delete
NEML2Object & operator=(const NEML2Object &)=delete
const std::string & doc() const
A readonly reference to the object's docstring.
Definition NEML2Object.h:89
NEML2Object & operator=(NEML2Object &&)=delete
std::shared_ptr< T > get_model(const std::string &name)
Get a model from the factory.
Definition NEML2Object.h:194
std::shared_ptr< T > get_scheduler(const std::string &name)
Get a scheduler from the factory.
Definition NEML2Object.h:208
const std::string & path() const
A readonly reference to the object's path.
Definition NEML2Object.h:87
const Settings & settings() const
Settings.
Definition NEML2Object.h:95
static OptionSet expected_options()
Definition NEML2Object.cxx:33
virtual ~NEML2Object()=default
std::shared_ptr< T > get_solver(const std::string &name)
Get a solver from the factory.
Definition NEML2Object.h:180
Definition errors.h:34
A custom map-like data structure. The keys are strings, and the values can be nonhomogeneously typed.
Definition OptionSet.h:51
const std::string & name() const
A readonly reference to the option set's name.
Definition OptionSet.h:74
const std::string & type() const
A readonly reference to the option set's type.
Definition OptionSet.h:78
T get(const std::string &) const
Definition OptionSet.h:242
const std::string & doc() const
A readonly reference to the option set's docstring.
Definition OptionSet.h:86
const std::string & path() const
A readonly reference to the option set's path.
Definition OptionSet.h:82
bool contains(const std::string &) const
Definition OptionSet.cxx:47
Definition Settings.h:35
Definition DiagnosticsInterface.cxx:30
std::string name(ElasticConstant p)
Definition ElasticityConverter.cxx:30