CASM  1.1.0
A Clusters Approach to Statistical Mechanics
MonteSettings.hh
Go to the documentation of this file.
1 #ifndef CASM_MonteSettings_HH
2 #define CASM_MonteSettings_HH
3 
4 #include <string>
5 
6 #include "casm/casm_io/Log.hh"
9 #include "casm/clex/PrimClex.hh"
14 
15 namespace CASM {
16 namespace Monte {
17 
18 class MonteCarlo;
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 CASM::jsonParser {
49  public:
50  using jsonParser::print;
51  using jsonParser::write;
52  using jsonParser::operator==;
53  using jsonParser::operator!=;
54 
55  typedef Index size_type;
56 
59 
61  MonteSettings(const PrimClex &_primclex, const fs::path &read_path);
62 
63  // --- Project root directory ---------------------------
64 
65  fs::path root() const;
66 
67  const PrimClex &primclex() const;
68 
69  // --- Type ---------------------------
70 
72  Monte::ENSEMBLE ensemble() const;
73 
75  Monte::METHOD method() const;
76 
78  bool debug() const;
79 
81  void set_debug(bool _debug);
82 
83  // --- Initialization ---------------------
84 
87  bool is_motif_configname() const;
88 
90  std::string motif_configname() const;
91 
94  bool is_motif_configdof() const;
95 
97  ConfigDoF motif_configdof(Index supercell_volume) const;
98 
100  fs::path motif_configdof_path() const;
101 
104 
105  // --- Driver ---------------------
106 
109  virtual const Monte::DRIVE_MODE drive_mode() const;
110 
114  bool dependent_runs() const;
115 
116  // --- Sampling -------------------
117 
120  // double tolerance() const;
121 
123  double confidence() const;
124 
126  bool write_trajectory() const;
127 
130  bool write_POSCAR_snapshots() const;
131 
133  bool write_observations() const;
134 
137  bool write_csv() const;
138 
140  bool write_json() const;
141 
143  const fs::path output_directory() const;
144 
145  // --- Enumerating Configurations ---
146 
148  bool is_enumeration() const;
149 
151  std::string enumeration_metric_args() const;
152 
154  std::string enumeration_check_args() const;
155 
159 
161  bool enumeration_insert_canonical() const;
162 
164  bool enumeration_check_existence() const;
165 
168 
170  double enumeration_tol() const;
171 
172  protected:
174  bool _is_setting(std::string level1, std::string level2) const;
175 
177  bool _is_setting(std::string level1, std::string level2,
178  std::string level3) const;
179 
181  template <typename T>
182  T _get_setting(std::string level1, std::string msg) const;
183 
185  template <typename T>
186  T _get_setting(std::string level1, std::string level2, std::string msg) const;
187 
189  template <typename T>
190  T _get_setting(std::string level1, std::string level2, std::string level3,
191  std::string msg) const;
192 
193  jsonParser _json() const { return *this; }
194 
195  private:
196  fs::path m_root;
199 };
200 
201 inline bool operator==(const jsonParser &json, const MonteSettings &settings) {
202  return settings == json;
203 }
204 
205 inline bool operator!=(const jsonParser &json, const MonteSettings &settings) {
206  return settings != json;
207 }
208 
210  public:
213 
215  EquilibriumMonteSettings(const PrimClex &_primclex, const fs::path &read_path)
216  : MonteSettings(_primclex, read_path) {}
217 
218  // --- MCData / Sampling ---------------------
219 
221  bool sample_by_pass() const;
222 
224  bool sample_by_step() const;
225 
227  size_type sample_period() const;
228 
232 
235 
239 
242 
244  bool is_N_pass() const;
245 
247  size_type N_pass() const;
248 
250  bool is_N_step() const;
251 
253  size_type N_step() const;
254 
256  bool is_N_sample() const;
257 
259  size_type N_sample() const;
260 
262  bool is_max_pass() const;
263 
265  size_type max_pass() const;
266 
268  bool is_min_pass() const;
269 
271  size_type min_pass() const;
272 
274  bool is_max_step() const;
275 
277  size_type max_step() const;
278 
280  bool is_min_step() const;
281 
283  size_type min_step() const;
284 
286  bool is_max_sample() const;
287 
290  size_type max_sample() const;
291 
293  bool is_min_sample() const;
294 
296  size_type min_sample() const;
297 
298  // --- Data ---------------------
299 
301  size_type max_data_length() const;
302 };
303 
305 template <typename T>
306 T MonteSettings::_get_setting(std::string level1, std::string msg) const {
307  try {
308  return (*this)[level1].get<T>();
309  }
310 
311  catch (std::runtime_error &e) {
313  std::stringstream ss;
314  ss << "Monte Carlo setting "
315  << "[\"" << level1 << "\"]";
316 
317  err_log.error<Log::standard>(ss.str());
318 
319  err_log << "Expected " << ss.str() << std::endl;
320  err_log << " Either this was not found, or the type is wrong."
321  << std::endl;
322  if (!msg.empty()) {
323  err_log << "[\"" << level1 << "\"]: ";
324  err_log << msg << std::endl;
325  }
326  throw;
327  }
328 }
329 
331 template <typename T>
332 T MonteSettings::_get_setting(std::string level1, std::string level2,
333  std::string msg) const {
334  try {
335  return (*this)[level1][level2].get<T>();
336  }
337 
338  catch (std::runtime_error &e) {
340  std::stringstream ss;
341  ss << "Monte Carlo setting [\"" << level1 << "\"][\"" << level2 << "\"]";
342 
343  err_log.error<Log::standard>(ss.str());
344 
345  err_log << "ERROR in MonteSettings::" << level2 << std::endl;
346  err_log << "Expected " << ss.str() << std::endl;
347  err_log << " Either this was not found, or the type is wrong."
348  << std::endl;
349  if (this->contains(level1)) {
350  err_log << "Found Settings[\"" << level1 << "\"], but not [\"" << level1
351  << "\"][\"" << level2 << "\"]" << std::endl;
352  err_log << "Settings[\"" << level1 << "\"]:\n"
353  << (*this)[level1] << std::endl;
354  } else {
355  err_log << "No Settings[\"" << level1 << "\"] found" << std::endl;
356  err_log << "Settings:\n"
357  << static_cast<const jsonParser &>(*this) << std::endl;
358  }
359  if (!msg.empty()) {
360  err_log << "[\"" << level1 << "\"][\"" << level2 << "\"]: ";
361  err_log << msg << std::endl;
362  }
363  throw;
364  }
365 }
366 
368 template <typename T>
369 T MonteSettings::_get_setting(std::string level1, std::string level2,
370  std::string level3, std::string msg) const {
371  try {
372  return (*this)[level1][level2][level3].get<T>();
373  }
374 
375  catch (std::runtime_error &e) {
377  std::stringstream ss;
378  ss << "Monte Carlo setting [\"" << level1 << "\"][\"" << level2 << "\"][\""
379  << level3 << "\"]";
380 
381  err_log.error<Log::standard>(ss.str());
382 
383  err_log << "Expected " << ss.str() << std::endl;
384  err_log << " Either this was not found, or the type is wrong."
385  << std::endl;
386  if (this->contains(level1)) {
387  if (this->contains(level2)) {
388  err_log << "Found Settings[\"" << level1 << "\"][\"" << level2
389  << "\"], \n"
390  "but not [\""
391  << level1 << "\"][\"" << level2 << "\"][\"" << level3 << "\"]"
392  << std::endl;
393  err_log << "Settings[\"" << level1 << "\"][\"" << level2 << "\"]:\n"
394  << (*this)[level1][level2] << std::endl;
395  } else {
396  err_log << "No Settings[\"" << level1 << "\"][\"" << level2
397  << "\"] found" << std::endl;
398  err_log << "Settings:\n" << (*this)[level1] << std::endl;
399  }
400  } else {
401  err_log << "No Settings[\"" << level1 << "\"] found" << std::endl;
402  err_log << "Settings:\n"
403  << static_cast<const jsonParser &>(*this) << std::endl;
404  }
405  if (!msg.empty()) {
406  err_log << "[\"" << level1 << "\"][\"" << level2 << "\"][\"" << level3
407  << "\"]: ";
408  err_log << msg << std::endl;
409  }
410  throw;
411  }
412 }
413 
414 } // namespace Monte
415 } // namespace CASM
416 
417 #endif
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.
EquilibriumMonteSettings()
Default constructor.
bool is_N_pass() const
Returns true if the number of passes has been specified.
EquilibriumMonteSettings(const PrimClex &_primclex, const fs::path &read_path)
Construct EquilibriumMonteSettings by reading a settings JSON file.
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.
Settings for Monte Carlo calculations.
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.
jsonParser _json() const
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.
T _get_setting(std::string level1, std::string msg) const
Returns (*this)[level1].get<T>();.
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
bool contains(const std::string &name) const
Return true if JSON object contains 'name'.
Definition: jsonParser.cc:601
void write(const std::string &file_name, unsigned int indent=2, unsigned int prec=12) const
Write json to file.
Definition: jsonParser.cc:196
void print(std::ostream &stream, unsigned int indent=2, unsigned int prec=12) const
Print json to stream.
Definition: jsonParser.cc:188
METHOD
Monte Carlo method type.
bool operator==(const jsonParser &json, const MonteSettings &settings)
DRIVE_MODE
How to change conditions.
bool operator!=(const jsonParser &json, const MonteSettings &settings)
ENSEMBLE
Monte Carlo ensemble type.
ENUM_SAMPLE_MODE
How often to sample runs.
Main CASM namespace.
Definition: APICommand.hh:8
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