CASM  1.1.0
A Clusters Approach to Statistical Mechanics
CanonicalIO.cc
Go to the documentation of this file.
2 
8 
9 namespace CASM {
10 namespace Monte {
11 
33 
38 
39  std::set<std::string> exclude;
40  std::string name;
41 
42  // always print temperature, Beta
43  formatter.push_back(MonteCarloTFormatter<Canonical>());
44  formatter.push_back(MonteCarloBetaFormatter<Canonical>());
45 
46  // always print comp
47  for (int i = 0;
48  i < mc.primclex().composition_axes().independent_compositions(); ++i) {
49  name = std::string("comp(") + mc.primclex().composition_axes().comp_var(i) +
50  ")";
51  formatter.push_back(MonteCarloCompFormatter<Canonical>(mc, i));
52  exclude.insert(name);
53  }
54 
55  // always print comp_n
56  auto struc_mol_name = xtal::struc_molecule_name(mc.primclex().prim());
57  for (int i = 0; i < struc_mol_name.size(); ++i) {
58  name = std::string("comp_n(") + struc_mol_name[i] + ")";
59  formatter.push_back(MonteCarloCompNFormatter<Canonical>(mc, i));
60  exclude.insert(name);
61  }
62 
63  // always print potential_energy, and formation_energy, though they are the
64  // same
65  {
66  name = "potential_energy";
69  exclude.insert(name);
70 
71  name = "formation_energy";
74  exclude.insert(name);
75  }
76 
77  // include mean/prec of other properties
78  for (auto it = mc.samplers().cbegin(); it != mc.samplers().cend(); ++it) {
79  if (exclude.find(it->first) == exclude.end()) {
80  formatter.push_back(MonteCarloMeanFormatter(it->first));
81  formatter.push_back(MonteCarloPrecFormatter(it->first));
82  }
83  }
84 
85  // include heat_capacity
86  formatter.push_back(MonteCarloHeatCapacityFormatter<Canonical>());
87 
88  return formatter;
89 }
90 
102 jsonParser &to_json(const CanonicalConditions &conditions, jsonParser &json) {
103  json.put_obj();
104  json["temperature"] = conditions.temperature();
105 
106  json["comp"] = jsonParser::object();
107  auto param_composition = conditions.param_composition();
108  for (int i = 0; i < param_composition.size(); i++) {
109  json["comp"][CompositionConverter::comp_var(i)] = param_composition(i);
110  }
111 
112  json["comp_n"] = jsonParser::object();
113  auto mol_composition = conditions.mol_composition();
114  auto components = conditions.primclex().composition_axes().components();
115  for (int i = 0; i < components.size(); ++i) {
116  json["comp_n"][components[i]] = mol_composition(i);
117  }
118 
119  json["tolerance"] = conditions.tolerance();
120 
121  return json;
122 }
123 
138  const jsonParser &json) {
139  double temp = json["temperature"].get<double>();
140  double tol = json["tolerance"].get<double>();
141 
143 
144  if (json.contains("comp")) {
145  if (json["comp"].is_array()) {
146  from_json(comp, json["comp"]);
147  } else {
148  int Nparam = primclex.composition_axes().independent_compositions();
149  comp.resize(Nparam);
150 
151  for (int i = 0; i < Nparam; i++) {
152  comp(i) = json["comp"][CompositionConverter::comp_var(i)].get<double>();
153  }
154  }
155  } else if (json.contains("comp_n")) {
157 
158  if (json["comp_n"].is_array()) {
159  from_json(comp_n, json["comp_n"]);
160  } else {
161  auto components = primclex.composition_axes().components();
162  comp_n.resize(components.size());
163 
164  for (int i = 0; i < components.size(); i++) {
165  comp_n(i) = json["comp_n"][components[i]].get<double>();
166  }
167  }
168 
169  comp_n = comp_n / comp_n.sum() * primclex.prim().basis().size();
170  comp = primclex.composition_axes().param_composition(comp_n);
171  } else {
172  throw std::runtime_error(
173  "Error reading conditions: No 'comp' or 'comp_n' specified");
174  }
175  conditions = CanonicalConditions(primclex, temp, comp, tol);
176 }
177 
178 } // namespace Monte
179 } // namespace CASM
static std::string comp_var(size_type i)
Composition variable names: "a", "b", ...
std::vector< std::string > components() const
The order of components in mol composition vectors.
Extract data from objects of 'DataObject' class.
void push_back(const BaseDatumFormatter< DataObject > &new_formatter, const std::string &args)
Eigen::VectorXd param_composition() const
parametric composition: comp_x
const PrimClex & primclex() const
Eigen::VectorXd mol_composition() const
mol composition: comp_n
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
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 contains(const std::string &name) const
Return true if JSON object contains 'name'.
Definition: jsonParser.cc:601
jsonParser & put_obj()
Puts new empty JSON object.
Definition: jsonParser.hh:354
Eigen::VectorXd comp(const Configuration &config)
Returns parametric composition, as calculated using PrimClex::param_comp.
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
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
jsonParser & to_json(const CanonicalConditions &conditions, jsonParser &json)
Store CanonicalConditions in JSON format.
Definition: CanonicalIO.cc:102
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
Main CASM namespace.
Definition: APICommand.hh:8
GenericDatumFormatter< std::string, DataObject > name()
Eigen::VectorXd VectorXd
PrimClex * primclex
Definition: settings.cc:135