CASM
AClustersApproachtoStatisticalMechanics
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
MonteSettings.cc
Go to the documentation of this file.
6 
7 namespace CASM {
8 
9  // --- MonteSettings Definitions -------------------------------------------------
10 
15  MonteSettings::MonteSettings(const PrimClex &_primclex, const fs::path &read_path):
16  jsonParser(read_path),
17  m_primclex(&_primclex) {
18 
19  m_root = find_casmroot(fs::absolute(read_path));
20  m_output_directory = fs::absolute(read_path).parent_path();
21 
22  }
23 
24 
25  // --- Project root directory ---------------------------
26 
28  return m_root;
29  }
30 
32  return *m_primclex;
33  }
34 
35 
36  // --- Type ---------------------------
37 
40  return _get_setting<Monte::ENSEMBLE>("ensemble", help<Monte::ENSEMBLE>());
41  }
42 
45  return _get_setting<Monte::METHOD>("method", help<Monte::METHOD>());
46  }
47 
49  bool MonteSettings::debug() const {
50  auto it = find("debug");
51  if(it == end()) {
52  return false;
53  }
54  return it->get<bool>();
55  }
56 
58  void MonteSettings::set_debug(bool _debug) {
59  (*this)["debug"] = _debug;
60  }
61 
62 
63  // --- Initialization ---------------------
64 
67  return _is_setting("driver", "motif", "configname");
68  }
69 
71  std::string MonteSettings::motif_configname() const {
72  std::string help = "string"
73  " The name of a configuration, of the form \"SCELV_A_B_C_D_E_F/N\"";
74  return _get_setting<std::string>("driver", "motif", "configname", help);
75  }
76 
79  return _is_setting("driver", "motif", "configdof");
80  }
81 
84  std::string help = "string\n"
85  " Path to file containing DoF, such as an \"final_state.json\" file.";
86  fs::path configdof_path = _get_setting<fs::path>("driver", "motif", "configdof", help);
87  return jsonParser(configdof_path).get<ConfigDoF>();
88  }
89 
92  std::string help = "";
93  return _get_setting<fs::path>("driver", "motif", "configdof", help);
94  }
95 
96 
98  Eigen::Matrix3i MonteSettings::simulation_cell_matrix() const {
99  std::string help = "3x3 transformation matrix, T, such that S = U*T,\n"
100  " where S, is the supercell lattice vectors,\n"
101  " and P, is the primitive cell lattice vectors.\n";
102  return _get_setting<Eigen::Matrix3i>("supercell", help);
103  }
104 
105 
106  // --- Driver ---------------------
107 
110  return _get_setting<Monte::DRIVE_MODE>("driver", "mode", help<Monte::DRIVE_MODE>());
111  }
112 
116  if(!_is_setting("driver", "dependent_runs")) {
117  return true;
118  }
119  std::string help = "bool (default=true)\n"
120  " If true, begin the next calculation with the final DoF from the previous \n"
121  " calculation.\n"
122  " If false, begin each calculation with the DoF specified for the \"motif\".\n";
123  return _get_setting<bool>("driver", "dependent_runs", help);
124  }
125 
128  return m_output_directory;
129  }
130 
131 
132  // --- MCData / Sampling ---------------------
133 
135  double MonteSettings::confidence() const {
136  std::string help = "number, range (0.0, 1.0), default 0.95)";
137  if(_is_setting("data", "confidence")) {
138  return _get_setting<double>("data", "confidence", help);
139  }
140  else {
141  return 0.95;
142  }
143  }
144 
147  std::string level1 = "data";
148  std::string level2 = "storage";
149  std::string level3 = "write_trajectory";
150  std::string help = "bool (default=false)";
151  if(!_is_setting(level1, level2, level3)) {
152  return false;
153  }
154 
155  return _get_setting<bool>(level1, level2, level3, help);
156  }
157 
160  std::string level1 = "data";
161  std::string level2 = "storage";
162  std::string level3 = "write_POSCAR_snapshots";
163  std::string help = "(bool, default=false)";
164  if(!_is_setting(level1, level2, level3)) {
165  return false;
166  }
167 
168  return _get_setting<bool>(level1, level2, level3, help);
169  }
170 
173  std::string level1 = "data";
174  std::string level2 = "storage";
175  std::string level3 = "write_observations";
176  std::string help = "(bool, default=false)";
177  if(!_is_setting(level1, level2, level3)) {
178  return false;
179  }
180 
181  return _get_setting<bool>(level1, level2, level3, help);
182  }
183 
186  std::string level1 = "data";
187  std::string level2 = "storage";
188  std::string level3 = "output_format";
189  std::string help = "(string or JSON array of string, optional, default='csv')\n"
190  " Accepts: 'csv' or 'CSV' to write .csv files\n"
191  " 'json' or 'JSON' to write .json files\n"
192  " Use an array to write in multiple formats.\n";
193 
194  if(!_is_setting(level1, level2, level3)) {
195  return true;
196  }
197 
198  const jsonParser &ref = (*this)[level1][level2][level3];
199 
200  if(ref.is_array()) {
201  std::vector<std::string> formats = _get_setting<std::vector<std::string> >(level1, level2, level3, help);
202  for(auto it = formats.begin(); it != formats.end(); ++it) {
203  if(*it == "csv" || *it == "CSV") {
204  return true;
205  }
206  }
207  return false;
208  }
209  else {
210  std::string input = _get_setting<std::string>(level1, level2, level3, help);
211  if(input == "csv" || input == "CSV") {
212  return true;
213  }
214  return false;
215  }
216  }
217 
220  std::string level1 = "data";
221  std::string level2 = "storage";
222  std::string level3 = "output_format";
223  std::string help = "(string or JSON array of string, optional, default='csv')\n"
224  " Accepts: 'csv' or 'CSV' to write .csv files\n"
225  " 'json' or 'JSON' to write .json files\n"
226  " Use an array to write in multiple formats.\n";
227 
228  if(!_is_setting(level1, level2, level3)) {
229  return false;
230  }
231 
232  const jsonParser &ref = (*this)[level1][level2][level3];
233 
234  if(ref.is_array()) {
235  std::vector<std::string> formats = _get_setting<std::vector<std::string> >(level1, level2, level3, help);
236  for(auto it = formats.begin(); it != formats.end(); ++it) {
237  if(*it == "json" || *it == "JSON") {
238  return true;
239  }
240  }
241  return false;
242  }
243  else {
244  std::string input = _get_setting<std::string>(level1, level2, level3, help);
245  if(input == "json" || input == "JSON") {
246  return true;
247  }
248  return false;
249  }
250  }
251 
252 
253  // --- Enumerating Configurations ---
254 
257  if(!_is_setting("data", "enumeration")) {
258  return false;
259  }
260  return true;
261  }
262 
270 
271  std::string help = "(string, optional, default=\"clex_hull_dist(ALL)\")\n"
272  " A 'casm query'-like string that provides a metric for \n"
273  " ranking configurations as they are encountered during \n"
274  " a Monte Carlo calculation. The resulting value is used\n"
275  " to create a hall of fame of 'best' configurations \n"
276  " encountered during the calculation. When the \n"
277  " calculation is complete configurations in the hall of \n"
278  " fame are added to the CASM project config list. \n"
279  " The 'casm query'-like command should evaluate to a \n"
280  " number.";
281 
282  if(!_is_setting("data", "enumeration", "metric")) {
283  return "clex_hull_dist(ALL)";
284  }
285  return _get_setting<std::string>("data", "enumeration", "metric", help);
286  }
287 
298 
299  std::string help = "(string, optional, default=\"eq(1,1)\")\n"
300  " A 'casm query'-like string that returns a boolean value \n"
301  " indicating if (true) a configuration should be considered\n"
302  " for the enumeration hall of fame.";
303 
304  if(!_is_setting("data", "enumeration", "check")) {
305  return "eq(1,1)";
306  }
307  return _get_setting<std::string>("data", "enumeration", "check", help);
308  }
309 
312  if(!_is_setting("data", "enumeration", "sample_mode")) {
314  }
315  return _get_setting<Monte::ENUM_SAMPLE_MODE>("data", "enumeration", "sample_mode", help<Monte::ENUM_SAMPLE_MODE>());
316  }
317 
320 
321  std::string help = "(bool, optional, default=true)\n"
322  " If true, only configurations that do not already exist\n"
323  " in the config list are inserted into the enumeration \n"
324  " hall of fame.";
325 
326  if(!_is_setting("data", "enumeration", "check_existence")) {
327  return true;
328  }
329  return _get_setting<bool>("data", "enumeration", "check_existence", help);
330  }
331 
334 
335  std::string help = "(bool, optional, default=true)\n"
336  " If true, configurations are inserted into the \n"
337  " enumeration hall of fame in their canonical form. If \n"
338  " 'check_existence' is true, this must be set to true.";
339 
340  bool val;
341  if(!_is_setting("data", "enumeration", "insert_canonical")) {
342  val = true;
343  }
344  else {
345  val = _get_setting<bool>("data", "enumeration", "insert_canonical", help);
346  }
347 
348  // if check_existence, insert_canonical must be true
349  if(enumeration_check_existence() && !val) {
350  throw std::runtime_error(
351  "Error in Monte Carlo enumeration in settings: "
352  "If 'check_existence' is true, then 'insert_canonical' must be true"
353  );
354  }
355  return val;
356  }
357 
360 
361  std::string help = "(integer, optional, default=100)\n"
362  " The number of configurations that are allowed in the \n"
363  " enumeration hall of fame.";
364 
365  if(!_is_setting("data", "enumeration", "N_halloffame")) {
366  return 100;
367  }
368  return _get_setting<Index>("data", "enumeration", "N_halloffame", help);
369  }
370 
373 
374  std::string help = "(number, optional, default=1e-8)\n"
375  " Tolerance used for floating point comparison of \n"
376  " configuration scores in the enumeration hall of fame.";
377 
378  if(!_is_setting("data", "enumeration", "tolerance")) {
379  return 1e-8;
380  }
381  return _get_setting<double>("data", "enumeration", "tolerance", help);
382  }
383 
384 
386  bool MonteSettings::_is_setting(std::string level1, std::string level2) const {
387  try {
388  return (*this)[level1].contains(level2);
389  }
390 
391  catch(std::runtime_error &e) {
393  err_log.error<Log::standard>("Reading Monte Carlo settings");
394  err_log << "No [\"" << level1 << "\"] setting found.\n" << std::endl;
395  throw e;
396  }
397  }
398 
400  bool MonteSettings::_is_setting(std::string level1, std::string level2, std::string level3) const {
401  try {
402  return (*this)[level1][level2].contains(level3);
403  }
404 
405  catch(std::runtime_error &e) {
407  err_log.error<Log::standard>("Reading Monte Carlo settings");
408  if(this->contains(level1)) {
409  err_log << "No [\"" << level1 << "\"][\"" << level2 << "\"] setting found" << std::endl;
410  }
411  else {
412  err_log << "No [\"" << level1 << "\"] setting found" << std::endl;
413  }
414  throw;
415  }
416  }
417 
418 
419 
420  // --- EquilibriumMonteSettings Definitions -------------------------------------------------
421 
422 
423  // --- MCData / Sampling ---------------------
424 
427  return Monte::SAMPLE_MODE::PASS == _get_setting<Monte::SAMPLE_MODE>("data", "sample_by", help<Monte::SAMPLE_MODE>());
428  }
429 
432  return Monte::SAMPLE_MODE::STEP == _get_setting<Monte::SAMPLE_MODE>("data", "sample_by", help<Monte::SAMPLE_MODE>());
433  }
434 
437 
438  std::string level1 = "data";
439  std::string level2 = "sample_period";
440  std::string help = "(int, default=1)\n"
441  " In conjunction with \"sample_by\", determines how often to make observations.";
442  if(!_is_setting(level1, level2)) {
443  return 1;
444  }
445 
446  return _get_setting<size_type>(level1, level2, help);
447  }
448 
451  return _is_setting("data", "equilibration_passes_first_run");
452  }
453 
456  std::string help = "int (optional)";
457  return _get_setting<size_type>("data", "equilibration_passes_first_run", help);
458  }
459 
462  return _is_setting("data", "equilibration_passes_each_run");
463  }
464 
467  std::string help = "int (optional)";
468  return _get_setting<size_type>("data", "equilibration_passes_each_run", help);
469  }
470 
471 
474  return _is_setting("data", "N_pass");
475  }
476 
479  std::string help = "int (optional)";
480  return _get_setting<size_type>("data", "N_pass", help);
481  }
482 
485  std::string help = "int (optional)";
486  return _is_setting("data", "N_step");
487  }
488 
491  std::string help = "int (optional)";
492  return _get_setting<size_type>("data", "N_step", help);
493  }
494 
497  return _is_setting("data", "N_sample");
498  }
499 
502  std::string help = "int (optional)";
503  return _get_setting<size_type>("data", "N_sample", help);
504  }
505 
506 
509  return _is_setting("data", "max_pass");
510  }
511 
514  std::string help = "int (optional)";
515  return _get_setting<size_type>("data", "max_pass", help);
516  }
517 
520  return _is_setting("data", "min_pass");
521  }
522 
525  std::string help = "int (optional)";
526  return _get_setting<size_type>("data", "min_pass", help);
527  }
528 
529 
532  return _is_setting("data", "max_step");
533  }
534 
537  std::string help = "int (optional)";
538  return _get_setting<size_type>("data", "max_step", help);
539  }
540 
543  return _is_setting("data", "min_step");
544 
545  }
546 
549  std::string help = "int (optional)";
550  return _get_setting<size_type>("data", "min_step", help);
551  }
552 
553 
556  return _is_setting("data", "max_sample");
557  }
558 
561  std::string help = "int (optional)";
562  return _get_setting<size_type>("data", "max_sample", help);
563  }
564 
567  return _is_setting("data", "min_sample");
568  }
569 
572  std::string help = "int (optional)";
573  return _get_setting<size_type>("data", "min_sample", help);
574  }
575 
576 
577  // --- Data ---------------------
578 
581  if(!_is_setting("data", "sample_by")) {
582  return 1024;
583  }
584  else if(sample_by_pass()) {
585  if(is_max_pass()) {
586  return (max_pass() / sample_period());
587  }
588  else if(is_N_pass()) {
589  return (N_pass() / sample_period());
590  }
591  else if(is_N_sample()) {
592  return N_sample();
593  }
594  else {
595  return 1024;
596  }
597  }
598  else if(sample_by_step()) {
599  if(is_max_step()) {
600  return (max_step() / sample_period());
601  }
602  else if(is_N_step()) {
603  return (N_step() / sample_period());
604  }
605  else if(is_N_sample()) {
606  return N_sample();
607  }
608  else {
609  return 1024;
610  }
611  }
612  else {
614  err_log.error<Log::standard>("Reading Monte Carlo settings");
615  err_log << "Could not determine max data length.\n";
616  err_log << "Please check 'sample_by', 'max_pass' or 'max_step', and 'sample_period' in your input file.\n" << std::endl;
617  throw std::runtime_error(std::string("Error determining max_data_length"));
618  }
619 
620  }
621 
622 
623 }
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.
jsonParser()
Create a new empty jsonParser.
Definition: jsonParser.hh:89
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.
iterator end()
Returns iterator to end of JSON object or JSON array.
Definition: jsonParser.cc:465
std::string help()
Print help message describing recognized strings for allowed enum values.
Definition: EnumIO.hh:38
const PrimClex & primclex() const
bool write_trajectory() const
Returns true if snapshots are requested.
bool sample_by_pass() const
Sample by pass?
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.
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
bool dependent_runs() const
If dependent runs, start subsequent calculations with the final state of the previous calculation...
fs::path find_casmroot(const fs::path &cwd)
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.
size_type min_pass() const
Minimum number of passes, default 0 if sample by pass.
pair_type ref
Definition: settings.cc:110
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 is_min_pass() const
Returns true if a minimum number of passes has been specified.
json_spirit::mObject::size_type size_type
Definition: jsonParser.hh:82
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)
iterator find(const std::string &name)
Return iterator to JSON object value with 'name'.
Definition: jsonParser.cc:490
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) ...
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
void error(const std::string &what)
Definition: Log.hh:86
Definition: Log.hh:9
bool is_array() const
Check if array type.
Definition: jsonParser.cc:281
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?