CASM  1.1.0
A Clusters Approach to Statistical Mechanics
ConfigDoF_json_io.cc
Go to the documentation of this file.
2 
3 #include "casm/casm_io/Log.hh"
7 #include "casm/clex/ConfigDoF.hh"
10 
11 namespace CASM {
12 
13 std::unique_ptr<ConfigDoF> jsonMake<ConfigDoF>::make_from_json(
14  const jsonParser &json, Structure const &prim, Index volume) {
15  auto result_ptr = make_unique_configdof(prim, volume);
16  from_json(*result_ptr, json);
17  return result_ptr;
18 }
19 
21  Structure const &prim,
22  Index volume) {
23  ConfigDoF result = make_configdof(prim, volume);
24  CASM::from_json(result, json);
25  return result;
26 }
27 
123 jsonParser &to_json(const ConfigDoF &configdof, jsonParser &json) {
124  json = jsonParser::object();
125  if (configdof.occupation().size()) {
126  to_json_array(configdof.occupation(), json["occ"]);
127  }
128  if (!configdof.local_dofs().empty()) {
129  for (auto const &local_dof : configdof.local_dofs()) {
130  to_json(local_dof.second.standard_values().transpose(),
131  json["local_dofs"][local_dof.first]["values"]);
132  }
133  }
134  if (!configdof.global_dofs().empty()) {
135  for (auto const &global_dof : configdof.global_dofs()) {
136  to_json_array(global_dof.second.standard_values(),
137  json["global_dofs"][global_dof.first]["values"]);
138  }
139  }
140 
141  return json;
142 }
143 
145 void from_json(ConfigDoF &configdof, const jsonParser &json) {
146  auto &log = CASM::log();
147  ParentInputParser parser{json};
148  std::runtime_error error_if_invalid{
149  "Error reading ConfigDoF from JSON input"};
150 
151  Eigen::VectorXi occ;
152  // For Backwards compatibility
153  if (parser.self.contains("occupation")) {
154  parser.require(occ, "occupation");
155  } else {
156  parser.require(occ, "occ");
157  try {
158  configdof.set_occupation(occ);
159  } catch (std::exception const &e) {
160  parser.insert_error("occ", e.what());
161  }
162  }
163 
164  for (auto const &local_dof : configdof.local_dofs()) {
165  Eigen::MatrixXd tvalues;
166  fs::path values_path = fs::path{"local_dofs"} / local_dof.first / "values";
167  parser.require(tvalues, values_path);
168  try {
169  configdof.local_dof(local_dof.first)
170  .from_standard_values(tvalues.transpose());
171  } catch (std::exception const &e) {
172  parser.insert_error(values_path, e.what());
173  }
174  }
175 
176  for (auto const &global_dof : configdof.global_dofs()) {
177  Eigen::VectorXd tvalues;
178  fs::path values_path =
179  fs::path{"global_dofs"} / global_dof.first / "values";
180  parser.require(tvalues, values_path);
181  try {
182  configdof.global_dof(global_dof.first).from_standard_values(tvalues);
183  } catch (std::exception const &e) {
184  parser.insert_error(values_path, e.what());
185  }
186  }
187 
188  report_and_throw_if_invalid(parser, log, error_if_invalid);
189 }
190 
191 } // namespace CASM
std::map< DoFKey, LocalContinuousConfigDoFValues > const & local_dofs() const
Definition: ConfigDoF.cc:119
std::map< DoFKey, GlobalContinuousConfigDoFValues > const & global_dofs() const
Definition: ConfigDoF.cc:65
GlobalContinuousConfigDoFValues const & global_dof(DoFKey const &_key) const
Definition: ConfigDoF.cc:69
Eigen::VectorXi const & occupation() const
Const reference occupation values.
Definition: ConfigDoF.cc:56
LocalContinuousConfigDoFValues const & local_dof(DoFKey const &_key) const
Definition: ConfigDoF.cc:124
void set_occupation(Eigen::Ref< const Eigen::VectorXi > const &_occupation)
Set occupation values.
Definition: ConfigDoF.cc:43
void from_standard_values(Eigen::Ref< const Eigen::MatrixXd > const &_standard_values)
Set global DoF values from standard DoF values.
void from_standard_values(Eigen::Ref< const Eigen::MatrixXd > const &_standard_values)
Set local DoF values from standard DoF values.
Structure specifies the lattice and atomic basis of a crystal.
Definition: Structure.hh:30
static jsonParser object()
Returns an empty json object.
Definition: jsonParser.hh:395
CASM::jsonParser & to_json_array(const Eigen::MatrixBase< Derived > &value, CASM::jsonParser &json)
Write Eigen Matrix with 1 row or 1 column to JSON array.
Definition: json_io.hh:313
GenericScelFormatter< double > volume()
Definition: SupercellIO.cc:258
Main CASM namespace.
Definition: APICommand.hh:8
Eigen::MatrixXd MatrixXd
jsonParser & to_json(const ClexDescription &desc, jsonParser &json)
Log & log()
Definition: Log.hh:424
std::unique_ptr< ConfigDoF > make_unique_configdof(Structure const &prim, Index volume)
Construct zero-valued std::unique_ptr<ConfigDoF>
void from_json(ClexDescription &desc, const jsonParser &json)
ConfigDoF make_configdof(Structure const &prim, Index volume)
Construct zero-valued ConfigDoF.
void report_and_throw_if_invalid(KwargsParser const &parser, Log &log, ErrorType error)
INDEX_TYPE Index
For long integer indexing:
Definition: definitions.hh:39
Eigen::VectorXd VectorXd
static ReturnType from_json(const jsonParser &json)
Default from_json is equivalent to.
Definition: jsonParser.hh:551
static std::unique_ptr< ValueType > make_from_json(const jsonParser &json)
Default make_from_json is equivalent to.
Definition: jsonParser.hh:567