CASM  1.1.0
A Clusters Approach to Statistical Mechanics
ChemicalReference_json_io.cc
Go to the documentation of this file.
2 
7 
8 namespace CASM {
9 
13  to_json(ref_state.species_num, json);
14  to_json(ref_state.energy_per_species, json["energy_per_species"]);
15  return json;
16 }
17 
20 template <>
22  ChemicalReferenceState ref_state;
23  bool found_value = false;
24  for (auto it = json.begin(); it != json.end(); ++it) {
25  if (it.name() == "energy_per_species") {
26  found_value = true;
27  from_json(ref_state.energy_per_species, *it);
28  } else if (xtal::is_vacancy(it.name())) {
29  std::cerr << "Error reading chemical reference state: " << json
30  << std::endl;
31  throw std::runtime_error(
32  "Error reading chemical reference: Input should not include "
33  "vacancies");
34  } else {
35  from_json(ref_state.species_num[it.name()], *it);
36  }
37  }
38  if (!found_value) {
39  std::cerr << "Error reading chemical reference state: " << json
40  << std::endl;
41  throw std::runtime_error(
42  "Error reading chemical reference: No 'energy_per_species' found");
43  }
44  return ref_state;
45 }
46 
49 void from_json(ChemicalReferenceState &ref_state, const jsonParser &json) {
50  ref_state = from_json<ChemicalReferenceState>(json);
51 }
52 
53 // --- HyperPlaneReference -------------------
54 
56  json.put_obj();
57  to_json(ref.global(), json["global"]);
58  to_json(ref.supercell(), json["supercell"]);
59  to_json(ref.supercell(), json["config"]);
60  return json;
61 }
62 
65  HyperPlaneReference ref(CASM::from_json<Eigen::VectorXd>(json["global"]), f);
66  CASM::from_json(ref.supercell(), json["supercell"]);
67  CASM::from_json(ref.config(), json["config"]);
68  return ref;
69 }
70 
74 }
75 
76 // --- ChemicalReference -------------------
77 
105  json.put_obj();
106 
107  json["species_order"] = xtal::struc_molecule_name(ref.prim());
108 
109  if (ref.global_ref_states().empty()) {
110  to_json(ref.global().transpose(), json["global"]);
111  json["global"].put_array();
112  for (int i = 0; i < ref.global().size(); ++i) {
113  json["global"].push_back(ref.global()(i));
114  }
115  } else {
116  to_json(ref.global_ref_states(), json["global"]);
117  }
118 
119  if (ref.supercell().size()) {
120  jsonParser &s_json = json["supercell"];
121  for (auto it = ref.supercell().begin(); it != ref.supercell().end(); ++it) {
122  auto res = ref.supercell_ref_states().find(it->first);
123  if (res == ref.supercell_ref_states().end()) {
124  s_json[it->first].put_array();
125  for (int i = 0; i < it->second.size(); ++i) {
126  s_json[it->first].push_back(it->second(i));
127  }
128  } else {
129  to_json(res->second, s_json[it->first]);
130  }
131  }
132  }
133 
134  if (ref.config().size()) {
135  jsonParser &c_json = json["config"];
136  for (auto it = ref.config().begin(); it != ref.config().end(); ++it) {
137  auto res = ref.config_ref_states().find(it->first);
138  if (res == ref.config_ref_states().end()) {
139  c_json[it->first].put_array();
140  for (int i = 0; i < it->second.size(); ++i) {
141  c_json[it->first].push_back(it->second(i));
142  }
143  } else {
144  to_json(res->second, c_json[it->first]);
145  }
146  }
147  }
148 
149  return json;
150 }
151 
176 std::pair<Eigen::VectorXd, std::vector<ChemicalReferenceState> >
178  jsonParser const &json) {
179  typedef std::pair<Eigen::VectorXd, std::vector<ChemicalReferenceState> >
180  ReturnType;
181 
182  ReturnType result;
183 
184  std::vector<std::string> struc_mol_name = xtal::struc_molecule_name(prim);
185 
186  // if: {"A": X, "C": X, "D": X} // expects all species in prim, except vacancy
187  if (json.is_obj()) {
188  for (auto it = json.begin(); it != json.end(); ++it) {
190  r.species_num[it.name()] = 1.0;
191  r.energy_per_species = it->get<double>();
192  result.second.push_back(r);
193  }
194  return result;
195  } else {
196  // if: [X, X, X, X]
197  if (json.begin()->is_number()) {
198  if (json.size() != struc_mol_name.size()) {
199  std::cerr << "received: " << json << std::endl;
200  std::cerr << "expected size: " << struc_mol_name.size() << std::endl;
201  throw std::runtime_error(
202  "Error in one_chemical_reference_from_json: Size mismatch with "
203  "vector input.");
204  }
205 
206  result.first = Eigen::VectorXd(json.size());
207  for (int i = 0; i < json.size(); ++i) {
208  result.first(i) = json[i].get<double>();
209  }
210  return result;
211  }
212 
213  // [
214  // {"A": 3.4, "C": 2.0, "energy_per_species": 2.0},
215  // {"B": 2.0, "energy_per_species": 4.0},
216  // {"C": 1.0, "energy_per_species": 3.0}
217  // ]
218  else {
219  std::for_each(json.begin(), json.end(), [&](const jsonParser &json) {
220  result.second.push_back(json.get<ChemicalReferenceState>());
221  });
222  return result;
223  }
224  }
225 }
226 
247  jsonParser const &json, xtal::BasicStructure const &prim, double tol) {
248  std::unique_ptr<ChemicalReference> ref;
249 
250  auto res = one_chemical_reference_from_json(prim, json["global"]);
251 
252  if (res.second.empty()) {
253  ref = notstd::make_unique<ChemicalReference>(prim, res.first);
254  } else {
255  ref = notstd::make_unique<ChemicalReference>(prim, res.second.begin(),
256  res.second.end(), tol);
257  }
258 
259  if (json.find("supercell") != json.end()) {
260  for (auto it = json["supercell"].begin(); it != json["supercell"].end();
261  ++it) {
262  auto res = one_chemical_reference_from_json(prim, *it);
263  if (res.second.empty()) {
264  ref->set_supercell(it.name(), res.first);
265  } else {
266  ref->set_supercell(it.name(), res.second.begin(), res.second.end(),
267  tol);
268  }
269  }
270  }
271 
272  if (json.find("config") != json.end()) {
273  for (auto it = json["config"].begin(); it != json["config"].end(); ++it) {
274  auto res = one_chemical_reference_from_json(prim, *it);
275  if (res.second.empty()) {
276  ref->set_config(it.name(), res.first);
277  } else {
278  ref->set_config(it.name(), res.second.begin(), res.second.end(), tol);
279  }
280  }
281  }
282 
283  return std::move(*ref);
284 }
285 
288  xtal::BasicStructure const &prim, double tol) {
290 }
291 
292 } // namespace CASM
std::function< Eigen::VectorXd(const Configuration &)> InputFunction
Definition: Reference.hh:71
Maps a Configuration to a scalar value via a hyperplane.
Definition: Reference.hh:198
iterator begin()
Returns const_iterator to beginning of JSON object or JSON array.
Definition: jsonParser.cc:497
iterator end()
Returns iterator to end of JSON object or JSON array.
Definition: jsonParser.cc:520
size_type size() const
Definition: jsonParser.cc:487
jsonParser & put_obj()
Puts new empty JSON object.
Definition: jsonParser.hh:354
iterator find(const std::string &name)
Return iterator to JSON object value with 'name'.
Definition: jsonParser.cc:543
jsonParser & put_array()
Puts new empty JSON array.
Definition: jsonParser.hh:362
bool is_obj() const
Check if object type.
Definition: jsonParser.cc:272
BasicStructure specifies the lattice and atomic basis of a crystal.
bool is_vacancy(const std::string &name)
A vacancy is any Specie/Molecule with (name == "VA" || name == "va" || name == "Va")
Definition: Molecule.hh:187
std::vector< std::string > struc_molecule_name(BasicStructure const &_struc)
Returns an Array of each possible Molecule in this Structure.
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
Main CASM namespace.
Definition: APICommand.hh:8
jsonParser & to_json(const ClexDescription &desc, jsonParser &json)
std::pair< Eigen::VectorXd, std::vector< ChemicalReferenceState > > one_chemical_reference_from_json(xtal::BasicStructure const &prim, jsonParser const &json)
Read chemical reference from one of 3 alternative forms.
void from_json(ClexDescription &desc, const jsonParser &json)
Eigen::VectorXd VectorXd
ChemicalReferenceState from_json< ChemicalReferenceState >(const jsonParser &json)
Read ChemicalReferenceState from: '{"A" : X, "B" : X, ..., "energy_per_species" : X }'.
pair_type ref
Definition: settings.cc:144
Stores the composition and energy in a single reference state.
std::map< std::string, double > species_num
Map of Molecule name : number of each species in reference state.
double energy_per_species
Energy in this reference state.
static ReturnType from_json(const jsonParser &json)
Default from_json is equivalent to.
Definition: jsonParser.hh:551