NEML2 2.0.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#pragma once
25
26#include <filesystem>
27#include "neml2/base/NEML2Object.h"
28#include "neml2/misc/error.h"
29#include "neml2/base/OptionCollection.h"
30#include "neml2/base/DiagnosticsInterface.h"
31
32namespace neml2
33{
34// Forward decl
35class Model;
36class Driver;
37class Settings;
38
47void load_input(const std::filesystem::path & path, const std::string & additional_input = "");
48
58void reload_input(const std::filesystem::path & path, const std::string & additional_input = "");
59
68Model & get_model(const std::string & mname, bool force_create = true);
69
76Model & load_model(const std::filesystem::path & path, const std::string & mname);
77
85Model & reload_model(const std::filesystem::path & path, const std::string & mname);
86
94Driver & get_driver(const std::string & dname);
95
102{
103public:
105 static Factory & get();
106
123 template <class T>
124 static std::shared_ptr<T> get_object_ptr(const std::string & section,
125 const std::string & name,
127 bool force_create = true);
128
145 template <class T>
146 static T & get_object(const std::string & section,
147 const std::string & name,
149 bool force_create = true);
150
157 static void load_options(const OptionCollection & all_options);
158
160 static const OptionCollection & loaded_options();
161
163 static void clear();
164
170 static void print(std::ostream & os = std::cout);
171
172protected:
179 void create_object(const std::string & section, const OptionSet & options);
180
181private:
183 OptionCollection _all_options;
184
189 std::map<std::string, std::map<std::string, std::vector<std::shared_ptr<NEML2Object>>>> _objects;
190
192 Settings _settings;
193};
194
195template <class T>
196inline std::shared_ptr<T>
197Factory::get_object_ptr(const std::string & section,
198 const std::string & name,
200 bool force_create)
201{
202 auto & factory = Factory::get();
203
204 // Easy if it already exists
205 if (!force_create)
206 if (factory._objects.count(section) && factory._objects.at(section).count(name))
207 for (const auto & neml2_obj : factory._objects[section][name])
208 {
209 // Check for option clash
210 if (!options_compatible(neml2_obj->input_options(), additional_options))
211 continue;
212
213 // Check for object type
214 auto obj = std::dynamic_pointer_cast<T>(neml2_obj);
215 neml_assert(obj != nullptr,
216 "Found object named ",
217 name,
218 " under section ",
219 section,
220 ". But dynamic cast failed. Did you specify the correct object type?");
221
222 return obj;
223 }
224
225 // Otherwise try to create it
226 for (auto & options : factory._all_options[section])
227 if (options.first == name)
228 {
229 auto new_options = options.second;
231 factory.create_object(section, new_options);
232 break;
233 }
234
235 neml_assert(factory._objects.count(section) && factory._objects.at(section).count(name),
236 "Failed to get object named ",
237 name,
238 " under section ",
239 section);
240
241 auto obj = std::dynamic_pointer_cast<T>(factory._objects[section][name].back());
242 neml_assert(obj != nullptr, "Internal error: Factory failed to create object ", name);
243 return obj;
244}
245
246template <class T>
247inline T &
248Factory::get_object(const std::string & section,
249 const std::string & name,
251 bool force_create)
252{
253 return *Factory::get_object_ptr<T>(section, name, additional_options, force_create);
254}
255} // namespace neml2
The wrapper (decorator) for cross-referencing unresolved values at parse time.
Definition CrossRef.h:54
Definition Factory.h:102
void create_object(const std::string &section, const OptionSet &options)
Manufacture a single NEML2Object.
Definition Factory.cxx:111
static T & get_object(const std::string &section, const std::string &name, const OptionSet &additional_options=OptionSet(), bool force_create=true)
Retrive an object reference under the given section with the given object name.
Definition Factory.h:248
static std::shared_ptr< T > get_object_ptr(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:197
static const OptionCollection & loaded_options()
Get the loaded options.
Definition Factory.cxx:104
static void load_options(const OptionCollection &all_options)
Provide all objects' options to the factory. The factory is ready to manufacture objects after this c...
Definition Factory.cxx:93
static void clear()
Destruct all the objects.
Definition Factory.cxx:146
static Factory & get()
Get the global Factory singleton.
Definition Factory.cxx:86
static void print(std::ostream &os=std::cout)
List all the manufactured objects.
Definition Factory.cxx:133
A data structure that holds options of multiple objects.
Definition OptionCollection.h:39
A custom map-like data structure. The keys are strings, and the values can be nonhomogeneously typed.
Definition OptionSet.h:85
Definition Settings.h:32
Definition CrossRef.cxx:31
Model & load_model(const std::filesystem::path &path, const std::string &mname)
A convenient function to load an input file and get a model.
Definition Factory.cxx:66
Model & get_model(const std::string &mname, bool force_create)
A convenient function to manufacture a neml2::Model.
Definition Factory.cxx:59
bool options_compatible(const OptionSet &opts, const OptionSet &additional_opts)
Definition OptionSet.cxx:31
std::string name(ElasticConstant p)
Definition ElasticityConverter.cxx:30
void reload_input(const std::filesystem::path &path, const std::string &additional_input)
Similar to neml2::load_input, but additionally clear the Factory before loading the options,...
Definition Factory.cxx:52
Model & reload_model(const std::filesystem::path &path, const std::string &mname)
Similar to neml2::load_model, but additionally clear the Factory before loading the model,...
Definition Factory.cxx:73
Driver & get_driver(const std::string &dname)
A convenient function to manufacture a neml2::Driver.
Definition Factory.cxx:80
void load_input(const std::filesystem::path &path, const std::string &additional_input)
A convenient function to parse all options from an input file.
Definition Factory.cxx:35
void neml_assert(bool assertion, Args &&... args)
Definition error.h:64