CASM  1.1.0
A Clusters Approach to Statistical Mechanics
ChemicalReference_file_io.cc
Go to the documentation of this file.
2 
3 #include "casm/casm_io/Log.hh"
7 
8 namespace CASM {
9 
15  BasicStructure const &prim,
16  double tol) {
17  try {
18  jsonParser json{filename};
19  if (json.find("chemical_reference") == json.end()) {
20  throw std::runtime_error(
21  "Error reading chemical reference states: Expected "
22  "\"chemical_reference\" entry");
23  }
24  return json["chemical_reference"].get<ChemicalReference>(prim, tol);
25  } catch (...) {
26  err_log() << "Error reading chemical reference states from " << filename
27  << std::endl;
29  throw;
30  }
31 }
32 
38  fs::path filename) {
39  SafeOfstream outfile;
40  outfile.open(filename);
41 
42  jsonParser json;
43  to_json(chem_ref, json["chemical_reference"]);
44  json.print(outfile.ofstream());
45 
46  outfile.close();
47 }
48 
49 } // namespace CASM
Write to a temporary file to ensure a good write, then rename.
Definition: SafeOfstream.hh:31
void close()
Closes stream, and if not a failed write, removes "file" and renames "file.tmp" to "file".
Definition: SafeOfstream.hh:84
void open(fs::path name, std::string tmp_ext="tmp")
Opens "file.tmp" for writing, with intended final target "file".
Definition: SafeOfstream.hh:65
fs::ofstream & ofstream()
Access underlying stream.
Definition: SafeOfstream.hh:80
void print(std::ostream &stream, unsigned int indent=2, unsigned int prec=12) const
Print json to stream.
Definition: jsonParser.cc:188
BasicStructure specifies the lattice and atomic basis of a crystal.
Main CASM namespace.
Definition: APICommand.hh:8
jsonParser & to_json(const ClexDescription &desc, jsonParser &json)
void write_chemical_reference(const ChemicalReference &chem_ref, fs::path filename)
Write chemical reference states to JSON file.
ChemicalReference read_chemical_reference(fs::path filename, const xtal::BasicStructure &prim, double tol)
Read chemical reference states from JSON file.
Log & err_log()
Definition: Log.hh:426