CASM
AClustersApproachtoStatisticalMechanics
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
MonteSettings.hh
Go to the documentation of this file.
1 #ifndef CASM_MonteSettings_HH
2 #define CASM_MonteSettings_HH
3 
4 #include <string>
9 #include "casm/clex/PrimClex.hh"
12 
13 namespace CASM {
14 
15  class MonteCarlo;
16  template<typename ObjectType, typename Metric, typename ObjectCompare>
17  class HallOfFame;
18 
19  class MonteCarloEnumMetric;
20  class MonteCarloEnumCheck;
21 
22  /*
23  * MonteSettings is nothing more than a jsonParser that you can expect
24  * to contain a set of values. It's a class meant to hold all the
25  * members of the base MonteCarlo class so you can write out all the
26  * relevant data and then also use it to reconstruct MonteCarlo.
27  *
28  * The point of the class is mostly to ensure that when you reconstruct
29  * your MonteCarlo you're not giving it some willy nilly jsonParser
30  * you came up with. It MUST be something that MonteCarlo itself
31  * spit out.
32  *
33  * Inheritance is private, so you can't use [] as an access operator.
34  * Instead use the prescribed accessing routines. This will keep the
35  * structure of the settings file consistent and changes will
36  * only have to occur through this class.
37  *
38  * To avoid having millions of access routines, there are jsonParser
39  * pointers internally that point to a "current state" of subtrees
40  * that may involve the same access routines. For example, instead
41  * of having different access routines for initial conditions and
42  * final conditions, you just set the current conditions mode, and
43  * then the same access routines can be used. From the outside, it
44  * looks like you're just requesting a subset of your MonteSettings routine.
45  */
46 
48  class MonteSettings: protected jsonParser {
49 
50  public:
51 
52  using jsonParser::print;
53  using jsonParser::write;
54  using jsonParser::operator==;
55  using jsonParser::operator!=;
56 
57  typedef Index size_type;
58 
61 
63  MonteSettings(const PrimClex &_primclex, const fs::path &read_path);
64 
65 
66  // --- Project root directory ---------------------------
67 
68  fs::path root() const;
69 
70  const PrimClex &primclex() const;
71 
72 
73  // --- Type ---------------------------
74 
76  Monte::ENSEMBLE ensemble() const;
77 
79  Monte::METHOD method() const;
80 
82  bool debug() const;
83 
85  void set_debug(bool _debug);
86 
87  // --- Initialization ---------------------
88 
90  bool is_motif_configname() const;
91 
93  std::string motif_configname() const;
94 
96  bool is_motif_configdof() const;
97 
99  ConfigDoF motif_configdof() const;
100 
103 
105  Eigen::Matrix3i simulation_cell_matrix() const;
106 
107 
108  // --- Driver ---------------------
109 
111  virtual const Monte::DRIVE_MODE drive_mode() const;
112 
115  bool dependent_runs() const;
116 
117 
118  // --- Sampling -------------------
119 
121  //double tolerance() const;
122 
124  double confidence() const;
125 
126 
128  bool write_trajectory() const;
129 
131  bool write_POSCAR_snapshots() const;
132 
134  bool write_observations() const;
135 
137  bool write_csv() const;
138 
140  bool write_json() const;
141 
143  const fs::path output_directory() const;
144 
145 
146  // --- Enumerating Configurations ---
147 
149  bool is_enumeration() const;
150 
152  std::string enumeration_metric_args() const;
153 
155  std::string enumeration_check_args() const;
156 
159 
161  bool enumeration_insert_canonical() const;
162 
164  bool enumeration_check_existence() const;
165 
168 
170  double enumeration_tol() const;
171 
172 
173  protected:
174 
176  bool _is_setting(std::string level1, std::string level2) const;
177 
179  bool _is_setting(std::string level1, std::string level2, std::string level3) const;
180 
182  template<typename T>
183  T _get_setting(std::string level1, std::string msg) const;
184 
186  template<typename T>
187  T _get_setting(std::string level1, std::string level2, std::string msg) const;
188 
190  template<typename T>
191  T _get_setting(std::string level1, std::string level2, std::string level3, std::string msg) const;
192 
193  jsonParser _json() const {
194  return *this;
195  }
196 
197  private:
198 
202 
203  };
204 
205  inline bool operator==(const jsonParser &json, const MonteSettings &settings) {
206  return settings == json;
207  }
208 
209  inline bool operator!=(const jsonParser &json, const MonteSettings &settings) {
210  return settings != json;
211  }
212 
213 
215 
216 
217  public:
218 
221 
223  EquilibriumMonteSettings(const PrimClex &_primclex, const fs::path &read_path) :
224  MonteSettings(_primclex, read_path) {}
225 
226 
227  // --- MCData / Sampling ---------------------
228 
230  bool sample_by_pass() const;
231 
233  bool sample_by_step() const;
234 
236  size_type sample_period() const;
237 
238 
239 
240 
243 
246 
249 
252 
253 
254 
256  bool is_N_pass() const;
257 
259  size_type N_pass() const;
260 
262  bool is_N_step() const;
263 
265  size_type N_step() const;
266 
268  bool is_N_sample() const;
269 
271  size_type N_sample() const;
272 
273 
274 
276  bool is_max_pass() const;
277 
279  size_type max_pass() const;
280 
282  bool is_min_pass() const;
283 
285  size_type min_pass() const;
286 
287 
289  bool is_max_step() const;
290 
292  size_type max_step() const;
293 
295  bool is_min_step() const;
296 
298  size_type min_step() const;
299 
300 
302  bool is_max_sample() const;
303 
305  size_type max_sample() const;
306 
308  bool is_min_sample() const;
309 
311  size_type min_sample() const;
312 
313 
314  // --- Data ---------------------
315 
317  size_type max_data_length() const;
318 
319 
320  };
321 
323  template<typename T>
324  T MonteSettings::_get_setting(std::string level1, std::string msg) const {
325  try {
326  return (*this)[level1].get<T>();
327  }
328 
329  catch(std::runtime_error &e) {
331  std::stringstream ss;
332  ss << "Monte Carlo setting " << "[\"" << level1 << "\"]";
333 
334  err_log.error<Log::standard>(ss.str());
335 
336  err_log << "Expected " << ss.str() << std::endl;
337  err_log << " Either this was not found, or the type is wrong." << std::endl;
338  if(!msg.empty()) {
339  err_log << "[\"" << level1 << "\"]: ";
340  err_log << msg << std::endl;
341  }
342  throw;
343  }
344  }
345 
347  template<typename T>
348  T MonteSettings::_get_setting(std::string level1, std::string level2, std::string msg) const {
349  try {
350  return (*this)[level1][level2].get<T>();
351  }
352 
353  catch(std::runtime_error &e) {
355  std::stringstream ss;
356  ss << "Monte Carlo setting [\"" << level1 << "\"][\"" << level2 << "\"]";
357 
358  err_log.error<Log::standard>(ss.str());
359 
360  err_log << "ERROR in MonteSettings::" << level2 << std::endl;
361  err_log << "Expected " << ss.str() << std::endl;
362  err_log << " Either this was not found, or the type is wrong." << std::endl;
363  if(this->contains(level1)) {
364  err_log << "Found Settings[\"" << level1 << "\"], but not [\"" << level1 << "\"][\"" << level2 << "\"]" << std::endl;
365  err_log << "Settings[\"" << level1 << "\"]:\n" << (*this)[level1] << std::endl;
366  }
367  else {
368  err_log << "No Settings[\"" << level1 << "\"] found" << std::endl;
369  err_log << "Settings:\n" << static_cast<const jsonParser &>(*this) << std::endl;
370  }
371  if(!msg.empty()) {
372  err_log << "[\"" << level1 << "\"][\"" << level2 << "\"]: ";
373  err_log << msg << std::endl;
374  }
375  throw;
376  }
377  }
378 
380  template<typename T>
381  T MonteSettings::_get_setting(std::string level1, std::string level2, std::string level3, std::string msg) const {
382  try {
383  return (*this)[level1][level2][level3].get<T>();
384  }
385 
386  catch(std::runtime_error &e) {
388  std::stringstream ss;
389  ss << "Monte Carlo setting [\"" << level1 << "\"][\"" << level2 << "\"][\"" << level3 << "\"]";
390 
391  err_log.error<Log::standard>(ss.str());
392 
393  err_log << "Expected " << ss.str() << std::endl;
394  err_log << " Either this was not found, or the type is wrong." << std::endl;
395  if(this->contains(level1)) {
396  if(this->contains(level2)) {
397  err_log << "Found Settings[\"" << level1 << "\"][\"" << level2 << "\"], \n"
398  "but not [\"" << level1 << "\"][\"" << level2 << "\"][\"" << level3 << "\"]" << std::endl;
399  err_log << "Settings[\"" << level1 << "\"][\"" << level2 << "\"]:\n" << (*this)[level1][level2] << std::endl;
400  }
401  else {
402  err_log << "No Settings[\"" << level1 << "\"][\"" << level2 << "\"] found" << std::endl;
403  err_log << "Settings:\n" << (*this)[level1] << std::endl;
404  }
405  }
406  else {
407  err_log << "No Settings[\"" << level1 << "\"] found" << std::endl;
408  err_log << "Settings:\n" << static_cast<const jsonParser &>(*this) << std::endl;
409  }
410  if(!msg.empty()) {
411  err_log << "[\"" << level1 << "\"][\"" << level2 << "\"][\"" << level3 << "\"]: ";
412  err_log << msg << std::endl;
413  }
414  throw;
415  }
416  }
417 
418 }
419 
420 #endif
bool is_N_step() const
Returns true if the number of steps has been specified.
bool is_max_step() const
Returns true if a maximum number of steps has been specified.
MonteSettings()
Default constructor.
size_type min_step() const
Minimum number of steps, default 0 if sample by step.
size_type max_step() const
Maximum number of steps, required if sample by step.
const PrimClex & primclex() const
bool write_trajectory() const
Returns true if snapshots are requested.
bool sample_by_pass() const
Sample by pass?
void write(const std::string &file_name, unsigned int indent=2, unsigned int prec=12) const
Write json to file.
Definition: jsonParser.cc:191
size_type N_step() const
Returns the number of steps requested.
bool is_enumeration() const
Returns true if enumeration is requested. (Default false)
bool write_observations() const
Writes all observations.
size_type max_data_length() const
Figure out how large data containers should be.
jsonParser _json() const
ENSEMBLE
Monte Carlo ensemble type.
Index enumeration_N_halloffame() const
Returns enumeration halloffame max size (default 100)
Main CASM namespace.
Definition: complete.cpp:8
const fs::path output_directory() const
Directory where output should go.
size_type sample_period() const
Figure out how often to take samples.
bool is_equilibration_passes_each_run() const
Returns true if explicit equilibration passes for each run have been specified.
const PrimClex * m_primclex
EquilibriumMonteSettings()
Default constructor.
bool dependent_runs() const
If dependent runs, start subsequent calculations with the final state of the previous calculation...
size_type equilibration_passes_each_run() const
Number of explicit equilibration passes requsted for each run.
bool is_max_pass() const
Returns true if a maximum number of passes has been specified.
bool operator!=(const UnitCellCoord &A, const UnitCellCoord &B)
Compare UnitCellCoord.
size_type min_pass() const
Minimum number of passes, default 0 if sample by pass.
void print(std::ostream &stream, unsigned int indent=2, unsigned int prec=12) const
Print json to stream.
Definition: jsonParser.cc:185
bool is_N_sample() const
Returns true if the number of samples has been specified.
size_type min_sample() const
Minimum number of steps, default 0.
bool is_min_step() const
Returns true if a minimum number of steps has been specified.
DRIVE_MODE
How to change conditions.
bool is_motif_configname() const
Returns true if configname of configuration to use as starting motif has been specified.
std::string motif_configname() const
Configname of configuration to use as starting motif.
static const int standard
Definition: Log.hh:15
size_type equilibration_passes_first_run() const
Number of explicit equilibration passes requsted for the first run.
bool is_N_pass() const
Returns true if the number of passes has been specified.
bool is_motif_configdof() const
Returns true if path to ConfigDoF file to use as starting motif has been specified.
bool operator==(const UnitCellCoord &A, const UnitCellCoord &B)
Compare UnitCellCoord.
bool is_min_pass() const
Returns true if a minimum number of passes has been specified.
METHOD
Monte Carlo method type.
EigenIndex Index
For long integer indexing:
A container class for the different degrees of freedom a Configuration might have.
Definition: ConfigDoF.hh:27
size_type N_sample() const
Returns the number of samples requested.
Monte::ENUM_SAMPLE_MODE enumeration_sample_mode() const
Enumeration sample mode (default Monte::ENUM_SAMPLE_MODE::ON_SAMPLE)
PrimClex is the top-level data structure for a CASM project.
Definition: PrimClex.hh:52
void set_debug(bool _debug)
Set debug mode.
bool is_min_sample() const
Returns true if a minimum number of samples has been specified.
std::string enumeration_metric_args() const
Returns 'casm query'-like enumeration metric args.
bool enumeration_insert_canonical() const
Insert configurations in their canonical form.
bool enumeration_check_existence() const
Only insert configurations that are not already enumerated.
ENUM_SAMPLE_MODE
How often to sample runs.
bool _is_setting(std::string level1, std::string level2) const
Returns true if (*this)[level1].contains(level2)
double enumeration_tol() const
Returns enumeration halloffame tolerance (default 1e-8)
bool is_equilibration_passes_first_run() const
Returns true if explicit equilibration passes for the first run have been specified.
virtual const Monte::DRIVE_MODE drive_mode() const
Given a settings jsonParser figure out the drive mode. Expects drive_mode/incremental,custom.
bool debug() const
Run in debug mode?
Monte::ENSEMBLE ensemble() const
Return type of Monte Carlo ensemble.
bool contains(const std::string &name) const
Return true if JSON object contains 'name'.
Definition: jsonParser.cc:500
ConfigDoF motif_configdof() const
ConfigDoF to use as starting motif.
bool write_csv() const
Write csv versions of files? (csv is the default format if no 'output_format' given) ...
T _get_setting(std::string level1, std::string msg) const
Returns (*this)[level1].get();.
bool write_json() const
Write json versions of files?
size_type N_pass() const
Returns the number of passes requested.
Monte::METHOD method() const
Return type of Monte Carlo method.
Log & err_log
Definition: settings.cc:106
bool write_POSCAR_snapshots() const
Returns true if POSCARs of snapshots are requsted. Requires write_trajectory.
Log & default_err_log()
Definition: Log.hh:206
EquilibriumMonteSettings(const PrimClex &_primclex, const fs::path &read_path)
Construct EquilibriumMonteSettings by reading a settings JSON file.
void error(const std::string &what)
Definition: Log.hh:86
Settings for Monte Carlo calculations.
Definition: Log.hh:9
size_type max_sample() const
Maximum number of steps, default std::numeric_limit::max()
std::string enumeration_check_args() const
Returns 'casm query'-like enumeration check args.
Eigen::Matrix3i simulation_cell_matrix() const
Supercell matrix defining the simulation cell.
double confidence() const
Given a settings jsonParser figure out the global tolerance (probably for == operator). Expects tolerance/value.
fs::path motif_configdof_path() const
Path to ConfigDoF file to use as starting motif.
size_type max_pass() const
Maximum number of passes, required if sample by pass.
bool is_max_sample() const
Returns true if a maximum number of samples has been specified.
fs::path root() const
bool sample_by_step() const
Sample by step?