CASM  1.1.0
A Clusters Approach to Statistical Mechanics
MonteCarloEnum.cc
Go to the documentation of this file.
5 
6 namespace CASM {
7 namespace Monte {
8 
11  const Configuration &config) {
12  if (insert_canonical()) {
13  return m_halloffame->insert(
14  config.in_canonical_supercell().canonical_form());
15  } else {
16  return m_halloffame->insert(config);
17  }
18 }
19 
29  const Configuration &config) {
30  bool check = (*m_enum_check)(config);
31 
32  if (!check) {
33  if (debug()) {
34  _log().custom("Config enumeration");
35  _log() << "enum check: " << std::boolalpha << check << std::endl;
36  _log() << std::endl;
37 
38  print_info();
39  }
40 
42  m_halloffame->end(), false, std::numeric_limits<double>::quiet_NaN(),
43  false, m_halloffame->end());
44  }
45 
46  auto res = _insert(config);
47 
48  if (debug()) {
49  _log().custom("Config enumeration");
50  _log() << "enum check: " << std::boolalpha << check << std::endl;
51  _log() << "score: " << res.score << std::endl;
52  _log() << "insert config in hall of fame: " << std::boolalpha << res.success
53  << std::endl;
54  if (!res.success) {
55  if (res.excluded) {
56  _log() << "already in config list: " << res.excluded_pos->second.name()
57  << std::endl;
58  } else if (res.pos != m_halloffame->end()) {
59  _log() << "already in hall of fame: #"
60  << std::distance(m_halloffame->begin(), res.pos) << std::endl;
61  } else {
62  _log() << "score not good enough" << std::endl;
63  }
64  }
65  _log() << std::endl;
66 
67  print_info();
68  }
69  return res;
70 }
71 
74  if (m_halloffame) {
75  return *m_halloffame;
76  } else {
77  throw std::runtime_error(
78  "Error accessing Monte Carlo HallOfFame: was not initialized");
79  }
80 }
81 
84  if (!halloffame().size()) {
85  _log().write("Enumerated configurations to master config list");
86  _log() << "No configurations in hall of fame\n";
87  _log() << std::endl;
88  return;
89  }
90 
91  std::vector<Configuration> output;
92  m_data.clear();
93 
94  // transform hall of fame configurations so that they fill the canonical
95  // equivalent supercell, and add to project
96  for (const auto &val : halloffame()) {
97  double score = val.first;
98  auto insert_res = val.second.insert();
99 
100  // store config source info
101  jsonParser json_src;
102  std::stringstream ss;
103  ss << std::setprecision(6) << score;
104  json_src["monte_carlo_enumeration"]["metric"] = m_metric_args;
105  json_src["monte_carlo_enumeration"]["score"] = ss.str();
106 
107  auto lambda = [&](const Configuration &config, bool is_new) {
108  if (is_new) {
109  // necessary if included now, but pushed out of HallOfFame later
110  this->_halloffame().exclude(config);
111  }
112 
113  // store source info
114  Configuration tconfig{config};
115  tconfig.push_back_source(json_src);
116  this->primclex().db<Configuration>().update(tconfig);
117 
118  // store info for printing
119  this->m_data[config.name()] = std::make_pair(is_new, score);
120  output.push_back(config);
121  };
122 
123  lambda(*insert_res.canonical_it, insert_res.insert_canonical);
124 
125  if (insert_res.canonical_it != insert_res.primitive_it) {
126  lambda(*insert_res.primitive_it, insert_res.insert_primitive);
127  }
128  }
129 
131 
132  auto formatter = m_dict.parse(
133  "configname is_primitive is_new score comp potential_energy");
134  auto flag = FormatFlag(_log()).print_header(true);
135 
136  _log().write("Enumerated configurations to master config list");
137  _log() << "configuration enumeration check: " << m_check_args << "\n";
138  _log() << "configuration enumeration metric: " << m_metric_args << "\n";
139  _log() << flag << formatter(output.begin(), output.end());
140  _log() << std::endl;
141 }
142 
144  _log().custom("Enumerated configurations hall of fame");
145  _log() << "configuration enumeration check: " << m_check_args << "\n";
146  _log() << "configuration enumeration metric: " << m_metric_args << "\n";
147  _log() << std::setw(16) << "position" << std::setw(16) << "score"
148  << "\n";
149  _log() << std::setw(16) << std::string("-", 12) << std::setw(16)
150  << std::string("-", 12) << "\n";
151 
152  Index i = 0;
153  for (const auto &val : halloffame()) {
154  _log() << std::setw(16) << i << std::setw(16) << val.first << "\n";
155  i++;
156  }
157  _log() << std::endl;
158 }
159 
162  m_halloffame->clear();
163  if (check_existence()) {
164  m_halloffame->clear_excluded();
165  // pushes back ALL configurations in database into the exclude set
166  const auto &db = primclex().db<Configuration>();
167  m_halloffame->exclude(db.begin(), db.end());
168  }
169 }
170 
172  if (m_halloffame) {
173  return *m_halloffame;
174  } else {
175  throw std::runtime_error(
176  "Error accessing Monte Carlo HallOfFame: was not initialized");
177  }
178 }
179 
180 } // namespace Monte
181 } // namespace CASM
bool print_header() const
Definition: FormatFlag.hh:31
A container for storing best scoring objects.
Definition: HallOfFame.hh:21
void exclude(const ObjectType &obj)
Add an object that should not be included in the hall of fame.
Definition: HallOfFame.hh:99
void custom(const std::string &what)
Definition: Log.hh:139
void write(const std::string &what)
Definition: Log.hh:109
std::map< std::string, std::pair< bool, double > > m_data
holds 'is_new, score' data
const HallOfFameType & halloffame() const
const Access the enumeration hall of fame
MonteCarloEnum::HallOfFameType::InsertResult _insert(const Configuration &config)
Insert in hall of fame if 'check' passes.
bool debug() const
return true if running in debug mode
void save_configs()
Save configurations in the hall of fame to the config list.
bool check_existence() const
If true, insert configurations in canonical form.
const PrimClex & primclex() const
const Access the PrimClex that *this is based on
notstd::cloneable_ptr< HallOfFameType > m_halloffame
Use for enumerating configurations via Monte Carlo.
bool insert_canonical() const
Map for faster? access of PrimClex's supercells.
void reset()
Clear hall of fame and reset excluded.
DataFormatterDictionary< Configuration > m_dict
Used for various purposes.
HallOfFameType & _halloffame()
HallOfFameType::InsertResult insert(const Configuration &config)
Attempt to insert (canonical) Configuration into enumeration hall of fame.
DB::Database< T > & db() const
Definition: PrimClex.cc:302
void commit(ProjectSettings const &set)
GenericDatumFormatter< bool, ConfigEnumDataType > is_new()
ConfigIO::GenericConfigFormatter< jsonParser > config()
Definition: ConfigIO.cc:777
GenericDatumFormatter< double, Result > score()
bool check(const Lattice &lat)
Main CASM namespace.
Definition: APICommand.hh:8
INDEX_TYPE Index
For long integer indexing:
Definition: definitions.hh:39
Results data structure for HallOfFame::insert.
Definition: HallOfFame.hh:53