CASM  1.1.0
A Clusters Approach to Statistical Mechanics
ConfigEnumRandomOccupationsInterface.cc
Go to the documentation of this file.
2 
3 #include "casm/app/APICommand.hh"
13 #include "casm/clex/PrimClex.hh"
16 
17 namespace CASM {
18 
20  std::string custom_options =
21  " n_config: integer (optional, default=100) \n"
22  " How many random configurations to generate. Includes duplicate and "
23  "pre-\n"
24  " existing configurations. "
25  " \n\n";
26 
27  std::string examples =
28  " Examples:\n"
29  " To enumerate 200 random occupations in supercells up to and "
30  "including size 4:\n"
31  " casm enum --method ConfigEnumRandomOccupations -i \n"
32  " '{\"supercells\":{\"max\":4}, \"n_config\": 200}' \n"
33  "\n"
34  " To enumerate 200 random occupations in all existing supercells:\n"
35  " casm enum --method ConfigEnumRandomOccupations --all -i "
36  "'{\"n_config\": 200}' \n"
37  "\n"
38  " To enumerate 100 random occupations in all existing supercells:\n"
39  " casm enum --method ConfigEnumRandomOccupations --all \n"
40  "\n"
41  " To enumerate 200 random occupations in particular supercells:\n"
42  " casm enum --method ConfigEnumRandomOccupations -i \n"
43  " '{ \n"
44  " \"scelnames\": [\n"
45  " \"SCEL1_1_1_1_0_0_0\",\n"
46  " \"SCEL2_1_2_1_0_0_0\",\n"
47  " \"SCEL4_1_4_1_0_0_0\"\n"
48  " ],\n"
49  " \"n_config\": 200\n"
50  " }' \n\n";
51 
52  return name() + ": \n\n" + custom_options + standard_ConfigEnumInput_help() +
53  examples;
54 }
55 
58 }
59 
61  MTRand &mtrand) {
62  Index n_config;
63  parser.optional_else(n_config, "n_config", Index{100});
64 
65  parser.value =
66  notstd::make_unique<ConfigEnumRandomOccupationsParams>(mtrand, n_config);
67 }
68 
70  PrimClex &primclex, jsonParser const &json_options,
71  jsonParser const &cli_options_as_json) const {
72  Log &log = CASM::log();
73 
74  log.subsection().begin("ConfigEnumRandomOccupations");
75  ParentInputParser parser =
76  make_enum_parent_parser(log, json_options, cli_options_as_json);
77  std::runtime_error error_if_invalid{
78  "Error reading ConfigEnumRandomOccupations JSON input"};
79 
80  log.custom("Checking input");
81 
82  // 1) Parse ConfigEnumOptions ------------------
83 
84  auto options_parser_ptr = parser.parse_as<ConfigEnumOptions>(
86  primclex.settings().query_handler<Configuration>().dict());
87  report_and_throw_if_invalid(parser, log, error_if_invalid);
88  ConfigEnumOptions const &options = *options_parser_ptr->value;
89  print_options(log, options);
90  log.set_verbosity(options.verbosity);
91 
92  // 2) Parse initial enumeration states ------------------
93 
94  auto input_parser_ptr =
95  parser.parse_as<std::vector<std::pair<std::string, ConfigEnumInput>>>(
96  primclex.shared_prim(), &primclex, primclex.db<Supercell>(),
97  primclex.db<Configuration>());
98  report_and_throw_if_invalid(parser, log, error_if_invalid);
99  auto const &named_initial_states = *input_parser_ptr->value;
100  print_initial_states(log, named_initial_states);
101 
102  // 3) Parse ConfigEnumRandomOccupationsParams ------------------
103 
104  MTRand mtrand;
105  auto random_occ_params_parser_ptr =
107  report_and_throw_if_invalid(parser, log, error_if_invalid);
108  ConfigEnumRandomOccupationsParams const &random_occ_params =
109  *random_occ_params_parser_ptr->value;
110 
111  // 4) Enumerate configurations ------------------
112 
113  auto make_enumerator_f = [&](Index index, std::string name,
114  ConfigEnumInput const &initial_state) {
115  return ConfigEnumRandomOccupations{initial_state, random_occ_params};
116  };
117 
121  formatter.push_back(ConfigEnumIO::name<ConfigEnumDataType>(),
122  ConfigEnumIO::selected<ConfigEnumDataType>(),
123  ConfigEnumIO::is_new<ConfigEnumDataType>(),
124  ConfigEnumIO::is_existing<ConfigEnumDataType>());
125  if (options.filter) {
126  formatter.push_back(
127  ConfigEnumIO::is_excluded_by_filter<ConfigEnumDataType>());
128  }
129  formatter.push_back(
130  ConfigEnumIO::initial_state_index<ConfigEnumDataType>(),
131  ConfigEnumIO::initial_state_name<ConfigEnumDataType>(),
132  ConfigEnumIO::initial_state_configname<ConfigEnumDataType>(),
133  ConfigEnumIO::n_selected_sites<ConfigEnumDataType>());
134 
135  log << std::endl;
136  log.begin("ConfigEnumRandomOccupations enumeration");
137 
138  enumerate_configurations(primclex, options, make_enumerator_f,
139  named_initial_states.begin(),
140  named_initial_states.end(), formatter);
141 
142  log.end_section();
143 }
144 
145 } // namespace CASM
Enumerate n random occupations in a particular Supercell.
void run(PrimClex &primclex, jsonParser const &json_options, jsonParser const &cli_options_as_json) const override
std::string name() const override
Enumeration method name (i.e. "ConfigEnumAllOccupations")
Extract data from objects of 'DataObject' class.
void push_back(const BaseDatumFormatter< DataObject > &new_formatter, const std::string &args)
RequiredType optional_else(fs::path option, const RequiredType &_default, Args &&... args)
std::shared_ptr< InputParser< RequiredType > > parse_as(Args &&... args)
std::unique_ptr< T > value
Definition: InputParser.hh:234
Definition: Log.hh:48
void end_section()
End a section.
Definition: Log.hh:192
void set_verbosity(int _verbosity)
Definition: Log.cc:57
void custom(const std::string &what)
Definition: Log.hh:139
Log & subsection()
Create a subsection.
Definition: Log.hh:186
void begin(const std::string &what)
Definition: Log.hh:114
PrimClex is the top-level data structure for a CASM project.
Definition: PrimClex.hh:55
Represents a supercell of the primitive parent crystal structure.
Definition: Supercell.hh:51
ConfigEnumData< ConfigEnumSiteDoFs, ConfigEnumInput > ConfigEnumDataType
Main CASM namespace.
Definition: APICommand.hh:8
std::string standard_ConfigEnumInput_help()
void print_initial_states(Log &log, NamedInitialStatesType const &named_initial_states)
Log & log()
Definition: Log.hh:424
ParentInputParser make_enum_parent_parser(Log &log, jsonParser const &json_options, jsonParser const &cli_options_as_json)
Combine –input / –settings JSON with CLI options.
void enumerate_configurations(PrimClex const &primclex, ConfigEnumOptions const &options, MakeEnumeratorFunction make_enumerator_f, InputNameValuePairIterator name_value_pairs_begin, InputNameValuePairIterator name_value_pairs_end, DataFormatter< ConfigEnumDataType > const &formatter)
Enumerate configurations.
void report_and_throw_if_invalid(KwargsParser const &parser, Log &log, ErrorType error)
void print_options(Log &log, ConfigEnumOptions const &options)
Definition: stream_io.cc:6
INDEX_TYPE Index
For long integer indexing:
Definition: definitions.hh:39
void parse(InputParser< ConfigEnumOptions > &parser, std::string method_name, PrimClex const &primclex, DataFormatterDictionary< Configuration > const &dict)
PrimClex * primclex
Definition: settings.cc:135
Options for the enumerate_configurations function.
int verbosity
Printing verbosity level.
std::function< bool(Configuration const &)> filter
If filter(configuration)==true, keep configuration, else skip.
Parameters controlling ConfigEnumRandomOccupations.