CASM
AClustersApproachtoStatisticalMechanics
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
clex.cc
Go to the documentation of this file.
4 
6 
7 namespace CASM {
8 
9  // --- ChemicalReferenceState -------------------
10 
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 
19  template<>
21  ChemicalReferenceState ref_state;
22  bool found_value = false;
23  for(auto it = json.begin(); it != json.end(); ++it) {
24  if(it.name() == "energy_per_species") {
25  found_value = true;
26  from_json(ref_state.energy_per_species, *it);
27  }
28  else if(is_vacancy(it.name())) {
29  std::cerr << "Error reading chemical reference state: " << json << std::endl;
30  throw std::runtime_error("Error reading chemical reference: Input should not include vacancies");
31  }
32  else {
33  from_json(ref_state.species_num[it.name()], *it);
34  }
35  }
36  if(!found_value) {
37  std::cerr << "Error reading chemical reference state: " << json << std::endl;
38  throw std::runtime_error("Error reading chemical reference: No 'energy_per_species' found");
39  }
40  return ref_state;
41  }
42 
44  void from_json(ChemicalReferenceState &ref_state, const jsonParser &json) {
45  ref_state = from_json<ChemicalReferenceState>(json);
46  }
47 
48 
49  // --- HyperPlaneReference -------------------
50 
52  json.put_obj();
53  to_json(ref.global(), json["global"]);
54  to_json(ref.supercell(), json["supercell"]);
55  to_json(ref.supercell(), json["config"]);
56  return json;
57  }
58 
60  const jsonParser &json,
62 
63  HyperPlaneReference ref(CASM::from_json<Eigen::VectorXd>(json["global"]), f);
64  CASM::from_json(ref.supercell(), json["supercell"]);
65  CASM::from_json(ref.config(), json["config"]);
66  return ref;
67  }
68 
70  const jsonParser &json,
73  }
74 
75 
76  // --- ChemicalReference -------------------
77 
105  json.put_obj();
106 
107  json["species_order"] = ref.prim().get_struc_molecule_name();
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  }
116  else {
117  to_json(ref.global_ref_states(), json["global"]);
118  }
119 
120  if(ref.supercell().size()) {
121  jsonParser &s_json = json["supercell"];
122  for(auto it = ref.supercell().begin(); it != ref.supercell().end(); ++it) {
123  auto res = ref.supercell_ref_states().find(it->first);
124  if(res == ref.supercell_ref_states().end()) {
125  s_json[it->first].put_array();
126  for(int i = 0; i < it->second.size(); ++i) {
127  s_json[it->first].push_back(it->second(i));
128  }
129  }
130  else {
131  to_json(res->second, s_json[it->first]);
132  }
133  }
134  }
135 
136  if(ref.config().size()) {
137  jsonParser &c_json = json["config"];
138  for(auto it = ref.config().begin(); it != ref.config().end(); ++it) {
139  auto res = ref.config_ref_states().find(it->first);
140  if(res == ref.config_ref_states().end()) {
141  c_json[it->first].put_array();
142  for(int i = 0; i < it->second.size(); ++i) {
143  c_json[it->first].push_back(it->second(i));
144  }
145  }
146  else {
147  to_json(res->second, c_json[it->first]);
148  }
149  }
150  }
151 
152  return json;
153  }
154 
179  std::pair<Eigen::VectorXd, std::vector<ChemicalReferenceState> >
181  const jsonParser &json) {
182 
183  typedef std::pair<Eigen::VectorXd, std::vector<ChemicalReferenceState> > ReturnType;
184 
185  ReturnType result;
186 
187  std::vector<std::string> struc_mol_name = prim.get_struc_molecule_name();
188 
189  // if: {"A": X, "C": X, "D": X} // expects all species in prim, except vacancy
190  if(json.is_obj()) {
191  for(auto it = json.begin(); it != json.end(); ++it) {
193  r.species_num[it.name()] = 1.0;
194  r.energy_per_species = it->get<double>();
195  result.second.push_back(r);
196  }
197  return result;
198  }
199  else {
200 
201  // if: [X, X, X, X]
202  if(json.begin()->is_number() || json.begin()->is_int()) {
203 
204  if(json.size() != struc_mol_name.size()) {
205  std::cerr << "received: " << json << std::endl;
206  std::cerr << "expected size: " << struc_mol_name.size() << std::endl;
207  throw std::runtime_error("Error in one_chemical_reference_from_json: Size mismatch with vector input.");
208  }
209 
210  result.first = Eigen::VectorXd(json.size());
211  for(int i = 0; i < json.size(); ++i) {
212  result.first(i) = json[i].get<double>();
213  }
214  return result;
215  }
216 
217  // [
218  // {"A": 3.4, "C": 2.0, "energy_per_species": 2.0},
219  // {"B": 2.0, "energy_per_species": 4.0},
220  // {"C": 1.0, "energy_per_species": 3.0}
221  // ]
222  else {
223  std::for_each(json.begin(),
224  json.end(),
225  [&](const jsonParser & json) {
226  result.second.push_back(json.get<ChemicalReferenceState>());
227  });
228  return result;
229  }
230  }
231  }
232 
253  const jsonParser &json,
254  const Structure &prim,
255  double tol) {
256 
257  std::unique_ptr<ChemicalReference> ref;
258 
259  auto res = one_chemical_reference_from_json(prim, json["global"]);
260 
261  if(res.second.empty()) {
262  ref = notstd::make_unique<ChemicalReference>(prim, res.first);
263  }
264  else {
265  ref = notstd::make_unique<ChemicalReference>(prim, res.second.begin(), res.second.end(), tol);
266  }
267 
268  if(json.find("supercell") != json.end()) {
269  for(auto it = json["supercell"].begin(); it != json["supercell"].end(); ++it) {
270  auto res = one_chemical_reference_from_json(prim, *it);
271  if(res.second.empty()) {
272  ref->set_supercell(it.name(), res.first);
273  }
274  else {
275  ref->set_supercell(it.name(), res.second.begin(), res.second.end(), tol);
276  }
277  }
278  }
279 
280  if(json.find("config") != json.end()) {
281  for(auto it = json["config"].begin(); it != json["config"].end(); ++it) {
282  auto res = one_chemical_reference_from_json(prim, *it);
283  if(res.second.empty()) {
284  ref->set_config(it.name(), res.first);
285  }
286  else {
287  ref->set_config(it.name(), res.second.begin(), res.second.end(), tol);
288  }
289  }
290  }
291 
292  return std::move(*ref);
293  }
294 
295 
298  const jsonParser &json,
299  const Structure &prim,
300  double tol) {
301  ref = jsonConstructor<ChemicalReference>::from_json(json, prim, tol);
302  }
303 
304 }
305 
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
std::function< Eigen::VectorXd(const Configuration &)> InputFunction
Definition: Reference.hh:79
double energy_per_species
Energy in this reference state.
void from_json(ClexDescription &desc, const jsonParser &json)
iterator end()
Returns iterator to end of JSON object or JSON array.
Definition: jsonParser.cc:465
const std::map< std::string, Eigen::VectorXd > & supercell() const
const Access a map of scelname to reference for Supercell specialized references
bool is_vacancy(const std::string &name)
A vacancy is any Specie/Molecule with (name == "VA" || name == "va" || name == "Va") ...
Definition: Molecule.hh:27
Structure specifies the lattice and atomic basis of a crystal.
Definition: Structure.hh:29
jsonParser & to_json(const ClexDescription &desc, jsonParser &json)
const RefStateMap & config_ref_states() const
const Access a map of configname to RefStateVec for Configuration specialized references ...
Main CASM namespace.
Definition: complete.cpp:8
std::map< std::string, double > species_num
Map of Molecule name : number of each species in reference state.
iterator begin()
Returns const_iterator to beginning of JSON object or JSON array.
Definition: jsonParser.cc:440
T get(Args...args) const
Get data from json, using one of several alternatives.
Definition: jsonParser.hh:729
bool is_obj() const
Check if object type.
Definition: jsonParser.cc:276
std::vector< std::string > get_struc_molecule_name() const
Returns an Array of each possible Molecule in this Structure.
Definition: Structure.cc:166
pair_type ref
Definition: settings.cc:110
const std::map< std::string, Eigen::VectorXd > & config() const
const Access a map of configname to reference for Configuration specialized references ...
double tol
std::pair< Eigen::VectorXd, std::vector< ChemicalReferenceState > > one_chemical_reference_from_json(const Structure &prim, const jsonParser &json)
Read chemical reference from one of 3 alternative forms.
Definition: clex.cc:180
Eigen::VectorXd VectorXd
Eigen::VectorXd & global()
Access the global reference.
Definition: Reference.hh:265
static ReturnType from_json(const jsonParser &json)
Default from_json is equivalent to.
Definition: jsonParser.hh:489
const Eigen::VectorXd & global() const
const Access the global reference
const RefStateVec & global_ref_states() const
const Access a map of configname to RefStateVec for Supercell specialized references ...
iterator find(const std::string &name)
Return iterator to JSON object value with 'name'.
Definition: jsonParser.cc:490
jsonParser & put_obj()
Puts new empty JSON object.
Definition: jsonParser.hh:276
Stores the composition and energy in a single reference state.
std::map< std::string, Eigen::VectorXd > & supercell()
Access a map of scelname to reference for Supercell specialized references.
Definition: Reference.hh:272
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
ChemicalReferenceState from_json< ChemicalReferenceState >(const jsonParser &json)
Read ChemicalReferenceState from: '{"A" : X, "B" : X, ..., "energy_per_species" : X }'...
const RefStateMap & supercell_ref_states() const
const Access a map of configname to RefStateVec for Supercell specialized references ...
std::map< std::string, Eigen::VectorXd > & config()
Access a map of configname to reference for Configuration specialized references. ...
Definition: Reference.hh:279
const Structure & prim() const
Get primitive Structure.
jsonParser & put_array()
Puts new empty JSON array.
Definition: jsonParser.hh:285
Maps a Configuration to a scalar value via a hyperplane.
Definition: Reference.hh:216