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/Parser.h"
28 : #include "neml2/base/LabeledAxisAccessor.h"
29 :
30 : namespace neml2
31 : {
32 : const std::vector<std::string> Parser::sections = {
33 : "Tensors", "Solvers", "Data", "Models", "Drivers", "Schedulers"};
34 :
35 : namespace utils
36 : {
37 : template <>
38 : bool
39 268 : parse_(bool & val, const std::string & raw_str)
40 : {
41 268 : std::string str_val;
42 268 : auto success = parse_<std::string>(str_val, raw_str);
43 268 : if (!success)
44 0 : return false;
45 :
46 268 : if (str_val == "true")
47 : {
48 145 : val = true;
49 145 : return true;
50 : }
51 :
52 123 : if (str_val == "false")
53 : {
54 122 : val = false;
55 122 : return true;
56 : }
57 :
58 1 : return false;
59 268 : }
60 :
61 : template <>
62 : bool
63 52 : parse_vector_(std::vector<bool> & vals, const std::string & raw_str)
64 : {
65 52 : auto tokens = split(raw_str, " \t\n\v\f\r");
66 52 : vals.resize(tokens.size());
67 196 : for (std::size_t i = 0; i < tokens.size(); i++)
68 : {
69 144 : bool val = false;
70 144 : auto success = parse_<bool>(val, tokens[i]);
71 144 : if (!success)
72 0 : return false;
73 144 : vals[i] = val;
74 : }
75 52 : return true;
76 52 : }
77 :
78 : template <>
79 : bool
80 1018 : parse_(VariableName & val, const std::string & raw_str)
81 : {
82 1018 : auto tokens = split(raw_str, "/ \t\n\v\f\r");
83 1018 : val = VariableName(tokens);
84 1018 : return true;
85 1018 : }
86 :
87 : template <>
88 : bool
89 1579 : parse_(TensorShape & val, const std::string & raw_str)
90 : {
91 1579 : if (!start_with(raw_str, "(") || !end_with(raw_str, ")"))
92 1 : return false;
93 :
94 3156 : auto inner = trim(raw_str, "() \t\n\v\f\r");
95 1578 : auto tokens = split(inner, ", \t\n\v\f\r");
96 :
97 1578 : val.resize(tokens.size());
98 4616 : for (std::size_t i = 0; i < tokens.size(); i++)
99 : {
100 3038 : auto success = parse_<Size>(val[i], tokens[i]);
101 3038 : if (!success)
102 0 : return false;
103 : }
104 1578 : return true;
105 1578 : }
106 :
107 : template <>
108 : Device
109 0 : parse(const std::string & raw_str)
110 : {
111 0 : return Device(parse<std::string>(raw_str));
112 : }
113 :
114 : template <>
115 : bool
116 90 : parse_(Device & val, const std::string & raw_str)
117 : {
118 90 : const auto str_val = parse<std::string>(raw_str);
119 : try
120 : {
121 90 : val = Device(str_val);
122 90 : return true;
123 : }
124 0 : catch (const std::exception & e)
125 : {
126 : std::cerr << "Failed to parse '" << raw_str << "' as a device. Error message:\n"
127 0 : << e.what() << std::endl;
128 0 : return false;
129 0 : }
130 90 : }
131 : } // namespace utils
132 : } // namespace neml2
|