NEML2 2.1.0
Loading...
Searching...
No Matches
OptionSet.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 <map>
28#include <string>
29#include <memory>
30
31#include "neml2/base/Option.h"
32#include "neml2/misc/errors.h"
33#include "neml2/misc/string_utils.h"
34
35namespace neml2
36{
37// Forward decl
38class OptionSet;
40
41bool options_compatible(const OptionSet & opts, const OptionSet & additional_opts);
42
43// Streaming operators
44std::ostream & operator<<(std::ostream & os, const OptionSet & p);
45
52{
53public:
54 OptionSet() = default;
55
57 OptionSet(OptionSet &&) noexcept;
58 OptionSet & operator=(const OptionSet &);
59 OptionSet & operator=(OptionSet &&) noexcept;
60 virtual ~OptionSet() = default;
61
63
70 void operator+=(const OptionSet & source);
71 void operator+=(OptionSet && source);
73
75 const std::string & name() const { return _metadata.name; }
77 std::string & name() { return _metadata.name; }
79 const std::string & type() const { return _metadata.type; }
81 std::string & type() { return _metadata.type; }
83 const std::string & path() const { return _metadata.path; }
85 std::string & path() { return _metadata.path; }
87 const std::string & doc() const { return _metadata.doc; }
89 std::string & doc() { return _metadata.doc; }
91 const std::string & section() const { return _metadata.section; }
93 std::string & section() { return _metadata.section; }
94
96 bool contains(const std::string &) const;
97
99 bool user_specified(const std::string & name) const;
100
102 std::size_t size() const { return _values.size(); }
103
105 void clear();
106
108 std::string to_str() const;
109
114 template <typename T>
115 T get(const std::string &) const;
116
118 const OptionBase & get(const std::string &) const;
119
124
135 template <typename K, typename V>
136 std::map<K, V> get_map(const std::string &, const std::string &) const;
137
139
144 template <typename T, FType f = FType::NONE>
145 T & set(const std::string &);
146 OptionBase & set(const std::string &);
148
150 LabeledAxisAccessor & set_input(const std::string &);
152 LabeledAxisAccessor & set_output(const std::string &);
154 template <typename T>
155 T & set_parameter(const std::string &);
157 template <typename T>
158 T & set_buffer(const std::string &);
159
161 using map_type = std::map<std::string, std::unique_ptr<OptionBase>, std::less<>>;
163 using iterator = map_type::iterator;
165 using const_iterator = map_type::const_iterator;
166
175
176protected:
180 struct Metadata
181 {
195 std::string name = "";
210 std::string type = "";
229 std::string path = "";
240 std::string doc = "";
248 std::string section = "";
250
253};
254
256// Implementation
258
259template <typename T>
260T
261OptionSet::get(const std::string & name) const
262{
263 if (!this->contains(name))
264 throw NEMLException("ERROR: no option named \"" + name + "\" found.\n\nKnown options:\n" +
265 to_str());
266
267 auto * opt_base = _values.at(name).get();
268 auto ptr = dynamic_cast<Option<T> *>(opt_base);
269 if (!ptr)
270 throw NEMLException("ERROR: option named \"" + name +
271 "\" is not of the requested type: " + opt_base->type());
272 return ptr->get();
273}
274
275template <typename K, typename V>
276std::map<K, V>
277OptionSet::get_map(const std::string & key_option, const std::string & value_option) const
278{
279 const auto keys = this->get<std::vector<K>>(key_option);
280 const auto values = this->get<std::vector<V>>(value_option);
281 if (keys.size() != values.size())
282 throw NEMLException("Trying to build a map from '" + key_option + "' and '" + value_option +
283 "' with " + std::to_string(keys.size()) + " keys and " +
284 std::to_string(values.size()) + " values.");
285 std::map<K, V> result;
286 for (size_t i = 0; i < keys.size(); i++)
287 {
288 if (result.find(keys[i]) != result.end())
289 throw NEMLException("Trying to build a map from '" + key_option + "' and '" + value_option +
290 "' with duplicate key: " + utils::stringify(keys[i]));
291 result[keys[i]] = values[i];
292 }
293 return result;
294}
295
296template <typename T, FType F>
297T &
298OptionSet::set(const std::string & name)
299{
300 if (!this->contains(name))
301 _values[name] = std::make_unique<Option<T>>(name);
302 auto ptr = dynamic_cast<Option<T> *>(_values[name].get());
303 ptr->ftype() = F;
304 return ptr->set();
305}
306
307template <typename T>
308T &
309OptionSet::set_parameter(const std::string & name)
310{
312}
313
314template <typename T>
315T &
316OptionSet::set_buffer(const std::string & name)
317{
319}
320} // namespace neml2
The accessor containing all the information needed to access an item in a LabeledAxis.
Definition LabeledAxisAccessor.h:56
Definition errors.h:34
Definition OptionBase.h:44
A custom map-like data structure. The keys are strings, and the values can be nonhomogeneously typed.
Definition OptionSet.h:52
map_type::iterator iterator
Option map iterator.
Definition OptionSet.h:163
map_type _values
Data structure to map names with values.
Definition OptionSet.h:252
std::map< K, V > get_map(const std::string &, const std::string &) const
Get two options and bind them to find a map.
Definition OptionSet.h:277
T & set_buffer(const std::string &)
Convenient method to request a buffer.
Definition OptionSet.h:316
const std::string & name() const
A readonly reference to the option set's name.
Definition OptionSet.h:75
const_iterator begin() const
Iterator pointing to the beginning of the set of options.
OptionSet()=default
const std::string & type() const
A readonly reference to the option set's type.
Definition OptionSet.h:79
OptionBase & set(const std::string &)
LabeledAxisAccessor & set_output(const std::string &)
OptionSet(const OptionSet &)
std::string to_str() const
Print the contents.
T get(const std::string &) const
Definition OptionSet.h:261
map_type::const_iterator const_iterator
Constant option map iterator.
Definition OptionSet.h:165
std::map< std::string, std::unique_ptr< OptionBase >, std::less<> > map_type
The type of the map that we store internally.
Definition OptionSet.h:161
std::string & name()
A writable reference to the option set's name.
Definition OptionSet.h:77
LabeledAxisAccessor & set_input(const std::string &)
const OptionBase & get(const std::string &) const
Get a const reference to the specified option value.
std::string & type()
A writable reference to the option set's type.
Definition OptionSet.h:81
const std::string & doc() const
A readonly reference to the option set's docstring.
Definition OptionSet.h:87
const std::string & section() const
A readonly reference to the option set's section.
Definition OptionSet.h:91
T & set_parameter(const std::string &)
Convenient method to request a parameter.
Definition OptionSet.h:309
const std::string & path() const
A readonly reference to the option set's path.
Definition OptionSet.h:83
std::string & doc()
A writable reference to the option set's docstring.
Definition OptionSet.h:89
bool user_specified(const std::string &name) const
struct neml2::OptionSet::Metadata _metadata
void clear()
Clear internal data structures & frees any allocated memory.
iterator end()
Iterator pointing to the end of the set of options.
const_iterator end() const
Iterator pointing to the end of the set of options.
std::string & path()
A writable reference to the option set's path.
Definition OptionSet.h:85
iterator begin()
Iterator pointing to the beginning of the set of options.
std::string & section()
A writable reference to the option set's section.
Definition OptionSet.h:93
std::size_t size() const
Definition OptionSet.h:102
bool contains(const std::string &) const
T & set(const std::string &)
Definition OptionSet.h:298
OptionSet(OptionSet &&) noexcept
Definition Option.h:70
std::string stringify(const T &t)
Definition string_utils.h:70
Definition DiagnosticsInterface.h:31
std::ostream & operator<<(std::ostream &, const EnumSelection &)
bool options_compatible(const OptionSet &opts, const OptionSet &additional_opts)
Definition OptionSet.h:181
std::string path
Path to the option set.
Definition OptionSet.h:229
std::string type
Type of the option set.
Definition OptionSet.h:210
std::string name
Name of the option set.
Definition OptionSet.h:195
std::string section
Which NEML2 input file section this object belongs to.
Definition OptionSet.h:248
std::string doc
Option set's doc string.
Definition OptionSet.h:240