CASM  1.1.0
A Clusters Approach to Statistical Mechanics
jsonStruc.hh
Go to the documentation of this file.
1 #ifndef JSONSTRUC_HH
2 #define JSONSTRUC_HH
3 
4 #include <string>
5 #include <vector>
6 
14 
15 namespace CASM {
20 // TODO: Stop doing using
21 using xtal::BasicStructure;
22 using xtal::COORD_MODE;
23 using xtal::Coordinate;
24 using xtal::Lattice;
25 using xtal::Site;
26 
28 template <bool IsConst>
30  public:
33  const std::string &_prefix = std::string())
34  : m_struc_ptr(&_struc), m_prefix(_prefix){};
35 
36  jsonParser &to_json(jsonParser &json) const;
37  void from_json(const jsonParser &json) const {
38  _from_json(*m_struc_ptr, json);
39  }
40 
41  private:
42  void _from_json(const BasicStructure &struc, const jsonParser &json) const {
43  std::cerr
44  << "WARNING: Attempting to populate a const Structure/BasicStructure "
45  "from a JSON object. This is not allowed.\n";
46  }
47 
48  void _from_json(BasicStructure &struc, const jsonParser &json) const;
49 
51  std::string m_prefix;
52 };
53 
54 template <bool IsConst>
56  if (COORD_MODE::IS_FRAC())
57  json["coord_mode"] = "direct";
58  else
59  json["coord_mode"] = "cartesian";
60 
61  StrucType &struc(*m_struc_ptr);
62  std::map<std::string, std::vector<Site> > site_map;
63  for (Index i = 0; i < struc.basis().size(); i++)
64  site_map[struc.basis()[i].occ_name()].push_back(struc.basis()[i]);
65 
66  json[m_prefix + "basis"].put_array();
67  json["atoms_per_type"].put_array();
68  json["atom_type"].put_array();
69  json[m_prefix + "lattice"] = struc.lattice();
70  auto it = site_map.cbegin(), end_it = site_map.cend();
71  for (; it != end_it; ++it) {
72  json["atoms_per_type"].push_back(it->second.size());
73  json["atom_type"].push_back(it->first);
74  auto it2 = it->second.cbegin(), end_it2 = it->second.cend();
75  for (; it2 != end_it2; ++it2) json[m_prefix + "basis"].push_back(*it2);
76  }
77  return json;
78 }
79 
80 template <bool IsConst>
82  BasicStructure &struc, const jsonParser &json) const {
83  struc.set_basis(std::vector<xtal::Site>());
84  try {
85  std::string tstr;
86  CASM::from_json(tstr, json["coord_mode"]);
87 
88  COORD_TYPE mode = CART;
89  if (tstr == "direct" || tstr == "Direct") mode = FRAC;
90 
91  Lattice tlat;
92  CASM::from_json(tlat, json[m_prefix + "lattice"], struc.lattice().tol());
93  struc.set_lattice(tlat, FRAC);
94  Index l = 0;
95 
96  Eigen::Vector3d tvec;
97  for (Index i = 0; i < json["atoms_per_type"].size(); i++) {
98  CASM::from_json(tstr, json["atom_type"][i]);
99  for (Index j = 0; j < json["atoms_per_type"][i].get<Index>(); j++) {
100  CASM::from_json(tvec, json[m_prefix + "basis"][l++]);
101  struc.push_back(Site(Coordinate(tvec, struc.lattice(), mode), tstr));
102  }
103  }
104  } catch (const std::exception &ex) {
105  throw std::runtime_error(
106  std::string("Unable to parse Structure/BasicStructure from JSON "
107  "object. One or more tags were improperly specified:\n") +
108  ex.what());
109  }
110  return;
111 }
112 
113 template <bool IsConst>
115  const jsonParser &json) {
116  jstruc.from_json(json);
117 }
118 
119 template <bool IsConst>
121  jsonParser &json) {
122  return jstruc.to_json(json);
123 }
124 
126  const std::string &prefix) {
127  return SimpleJSonSiteStructure<true>(struc, prefix);
128 }
129 
131  const std::string &prefix) {
132  return SimpleJSonSiteStructure<false>(struc, prefix);
133 }
134 
136 } // namespace CASM
137 
138 #endif
Used to construct a BasicStructure from a 'properties.calc.json' object.
Definition: jsonStruc.hh:29
CASM_TMP::ConstSwitch< IsConst, BasicStructure > StrucType
Definition: jsonStruc.hh:31
SimpleJSonSiteStructure(StrucType &_struc, const std::string &_prefix=std::string())
Definition: jsonStruc.hh:32
void from_json(const jsonParser &json) const
Definition: jsonStruc.hh:37
jsonParser & to_json(jsonParser &json) const
Definition: jsonStruc.hh:55
void _from_json(const BasicStructure &struc, const jsonParser &json) const
Definition: jsonStruc.hh:42
size_type size() const
Definition: jsonParser.cc:487
jsonParser & put_array()
Puts new empty JSON array.
Definition: jsonParser.hh:362
const_iterator cbegin() const
Returns const_iterator to beginning of JSON object or JSON array.
Definition: jsonParser.cc:510
BasicStructure specifies the lattice and atomic basis of a crystal.
void set_lattice(const Lattice &lattice, COORD_TYPE mode)
std::vector< Site > & set_basis()
const Lattice & lattice() const
void push_back(Site const &_site, COORD_TYPE mode=CART)
Manually set the basis sites.
static bool IS_FRAC()
Static method to check if mode is FRAC (call using COORD_MODE::IS_FRAC() )
Represents cartesian and fractional coordinates.
Definition: Coordinate.hh:34
double tol() const
Definition: Lattice.hh:195
jsonParser & push_back(const T &value, Args &&... args)
Definition: jsonParser.hh:684
T get(Args &&... args) const
Get data from json, using one of several alternatives.
Definition: jsonParser.hh:716
typename std::conditional< IsConst, const T, T >::type ConstSwitch
Definition: CASM_TMP.hh:78
Main CASM namespace.
Definition: APICommand.hh:8
SimpleJSonSiteStructure< true > simple_json(const BasicStructure &struc, const std::string &prefix)
Definition: jsonStruc.hh:125
jsonParser & to_json(const ClexDescription &desc, jsonParser &json)
COORD_TYPE
Definition: enum.hh:6
const COORD_TYPE FRAC
Definition: enum.hh:8
void from_json(ClexDescription &desc, const jsonParser &json)
const COORD_TYPE CART
Definition: enum.hh:9
INDEX_TYPE Index
For long integer indexing:
Definition: definitions.hh:39