CASM
AClustersApproachtoStatisticalMechanics
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
ConfigEnumRandomOccupations.cc
Go to the documentation of this file.
3 #include "casm/clex/ScelEnum.hh"
4 #include "casm/external/MersenneTwister/MersenneTwister.h"
6 
7 extern "C" {
10  }
11 }
12 
13 namespace CASM {
14 
15  const std::string ConfigEnumRandomOccupations::enumerator_name = "ConfigEnumRandomOccupations";
16 
18  "ConfigEnumRandomOccupations: \n\n"
19 
20  " supercells: ScelEnum JSON settings (default='{\"existing_only\"=true}')\n"
21  " Indicate supercells to enumerate all occupational configurations in. May \n"
22  " be a JSON array of supercell names, or a JSON object specifying \n"
23  " supercells in terms of size and unit cell. By default, all existing \n"
24  " supercells are used. See 'ScelEnum' description for details. \n\n"
25 
26  " n_config: integer (optional, default=100) \n"
27  " How many random configurations to generate. Includes duplicate and pre-\n"
28  " existing configurations. \n\n"
29 
30  " primitive_only: bool (default=true)\n"
31  " If true, only the primitive form of a configuration is saved in the \n"
32  " configuration list. Otherwise, both primitive and non-primitive \n"
33  " configurations are saved. \n\n"
34 
35  " filter: string (optional, default=None)\n"
36  " A query command to use to filter which Configurations are kept. \n\n"
37 
38  " Examples:\n"
39  " To enumerate 200 random occupations in supercells up to and including size 4:\n"
40  " casm enum --method ConfigEnumRandomOccupations -i \n"
41  " '{\"supercell\":{\"max\":4}, \"n_config\": 200}' \n"
42  "\n"
43  " To enumerate 200 random occupations in all existing supercells:\n"
44  " casm enum --method ConfigEnumRandomOccupations -i '{\"n_config\": 200}' \n"
45  "\n"
46  " To enumerate 100 random occupations in all existing supercells:\n"
47  " casm enum --method ConfigEnumRandomOccupations' \n"
48  "\n"
49  " To enumerate 200 random occupations in particular supercells:\n"
50  " casm enum --method ConfigEnumRandomOccupations -i \n"
51  " '{ \n"
52  " \"supercells\": { \n"
53  " \"name\": [\n"
54  " \"SCEL1_1_1_1_0_0_0\",\n"
55  " \"SCEL2_1_2_1_0_0_0\",\n"
56  " \"SCEL4_1_4_1_0_0_0\"\n"
57  " ]\n"
58  " }, \n"
59  " \"n_config\": 200\n"
60  " }' \n\n";
61 
64  const jsonParser &_kwargs,
65  const Completer::EnumOption &enum_opt) {
66 
67  std::unique_ptr<ScelEnum> scel_enum = make_enumerator_scel_enum(primclex, _kwargs, enum_opt);
68  std::vector<std::string> filter_expr = make_enumerator_filter_expr(_kwargs, enum_opt);
69  MTRand mtrand;
70 
71  Index n_config;
72  _kwargs.get_else<Index>(n_config, "n_config", 100);
73 
74  bool primitive_only = true;
75  _kwargs.get_if(primitive_only, "primitive_only");
76 
77 
78  auto lambda = [&](Supercell & scel) {
79  return notstd::make_unique<ConfigEnumRandomOccupations>(scel, n_config, mtrand);
80  };
81 
82  int returncode = insert_configs(
84  primclex,
85  scel_enum->begin(),
86  scel_enum->end(),
87  lambda,
88  filter_expr,
89  primitive_only);
90 
91  return returncode;
92  }
93 
104  Supercell &_scel,
105  Index _n_config,
106  MTRand &_mtrand):
107  m_n_config(_n_config),
108  m_mtrand(&_mtrand),
109  m_max_allowed(_scel.max_allowed_occupation()) {
110 
111  if(m_n_config < 0) {
112  throw std::runtime_error("Error in ConfigEnumRandomOccupations: n_config < 0");
113  }
114  if(m_n_config == 0) {
115  this->_invalidate();
116  return;
117  }
118 
119  m_current = notstd::make_cloneable<Configuration>(_scel, this->source(0));
120  m_current->init_occupation();
121 
123  this->_initialize(&(*m_current));
124 
125  // Make initial random config
126  this->randomize();
127  _set_step(0);
128  _current().set_source(this->source(step()));
129  }
130 
133 
134  this->_increment_step();
135  if(step() < m_n_config) {
136  this->randomize();
137  _current().set_source(this->source(step()));
138  }
139  else {
140  this->_invalidate();
141  }
142 
143  }
144 
146  for(Index i = 0; i < m_current->size(); ++i) {
147  m_current->set_occ(i, m_mtrand->randInt(m_max_allowed[i]));
148  }
149  }
150 
151 }
void reset_properties(ConfigDoF &_dof)
Definition: ConfigDoF.hh:202
std::vector< std::string > make_enumerator_filter_expr(const jsonParser &_kwargs, const Completer::EnumOption &enum_opt)
Standardizes parsing casm enum filter expressions.
Definition: Enumerator.cc:91
bool get_if(T &t, const std::string &key, Args...args) const
Definition: jsonParser.hh:740
PrimClex * primclex
Definition: settings.cc:101
notstd::cloneable_ptr< Configuration > m_current
ConfigEnumRandomOccupations(Supercell &_scel, Index _n_config, MTRand &_mtrand)
Construct with a Supercell, using all permutations.
Base class for generic use of enumerators that may be accessed through the API.
Definition: Enumerator.hh:418
virtual jsonParser source(step_type step) const
Definition: Enumerator.hh:131
Main CASM namespace.
Definition: complete.cpp:8
Represents a supercell of the primitive parent crystal structure.
Definition: Supercell.hh:37
static int run(PrimClex &primclex, const jsonParser &kwargs, const Completer::EnumOption &enum_opt)
int insert_configs(std::string method, PrimClex &primclex, ScelIterator begin, ScelIterator end, ConfigEnumConstructor f, std::vector< std::string > filter_expr, bool primitive_only)
Standardizes insertion from enumerators that construct configurations.
Template class to be specialized for each enumerator that may be accessed via the API...
Definition: Enumerator.hh:521
value_type & _current()
Access the current ObjectType by reference.
Definition: Enumerator.hh:244
EigenIndex Index
For long integer indexing:
void _invalidate()
Call if enumeration complete.
Definition: Enumerator.hh:169
step_type step() const
Increments with each enumerated object.
Definition: Enumerator.hh:113
bool get_else(T &t, const std::string &key, const T &default_value, Args...args) const
Definition: jsonParser.hh:749
PrimClex is the top-level data structure for a CASM project.
Definition: PrimClex.hh:52
void _set_step(step_type val)
Set current step value.
Definition: Enumerator.hh:154
void set_source(const jsonParser &source)
void increment() override
Implements increment.
std::unique_ptr< ScelEnum > make_enumerator_scel_enum(PrimClex &primclex, const jsonParser &_kwargs, const Completer::EnumOption &enum_opt)
Standardizes parsing casm enum input options to make an ScelEnum.
Definition: Enumerator.cc:78
void _increment_step()
Increment current step value.
Definition: Enumerator.hh:159
CASM::EnumInterfaceBase * make_ConfigEnumRandomOccupations_interface()