CASM  1.1.0
A Clusters Approach to Statistical Mechanics
ConfigEnumRandomLocalInterface.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  " magnitude: positive number (optional, default=1.0) \n"
28  " Magnitude used to scale random vector at each site. If "
29  "\"distribution\" == \"normal\",\n"
30  " magnitude specifies standard deviation of D-dimensional Gaussian (D "
31  "is dimension of \n"
32  " site DoF value). If \"distribution\" == \"uniform\", magnitude is "
33  "radius of D-dimensional\n"
34  " ball from which random vectors are chosen.\n\n"
35 
36  " dof: string (required) \n"
37  " Name of site degree of freecom for which normal coordinates are to "
38  "be generated.\n"
39  " Must be one of the degrees of freedom under consideration in the "
40  "current project,\n"
41  " as determined by prim.json\n\n"
42 
43  " distribution: string (optional, default=\"normal\") \n"
44  " Distribution from which perturbation vectors are drawn. Options are "
45  "\"uniform\"\n"
46  " (i.e., uniform on the unit sphere), or \"normal\" (i.e., "
47  "zero-centered Gaussian).\n\n";
48 
49  std::string examples =
50  " Examples:\n"
51  " To enumerate 200 random occupations in supercells up to and "
52  "including size 4:\n"
53  " casm enum --method ConfigEnumRandomLocal -i \n"
54  " '{\"supercells\":{\"max\":4}, \"n_config\": 200}' \n"
55  "\n"
56  " To enumerate 200 random occupations in all existing supercells:\n"
57  " casm enum --method ConfigEnumRandomLocal -i '{\"n_config\": 200}' "
58  "\n"
59  "\n"
60  " To enumerate 100 random occupations in all existing supercells:\n"
61  " casm enum --method ConfigEnumRandomLocal --all \n"
62  "\n"
63  " To enumerate 200 random occupations in particular supercells:\n"
64  " casm enum --method ConfigEnumRandomLocal -i \n"
65  " '{ \n"
66  " \"scelnames\": [\n"
67  " \"SCEL1_1_1_1_0_0_0\",\n"
68  " \"SCEL2_1_2_1_0_0_0\",\n"
69  " \"SCEL4_1_4_1_0_0_0\"\n"
70  " ],\n"
71  " \"n_config\": 200\n"
72  " }' \n\n";
73 
74  return name() + ": \n\n" + custom_options + standard_ConfigEnumInput_help() +
75  examples;
76 }
77 
80 }
81 
82 void parse(InputParser<ConfigEnumRandomLocalParams> &parser, MTRand &mtrand) {
83  DoFKey dof_key;
84  double mag;
85  Index n_config;
86  bool normal_distribution;
87 
88  parser.require(dof_key, "dof");
89  parser.optional_else(mag, "magnitude", double{1.0});
90  parser.optional_else(n_config, "n_config", Index{100});
91 
92  std::string distribution_name;
93  parser.optional_else(distribution_name, "distribution",
94  std::string{"normal"});
95  if (distribution_name == "normal") {
96  normal_distribution = true;
97  } else if (distribution_name == "uniform") {
98  normal_distribution = false;
99  } else {
100  std::stringstream msg;
101  msg << "Error: \"distribution\" must be \"normal\" (default) or "
102  "\"uniform\"";
103  parser.error.insert(msg.str());
104  }
105 
106  parser.value = notstd::make_unique<ConfigEnumRandomLocalParams>(
107  mtrand, dof_key, n_config, mag, normal_distribution);
108 }
109 
111  PrimClex &primclex, jsonParser const &json_options,
112  jsonParser const &cli_options_as_json) const {
113  Log &log = CASM::log();
114 
115  log.subsection().begin("ConfigEnumRandomLocal");
116  ParentInputParser parser =
117  make_enum_parent_parser(log, json_options, cli_options_as_json);
118  std::runtime_error error_if_invalid{
119  "Error reading ConfigEnumRandomLocal JSON input"};
120 
121  log.custom("Checking input");
122 
123  // 1) Parse ConfigEnumOptions ------------------
124 
125  auto options_parser_ptr = parser.parse_as<ConfigEnumOptions>(
127  primclex.settings().query_handler<Configuration>().dict());
128  report_and_throw_if_invalid(parser, log, error_if_invalid);
129  ConfigEnumOptions const &options = *options_parser_ptr->value;
130  print_options(log, options);
131  log.set_verbosity(options.verbosity);
132 
133  // 2) Parse initial enumeration states ------------------
134 
135  auto input_parser_ptr =
136  parser.parse_as<std::vector<std::pair<std::string, ConfigEnumInput>>>(
137  primclex.shared_prim(), &primclex, primclex.db<Supercell>(),
138  primclex.db<Configuration>());
139  report_and_throw_if_invalid(parser, log, error_if_invalid);
140  auto const &named_initial_states = *input_parser_ptr->value;
141  print_initial_states(log, named_initial_states);
142 
143  // 3) Parse ConfigEnumRandomLocalParams ------------------
144 
145  MTRand mtrand;
146  auto random_local_params_parser_ptr =
147  parser.parse_as<ConfigEnumRandomLocalParams>(mtrand);
148  report_and_throw_if_invalid(parser, log, error_if_invalid);
149  ConfigEnumRandomLocalParams const &random_local_params =
150  *random_local_params_parser_ptr->value;
151 
152  // 4) Enumerate configurations ------------------
153 
154  auto make_enumerator_f = [&](Index index, std::string name,
155  ConfigEnumInput const &initial_state) {
156  return ConfigEnumRandomLocal{initial_state, random_local_params};
157  };
158 
162  formatter.push_back(ConfigEnumIO::name<ConfigEnumDataType>(),
163  ConfigEnumIO::selected<ConfigEnumDataType>(),
164  ConfigEnumIO::is_new<ConfigEnumDataType>(),
165  ConfigEnumIO::is_existing<ConfigEnumDataType>());
166  if (options.filter) {
167  formatter.push_back(
168  ConfigEnumIO::is_excluded_by_filter<ConfigEnumDataType>());
169  }
170  formatter.push_back(
171  ConfigEnumIO::initial_state_index<ConfigEnumDataType>(),
172  ConfigEnumIO::initial_state_name<ConfigEnumDataType>(),
173  ConfigEnumIO::initial_state_configname<ConfigEnumDataType>(),
174  ConfigEnumIO::n_selected_sites<ConfigEnumDataType>());
175 
176  log << std::endl;
177  log.begin("ConfigEnumRandomLocal enumeration");
178 
179  enumerate_configurations(primclex, options, make_enumerator_f,
180  named_initial_states.begin(),
181  named_initial_states.end(), formatter);
182 
183  log.end_section();
184 }
185 
186 } // namespace CASM
Enumerate random values for continuous degrees of freedom.
static const std::string enumerator_name
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::unique_ptr< RequiredType > require(fs::path option, 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.
std::string DoFKey
Definition: DoFDecl.hh:7
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 ConfigEnumRandomLocal.
std::set< std::string > error
Definition: Validator.hh:11