NEML2 2.1.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 EquationSystem;
38class Solver;
39class Data;
40class Model;
41class Driver;
42class WorkScheduler;
43
52{
53public:
55
56 NEML2Object() = delete;
58 NEML2Object(const NEML2Object &) = delete;
60 NEML2Object & operator=(const NEML2Object &) = delete;
61 virtual ~NEML2Object() = default;
62
68 NEML2Object(const OptionSet & options);
69
70 const OptionSet & input_options() const { return _input_options; }
71
81 virtual void setup() {}
82
84 const std::string & name() const { return _input_options.name(); }
86 std::string type() const { return _input_options.type(); }
88 const std::string & path() const { return _input_options.path(); }
90 const std::string & doc() const { return _input_options.doc(); }
91
93 Factory * factory() const { return _factory; }
94
96 const Settings & settings() const { return *_settings; }
97
99 template <typename T = NEML2Object>
100 const T * host() const;
101
103 template <typename T = NEML2Object>
104 T * host();
105
107 template <typename T>
108 const T & resolve_tensor(const std::string & name);
109
112 template <class T>
113 std::shared_ptr<T> get_object(const std::string & section, const std::string & name);
115 template <class T = EquationSystem>
116 std::shared_ptr<T> get_es(const std::string & name);
118 template <class T = Solver>
119 std::shared_ptr<T> get_solver(const std::string & name);
121 template <class T = Data>
122 std::shared_ptr<T> get_data(const std::string & name);
124 template <class T = Model>
125 std::shared_ptr<T> get_model(const std::string & name);
127 template <class T = Driver>
128 std::shared_ptr<T> get_driver(const std::string & name);
130 template <class T = WorkScheduler>
131 std::shared_ptr<T> get_scheduler(const std::string & name);
133
134protected:
146 VariableName history_name(const VariableName & var, std::size_t nstep) const;
147
159
171
172private:
173 const OptionSet _input_options;
174
181 Factory * _factory;
182
184 const std::shared_ptr<Settings> _settings;
185
187 NEML2Object * _host;
188};
189
190template <typename T>
191const T *
193{
194 auto host_ptr = dynamic_cast<const T *>(_host ? _host : this);
195 if (!host_ptr)
196 throw NEMLException("Internal error: Failed to retrieve host of object " + name());
197 return host_ptr;
198}
199
200template <typename T>
201T *
203{
204 auto host_ptr = dynamic_cast<T *>(_host ? _host : this);
205 if (!host_ptr)
206 throw NEMLException("Internal error: Failed to retrieve host of object " + name());
207 return host_ptr;
208}
209
210template <class T>
211std::shared_ptr<T>
212NEML2Object::get_object(const std::string & section, const std::string & name)
213{
214 auto obj_name = _input_options.contains(name) ? _input_options.get<std::string>(name) : name;
215
216 if (!_factory)
217 throw NEMLException("Internal error: factory is nullptr for object " + this->name());
218
219 if (!_factory->has_object(section, obj_name))
220 {
221 if (_input_options.contains(name))
222 throw NEMLException(
223 path() + " failed to get an object via option '" + name + "' under section " + section +
224 ". Currently, " + path() + "/" + name + " = '" + obj_name +
225 "'. Check to make sure the object name is specified correctly in the input file.");
226 else
227 throw NEMLException(path() + " failed to get an object named '" + obj_name +
228 "' under section " + section + ".");
229 }
230
231 OptionSet extra_opts;
232 extra_opts.add_private<NEML2Object *>("_host", host());
233 return _factory->get_object<T>(section, obj_name, extra_opts, /*force_create=*/false);
234}
235
236template <class T>
237std::shared_ptr<T>
238NEML2Object::get_es(const std::string & name)
239{
240 return get_object<T>("EquationSystems", name);
241}
242
243template <class T>
244std::shared_ptr<T>
245NEML2Object::get_solver(const std::string & name)
246{
247 return get_object<T>("Solvers", name);
248}
249
250template <class T>
251std::shared_ptr<T>
252NEML2Object::get_data(const std::string & name)
253{
254 return get_object<T>("Data", name);
255}
256
257template <class T>
258std::shared_ptr<T>
259NEML2Object::get_model(const std::string & name)
260{
261 return get_object<T>("Models", name);
262}
263
264template <class T>
265std::shared_ptr<T>
266NEML2Object::get_driver(const std::string & name)
267{
268 return get_object<T>("Drivers", name);
269}
270
271template <class T>
272std::shared_ptr<T>
274{
275 return get_object<T>("Schedulers", name);
276}
277} // namespace neml2
Definition Data.h:40
The Driver drives the execution of a NEML2 Model.
Definition Driver.h:44
Base class for manufacturable objects under the EquationSystems section.
Definition EquationSystem.h:33
Definition Factory.h:49
The base class for all constitutive models.
Definition Model.h:82
NEML2Object(const NEML2Object &)=delete
std::shared_ptr< T > get_driver(const std::string &name)
Get a driver from the factory.
Definition NEML2Object.h:266
VariableName residual_name(const VariableName &var) const
Helper method to wrap a variable name into its residual form.
const T & resolve_tensor(const std::string &name)
Resolve a TensorName to a Tensor.
const std::string & name() const
A readonly reference to the object's name.
Definition NEML2Object.h:84
const T * host() const
Get a readonly pointer to the host.
Definition NEML2Object.h:192
std::shared_ptr< T > get_data(const std::string &name)
Get a data from the factory.
Definition NEML2Object.h:252
Factory * factory() const
Get the factory that created this object.
Definition NEML2Object.h:93
virtual void setup()
Setup this object.
Definition NEML2Object.h:81
const OptionSet & input_options() const
Definition NEML2Object.h:70
std::shared_ptr< T > get_es(const std::string &name)
Get an equation system from the factory.
Definition NEML2Object.h:238
std::shared_ptr< T > get_object(const std::string &section, const std::string &name)
Definition NEML2Object.h:212
NEML2Object(NEML2Object &&)=delete
NEML2Object & operator=(const NEML2Object &)=delete
const std::string & doc() const
A readonly reference to the object's docstring.
Definition NEML2Object.h:90
NEML2Object & operator=(NEML2Object &&)=delete
std::string type() const
The object's type.
Definition NEML2Object.h:86
std::shared_ptr< T > get_model(const std::string &name)
Get a model from the factory.
Definition NEML2Object.h:259
std::shared_ptr< T > get_scheduler(const std::string &name)
Get a scheduler from the factory.
Definition NEML2Object.h:273
VariableName history_name(const VariableName &var, std::size_t nstep) const
Helper method to wrap a variable name into its history form.
const std::string & path() const
A readonly reference to the object's path.
Definition NEML2Object.h:88
const Settings & settings() const
Settings.
Definition NEML2Object.h:96
NEML2Object(const OptionSet &options)
Construct a new NEML2Object object.
VariableName rate_name(const VariableName &var) const
Helper method to wrap a variable name into its rate form.
static OptionSet expected_options()
virtual ~NEML2Object()=default
std::shared_ptr< T > get_solver(const std::string &name)
Get a solver from the factory.
Definition NEML2Object.h:245
Definition errors.h:34
A custom map-like data structure. The keys are strings, and the values can be nonhomogeneously typed.
Definition OptionSet.h:54
void add_private(const std::string &name, const T &default_value)
Create a private option with its default value.
Definition OptionSet.h:488
Definition Settings.h:35
The solver solves a system of equations.
Definition Solver.h:37
Scheduler for work dispatching.
Definition WorkScheduler.h:48
Definition DiagnosticsInterface.h:31
std::string VariableName
Definition types.h:75