CASM  1.1.0
A Clusters Approach to Statistical Mechanics
CanonicalSettings.cc
Go to the documentation of this file.
4 
5 namespace CASM {
6 namespace Monte {
7 namespace {
8 std::string _help() {
9  std::string s =
10  "For CanonicalConditions, expect a JSON object of form:\n"
11  " {\n"
12  " \"comp\": { // option 1: parameteric composition "
13  "object\n"
14  " \"a\" : 0.3,\n"
15  " ...\n"
16  " },\n"
17  " \"comp\": [0.3, 0.2, ...], // option 2: parameteric composition "
18  "array\n"
19  " \"comp_n\": { // option 3: mol per prim composition "
20  "object\n"
21  " \"A\" : 1.2,\n"
22  " ...\n"
23  " },\n"
24  " \"comp_n\": [1.2, 0.3, ...], // option 4: mol per prim composition "
25  "array\n"
26  " \"temperature\" : 350.0,\n"
27  " \"tolerance\" : 0.001\n"
28  " }\n";
29  return s;
30 }
31 } // namespace
32 
35  const fs::path &read_path)
36  : EquilibriumMonteSettings(_primclex, read_path) {
37  if (!_primclex.has_composition_axes()) {
38  throw std::runtime_error("No composition axes selected.");
39  }
40 }
41 
42 // --- CanonicalConditions settings ---------------------
43 
47  return _conditions("initial_conditions");
48  } else if (drive_mode() == Monte::DRIVE_MODE::CUSTOM) {
49  return custom_conditions()[0];
50  } else {
51  throw std::runtime_error("ERROR: Invalid drive mode.");
52  }
53 }
54 
57  return _conditions("final_conditions");
58 }
59 
62  return _conditions("incremental_conditions");
63 }
64 
66 std::vector<CanonicalConditions> CanonicalSettings::custom_conditions() const {
67  std::string level1 = "driver";
68  std::string level2 = "custom_conditions";
69 
70  try {
71  std::vector<CanonicalConditions> cond;
72  const jsonParser &json = (*this)[level1][level2];
73  for (auto it = json.begin(); it != json.end(); ++it) {
74  cond.push_back(_conditions(*it));
75  }
76  return cond;
77  } catch (std::runtime_error &e) {
79  err_log.error<Log::standard>("Reading Monte Carlo settings");
80  err_log << "Tried to read an array of CanonicalConditions from [\""
81  << level1 << "\"][\"" << level2 << "\"]" << std::endl;
82  err_log << _help() << std::endl;
83  throw e;
84  }
85 }
86 
87 // --- Project settings ---------------------
88 
91  const PrimClex &primclex) const {
93  std::string level1 = "model";
94  // deprecated
95  if (_is_setting(level1, "clex")) {
96  // expect "clex" is "formation_energy"
97 
98  std::vector<std::string> var{"clex", "calctype", "ref", "bset", "eci"};
99  std::vector<std::string> help{
100  "string\n Names the cluster expansion to be used.\n",
101  "string\n Names the calctype to be used.\n",
102  "string\n Names the reference to be used.\n",
103  "string\n Names the basis set to be used.\n",
104  "string\n Names the ECI to be used.\n"};
105 
106  return ClexDescription(_get_setting<std::string>(level1, var[0], help[0]),
107  _get_setting<std::string>(level1, var[0], help[0]),
108  _get_setting<std::string>(level1, var[1], help[1]),
109  _get_setting<std::string>(level1, var[2], help[2]),
110  _get_setting<std::string>(level1, var[3], help[3]),
111  _get_setting<std::string>(level1, var[4], help[4]));
112  }
113 
114  std::string help =
115  "(string, default='formation_energy')\n"
116  " Names the formation_energy cluster expansion to be used.\n";
117 
118  std::string formation_energy = "formation_energy";
119  if (_is_setting(level1, "formation_energy")) {
121  _get_setting<std::string>(level1, "formation_energy", help);
122  }
123 
124  if (!set.has_clex(formation_energy)) {
126  err_log.error<Log::standard>("Reading Monte Carlo settings");
127  err_log << "Error reading [\"model\"][\"formation_energy\"]\n";
128  err_log << "[\"model\"][\"formation_energy\"]: (string, optional, "
129  "default='formation_energy')\n";
130  err_log << " Names the cluster expansion to be used for calculating "
131  "formation_energy.\n";
132  err_log << "No cluster expansion named: '" << formation_energy
133  << "' exists.\n";
134  }
135  return set.clex(formation_energy);
136 }
137 
138 // --- Sampler settings ---------------------
139 
142  std::string level1 = "data";
143  std::string level2 = "measurements";
144  try {
145  const jsonParser &json = (*this)[level1][level2];
146  for (auto it = json.cbegin(); it != json.cend(); ++it) {
147  if (it->contains("quantity") &&
148  (*it)["quantity"].get<std::string>() == "all_correlations") {
149  return true;
150  }
151  }
152  return false;
153  } catch (std::runtime_error &e) {
155  err_log.error<Log::standard>("Reading Monte Carlo settings");
156  err_log << "Error checking if 'all_correlations' should be sampled.\n";
157  err_log << "Error reading settings at [\"" << level1 << "\"][\"" << level2
158  << "\"]\n";
159  err_log << "See 'casm format --monte' for help.\n" << std::endl;
160  throw e;
161  }
162 }
163 
165  std::string level1 = "driver";
166  std::string level2 = name;
167  try {
168  return _conditions((*this)[level1][level2]);
169  } catch (std::runtime_error &e) {
171  err_log.error<Log::standard>("Reading Monte Carlo settings");
172  err_log << "Error reading: " << name << std::endl;
173  err_log << "Tried to construct CanonicalCondtions from [\"" << level1
174  << "\"][\"" << level2 << "\"]" << std::endl;
175  err_log << _help() << std::endl;
176  throw e;
177  }
178 }
179 
181  const jsonParser &json) const {
182  CanonicalConditions result;
183  from_json(result, primclex(), json);
184  return result;
185 }
186 
187 } // namespace Monte
188 } // 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
CanonicalSettings()
Default constructor.
CanonicalConditions final_conditions() const
Expects final_conditions.
ClexDescription formation_energy(const PrimClex &primclex) const
Get formation energy cluster expansion.
std::vector< CanonicalConditions > custom_conditions() const
Expects custom_conditions.
CanonicalConditions incremental_conditions() const
Expects incremental_conditions.
CanonicalConditions _conditions(std::string name) const
CanonicalConditions initial_conditions() const
Expects initial_conditions.
bool all_correlations() const
Return true if all correlations should be sampled.
const PrimClex & primclex() const
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.