Line data Source code
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 : #include <iostream>
26 :
27 : #include "neml2/base/Option.h"
28 : #include "neml2/misc/string_utils.h"
29 : #include "neml2/base/LabeledAxisAccessor.h"
30 : #include "neml2/base/EnumSelection.h"
31 : #include "neml2/base/MultiEnumSelection.h"
32 : #include "neml2/base/TensorName.h"
33 : #include "neml2/base/NEML2Object.h"
34 : #include "neml2/base/Factory.h"
35 : #include "neml2/base/Settings.h"
36 :
37 : namespace neml2
38 : {
39 : template <typename T>
40 239308 : Option<T>::Option(const std::string & name)
41 239308 : : _value()
42 : {
43 239308 : _metadata.name = name;
44 239308 : _metadata.type = utils::demangle(typeid(T).name());
45 239308 : }
46 :
47 : template <>
48 36 : Option<Device>::Option(const std::string & name)
49 36 : : _value(kCPU)
50 : {
51 36 : _metadata.name = name;
52 36 : _metadata.type = utils::demangle(typeid(Device).name());
53 36 : }
54 :
55 : template <typename T>
56 : bool
57 40 : Option<T>::operator==(const OptionBase & other) const
58 : {
59 40 : const auto other_ptr = dynamic_cast<const Option<T> *>(&other);
60 40 : if (!other_ptr)
61 0 : return false;
62 :
63 40 : return (_metadata == other_ptr->_metadata) && (other_ptr->get() == this->get());
64 : }
65 :
66 : template <typename T>
67 : bool
68 40 : Option<T>::operator!=(const OptionBase & other) const
69 : {
70 40 : return !(*this == other);
71 : }
72 :
73 : // LCOV_EXCL_START
74 : template <typename T>
75 : void
76 : Option<T>::print(std::ostream & os) const
77 : {
78 : details::_print_helper(os, static_cast<const T *>(&_value));
79 : }
80 : // LCOV_EXCL_STOP
81 :
82 : template <typename T>
83 : std::unique_ptr<OptionBase>
84 225227 : Option<T>::clone() const
85 : {
86 225227 : auto copy = std::make_unique<Option<T>>(this->name());
87 225227 : copy->_value = this->_value;
88 225227 : copy->_metadata = this->_metadata;
89 450454 : return copy;
90 225227 : }
91 :
92 : template class Option<bool>;
93 : template class Option<std::vector<bool>>;
94 : template class Option<std::vector<std::vector<bool>>>;
95 :
96 : template class Option<int>;
97 : template class Option<std::vector<int>>;
98 : template class Option<std::vector<std::vector<int>>>;
99 :
100 : template class Option<unsigned int>;
101 : template class Option<std::vector<unsigned int>>;
102 : template class Option<std::vector<std::vector<unsigned int>>>;
103 :
104 : template class Option<std::size_t>;
105 : template class Option<std::vector<std::size_t>>;
106 : template class Option<std::vector<std::vector<std::size_t>>>;
107 :
108 : template class Option<Size>;
109 : template class Option<std::vector<Size>>;
110 : template class Option<std::vector<std::vector<Size>>>;
111 :
112 : template class Option<double>;
113 : template class Option<std::vector<double>>;
114 : template class Option<std::vector<std::vector<double>>>;
115 :
116 : template class Option<std::string>;
117 : template class Option<std::vector<std::string>>;
118 : template class Option<std::vector<std::vector<std::string>>>;
119 :
120 : template class Option<VariableName>;
121 : template class Option<std::vector<VariableName>>;
122 : template class Option<std::vector<std::vector<VariableName>>>;
123 :
124 : template class Option<TensorShape>;
125 : template class Option<std::vector<TensorShape>>;
126 : template class Option<std::vector<std::vector<TensorShape>>>;
127 :
128 : template class Option<TensorName<ATensor>>;
129 : template class Option<std::vector<TensorName<ATensor>>>;
130 : template class Option<std::vector<std::vector<TensorName<ATensor>>>>;
131 :
132 : #define INSTANTIATE_TENSORNAME(T) \
133 : template class Option<TensorName<T>>; \
134 : template class Option<std::vector<TensorName<T>>>; \
135 : template class Option<std::vector<std::vector<TensorName<T>>>>
136 : FOR_ALL_TENSORBASE(INSTANTIATE_TENSORNAME);
137 :
138 : template class Option<Device>;
139 : template class Option<std::vector<Device>>;
140 : template class Option<std::vector<std::vector<Device>>>;
141 :
142 : // Special instantiations
143 : template class Option<EnumSelection>;
144 : template class Option<MultiEnumSelection>;
145 : template class Option<Factory *>;
146 : template class Option<std::shared_ptr<Settings>>;
147 : template class Option<NEML2Object *>;
148 :
149 : namespace details
150 : {
151 : template <>
152 : void
153 0 : _print_helper(std::ostream & os, const char * option)
154 : {
155 0 : os << static_cast<int>(*option);
156 0 : }
157 :
158 : template <>
159 : void
160 0 : _print_helper(std::ostream & os, const unsigned char * option)
161 : {
162 0 : os << static_cast<int>(*option);
163 0 : }
164 :
165 : template <>
166 : void
167 0 : _print_helper(std::ostream & os, const std::vector<bool> * option)
168 : {
169 0 : for (const auto p : *option)
170 0 : os << static_cast<bool>(p) << " ";
171 0 : }
172 : } // namespace details
173 : } // namespace neml2
|