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