CASM
AClustersApproachtoStatisticalMechanics
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
jsonStruc.hh
Go to the documentation of this file.
1 #ifndef JSONSTRUC_HH
2 #define JSONSTRUC_HH
3 
8 
9 
10 namespace CASM {
11 
16  template< bool IsConst >
18  public:
20  SimpleJSonSiteStructure(StrucType &_struc, const std::string &_prefix = std::string()) :
21  m_struc_ptr(&_struc), m_prefix(_prefix) {};
22 
23  jsonParser &to_json(jsonParser &json) const;
24  void from_json(const jsonParser &json) const {
25  _from_json(*m_struc_ptr, json);
26  }
27  private:
28  void _from_json(const BasicStructure<Site> &struc, const jsonParser &json) const {
29  std::cerr << "WARNING: Attempting to populate a const Structure/BasicStructure from a JSON object. This is not allowed.\n";
30  }
31 
32  void _from_json(BasicStructure<Site> &struc, const jsonParser &json) const;
33 
34  StrucType *m_struc_ptr;
35  std::string m_prefix;
36  };
37 
38 
39  template< bool IsConst >
42  json["coord_mode"] = "direct";
43  else
44  json["coord_mode"] = "cartesian";
45 
46  StrucType &struc(*m_struc_ptr);
47  std::map<std::string, std::vector<Site> > site_map;
48  for(Index i = 0; i < struc.basis.size(); i++)
49  site_map[struc.basis[i].occ_name()].push_back(struc.basis[i]);
50 
51  json[m_prefix + "basis"].put_array();
52  json["atoms_per_type"].put_array();
53  json["atom_type"].put_array();
54  json[m_prefix + "lattice"] = struc.lattice();
55  auto it = site_map.cbegin(), end_it = site_map.cend();
56  for(; it != end_it; ++it) {
57  json["atoms_per_type"].push_back(it->second.size());
58  json["atom_type"].push_back(it->first);
59  auto it2 = it->second.cbegin(), end_it2 = it->second.cend();
60  for(; it2 != end_it2; ++it2)
61  json[m_prefix + "basis"].push_back(*it2);
62  }
63  return json;
64  }
65 
66  template< bool IsConst >
68  struc.basis.clear();
69  struc.reset();
70  try {
71  std::string tstr;
72  CASM::from_json(tstr, json["coord_mode"]);
73 
74  COORD_TYPE mode = CART;
75  if(tstr == "direct" || tstr == "Direct")
76  mode = FRAC;
77 
78  Lattice tlat;
79  CASM::from_json(tlat, json[m_prefix + "lattice"]);
80  struc.set_lattice(tlat, FRAC);
81  Index l = 0;
82 
83  Eigen::Vector3d tvec;
84  for(Index i = 0; i < json["atoms_per_type"].size(); i++) {
85  CASM::from_json(tstr, json["atom_type"][i]);
86  for(Index j = 0; j < json["atoms_per_type"][i].get<Index>(); j++) {
87  CASM::from_json(tvec, json[m_prefix + "basis"][l++]);
88  struc.basis.push_back(Site(Coordinate(tvec, struc.lattice(), mode), tstr));
89  }
90  }
91  }
92  catch(const std::exception &ex) {
93  throw std::runtime_error(std::string("Unable to parse Structure/BasicStructure from JSON object. One or more tags were improperly specified:\n") + ex.what());
94  }
95  struc.update();
96 
97  }
98 
99  template< bool IsConst >
100  void from_json(const SimpleJSonSiteStructure<IsConst> &jstruc, const jsonParser &json) {
101  jstruc.from_json(json);
102  }
103 
104  template< bool IsConst >
106  return jstruc.to_json(json);
107  }
108 
109  inline
110  SimpleJSonSiteStructure<true> simple_json(const BasicStructure<Site> &struc, const std::string &prefix) {
111  return SimpleJSonSiteStructure<true>(struc, prefix);
112  }
113 
114  inline
116  return SimpleJSonSiteStructure<false>(struc, prefix);
117  }
118 
120 }
121 
122 #endif
size_type size() const
Returns array size if *this is a JSON array, object size if *this is a JSON object, 1 otherwise.
Definition: jsonParser.cc:430
typename std::conditional< IsConst, const T, T >::type ConstSwitch
Definition: CASM_TMP.hh:93
void from_json(ClexDescription &desc, const jsonParser &json)
void push_back(const T &toPush)
Definition: Array.hh:513
jsonParser & to_json(const ClexDescription &desc, jsonParser &json)
Main CASM namespace.
Definition: complete.cpp:8
const Lattice & lattice() const
void set_lattice(const Lattice &lattice, COORD_TYPE mode)
T get(Args...args) const
Get data from json, using one of several alternatives.
Definition: jsonParser.hh:729
void clear()
Definition: Array.hh:216
Represents cartesian and fractional coordinates.
Definition: Coordinate.hh:34
EigenIndex Index
For long integer indexing:
Array< CoordType > basis
Lattice vectors that specifies periodicity of the crystal.
static bool IS_FRAC()
Static method to check if mode is FRAC (call using COORD_MODE::IS_FRAC() )
SimpleJSonSiteStructure< true > simple_json(const BasicStructure< Site > &struc, const std::string &prefix)
Definition: jsonStruc.hh:110
void from_json(const jsonParser &json) const
Definition: jsonStruc.hh:24
CASM_TMP::ConstSwitch< IsConst, BasicStructure< Site > > StrucType
Definition: jsonStruc.hh:19
SimpleJSonSiteStructure(StrucType &_struc, const std::string &_prefix=std::string())
Definition: jsonStruc.hh:20
jsonParser & push_back(const T &value)
Puts new valued element at end of array of any type T for which 'jsonParser& to_json( const T &value...
Definition: jsonParser.hh:696
void _from_json(const BasicStructure< Site > &struc, const jsonParser &json) const
Definition: jsonStruc.hh:28
const_iterator cbegin() const
Returns const_iterator to beginning of JSON object or JSON array.
Definition: jsonParser.cc:455
jsonParser & put_array()
Puts new empty JSON array.
Definition: jsonParser.hh:285
jsonParser & to_json(jsonParser &json) const
Definition: jsonStruc.hh:40