CASM  1.1.0
A Clusters Approach to Statistical Mechanics
MonteSettings.cc
Go to the documentation of this file.
2 
3 #include <boost/filesystem.hpp>
4 
11 #include "casm/misc/HallOfFame.hh"
14 
15 namespace CASM {
16 namespace Monte {
17 
18 // --- MonteSettings Definitions
19 // -------------------------------------------------
20 
26  const fs::path &read_path)
27  : jsonParser(read_path), m_primclex(&_primclex) {
28  m_root = find_casmroot(fs::absolute(read_path));
29  m_output_directory = fs::absolute(read_path).parent_path();
30 }
31 
32 // --- Project root directory ---------------------------
33 
34 fs::path MonteSettings::root() const { return m_root; }
35 
36 const PrimClex &MonteSettings::primclex() const { return *m_primclex; }
37 
38 // --- Type ---------------------------
39 
42  return _get_setting<Monte::ENSEMBLE>("ensemble", help<Monte::ENSEMBLE>());
43 }
44 
47  return _get_setting<Monte::METHOD>("method", help<Monte::METHOD>());
48 }
49 
51 bool MonteSettings::debug() const {
52  auto it = find("debug");
53  if (it == end()) {
54  return false;
55  }
56  return it->get<bool>();
57 }
58 
60 void MonteSettings::set_debug(bool _debug) { (*this)["debug"] = _debug; }
61 
62 // --- Initialization ---------------------
63 
67  return _is_setting("driver", "motif", "configname");
68 }
69 
71 std::string MonteSettings::motif_configname() const {
72  std::string help =
73  "string"
74  " The name of a configuration, of the form \"SCELV_A_B_C_D_E_F/N\"";
75  return _get_setting<std::string>("driver", "motif", "configname", help);
76 }
77 
81  return _is_setting("driver", "motif", "configdof");
82 }
83 
86  std::string help =
87  "string\n"
88  " Path to file containing DoF, such as an \"final_state.json\" file.";
89  fs::path configdof_path =
90  _get_setting<fs::path>("driver", "motif", "configdof", help);
91  return jsonParser(configdof_path)
92  .get<ConfigDoF>(primclex().prim(), supercell_volume);
93 }
94 
97  std::string help = "";
98  return _get_setting<fs::path>("driver", "motif", "configdof", help);
99 }
100 
103  std::string help =
104  "3x3 transformation matrix, T, such that S = U*T,\n"
105  " where S, is the supercell lattice vectors,\n"
106  " and P, is the primitive cell lattice vectors.\n";
107  return _get_setting<Eigen::Matrix3l>("supercell", help);
108 }
109 
110 // --- Driver ---------------------
111 
115  return _get_setting<Monte::DRIVE_MODE>("driver", "mode",
116  help<Monte::DRIVE_MODE>());
117 }
118 
122  if (!_is_setting("driver", "dependent_runs")) {
123  return true;
124  }
125  std::string help =
126  "bool (default=true)\n"
127  " If true, begin the next calculation with the final DoF from the "
128  "previous \n"
129  " calculation.\n"
130  " If false, begin each calculation with the DoF specified for the "
131  "\"motif\".\n";
132  return _get_setting<bool>("driver", "dependent_runs", help);
133 }
134 
136 const fs::path MonteSettings::output_directory() const {
137  return m_output_directory;
138 }
139 
140 // --- MCData / Sampling ---------------------
141 
144  std::string help = "number, range (0.0, 1.0), default 0.95)";
145  if (_is_setting("data", "confidence")) {
146  return _get_setting<double>("data", "confidence", help);
147  } else {
148  return 0.95;
149  }
150 }
151 
154  std::string level1 = "data";
155  std::string level2 = "storage";
156  std::string level3 = "write_trajectory";
157  std::string help = "bool (default=false)";
158  if (!_is_setting(level1, level2, level3)) {
159  return false;
160  }
161 
162  return _get_setting<bool>(level1, level2, level3, help);
163 }
164 
168  std::string level1 = "data";
169  std::string level2 = "storage";
170  std::string level3 = "write_POSCAR_snapshots";
171  std::string help = "(bool, default=false)";
172  if (!_is_setting(level1, level2, level3)) {
173  return false;
174  }
175 
176  return _get_setting<bool>(level1, level2, level3, help);
177 }
178 
181  std::string level1 = "data";
182  std::string level2 = "storage";
183  std::string level3 = "write_observations";
184  std::string help = "(bool, default=false)";
185  if (!_is_setting(level1, level2, level3)) {
186  return false;
187  }
188 
189  return _get_setting<bool>(level1, level2, level3, help);
190 }
191 
195  std::string level1 = "data";
196  std::string level2 = "storage";
197  std::string level3 = "output_format";
198  std::string help =
199  "(string or JSON array of string, optional, default='csv')\n"
200  " Accepts: 'csv' or 'CSV' to write .csv files\n"
201  " 'json' or 'JSON' to write .json files\n"
202  " Use an array to write in multiple formats.\n";
203 
204  if (!_is_setting(level1, level2, level3)) {
205  return true;
206  }
207 
208  const jsonParser &ref = (*this)[level1][level2][level3];
209 
210  if (ref.is_array()) {
211  std::vector<std::string> formats =
212  _get_setting<std::vector<std::string> >(level1, level2, level3, help);
213  for (auto it = formats.begin(); it != formats.end(); ++it) {
214  if (*it == "csv" || *it == "CSV") {
215  return true;
216  }
217  }
218  return false;
219  } else {
220  std::string input = _get_setting<std::string>(level1, level2, level3, help);
221  if (input == "csv" || input == "CSV") {
222  return true;
223  }
224  return false;
225  }
226 }
227 
230  std::string level1 = "data";
231  std::string level2 = "storage";
232  std::string level3 = "output_format";
233  std::string help =
234  "(string or JSON array of string, optional, default='csv')\n"
235  " Accepts: 'csv' or 'CSV' to write .csv files\n"
236  " 'json' or 'JSON' to write .json files\n"
237  " Use an array to write in multiple formats.\n";
238 
239  if (!_is_setting(level1, level2, level3)) {
240  return false;
241  }
242 
243  const jsonParser &ref = (*this)[level1][level2][level3];
244 
245  if (ref.is_array()) {
246  std::vector<std::string> formats =
247  _get_setting<std::vector<std::string> >(level1, level2, level3, help);
248  for (auto it = formats.begin(); it != formats.end(); ++it) {
249  if (*it == "json" || *it == "JSON") {
250  return true;
251  }
252  }
253  return false;
254  } else {
255  std::string input = _get_setting<std::string>(level1, level2, level3, help);
256  if (input == "json" || input == "JSON") {
257  return true;
258  }
259  return false;
260  }
261 }
262 
263 // --- Enumerating Configurations ---
264 
267  if (!_is_setting("data", "enumeration")) {
268  return false;
269  }
270  return true;
271 }
272 
280  std::string help =
281  "(string, optional, default=\"clex_hull_dist(ALL)\")\n"
282  " A 'casm query'-like string that provides a metric for \n"
283  " ranking configurations as they are encountered during \n"
284  " a Monte Carlo calculation. The resulting value is used\n"
285  " to create a hall of fame of 'best' configurations \n"
286  " encountered during the calculation. When the \n"
287  " calculation is complete configurations in the hall of \n"
288  " fame are added to the CASM project config list. \n"
289  " The 'casm query'-like command should evaluate to a \n"
290  " number.";
291 
292  if (!_is_setting("data", "enumeration", "metric")) {
293  return "clex_hull_dist(ALL)";
294  }
295  return _get_setting<std::string>("data", "enumeration", "metric", help);
296 }
297 
308  std::string help =
309  "(string, optional, default=\"eq(1,1)\")\n"
310  " A 'casm query'-like string that returns a boolean value \n"
311  " indicating if (true) a configuration should be considered\n"
312  " for the enumeration hall of fame.";
313 
314  if (!_is_setting("data", "enumeration", "check")) {
315  return "eq(1,1)";
316  }
317  return _get_setting<std::string>("data", "enumeration", "check", help);
318 }
319 
322  if (!_is_setting("data", "enumeration", "sample_mode")) {
324  }
325  return _get_setting<Monte::ENUM_SAMPLE_MODE>(
326  "data", "enumeration", "sample_mode", help<Monte::ENUM_SAMPLE_MODE>());
327 }
328 
331  std::string help =
332  "(bool, optional, default=true)\n"
333  " If true, only configurations that do not already exist\n"
334  " in the config list are inserted into the enumeration \n"
335  " hall of fame.";
336 
337  if (!_is_setting("data", "enumeration", "check_existence")) {
338  return true;
339  }
340  return _get_setting<bool>("data", "enumeration", "check_existence", help);
341 }
342 
345  std::string help =
346  "(bool, optional, default=true)\n"
347  " If true, configurations are inserted into the \n"
348  " enumeration hall of fame in their canonical form. If \n"
349  " 'check_existence' is true, this must be set to true.";
350 
351  bool val;
352  if (!_is_setting("data", "enumeration", "insert_canonical")) {
353  val = true;
354  } else {
355  val = _get_setting<bool>("data", "enumeration", "insert_canonical", help);
356  }
357 
358  // if check_existence, insert_canonical must be true
359  if (enumeration_check_existence() && !val) {
360  throw std::runtime_error(
361  "Error in Monte Carlo enumeration in settings: "
362  "If 'check_existence' is true, then 'insert_canonical' must be true");
363  }
364  return val;
365 }
366 
369  std::string help =
370  "(integer, optional, default=100)\n"
371  " The number of configurations that are allowed in the \n"
372  " enumeration hall of fame.";
373 
374  if (!_is_setting("data", "enumeration", "N_halloffame")) {
375  return 100;
376  }
377  return _get_setting<Index>("data", "enumeration", "N_halloffame", help);
378 }
379 
382  std::string help =
383  "(number, optional, default=1e-8)\n"
384  " Tolerance used for floating point comparison of \n"
385  " configuration scores in the enumeration hall of fame.";
386 
387  if (!_is_setting("data", "enumeration", "tolerance")) {
388  return 1e-8;
389  }
390  return _get_setting<double>("data", "enumeration", "tolerance", help);
391 }
392 
394 bool MonteSettings::_is_setting(std::string level1, std::string level2) const {
395  try {
396  return (*this)[level1].contains(level2);
397  }
398 
399  catch (std::runtime_error &e) {
401  err_log.error<Log::standard>("Reading Monte Carlo settings");
402  err_log << "No [\"" << level1 << "\"] setting found.\n" << std::endl;
403  throw e;
404  }
405 }
406 
408 bool MonteSettings::_is_setting(std::string level1, std::string level2,
409  std::string level3) const {
410  try {
411  return (*this)[level1][level2].contains(level3);
412  }
413 
414  catch (std::runtime_error &e) {
416  err_log.error<Log::standard>("Reading Monte Carlo settings");
417  if (this->contains(level1)) {
418  err_log << "No [\"" << level1 << "\"][\"" << level2 << "\"] setting found"
419  << std::endl;
420  } else {
421  err_log << "No [\"" << level1 << "\"] setting found" << std::endl;
422  }
423  throw;
424  }
425 }
426 
427 // --- EquilibriumMonteSettings Definitions
428 // -------------------------------------------------
429 
430 // --- MCData / Sampling ---------------------
431 
434  return Monte::SAMPLE_MODE::PASS ==
435  _get_setting<Monte::SAMPLE_MODE>("data", "sample_by",
436  help<Monte::SAMPLE_MODE>());
437 }
438 
441  return Monte::SAMPLE_MODE::STEP ==
442  _get_setting<Monte::SAMPLE_MODE>("data", "sample_by",
443  help<Monte::SAMPLE_MODE>());
444 }
445 
448  const {
449  std::string level1 = "data";
450  std::string level2 = "sample_period";
451  std::string help =
452  "(int, default=1)\n"
453  " In conjunction with \"sample_by\", determines how often to make "
454  "observations.";
455  if (!_is_setting(level1, level2)) {
456  return 1;
457  }
458 
459  return _get_setting<size_type>(level1, level2, help);
460 }
461 
465  return _is_setting("data", "equilibration_passes_first_run");
466 }
467 
471  std::string help = "int (optional)";
472  return _get_setting<size_type>("data", "equilibration_passes_first_run",
473  help);
474 }
475 
479  return _is_setting("data", "equilibration_passes_each_run");
480 }
481 
485  std::string help = "int (optional)";
486  return _get_setting<size_type>("data", "equilibration_passes_each_run", help);
487 }
488 
491  return _is_setting("data", "N_pass");
492 }
493 
496  std::string help = "int (optional)";
497  return _get_setting<size_type>("data", "N_pass", help);
498 }
499 
502  std::string help = "int (optional)";
503  return _is_setting("data", "N_step");
504 }
505 
508  std::string help = "int (optional)";
509  return _get_setting<size_type>("data", "N_step", help);
510 }
511 
514  return _is_setting("data", "N_sample");
515 }
516 
519  std::string help = "int (optional)";
520  return _get_setting<size_type>("data", "N_sample", help);
521 }
522 
525  return _is_setting("data", "max_pass");
526 }
527 
530  std::string help = "int (optional)";
531  return _get_setting<size_type>("data", "max_pass", help);
532 }
533 
536  return _is_setting("data", "min_pass");
537 }
538 
541  std::string help = "int (optional)";
542  return _get_setting<size_type>("data", "min_pass", help);
543 }
544 
547  return _is_setting("data", "max_step");
548 }
549 
552  std::string help = "int (optional)";
553  return _get_setting<size_type>("data", "max_step", help);
554 }
555 
558  return _is_setting("data", "min_step");
559 }
560 
563  std::string help = "int (optional)";
564  return _get_setting<size_type>("data", "min_step", help);
565 }
566 
569  return _is_setting("data", "max_sample");
570 }
571 
574  const {
575  std::string help = "int (optional)";
576  return _get_setting<size_type>("data", "max_sample", help);
577 }
578 
581  return _is_setting("data", "min_sample");
582 }
583 
586  const {
587  std::string help = "int (optional)";
588  return _get_setting<size_type>("data", "min_sample", help);
589 }
590 
591 // --- Data ---------------------
592 
595  const {
596  if (!_is_setting("data", "sample_by")) {
597  return 1024;
598  } else if (sample_by_pass()) {
599  if (is_max_pass()) {
600  return (max_pass() / sample_period());
601  } else if (is_N_pass()) {
602  return (N_pass() / sample_period());
603  } else if (is_N_sample()) {
604  return N_sample();
605  } else {
606  return 1024;
607  }
608  } else if (sample_by_step()) {
609  if (is_max_step()) {
610  return (max_step() / sample_period());
611  } else if (is_N_step()) {
612  return (N_step() / sample_period());
613  } else if (is_N_sample()) {
614  return N_sample();
615  } else {
616  return 1024;
617  }
618  } else {
620  err_log.error<Log::standard>("Reading Monte Carlo settings");
621  err_log << "Could not determine max data length.\n";
622  err_log << "Please check 'sample_by', 'max_pass' or 'max_step', and "
623  "'sample_period' in your input file.\n"
624  << std::endl;
625  throw std::runtime_error(std::string("Error determining max_data_length"));
626  }
627 }
628 
629 } // namespace Monte
630 } // namespace CASM
Definition: Log.hh:48
void error(const std::string &what)
Definition: Log.hh:129
static const int standard
Definition: Log.hh:52
size_type min_step() const
Minimum number of steps, default 0 if sample by step.
bool is_max_step() const
Returns true if a maximum number of steps has been specified.
size_type max_data_length() const
Figure out how large data containers should be.
bool sample_by_pass() const
Sample by pass?
bool is_min_step() const
Returns true if a minimum number of steps has been specified.
bool is_N_pass() const
Returns true if the number of passes has been specified.
bool is_max_pass() const
Returns true if a maximum number of passes has been specified.
size_type max_sample() const
Maximum number of steps, default std::numeric_limit<size_type>::max()
size_type sample_period() const
Figure out how often to take samples.
size_type max_step() const
Maximum number of steps, required if sample by step.
bool is_min_pass() const
Returns true if a minimum number of passes has been specified.
bool sample_by_step() const
Sample by step?
size_type N_sample() const
Returns the number of samples requested.
bool is_equilibration_passes_each_run() const
Returns true if explicit equilibration passes for each run have been specified.
size_type min_sample() const
Minimum number of steps, default 0.
size_type max_pass() const
Maximum number of passes, required if sample by pass.
bool is_N_sample() const
Returns true if the number of samples has been specified.
size_type equilibration_passes_each_run() const
Number of explicit equilibration passes requsted for each run.
bool is_equilibration_passes_first_run() const
Returns true if explicit equilibration passes for the first run have been specified.
size_type N_pass() const
Returns the number of passes requested.
bool is_max_sample() const
Returns true if a maximum number of samples has been specified.
size_type min_pass() const
Minimum number of passes, default 0 if sample by pass.
bool is_min_sample() const
Returns true if a minimum number of samples has been specified.
size_type N_step() const
Returns the number of steps requested.
bool is_N_step() const
Returns true if the number of steps has been specified.
size_type equilibration_passes_first_run() const
Number of explicit equilibration passes requsted for the first run.
bool dependent_runs() const
If dependent runs, start subsequent calculations with the final state of the previous calculation....
const PrimClex & primclex() const
Monte::METHOD method() const
Return type of Monte Carlo method.
std::string enumeration_metric_args() const
Returns 'casm query'-like enumeration metric args.
double enumeration_tol() const
Returns enumeration halloffame tolerance (default 1e-8)
bool debug() const
Run in debug mode?
double confidence() const
Given a settings jsonParser figure out the global tolerance (probably for == operator)....
const fs::path output_directory() const
Directory where output should go.
bool write_trajectory() const
Returns true if snapshots are requested.
bool enumeration_check_existence() const
Only insert configurations that are not already enumerated.
bool is_motif_configdof() const
Returns true if path to ConfigDoF file to use as starting motif has been specified.
std::string motif_configname() const
Configname of configuration to use as starting motif.
virtual const Monte::DRIVE_MODE drive_mode() const
Given a settings jsonParser figure out the drive mode. Expects drive_mode/incremental,...
std::string enumeration_check_args() const
Returns 'casm query'-like enumeration check args.
const PrimClex * m_primclex
bool write_observations() const
Writes all observations.
Eigen::Matrix3l simulation_cell_matrix() const
Supercell matrix defining the simulation cell.
Index enumeration_N_halloffame() const
Returns enumeration halloffame max size (default 100)
bool is_motif_configname() const
Returns true if configname of configuration to use as starting motif has been specified.
bool enumeration_insert_canonical() const
Insert configurations in their canonical form.
bool write_csv() const
Write csv versions of files? (csv is the default format if no 'output_format' given)
ConfigDoF motif_configdof(Index supercell_volume) const
ConfigDoF to use as starting motif.
Monte::ENUM_SAMPLE_MODE enumeration_sample_mode() const
Enumeration sample mode (default Monte::ENUM_SAMPLE_MODE::ON_SAMPLE)
fs::path motif_configdof_path() const
Path to ConfigDoF file to use as starting motif.
bool write_POSCAR_snapshots() const
Returns true if POSCARs of snapshots are requsted. Requires write_trajectory.
void set_debug(bool _debug)
Set debug mode.
MonteSettings()
Default constructor.
bool write_json() const
Write json versions of files?
bool is_enumeration() const
Returns true if enumeration is requested. (Default false)
bool _is_setting(std::string level1, std::string level2) const
Returns true if (*this)[level1].contains(level2)
Monte::ENSEMBLE ensemble() const
Return type of Monte Carlo ensemble.
PrimClex is the top-level data structure for a CASM project.
Definition: PrimClex.hh:55
jsonParser()
Create a new empty jsonParser.
Definition: jsonParser.hh:94
bool contains(const std::string &name) const
Return true if JSON object contains 'name'.
Definition: jsonParser.cc:601
iterator end()
Returns iterator to end of JSON object or JSON array.
Definition: jsonParser.cc:520
json_spirit::mObject::size_type size_type
Definition: jsonParser.hh:87
iterator find(const std::string &name)
Return iterator to JSON object value with 'name'.
Definition: jsonParser.cc:543
const PrimType & prim() const
const Access to primitive Structure
Definition: PrimClex.cc:262
METHOD
Monte Carlo method type.
DRIVE_MODE
How to change conditions.
ENSEMBLE
Monte Carlo ensemble type.
ENUM_SAMPLE_MODE
How often to sample runs.
Main CASM namespace.
Definition: APICommand.hh:8
fs::path find_casmroot(const fs::path &cwd)
std::string help()
Uses 'multiline_help<T>()' by default.
Definition: Help.hh:22
INDEX_TYPE Index
For long integer indexing:
Definition: definitions.hh:39
Log & err_log()
Definition: Log.hh:426
Matrix< long int, 3, 3 > Matrix3l
Definition: eigen.hh:12
pair_type ref
Definition: settings.cc:144