NEML2 2.1.0
Loading...
Searching...
No Matches
Factory.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 <filesystem>
28#include <iostream>
29
30#include "neml2/misc/errors.h"
31#include "neml2/base/InputFile.h"
32
33namespace neml2
34{
35// Forward decl
36class Settings;
37class Factory;
38class NEML2Object;
39class EquationSystem;
40class Solver;
41class Data;
42class Model;
43class Driver;
44class WorkScheduler;
45
52{
53public:
55
57 InputFile & input_file() { return _input_file; }
58
60 const InputFile & input_file() const { return _input_file; }
61
63 const std::shared_ptr<Settings> & settings() const { return _input_file.settings(); }
64
66 bool has_object(const std::string & section, const std::string & name);
67
84 template <class T>
85 std::shared_ptr<T> get_object(const std::string & section,
86 const std::string & name,
87 const OptionSet & additional_options = OptionSet(),
88 bool force_create = true);
89
91 template <class T = EquationSystem>
92 std::shared_ptr<T> get_es(const std::string & name);
94 template <class T = Solver>
95 std::shared_ptr<T> get_solver(const std::string & name);
97 template <class T = Data>
98 std::shared_ptr<T> get_data(const std::string & name);
100 template <class T = Model>
101 std::shared_ptr<T> get_model(const std::string & name);
103 template <class T = Driver>
104 std::shared_ptr<T> get_driver(const std::string & name);
106 template <class T = WorkScheduler>
107 std::shared_ptr<T> get_scheduler(const std::string & name);
108
110 void clear();
111
117 void print(std::ostream & os = std::cout);
118
119protected:
126 void create_object(const std::string & section, const OptionSet & options);
127
128private:
130 bool options_compatible(const std::shared_ptr<NEML2Object> & obj, const OptionSet & opts) const;
131
133 InputFile _input_file;
134
139 std::map<std::string, std::map<std::string, std::vector<std::shared_ptr<NEML2Object>>>> _objects;
140};
141
142template <class T>
143std::shared_ptr<T>
144Factory::get_object(const std::string & section,
145 const std::string & name,
146 const OptionSet & additional_options,
147 bool force_create)
148{
149 if (input_file().data().empty())
150 throw FactoryException("The input file is empty.");
151
152 // Easy if it already exists
153 if (!force_create)
154 if (_objects.count(section) && _objects.at(section).count(name))
155 for (const auto & neml2_obj : _objects[section][name])
156 {
157 // Check for option clash
158 if (!options_compatible(neml2_obj, additional_options))
159 continue;
160
161 // Check for object type
162 auto obj = std::dynamic_pointer_cast<T>(neml2_obj);
163 if (!obj)
164 throw FactoryException(
165 "Found object named " + name + " under section " + section +
166 ". But dynamic cast failed. Did you specify the correct object type?");
167
168 return obj;
169 }
170
171 // Otherwise try to create it
172 for (const auto & options : _input_file[section])
173 if (options.first == name)
174 {
175 auto new_options = options.second;
176 new_options.set<Factory *>("_factory") = this;
177 new_options.set<std::shared_ptr<Settings>>("_settings") = settings();
178 new_options += additional_options;
179 create_object(section, new_options);
180 break;
181 }
182
183 if (!_objects.count(section) || !_objects.at(section).count(name))
184 throw FactoryException("Failed to get object named " + name + " under section " + section +
185 ". Check to make sure the object is defined in the input file.");
186
187 auto obj = std::dynamic_pointer_cast<T>(_objects[section][name].back());
188
189 if (!obj)
190 throw FactoryException("Found object named " + name + " under section " + section +
191 ". But dynamic cast failed. Did you specify the correct object type?");
192
193 return obj;
194}
195
196template <class T>
197std::shared_ptr<T>
198Factory::get_es(const std::string & name)
199{
200 return get_object<T>("EquationSystems", name);
201}
202
203template <class T>
204std::shared_ptr<T>
205Factory::get_solver(const std::string & name)
206{
207 return get_object<T>("Solvers", name);
208}
209
210template <class T>
211std::shared_ptr<T>
212Factory::get_data(const std::string & name)
213{
214 return get_object<T>("Data", name);
215}
216
217template <class T>
218std::shared_ptr<T>
219Factory::get_model(const std::string & name)
220{
221 return get_object<T>("Models", name);
222}
223
224template <class T>
225std::shared_ptr<T>
226Factory::get_driver(const std::string & name)
227{
228 return get_object<T>("Drivers", name);
229}
230
231template <class T>
232std::shared_ptr<T>
233Factory::get_scheduler(const std::string & name)
234{
235 return get_object<T>("Schedulers", name);
236}
237} // 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 errors.h:73
Definition Factory.h:52
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:144
std::shared_ptr< T > get_driver(const std::string &name)
Get a driver by its name.
Definition Factory.h:226
std::shared_ptr< T > get_data(const std::string &name)
Get a data by its name.
Definition Factory.h:212
void create_object(const std::string &section, const OptionSet &options)
Manufacture a single NEML2Object.
std::shared_ptr< T > get_es(const std::string &name)
Get an equation system by its name.
Definition Factory.h:198
const std::shared_ptr< Settings > & settings() const
Global settings.
Definition Factory.h:63
Factory(InputFile)
std::shared_ptr< T > get_model(const std::string &name)
Get a model by its name.
Definition Factory.h:219
std::shared_ptr< T > get_scheduler(const std::string &name)
Get a scheduler by its name.
Definition Factory.h:233
bool has_object(const std::string &section, const std::string &name)
Check if an object with the given name exists under the given section.
InputFile & input_file()
Get the input file.
Definition Factory.h:57
const InputFile & input_file() const
Get the input file.
Definition Factory.h:60
void clear()
Delete all factories and destruct all the objects.
std::shared_ptr< T > get_solver(const std::string &name)
Get a solver by its name.
Definition Factory.h:205
void print(std::ostream &os=std::cout)
List all the manufactured objects.
A data structure that holds options of multiple objects.
Definition InputFile.h:37
The base class for all constitutive models.
Definition Model.h:83
The base class of all "manufacturable" objects in the NEML2 library.
Definition NEML2Object.h:52
A custom map-like data structure. The keys are strings, and the values can be nonhomogeneously typed.
Definition OptionSet.h:52
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 name(ElasticConstant p)