CASM  1.1.0
A Clusters Approach to Statistical Mechanics
DoFSetIO.cc
Go to the documentation of this file.
2 
6 
7 namespace CASM {
8 jsonParser &to_json(xtal::DoFSet const &_dof, jsonParser &json) {
9  json["basis"] = _dof.basis().transpose();
10  json["axis_names"] = _dof.component_names();
11  // json["traits"] = _dof.traits().name();
12  return json;
13 }
14 
16  to_json(static_cast<const xtal::DoFSet &>(_dof), json);
17  if (_dof.excluded_occupants().size()) {
18  json["excluded_occupants"] = _dof.excluded_occupants();
19  }
20  return json;
21 }
22 
24  const jsonParser &json, AnisoValTraits const &traits) {
25  if (json.contains("basis") != json.contains("axis_names")) {
26  std::stringstream msg;
27  msg << "Error reading DoF input: if either is present, 'axis_names' and "
28  "'basis' must both be present";
29  throw std::runtime_error(msg.str());
30  }
31 
32  if (!json.contains("axis_names")) {
33  return xtal::DoFSet(traits);
34  }
35 
36  Eigen::MatrixXd row_vector_basis;
37  json["basis"].get(row_vector_basis);
38  Eigen::MatrixXd basis = row_vector_basis.transpose();
39 
40  if (row_vector_basis.cols() != traits.dim()) {
41  throw std::runtime_error("Cannot construct DoFSet of type " +
42  traits.name() + ", basis vectors are of size " +
43  std::to_string(row_vector_basis.cols()) +
44  " but must be size " +
45  std::to_string(traits.dim()));
46  }
47 
48  if (row_vector_basis.rows() > traits.dim()) {
49  throw std::runtime_error("Cannot construct DoFSet of type " +
50  traits.name() + ", found " +
51  std::to_string(row_vector_basis.rows()) +
52  " basis vectors, but must be " +
53  std::to_string(traits.dim()) + " or fewer.");
54  }
55 
56  std::vector<std::string> component_names;
57  json["axis_names"].get(component_names);
58 
59  if (row_vector_basis.rows() != component_names.size()) {
60  throw std::runtime_error(
61  "Cannot construct DoFSet of type " + traits.name() + ", found " +
62  std::to_string(row_vector_basis.rows()) +
63  " basis vectors, but the number of 'axis_names' is " +
64  std::to_string(traits.dim()) + ". These numbers must be equal.");
65  }
66 
67  return xtal::DoFSet(traits, component_names, basis);
68 }
69 
71  const jsonParser &json, AnisoValTraits const &traits) {
72  std::unordered_set<std::string> excluded_occupants;
73  json.get_if(excluded_occupants, "excluded_occupants");
74 
75  return xtal::SiteDoFSet(json.get<xtal::DoFSet>(traits), excluded_occupants);
76 }
77 
78 } // namespace CASM
Specifies traits of (possibly) anisotropic crystal properties.
bool contains(const std::string &name) const
Return true if JSON object contains 'name'.
Definition: jsonParser.cc:601
Eigen::MatrixXd const & basis() const
Matrix that relates DoFSet variables to a conventional coordiante system.
Definition: DoFSet.hh:67
const std::vector< std::string > & component_names() const
Returns the names of each of the component axes.
Definition: DoFSet.hh:55
const std::unordered_set< std::string > & excluded_occupants() const
Return all occupants that the DoFSet should not be applied to.
Definition: DoFSet.hh:121
std::string to_string(ENUM val)
Return string representation of enum class.
Definition: io_traits.hh:172
bool get_if(T &t, const std::string &key, Args &&... args) const
Definition: jsonParser.hh:740
T get(Args &&... args) const
Get data from json, using one of several alternatives.
Definition: jsonParser.hh:716
Main CASM namespace.
Definition: APICommand.hh:8
Eigen::MatrixXd MatrixXd
jsonParser & to_json(const ClexDescription &desc, jsonParser &json)
static ReturnType from_json(const jsonParser &json)
Default from_json is equivalent to.
Definition: jsonParser.hh:551