CASM  1.1.0
A Clusters Approach to Statistical Mechanics
GrandCanonicalIO.cc
Go to the documentation of this file.
2 
8 
9 namespace CASM {
10 namespace Monte {
11 
37  const GrandCanonical &mc) {
39 
44 
45  formatter.push_back(MonteCarloTFormatter<GrandCanonical>());
46 
47  std::set<std::string> exclude;
48  std::string name;
49 
50  // always sample Beta, potential_energy, and formation_energy
51  {
52  formatter.push_back(MonteCarloBetaFormatter<GrandCanonical>());
53  name = "potential_energy";
56  exclude.insert(name);
57 
58  name = "formation_energy";
61  exclude.insert(name);
62  }
63 
64  // always sample param_chem_pot, comp
65  for (int i = 0;
66  i < mc.primclex().composition_axes().independent_compositions(); ++i) {
67  formatter.push_back(MonteCarloParamChemPotFormatter<GrandCanonical>(mc, i));
68  }
69 
70  for (int i = 0;
71  i < mc.primclex().composition_axes().independent_compositions(); i++) {
72  name = std::string("comp(") + mc.primclex().composition_axes().comp_var(i) +
73  ")";
76  exclude.insert(name);
77  }
78 
79  // always sample comp_n
80  auto struc_mol_name = xtal::struc_molecule_name(mc.primclex().prim());
81  for (int i = 0; i < struc_mol_name.size(); ++i) {
82  name = std::string("comp_n(") + struc_mol_name[i] + ")";
85  exclude.insert(name);
86  }
87 
88  // include mean/prec of other properties
89  for (auto it = mc.samplers().cbegin(); it != mc.samplers().cend(); ++it) {
90  if (exclude.find(it->first) == exclude.end()) {
91  formatter.push_back(MonteCarloMeanFormatter(it->first));
92  formatter.push_back(MonteCarloPrecFormatter(it->first));
93  }
94  }
95 
96  // include heat_capacity
97  formatter.push_back(MonteCarloHeatCapacityFormatter<GrandCanonical>());
98 
99  // include susc_x
100  for (int i = 0;
101  i < mc.primclex().composition_axes().independent_compositions(); i++) {
102  for (int j = i;
103  j < mc.primclex().composition_axes().independent_compositions(); j++) {
104  auto comp_var_i = mc.primclex().composition_axes().comp_var(i);
105  auto comp_var_j = mc.primclex().composition_axes().comp_var(j);
106  formatter.push_back(
107  MonteCarloSuscXFormatter<GrandCanonical>(comp_var_i, comp_var_j));
108  }
109  }
110 
111  // include thermo-chem susc
112  for (int i = 0;
113  i < mc.primclex().composition_axes().independent_compositions(); i++) {
114  auto comp_var_i = mc.primclex().composition_axes().comp_var(i);
115  formatter.push_back(
116  MonteCarloThermoChemSuscXFormatter<GrandCanonical>(comp_var_i));
117  }
118 
119  // include susc_n
120  for (int i = 0; i < struc_mol_name.size(); ++i) {
121  for (int j = i; j < struc_mol_name.size(); ++j) {
122  auto species_i = struc_mol_name[i];
123  auto species_j = struc_mol_name[j];
124  formatter.push_back(
125  MonteCarloSuscNFormatter<GrandCanonical>(species_i, species_j));
126  }
127  }
128 
129  // include thermo-chem susc
130  for (int i = 0; i < struc_mol_name.size(); ++i) {
131  auto species_i = struc_mol_name[i];
132  formatter.push_back(
133  MonteCarloThermoChemSuscNFormatter<GrandCanonical>(species_i));
134  }
135 
136  return formatter;
137 }
138 
158  const GrandCanonical &mc, const double &phi_LTE1,
159  const std::string &configname) {
161 
162  bool print_json = true;
164  "configname", configname, print_json));
165  formatter.push_back(MonteCarloTFormatter<GrandCanonical>());
166  formatter.push_back(GrandCanonicalLTEFormatter(phi_LTE1));
167  std::set<std::string> exclude;
168  std::string name;
169 
170  // always sample Beta, potential_energy, and formation_energy
171  {
172  formatter.push_back(MonteCarloBetaFormatter<GrandCanonical>());
173  name = "gs_potential_energy";
174  auto evaluator = [=](const ConstMonteCarloPtr &ptr) {
175  return static_cast<const GrandCanonical *>(ptr)->potential_energy();
176  };
178  name, name, evaluator));
179  exclude.insert(name);
180  }
181 
182  {
183  name = "gs_formation_energy";
184  auto evaluator = [=](const ConstMonteCarloPtr &ptr) {
185  return static_cast<const GrandCanonical *>(ptr)->formation_energy();
186  };
188  name, name, evaluator));
189  exclude.insert(name);
190  }
191 
192  // always sample param_chem_pot, comp
193  for (int i = 0;
195  formatter.push_back(MonteCarloParamChemPotFormatter<GrandCanonical>(mc, i));
196  }
197 
198  for (int i = 0;
200  name = std::string("gs_comp(") +
201  mc.primclex().composition_axes().comp_var(i) + ")";
202  auto evaluator = [=](const ConstMonteCarloPtr &ptr) {
203  const GrandCanonical *_ptr = static_cast<const GrandCanonical *>(ptr);
205  _ptr->comp_n())[i];
206  };
208  name, name, evaluator));
209  exclude.insert(name);
210  }
211 
212  // always sample comp_n
213  auto struc_mol_name = xtal::struc_molecule_name(mc.primclex().prim());
214  for (int i = 0; i < struc_mol_name.size(); ++i) {
215  name = std::string("gs_comp_n(") + struc_mol_name[i] + ")";
216  auto evaluator = [=](const ConstMonteCarloPtr &ptr) {
217  return static_cast<const GrandCanonical *>(ptr)->comp_n()[i];
218  };
220  name, name, evaluator));
221  exclude.insert(name);
222  }
223 
224  return formatter;
225 }
226 
238  jsonParser &json) {
239  json.put_obj();
240  json["temperature"] = conditions.temperature();
241  json["param_chem_pot"] = jsonParser::object();
242  auto param_chem_pot = conditions.param_chem_pot();
243  for (int i = 0; i < param_chem_pot.size(); i++) {
244  json["param_chem_pot"][CompositionConverter::comp_var(i)] =
245  param_chem_pot(i);
246  }
247  json["tolerance"] = conditions.tolerance();
248 
249  return json;
250 }
251 
263  const jsonParser &json) {
264  double temp = json["temperature"].get<double>();
265  double tol = json["tolerance"].get<double>();
266 
267  int Nparam = primclex.composition_axes().independent_compositions();
268  Eigen::VectorXd param_chem_pot(Nparam);
269 
270  for (int i = 0; i < Nparam; i++) {
271  param_chem_pot(i) =
272  json["param_chem_pot"][CompositionConverter::comp_var(i)].get<double>();
273  }
274 
275  conditions = GrandCanonicalConditions(primclex, temp, param_chem_pot, tol);
276 }
277 
280  const double &phi_LTE1) {
281  auto evaluator = [=](const ConstMonteCarloPtr &mc) { return phi_LTE1; };
282  return GenericDatumFormatter<double, ConstMonteCarloPtr>("phi_LTE", "phi_LTE",
283  evaluator);
284 }
285 
288 void write_lte_results(const MonteSettings &settings, const GrandCanonical &mc,
289  const double &phi_LTE1, const std::string &configname,
290  Log &_log) {
291  try {
292  fs::create_directories(settings.output_directory());
294  auto formatter = make_lte_results_formatter(mc, phi_LTE1, configname);
295 
296  // write csv path results
297  if (settings.write_csv()) {
298  fs::path file = dir.results_csv();
299  _log << "write: " << dir.results_csv() << "\n";
300  fs::ofstream sout;
301 
302  if (!fs::exists(file)) {
303  sout.open(file);
304  formatter.print_header(&mc, sout);
305  } else {
306  sout.open(file, std::ios::app);
307  }
308 
309  formatter.print(&mc, sout);
310 
311  sout.close();
312  }
313 
314  // write json path results
315  if (settings.write_json()) {
316  fs::path file = dir.results_json();
317  _log << "write: " << dir.results_json() << "\n";
318 
319  jsonParser results;
320  if (fs::exists(file)) {
321  results.read(file);
322  } else {
323  results = jsonParser::object();
324  }
325 
326  formatter.to_json_arrays(&mc, results);
327  results.write(file);
328  }
329  } catch (...) {
330  std::cerr << "ERROR writing results" << std::endl;
331  throw;
332  }
333 }
334 
335 } // namespace Monte
336 } // namespace CASM
Eigen::VectorXd param_composition(const Eigen::VectorXd &n) const
Convert number of mol per prim, 'n' to parametric composition 'x'.
static std::string comp_var(size_type i)
Composition variable names: "a", "b", ...
size_type independent_compositions() const
The dimensionality of the composition space.
Prints a string value specified at construction. A header string can also be passed.
Extract data from objects of 'DataObject' class.
void push_back(const BaseDatumFormatter< DataObject > &new_formatter, const std::string &args)
A DatumFormatter that returns a value of specified type, via functions that may be specified at runti...
Definition: Log.hh:48
Eigen::VectorXd param_chem_pot() const
parametric chemical potential: dg/dcomp_x
const Eigen::VectorXd & comp_n() const
Number of atoms of each type, normalized per primitive cell.
Interface base class for all types of Monte Carlo simulations (not meant to be used polymorphically)
Definition: MonteCarlo.hh:44
const PrimClex & primclex() const
const Access the PrimClex that *this is based on
Definition: MonteCarlo.hh:77
const SamplerMap & samplers() const
const Access sampler map
Definition: MonteCarlo.hh:149
Settings for Monte Carlo calculations.
const fs::path output_directory() const
Directory where output should go.
bool write_csv() const
Write csv versions of files? (csv is the default format if no 'output_format' given)
bool write_json() const
Write json versions of files?
PrimClex is the top-level data structure for a CASM project.
Definition: PrimClex.hh:55
static jsonParser object()
Returns an empty json object.
Definition: jsonParser.hh:395
bool read(std::istream &stream)
Reads json from the stream.
Definition: jsonParser.cc:168
jsonParser & put_obj()
Puts new empty JSON object.
Definition: jsonParser.hh:354
void write(const std::string &file_name, unsigned int indent=2, unsigned int prec=12) const
Write json to file.
Definition: jsonParser.cc:196
double formation_energy(const Configuration &config)
Returns the formation energy, normalized per unit cell.
Eigen::VectorXd comp_n(const Configuration &config)
Returns the composition, as number of each species per unit cell.
const CompositionConverter & composition_axes() const
const Access CompositionConverter object
Definition: PrimClex.cc:243
const PrimType & prim() const
const Access to primitive Structure
Definition: PrimClex.cc:262
std::vector< std::string > struc_molecule_name(BasicStructure const &_struc)
Returns an Array of each possible Molecule in this Structure.
T get(Args &&... args) const
Get data from json, using one of several alternatives.
Definition: jsonParser.hh:716
ConfigIO::GenericConfigFormatter< std::string > configname()
Constructs DataFormmaterDictionary containing all Configuration DatumFormatters.
Definition: ConfigIO.cc:563
GenericDatumFormatter< double, ConstMonteCarloPtr > MonteCarloPrecFormatter(std::string prop_name)
Print calculated precision of property values: prec(<prop_name>)
Definition: MonteIO.cc:49
void from_json(CanonicalConditions &conditions, const PrimClex &primclex, const jsonParser &json)
Read CanonicalConditions from JSON format.
Definition: CanonicalIO.cc:137
GenericDatumFormatter< double, ConstMonteCarloPtr > MonteCarloMeanFormatter(std::string prop_name)
Print mean property values: <prop_name>
Definition: MonteIO.cc:31
GenericDatumFormatter< size_type, ConstMonteCarloPtr > MonteCarloNAvgSamplesFormatter()
Print number of samples used in calculating means.
Definition: MonteIO.cc:148
GenericDatumFormatter< bool, ConstMonteCarloPtr > MonteCarloIsConvergedFormatter()
Print if converged.
Definition: MonteIO.cc:125
DataFormatter< ConstMonteCarloPtr > make_lte_results_formatter(const GrandCanonical &mc, const double &phi_LTE1, const std::string &configname)
Make a results formatter.
jsonParser & to_json(const CanonicalConditions &conditions, jsonParser &json)
Store CanonicalConditions in JSON format.
Definition: CanonicalIO.cc:102
void write_lte_results(const MonteSettings &settings, const GrandCanonical &mc, const double &phi_LTE1, const std::string &configname, Log &_log)
Will create new file or append to existing results file the results of the latest run.
GenericDatumFormatter< bool, ConstMonteCarloPtr > MonteCarloIsEquilibratedFormatter()
Print if equilibrated (not counting explicitly requested equilibration)
Definition: MonteIO.cc:114
GenericDatumFormatter< size_type, ConstMonteCarloPtr > MonteCarloNEquilSamplesFormatter()
Print number of samples used for equilibration (not counting explicitly requested equilibration)
Definition: MonteIO.cc:137
DataFormatter< ConstMonteCarloPtr > make_results_formatter(const Canonical &mc)
Make a LTE results formatter.
Definition: CanonicalIO.cc:31
GenericDatumFormatter< double, ConstMonteCarloPtr > GrandCanonicalLTEFormatter(const double &phi_LTE1)
Print single spin flip LTE.
Main CASM namespace.
Definition: APICommand.hh:8
GenericDatumFormatter< std::string, DataObject > name()
Eigen::VectorXd VectorXd
PrimClex * primclex
Definition: settings.cc:135
DirectoryStructure const & dir
Definition: settings.cc:136