CASM  1.1.0
A Clusters Approach to Statistical Mechanics
json_io.cc
Go to the documentation of this file.
1 // #include "casm/app/enum/io/json_io.hh"
2 // #include "casm/casm_io/container/json_io.hh"
3 // #include "casm/casm_io/json/InputParser_impl.hh"
4 // #include "casm/misc/CASM_Eigen_math.hh"
5 //
6 // namespace CASM {
7 //
8 // /// Parse a number or array value and return a vector
9 // ///
10 // /// \param parser Generic KwargsParser
11 // /// \param attribute_name std::string containing the name of the attribute
12 // to be parsed
13 // /// \param dimension If a number is given, return a constant vector of this
14 // dimension and the
15 // /// given value.
16 // /// \param default_value Optional, pointer to default value to return if
17 // attribute does not
18 // /// exist or is null. If `default_value==nullptr`, the attribute is
19 // a required input and an
20 // /// error will be inserted.
21 // ///
22 // /// Options:
23 // /// - If attribute is not present or null and `default_value!=nullptr`:
24 // return *default_value;
25 // /// - If attribute is not present or null and `default_value==nullptr`:
26 // insert error message and return Eigen::VectorXd{}
27 // /// - If attribute is number: return a Eigen::VectorXd::Constant(dimension,
28 // number)
29 // /// - If attribute is an array: return array as a Eigen::VectorXd
30 // /// - If attribute is any other type: insert error message and return
31 // Eigen::VectorXd{} Eigen::VectorXd parse_vector_from_number_or_array(
32 // KwargsParser &parser,
33 // std::string attribute_name,
34 // Index dimension,
35 // Eigen::VectorXd const *default_value) {
36 //
37 // Eigen::VectorXd result;
38 // if(!parser.self.contains(attribute_name) ||
39 // parser.self[attribute_name].is_null()) {
40 // if(default_value != nullptr) {
41 // return *default_value;
42 // }
43 // else {
44 // parser.require(result, attribute_name);
45 // return result;
46 // }
47 // }
48 // if(parser.self[attribute_name].is_number()) {
49 // return Eigen::VectorXd::Constant(dimension,
50 // parser.self[attribute_name].get<double>());
51 // }
52 // else if(parser.self[attribute_name].is_array()) {
53 // parser.optional(result, attribute_name);
54 // if(result.size() != dimension) {
55 // std::stringstream msg;
56 // std::string option_type = (default_value != nullptr) ? "(optional)" :
57 // "(required)"; msg << "Error: \"" << attribute_name << "\" " <<
58 // option_type
59 // << " must be a number or array of number. Expected array of size
60 // " << dimension << ".";
61 // parser.error.insert(msg.str());
62 // }
63 // return result;
64 // }
65 // else {
66 // std::stringstream msg;
67 // std::string option_type = (default_value != nullptr) ? "(optional)" :
68 // "(required)"; msg << "Error: \"" << attribute_name << "\" " <<
69 // option_type
70 // << " must be a number or array of number.";
71 // parser.error.insert(msg.str());
72 // return result;
73 // }
74 // }
75 //
76 // /// Parse DoF space axes from a JSON object
77 // ///
78 // /// Expected format: (this parses "axes" only)
79 // ///
80 // /// Full space case:
81 // /// {
82 // /// "axes": {
83 // /// "q1": [q10, q11, q12, ...], /// note: starts with "q1",
84 // not "q0"
85 // /// "q2": [q20, q21, q22, ...],
86 // /// "q3": [q30, q31, q32, ...],
87 // /// ...
88 // /// },
89 // /// "min": [q1min, q2min, q3min, ...], /// optional, default is
90 // zeros vector
91 // /// "max": [q1max, q2max, q3max, ...], /// required
92 // /// "increment": [q1inc, q2inc, q3inc, ...], /// required
93 // /// }
94 // ///
95 // /// Subspace case:
96 // /// - if some qi (i in range [1, dof_space_dimension]) are missing, then
97 // use the subspace
98 // /// specified by the axes that are provided
99 // /// - min.size() / max.size() / increment.size() must equal axes.size()
100 // /// - example:
101 // /// {
102 // /// "axes": {
103 // /// "q1": [q10, q11, q12, ...],
104 // /// "q2": [q20, q21, q22, ...],
105 // /// "q3": [q30, q31, q32, ...],
106 // /// "q5": [q50, q51, q52, ...],
107 // /// },
108 // /// "min": [q1min, q2min, q3min, q5min], /// optional, default is
109 // zeros vector
110 // /// "max": [q1max, q2max, q3max, q5max], /// required
111 // /// "increment": [q1inc, q2inc, q3inc, q5inc], /// required
112 // /// }
113 // ///
114 // /// notes:
115 // /// - if total dof_space_dimension is double digit, then use "q01", "q02",
116 // ... etc.
117 // /// - if total dof_space_dimension is triple digit, then use "q001",
118 // "q002", ... etc.
119 // ///
120 // void parse_axes_from_object(KwargsParser &parser,
121 // Eigen::MatrixXd &axes,
122 // Index dof_space_dimension) {
123 //
124 // Eigen::MatrixXd inaxes = Eigen::MatrixXd::Zero(dof_space_dimension,
125 // dof_space_dimension);
126 //
127 // std::set<Index> found;
128 // for(Index i = 0; i < dof_space_dimension; ++i) {
129 // std::string axis_name = "q" + to_sequential_string(i + 1,
130 // dof_space_dimension); fs::path location = fs::path {"axes"} /
131 // axis_name; auto value_ptr =
132 // parser.optional_at<Eigen::VectorXd>(location);
133 //
134 // if(value_ptr != nullptr) {
135 // found.insert(i);
136 // Eigen::VectorXd const &value = *value_ptr;
137 // if(value.size() != dof_space_dimension) {
138 // std::stringstream msg;
139 // msg << "Error reading axis vector \"" << axis_name
140 // << "\": expected size=" << dof_space_dimension
141 // << " found size=" << value.size();
142 // parser.error.insert(msg.str());
143 // }
144 // else {
145 // inaxes.col(found.size()) = value;
146 // }
147 // }
148 // }
149 //
150 // Index subspace_dimension = found.size();
151 // axes = inaxes.leftCols(subspace_dimension);
152 //
153 // }
154 //
155 // /// Read "axes" from a row-vector JSON matrix, store in `axes` argument as
156 // a column vector matrix void parse_axes_from_array(KwargsParser &parser,
157 // Eigen::MatrixXd &axes,
158 // Index dof_space_dimension) {
159 // Eigen::MatrixXd row_vector_axes;
160 // parser.require(row_vector_axes, "axes");
161 // axes = row_vector_axes.transpose();
162 //
163 // // check axes dimensions:
164 // if(axes.rows() != dof_space_dimension) {
165 // // Note: message "columns" refers to JSON input axes, transpose of
166 // params.axes std::stringstream msg; msg << "Number of columns of
167 // \"axes\" must be equal to site DoF space dimension ("
168 // << dof_space_dimension << "). Size as parsed: " << axes.rows();
169 // parser.error.insert(msg.str());
170 // }
171 // if(axes.cols() > dof_space_dimension) {
172 // // Note: message "rows" refers to JSON input axes, transpose of
173 // params.axes std::stringstream msg; msg << "Number of coordinate axes
174 // (number of rows of \"axes\") must be less than or equal to "
175 // "site DoF space dimension (" << dof_space_dimension << "). Number
176 // of axes parsed: "
177 // << axes.cols();
178 // parser.error.insert(msg.str());
179 // }
180 // }
181 //
182 //
183 //
184 // /// Parse "axes", "min", "max", and "increment" for continuous DoF
185 // enumeration
186 // ///
187 // /// Accepts a JSON object of axes or a row vector matrix of axes. May be
188 // rank deficient. For JSON
189 // /// object format see `parse_axes_from_object`
190 // ///
191 // /// \param parser JSON to be parsed
192 // /// \param axes Set to be column vector matrix of axes
193 // /// \param min_val Set to be minimum counter value. Must be same size as
194 // axes.cols().
195 // /// \param max_val Set to be maximum counter value. Must be same size as
196 // axes.cols().
197 // /// \param inc_val Set to be counter increment value. Must be same size as
198 // axes.cols().
199 // /// \param dof_space_dimension Full DoF space dimension. If no `"axes"` in
200 // the JSON, the default
201 // /// axes are identity matrix of size dof_space_dimension. Errors are
202 // inserted into parser
203 // /// if axes vectors are not of length equal to dof_space_dimension.
204 // ///
205 // void parse_dof_space_axes(KwargsParser &parser,
206 // Eigen::MatrixXd &axes,
207 // Eigen::VectorXd &min_val,
208 // Eigen::VectorXd &max_val,
209 // Eigen::VectorXd &inc_val,
210 // Index dof_space_dimension) {
211 //
212 // // "axes" -> axes
213 // if(!parser.self.contains("axes")) {
214 // axes = Eigen::MatrixXd::Identity(dof_space_dimension,
215 // dof_space_dimension);
216 // }
217 // else if(parser.self.contains("axes") && parser.self["axes"].is_obj()) {
218 // parse_axes_from_object(parser, axes, dof_space_dimension);
219 // }
220 // else if(parser.self.contains("axes") && parser.self["axes"].is_array()) {
221 // parse_axes_from_array(parser, axes, dof_space_dimension);
222 // }
223 // else {
224 // std::stringstream msg;
225 // msg << "The \"axes\" must be a JSON object of axis vectors (named
226 // \"q1\", \"q2\", etc.), or a row-vector matrix";
227 // parser.error.insert(msg.str());
228 // }
229 //
230 // // "min" -> min_val (number or array of number, else zeros vector)
231 // Eigen::VectorXd default_value = Eigen::VectorXd::Zero(axes.cols());
232 // min_val = parse_vector_from_number_or_array(parser, "min", axes.cols(),
233 // &default_value);
234 //
235 // // "max" -> max_val (number or array of number, required)
236 // max_val = parse_vector_from_number_or_array(parser, "max", axes.cols());
237 //
238 // // "increment" -> inc_val (number or array of number, required)
239 // inc_val = parse_vector_from_number_or_array(parser, "increment",
240 // axes.cols());
241 // }
242 //
243 //
244 // }