CASM  1.1.0
A Clusters Approach to Statistical Mechanics
IntegralCluster_json_io.cc
Go to the documentation of this file.
2 
3 #include "casm/casm_io/Log.hh"
15 
16 namespace CASM {
17 
32  json.put_obj();
33  ClusterInvariants invariants{clust};
34  if (invariants.displacement().size()) {
35  json["min_length"] = invariants.displacement().front();
36  json["max_length"] = invariants.displacement().back();
37  } else {
38  json["min_length"] = 0.0;
39  json["max_length"] = 0.0;
40  }
41  json["sites"].put_array(clust.begin(), clust.end());
42  return json;
43 }
44 
61 void from_json(IntegralCluster &clust, const jsonParser &json) {
63 }
64 
66  const jsonParser &json, Structure const &prim) {
67  jsonParser tmp{json}; // TODO: this shouldn't be necessary
68  InputParser<IntegralCluster> parser{tmp, prim};
69  std::stringstream ss;
70  ss << "Error: Invalid cluster JSON object";
71  report_and_throw_if_invalid(parser, err_log(), std::runtime_error{ss.str()});
72  return *parser.value;
73 }
74 
89 void parse(InputParser<IntegralCluster> &parser, Structure const &prim) {
90  std::string name;
91  const jsonParser &json = parser.self;
92  if (json.contains("sites")) {
93  name = "sites";
94  } else if (json.contains("prototype")) {
95  name = "prototype";
96  } else {
97  parser.error.insert(
98  "Error reading IntegralCluster from JSON: Expected 'sites' or "
99  "'prototype' containing a list of cluster site coordinates.");
100  return;
101  }
102 
103  double xtal_tol = prim.lattice().tol();
104  CASM::COORD_TYPE coord_type;
105  parser.optional_else(coord_type, "coordinate_mode", INTEGRAL);
106 
107  parser.value = notstd::make_unique<IntegralCluster>(prim);
108  auto &clust = *parser.value;
109 
110  if (coord_type == INTEGRAL) {
111  parser.require(clust.elements(), name);
112  } else {
113  std::vector<Eigen::Vector3d> coord_vec;
114  parser.require(coord_vec, name);
115 
116  try {
117  for (const auto &coord : coord_vec) {
118  xtal::Coordinate tcoord{coord, prim.lattice(), coord_type};
119  clust.elements().emplace_back(
120  xtal::UnitCellCoord::from_coordinate(prim, tcoord, xtal_tol));
121  }
122  } catch (std::exception &e) {
123  parser.error.insert("Error: could not read coordinates from '" + name +
124  "'");
125  return;
126  }
127  }
128  return;
129 }
130 } // namespace CASM
Stores cluster invariants: number of sites and site distances.
iterator end()
Iterator to the past-the-last element in the cluster.
iterator begin()
Iterator to first element in the cluster.
RequiredType optional_else(fs::path option, const RequiredType &_default, Args &&... args)
std::unique_ptr< RequiredType > require(fs::path option, Args &&... args)
std::unique_ptr< T > value
Definition: InputParser.hh:234
const PrimType & prim() const
Structure specifies the lattice and atomic basis of a crystal.
Definition: Structure.hh:30
const Lattice & lattice() const
Definition: Structure.hh:100
bool contains(const std::string &name) const
Return true if JSON object contains 'name'.
Definition: jsonParser.cc:601
jsonParser & put_obj()
Puts new empty JSON object.
Definition: jsonParser.hh:354
jsonParser & put_array()
Puts new empty JSON array.
Definition: jsonParser.hh:362
Represents cartesian and fractional coordinates.
Definition: Coordinate.hh:34
double tol() const
Definition: Lattice.hh:195
static UnitCellCoord from_coordinate(const PrimType &, const Coordinate &coord, double tol)
Main CASM namespace.
Definition: APICommand.hh:8
jsonParser & to_json(const ClexDescription &desc, jsonParser &json)
COORD_TYPE
Definition: enum.hh:6
GenericDatumFormatter< std::string, DataObject > name()
const COORD_TYPE INTEGRAL
Definition: enum.hh:10
void from_json(ClexDescription &desc, const jsonParser &json)
void report_and_throw_if_invalid(KwargsParser const &parser, Log &log, ErrorType error)
void parse(InputParser< ConfigEnumOptions > &parser, std::string method_name, PrimClex const &primclex, DataFormatterDictionary< Configuration > const &dict)
Log & err_log()
Definition: Log.hh:426
jsonParser const & self
Definition: InputParser.hh:81
std::set< std::string > error
Definition: Validator.hh:11
static ReturnType from_json(const jsonParser &json)
Default from_json is equivalent to.
Definition: jsonParser.hh:551