CASM  1.1.0
A Clusters Approach to Statistical Mechanics
GrandCanonicalSettings.cc
Go to the documentation of this file.
2 
6 
7 namespace CASM {
8 namespace Monte {
9 
10 namespace {
11 std::string _help() {
12  std::string s =
13  "For GrandCanonicalConditions, expect a JSON object of form:\n"
14  " {\n"
15  " \"param_chem_pot\": {\n"
16  " \"a\" : -1.0,\n"
17  " ...\n"
18  " },\n"
19  " \"temperature\" : 350.0,\n"
20  " \"tolerance\" : 0.001\n"
21  " }\n";
22  return s;
23 }
24 } // namespace
25 
28  const fs::path &read_path)
29  : EquilibriumMonteSettings(_primclex, read_path) {
30  if (!_primclex.has_composition_axes()) {
31  throw std::runtime_error("No composition axes selected.");
32  }
33 }
34 
35 // --- GrandCanonicalConditions settings ---------------------
36 
40  return _conditions("initial_conditions");
41  } else if (drive_mode() == Monte::DRIVE_MODE::CUSTOM) {
42  return custom_conditions()[0];
43  } else {
44  throw std::runtime_error("ERROR: Invalid drive mode.");
45  }
46 }
47 
50  return _conditions("final_conditions");
51 }
52 
55  const {
56  return _conditions("incremental_conditions");
57 }
58 
60 std::vector<GrandCanonicalConditions>
62  std::string level1 = "driver";
63  std::string level2 = "custom_conditions";
64 
65  try {
66  std::vector<GrandCanonicalConditions> cond;
67  const jsonParser &json = (*this)[level1][level2];
68  for (auto it = json.begin(); it != json.end(); ++it) {
69  cond.push_back(_conditions(*it));
70  }
71  return cond;
72  } catch (std::runtime_error &e) {
74  err_log.error<Log::standard>("Reading Monte Carlo settings");
75  err_log << "Tried to read an array of GrandCanonicalConditions from [\""
76  << level1 << "\"][\"" << level2 << "\"]" << std::endl;
77  err_log << _help() << std::endl;
78  throw e;
79  }
80 }
81 
82 // --- Project settings ---------------------
83 
86  const PrimClex &primclex) const {
88  std::string level1 = "model";
89  // deprecated
90  if (_is_setting(level1, "clex")) {
91  // expect "clex" is "formation_energy"
92 
93  std::vector<std::string> var{"clex", "calctype", "ref", "bset", "eci"};
94  std::vector<std::string> help{
95  "string\n Names the cluster expansion to be used.\n",
96  "string\n Names the calctype to be used.\n",
97  "string\n Names the reference to be used.\n",
98  "string\n Names the basis set to be used.\n",
99  "string\n Names the ECI to be used.\n"};
100 
101  return ClexDescription(_get_setting<std::string>(level1, var[0], help[0]),
102  _get_setting<std::string>(level1, var[0], help[0]),
103  _get_setting<std::string>(level1, var[1], help[1]),
104  _get_setting<std::string>(level1, var[2], help[2]),
105  _get_setting<std::string>(level1, var[3], help[3]),
106  _get_setting<std::string>(level1, var[4], help[4]));
107  }
108 
109  std::string help =
110  "(string, default='formation_energy')\n"
111  " Names the formation_energy cluster expansion to be used.\n";
112 
113  std::string formation_energy = "formation_energy";
114  if (_is_setting(level1, "formation_energy")) {
116  _get_setting<std::string>(level1, "formation_energy", help);
117  }
118 
119  if (!set.has_clex(formation_energy)) {
121  err_log.error<Log::standard>("Reading Monte Carlo settings");
122  err_log << "Error reading [\"model\"][\"formation_energy\"]\n";
123  err_log << "[\"model\"][\"formation_energy\"]: (string, optional, "
124  "default='formation_energy')\n";
125  err_log << " Names the cluster expansion to be used for calculating "
126  "formation_energy.\n";
127  err_log << "No cluster expansion named: '" << formation_energy
128  << "' exists.\n";
129  }
130  return set.clex(formation_energy);
131 }
132 
133 // --- Sampler settings ---------------------
134 
137  if (method() == Monte::METHOD::LTE1) { // hack
138  return false;
139  }
140  std::string level1 = "data";
141  std::string level2 = "measurements";
142  try {
143  const jsonParser &json = (*this)[level1][level2];
144  for (auto it = json.cbegin(); it != json.cend(); ++it) {
145  if (it->contains("quantity") &&
146  (*it)["quantity"].get<std::string>() == "all_correlations") {
147  return true;
148  }
149  }
150  return false;
151  } catch (std::runtime_error &e) {
153  err_log.error<Log::standard>("Reading Monte Carlo settings");
154  err_log << "Error checking if 'all_correlations' should be sampled.\n";
155  err_log << "Error reading settings at [\"" << level1 << "\"][\"" << level2
156  << "\"]\n";
157  err_log << "See 'casm format --monte' for help.\n" << std::endl;
158  throw e;
159  }
160 }
161 
163  std::string name) const {
164  std::string level1 = "driver";
165  std::string level2 = name;
166  try {
167  return _conditions((*this)[level1][level2]);
168  } catch (std::runtime_error &e) {
170  err_log.error<Log::standard>("Reading Monte Carlo settings");
171  err_log << "Error reading: " << name << std::endl;
172  err_log << "Tried to construct GrandCanonicalCondtions from [\"" << level1
173  << "\"][\"" << level2 << "\"]" << std::endl;
174  err_log << _help() << std::endl;
175  throw e;
176  }
177 }
178 
180  const jsonParser &json) const {
182  from_json(result, primclex(), json);
183  return result;
184 }
185 
186 } // namespace Monte
187 } // namespace CASM
std::set< std::string > & s
Definition: Log.hh:48
void error(const std::string &what)
Definition: Log.hh:129
static const int standard
Definition: Log.hh:52
bool all_correlations() const
Return true if all correlations should be sampled.
std::vector< GrandCanonicalConditions > custom_conditions() const
Expects custom_conditions.
GrandCanonicalConditions initial_conditions() const
Expects initial_conditions.
GrandCanonicalConditions final_conditions() const
Expects final_conditions.
GrandCanonicalConditions incremental_conditions() const
Expects incremental_conditions.
GrandCanonicalConditions _conditions(std::string name) const
ClexDescription formation_energy(const PrimClex &primclex) const
Get formation energy cluster expansion.
const PrimClex & primclex() const
Monte::METHOD method() const
Return type of Monte Carlo method.
virtual const Monte::DRIVE_MODE drive_mode() const
Given a settings jsonParser figure out the drive mode. Expects drive_mode/incremental,...
bool _is_setting(std::string level1, std::string level2) const
Returns true if (*this)[level1].contains(level2)
PrimClex is the top-level data structure for a CASM project.
Definition: PrimClex.hh:55
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
const_iterator cend() const
Returns const_iterator to end of JSON object or JSON array.
Definition: jsonParser.cc:533
const_iterator cbegin() const
Returns const_iterator to beginning of JSON object or JSON array.
Definition: jsonParser.cc:510
ProjectSettings & settings()
Definition: PrimClex.cc:224
bool has_composition_axes() const
check if CompositionConverter object initialized
Definition: PrimClex.cc:238
jsonParser & push_back(const T &value, Args &&... args)
Definition: jsonParser.hh:684
void from_json(CanonicalConditions &conditions, const PrimClex &primclex, const jsonParser &json)
Read CanonicalConditions from JSON format.
Definition: CanonicalIO.cc:137
Main CASM namespace.
Definition: APICommand.hh:8
GenericDatumFormatter< std::string, DataObject > name()
std::string help()
Uses 'multiline_help<T>()' by default.
Definition: Help.hh:22
Log & err_log()
Definition: Log.hh:426
PrimClex * primclex
Definition: settings.cc:135
ProjectSettings & set
Definition: settings.cc:137
Specifies a particular cluster expansion.