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 const 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
134private:
135 const OptionSet _input_options;
136
143 Factory * _factory;
144
146 const std::shared_ptr<Settings> _settings;
147
149 NEML2Object * _host;
150};
151
152template <typename T>
153const T *
155{
156 auto host_ptr = dynamic_cast<const T *>(_host ? _host : this);
157 if (!host_ptr)
158 throw NEMLException("Internal error: Failed to retrieve host of object " + name());
159 return host_ptr;
160}
161
162template <typename T>
163T *
165{
166 auto host_ptr = dynamic_cast<T *>(_host ? _host : this);
167 if (!host_ptr)
168 throw NEMLException("Internal error: Failed to retrieve host of object " + name());
169 return host_ptr;
170}
171
172template <class T>
173std::shared_ptr<T>
174NEML2Object::get_object(const std::string & section, const std::string & name)
175{
176 auto obj_name = _input_options.contains(name) ? _input_options.get<std::string>(name) : name;
177
178 if (!_factory)
179 throw NEMLException("Internal error: factory is nullptr for object " + this->name());
180
181 if (!_factory->has_object(section, obj_name))
182 {
183 if (_input_options.contains(name))
184 throw NEMLException(
185 path() + " failed to get an object via option '" + name + "' under section " + section +
186 ". Currently, " + path() + "/" + name + " = '" + obj_name +
187 "'. Check to make sure the object name is specified correctly in the input file.");
188 else
189 throw NEMLException(path() + " failed to get an object named '" + obj_name +
190 "' under section " + section + ".");
191 }
192
193 OptionSet extra_opts;
194 extra_opts.set<NEML2Object *>("_host") = host();
195 return _factory->get_object<T>(section, obj_name, extra_opts, /*force_create=*/false);
196}
197
198template <class T>
199std::shared_ptr<T>
200NEML2Object::get_es(const std::string & name)
201{
202 return get_object<T>("EquationSystems", name);
203}
204
205template <class T>
206std::shared_ptr<T>
207NEML2Object::get_solver(const std::string & name)
208{
209 return get_object<T>("Solvers", name);
210}
211
212template <class T>
213std::shared_ptr<T>
214NEML2Object::get_data(const std::string & name)
215{
216 return get_object<T>("Data", name);
217}
218
219template <class T>
220std::shared_ptr<T>
221NEML2Object::get_model(const std::string & name)
222{
223 return get_object<T>("Models", name);
224}
225
226template <class T>
227std::shared_ptr<T>
228NEML2Object::get_driver(const std::string & name)
229{
230 return get_object<T>("Drivers", name);
231}
232
233template <class T>
234std::shared_ptr<T>
236{
237 return get_object<T>("Schedulers", name);
238}
239} // 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:52
The base class for all constitutive models.
Definition Model.h:83
NEML2Object(const NEML2Object &)=delete
std::shared_ptr< T > get_driver(const std::string &name)
Get a driver from the factory.
Definition NEML2Object.h:228
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:154
std::shared_ptr< T > get_data(const std::string &name)
Get a data from the factory.
Definition NEML2Object.h:214
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
const std::string & type() const
A readonly reference to the object's type.
Definition NEML2Object.h:86
std::shared_ptr< T > get_es(const std::string &name)
Get an equation system from the factory.
Definition NEML2Object.h:200
std::shared_ptr< T > get_object(const std::string &section, const std::string &name)
Definition NEML2Object.h:174
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::shared_ptr< T > get_model(const std::string &name)
Get a model from the factory.
Definition NEML2Object.h:221
std::shared_ptr< T > get_scheduler(const std::string &name)
Get a scheduler from the factory.
Definition NEML2Object.h:235
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.
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:207
Definition errors.h:34
A custom map-like data structure. The keys are strings, and the values can be nonhomogeneously typed.
Definition OptionSet.h:52
T & set(const std::string &)
Definition OptionSet.h:298
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