CASM
AClustersApproachtoStatisticalMechanics
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
MonteCarlo.cc
Go to the documentation of this file.
3 
4 namespace CASM {
5 
6 
7  bool SamplerNameCompare::operator()(const std::string &A, const std::string &B) const {
8  std::string::size_type Apos1 = A.find_first_of("([");
9  std::string::size_type Bpos1 = B.find_first_of("([");
10  if(A.substr(0, Apos1) == B.substr(0, Bpos1)) {
11  std::string::size_type Apos2 = A.find_first_of("])");
12  std::string::size_type Bpos2 = B.find_first_of("])");
13 
14  std::string Aindex = A.substr(Apos1 + 1, Apos2 - Apos1 - 1);
15  std::string Bindex = B.substr(Bpos1 + 1, Bpos2 - Bpos1 - 1);
16 
17  for(int i = 0; i < Aindex.size(); i++) {
18  if(!std::isdigit(Aindex[i])) {
19  return Aindex < Bindex;
20  }
21  }
22 
23  return std::stoi(Aindex) < std::stoi(Bindex);
24  }
25  return A.substr(0, Apos1) < B.substr(0, Bpos1);
26  }
27 
29  void MonteCarlo::sample_data(const MonteCounter &counter) {
30 
31  // call MonteSamper::sample(*this) for all samplers
32  for(auto it = m_sampler.begin(); it != m_sampler.end(); ++it) {
33  it->second->sample(*this, counter);
34  }
35  m_sample_time.push_back(std::make_pair(counter.pass(), counter.step()));
36 
37  if(m_write_trajectory) {
38  m_trajectory.push_back(configdof());
39  }
40 
41  m_is_equil_uptodate = false;
43  }
44 
47  for(auto it = m_sampler.begin(); it != m_sampler.end(); ++it) {
48  it->second->clear();
49  }
50  m_trajectory.clear();
51  m_sample_time.clear();
52 
53  m_is_equil_uptodate = false;
56  }
57 
63  std::pair<bool, MonteSampler::size_type> MonteCarlo::is_equilibrated() const {
64 
66  return m_is_equil;
67  }
68 
69  m_is_equil_uptodate = true;
70 
71 
72  _log().check<Log::verbose>("Equilibration");
73  _log() << std::boolalpha
74  << std::setw(24) << "quantity"
75  << std::setw(20) << "is_equilibrated"
76  << std::setw(16) << "at_sample"
77  << std::endl;
78  // check if all samplers that must converge have equilibrated, and find the maximum
79  // of the number of samples needed to equilibrate
80  MonteSampler::size_type max_equil_samples = 0;
81  for(auto it = m_sampler.cbegin(); it != m_sampler.cend(); ++it) {
82  if(it->second->must_converge()) {
83 
84  // is_equilibrated returns std::pair(is_equilibrated?, equil_samples)
85  auto equil = it->second->is_equilibrated();
86 
87  _log() << std::setw(24) << it->second->name()
88  << std::setw(20) << (equil.first ? "true" : "false"); // why isn't boolapha working?
89  if(equil.first) {
90  _log() << std::setw(16) << equil.second << std::endl;
91  }
92  else {
93  _log() << std::setw(16) << "unknown" << std::endl;
94  }
95 
96  if(!equil.first) {
97  return m_is_equil = std::make_pair(false, MonteSampler::size_type(0));
98  }
99  if(equil.second > max_equil_samples) {
100  max_equil_samples = equil.second;
101  }
102  }
103  else {
104  // _log() << std::setw(24) << it->second->name()
105  // << std::setw(20) << "unknown"
106  // << std::setw(16) << "unknown" << std::endl;
107  }
108  }
109 
110  _log() << "Overall equilibration at sample: " << max_equil_samples << "\n" << std::endl;
111 
112  return m_is_equil = std::make_pair(true, max_equil_samples);
113  }
114 
123 
124  // if we've already calculated convergence, return result
126  return m_is_converged;
127  }
128 
130 
131  // set the next time for a convergence check
133 
134  if(!m_sampler.size()) {
135  m_is_converged = false;
136  return m_is_converged;
137  }
138 
139  // check if required equilibration has occured, and get the number of samples required to equilibrate
140  auto equil = is_equilibrated();
141  if(!equil.first) {
142  m_is_converged = false;
143  return m_is_converged;
144  }
145 
146  _log().check<Log::verbose>("Convergence");
147  _log() << std::boolalpha
148  << std::setw(24) << "quantity"
149  << std::setw(16) << "mean"
150  << std::setw(16) << "req_prec"
151  << std::setw(16) << "calc_prec"
152  << std::setw(16) << "is_converged"
153  << std::endl;
154 
155  // check if all samplers that must converge have converged using data in range [max_equil_samples, end)
156  m_is_converged = std::all_of(m_sampler.cbegin(),
157  m_sampler.cend(),
158  [ = ](const SamplerMap::value_type & val) {
159  if(val.second->must_converge()) {
160  bool result = val.second->is_converged(equil.second);
161  _log() << std::setw(24) << val.second->name()
162  << std::setw(16) << val.second->mean(equil.second)
163  << std::setw(16) << val.second->requested_precision()
164  << std::setw(16) << val.second->calculated_precision(equil.second)
165  << std::setw(16) << (result ? "true" : "false") // why isn't boolapha working?
166  << std::endl;
167  return result;
168  }
169  else {
170  // _log() << std::setw(24) << val.second->name()
171  // << std::setw(16) << val.second->mean(equil.second)
172  // << std::setw(16) << "none"
173  // << std::setw(16) << "unknown"
174  // << std::setw(16) << "unknown"
175  // << std::endl;
176  return true;
177  }
178  });
179 
180  _log() << "Overall convergence?: " << std::boolalpha << m_is_converged << "\n" << std::endl;
181 
182  return m_is_converged;
183  }
184 
189 
191  return true;
192  }
193  return false;
194  }
195 
200 
202 
203  }
204 
205 }
void clear_samples()
Clear all data from all samplers.
Definition: MonteCarlo.cc:46
std::pair< bool, MonteSampler::size_type > m_is_equil
Definition: MonteCarlo.hh:302
bool m_is_converged_uptodate
Definition: MonteCarlo.hh:305
std::pair< bool, MonteSampler::size_type > is_equilibrated() const
Returns pair(true, equil_samples) if required equilibration has occured for all samplers that must co...
Definition: MonteCarlo.cc:63
SampleTimes m_sample_time
a vector of std::pair(pass, step) indicating when samples were taken
Definition: MonteCarlo.hh:297
const ConfigDoF & configdof() const
const Access current microstate
Definition: MonteCarlo.hh:93
Main CASM namespace.
Definition: complete.cpp:8
bool m_is_equil_uptodate
Definition: MonteCarlo.hh:303
bool operator()(const std::string &A, const std::string &B) const
Definition: MonteCarlo.cc:7
Track the number of passes, steps and samples taken in a Monte Carlo calculation. ...
Definition: MonteCounter.hh:20
size_type pass() const
Number of complete passes performed.
Definition: MonteCounter.cc:91
MonteSampler::size_type m_next_convergence_check
Definition: MonteCarlo.hh:308
static const int verbose
Definition: Log.hh:16
Log & _log() const
Definition: MonteCarlo.hh:208
SamplerMap m_sampler
a map of pair to MonteSampler
Definition: MonteCarlo.hh:294
size_type step() const
Number of steps into the current pass.
Definition: MonteCounter.cc:95
bool is_converged() const
Check to see if all the properties required to converge have converged.
Definition: MonteCarlo.cc:122
MCData::size_type size_type
Definition: MonteSampler.hh:26
void sample_data(const MonteCounter &counter)
Samples all requested property data, and stores pass and step number sample was taken at...
Definition: MonteCarlo.cc:29
void _set_check_convergence_time() const
Set the next time convergence is due to be checked.
Definition: MonteCarlo.cc:199
bool check_convergence_time() const
Returns true if a convergence check is due.
Definition: MonteCarlo.cc:188
MonteSampler::size_type m_convergence_check_period
Definition: MonteCarlo.hh:309
void check(const std::string &what)
Definition: Log.hh:51
std::vector< ConfigDoF > m_trajectory
Snapshots of the Monte Carlo simulation, taken by sample_data() if m_write_trajectory is true...
Definition: MonteCarlo.hh:278
bool m_write_trajectory
Save trajectory?
Definition: MonteCarlo.hh:275