CASM
AClustersApproachtoStatisticalMechanics
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
MonteCarloEnum.cc
Go to the documentation of this file.
3 
4 namespace CASM {
5 
8  if(insert_canonical()) {
9  return m_halloffame->insert(config.in_canonical_supercell().canonical_form());
10  }
11  else {
12  return m_halloffame->insert(config);
13  }
14  }
15 
25 
26  bool check = (*m_enum_check)(config);
27 
28  if(!check) {
29  if(debug()) {
30  _log().custom("Config enumeration");
31  _log() << "enum check: " << std::boolalpha << check << std::endl;
32  _log() << std::endl;
33 
34  print_info();
35  }
36 
38  m_halloffame->end(),
39  false,
40  std::numeric_limits<double>::quiet_NaN(),
41  false,
42  m_halloffame->end());
43  }
44 
45  auto res = _insert(config);
46 
47  if(debug()) {
48  _log().custom("Config enumeration");
49  _log() << "enum check: " << std::boolalpha << check << std::endl;
50  _log() << "score: " << res.score << std::endl;
51  _log() << "insert config in hall of fame: " << std::boolalpha << res.success << std::endl;
52  if(!res.success) {
53  if(res.excluded) {
54  _log() << "already in config list: " << res.excluded_pos->second.name() << std::endl;
55  }
56  else if(res.pos != m_halloffame->end()) {
57  _log() << "already in hall of fame: #" << std::distance(m_halloffame->begin(), res.pos) << std::endl;
58  }
59  else {
60  _log() << "score not good enough" << std::endl;
61  }
62  }
63  _log() << std::endl;
64 
65  print_info();
66  }
67  return res;
68  }
69 
72  if(m_halloffame) {
73  return *m_halloffame;
74  }
75  else {
76  throw std::runtime_error("Error accessing Monte Carlo HallOfFame: was not initialized");
77  }
78  }
79 
80  namespace {
81 
87  std::pair<std::string, bool> _import_primitive(PrimClex &primclex, Supercell &scel, Index config_index) {
88 
89  BasicStructure<Site> nonprim = scel.superstructure(config_index);
90 
92  nonprim.is_primitive(prim);
93 
94  int map_opt = ConfigMapper::none;
95  double tol = TOL;
96  double vol_tol = 0.25;
97  double lattice_weight = 0.5;
98  ConfigMapper configmapper(primclex, lattice_weight, vol_tol, map_opt, tol);
99 
100  std::string imported_name;
101  Eigen::Matrix3d cart_op;
102  std::vector<Index> best_assignment;
103  jsonParser fullrelax_data;
104  bool is_new = configmapper.import_structure_occupation(prim,
105  imported_name,
106  fullrelax_data,
107  best_assignment,
108  cart_op,
109  true);
110  return std::make_pair(imported_name, is_new);
111  }
112  }
113 
116 
117  if(!halloffame().size()) {
118  _log().write("Enumerated configurations to master config list");
119  _log() << "No configurations in hall of fame\n";
120  _log() << std::endl;
121  return;
122  }
123 
124  std::vector<Configuration> output;
125  m_data.clear();
126 
127  Index config_index;
129  bool is_new, is_prim;
130  std::string configname;
131 
132  // transform hall of fame configurations so that they fill the canonical
133  // equivalent supercell, and add to project
134  for(const auto &val : halloffame()) {
135 
136  // get equivalent configuration (not necessarily canonical) in the
137  // canonical equivalent supercell stored in the primclex
138  Configuration config = val.second.in_canonical_supercell();
139  Supercell &canon_scel = config.get_supercell();
140 
141  // add config to supercell (the saved config will be canonical)
142  is_new = canon_scel.add_config(config, config_index, permute_it);
143  Configuration &canon_config = canon_scel.get_config(config_index);
144 
145  is_prim = is_primitive(canon_config);
146  if(is_new) {
147  _halloffame().exclude(canon_config);
148  }
149 
150  double score = val.first;
151 
152  // store config source info
153  jsonParser json_src;
154  std::stringstream ss;
155  ss << std::setprecision(6) << score;
156  json_src["monte_carlo_enumeration"]["metric"] = m_metric_args;
157  json_src["monte_carlo_enumeration"]["score"] = ss.str();
158  canon_config.push_back_source(json_src);
159 
160  // store info for printing
161  m_data[canon_config.name()] = std::make_pair(is_new, score);
162  output.push_back(canon_config);
163 
164  // if not primitive, generate and import the primitive configuration
165  if(!is_prim) {
166  std::tie(configname, is_new) = _import_primitive(this->_primclex(), canon_scel, config_index);
167  Configuration &prim_canon_config = _primclex().configuration(configname);
168  if(is_new) {
169  _halloffame().exclude(prim_canon_config);
170  }
171 
172  // store config source info
173  prim_canon_config.push_back_source(json_src);
174 
175  // store info for printing
176  m_data[prim_canon_config.name()] = std::make_pair(is_new, score);
177  output.push_back(prim_canon_config);
178  }
179  }
181 
182  auto formatter = m_dict.parse("configname is_primitive is_new score comp potential_energy");
183  auto flag = FormatFlag(_log()).print_header(true);
184 
185  _log().write("Enumerated configurations to master config list");
186  _log() << "configuration enumeration check: " << m_check_args << "\n";
187  _log() << "configuration enumeration metric: " << m_metric_args << "\n";
188  _log() << flag << formatter(output.begin(), output.end());
189  _log() << std::endl;
190 
191  }
192 
194 
195  _log().custom("Enumerated configurations hall of fame");
196  _log() << "configuration enumeration check: " << m_check_args << "\n";
197  _log() << "configuration enumeration metric: " << m_metric_args << "\n";
198  _log() << std::setw(16) << "position" << std::setw(16) << "score" << "\n";
199  _log() << std::setw(16) << std::string("-", 12) << std::setw(16) << std::string("-", 12) << "\n";
200 
201  Index i = 0;
202  for(const auto &val : halloffame()) {
203  _log() << std::setw(16) << i << std::setw(16) << val.first << "\n";
204  i++;
205  }
206  _log() << std::endl;
207  }
208 
211  m_halloffame->clear();
212  if(check_existence()) {
213  m_halloffame->clear_excluded();
214  m_halloffame->exclude(this->primclex().config_begin(), this->primclex().config_end());
215  }
216  }
217 
219  if(m_halloffame) {
220  return *m_halloffame;
221  }
222  else {
223  throw std::runtime_error("Error accessing Monte Carlo HallOfFame: was not initialized");
224  }
225  }
226 }
227 
const Configuration & get_config(Index i) const
Definition: Supercell.hh:287
void exclude(const ObjectType &obj)
Add an object that should not be included in the hall of fame.
Definition: HallOfFame.hh:92
void write_config_list(std::set< std::string > scel_to_delete={})
Definition: PrimClex.cc:450
bool print_header() const
Definition: FormatFlag.hh:33
const HallOfFameType & halloffame() const
const Access the enumeration hall of fame
void reset()
Clear hall of fame and reset excluded.
notstd::cloneable_ptr< HallOfFameType > m_halloffame
Use for enumerating configurations via Monte Carlo.
bool debug() const
return true if running in debug mode
void write(const std::string &what)
Definition: Log.hh:66
std::map< std::string, std::pair< bool, double > > m_data
holds 'is_new, score' data
PrimClex * primclex
Definition: settings.cc:101
const PrimClex & primclex() const
const Access the PrimClex that *this is based on
bool is_primitive(double prim_tol=TOL) const
Configuration canonical_form() const
Returns the canonical form Configuration in the same Supercell.
MonteCarloEnum::HallOfFameType::InsertResult _insert(const Configuration &config)
Insert in hall of fame if 'check' passes.
Results data structure for HallOfFame::insert.
Definition: HallOfFame.hh:58
void push_back_source(const jsonParser &source)
A container for storing best scoring objects.
Definition: HallOfFame.hh:19
Main CASM namespace.
Definition: complete.cpp:8
const double TOL
Represents a supercell of the primitive parent crystal structure.
Definition: Supercell.hh:37
void save_configs()
Save configurations in the hall of fame to the config list.
bool insert_canonical() const
Map for faster? access of PrimClex's supercells.
double tol
void custom(const std::string &what)
Definition: Log.hh:96
HallOfFameType::InsertResult insert(const Configuration &config)
Attempt to insert (canonical) Configuration into enumeration hall of fame.
EigenIndex Index
For long integer indexing:
Structure superstructure() const
Definition: Supercell.cc:1037
bool add_config(const Configuration &config)
Definition: Supercell.cc:568
PrimClex is the top-level data structure for a CASM project.
Definition: PrimClex.hh:52
std::string name() const
SCELV_A_B_C_D_E_F/i.
const Configuration & configuration(const std::string &configname) const
access configuration by name (of the form "scellname/[NUMBER]", e.g., ("SCEL1_1_1_1_0_0_0/0") ...
Definition: PrimClex.cc:347
HallOfFameType & _halloffame()
bool check_existence() const
If true, insert configurations in canonical form.
ConfigIO::GenericConfigFormatter< std::string > configname()
Constructs DataFormmaterDictionary containing all Configuration DatumFormatters.
Definition: ConfigIO.cc:340
bool check(std::string test, const jsonParser &expected, const jsonParser &calculated, fs::path test_cases_path, bool quiet, double tol=0.0)
Check expected JSON vs calculated JSON using BOOST_CHECK_EQUAL.
PrimClex & _primclex() const
Access the PrimClex that *this is based on.
void print_info() const
Eigen::Matrix3d Matrix3d
Configuration in_canonical_supercell() const
Returns the canonical form Configuration in the canonical Supercell.
DataFormatterDictionary< Configuration > m_dict
Used for various purposes.
Supercell & get_supercell() const
Get the Supercell for this Configuration.
bool is_primitive(const Configuration &_config)
returns true if _config describes primitive cell of the configuration it describes ...
A Configuration represents the values of all degrees of freedom in a Supercell.