CASM  1.1.0
A Clusters Approach to Statistical Mechanics
MonteSampler.hh
Go to the documentation of this file.
1 #ifndef CASM_MonteSampler_HH
2 #define CASM_MonteSampler_HH
3 
10 
11 namespace CASM {
12 namespace Monte {
13 
14 class MonteCarlo;
15 
26 class MonteSampler {
27  public:
29  MonteSampler(const std::string &print_name, double data_confidence,
30  size_type data_initsize);
31 
33  MonteSampler(const std::string &print_name, double data_prec,
34  double data_confidence, size_type data_initsize);
35 
36  virtual ~MonteSampler() {}
37 
38  virtual void sample(const MonteCarlo &mc, const MonteCounter &counter) {
39  throw std::runtime_error("Error: MonteSampler base class used to sample");
40  }
41 
43  void clear() { m_data.clear(); }
44 
50  std::pair<bool, size_type> is_equilibrated() const {
51  if (!must_converge()) {
52  return std::make_pair(false, m_data.size());
53  }
54 
58  }
59 
60  return std::make_pair(m_equilibration.is_equilibrated(),
62  }
63 
66  bool must_converge() const { return m_must_converge; }
67 
69  double requested_precision() const { return m_prec; }
70 
75  bool is_converged(size_type equil_samples) const {
76  if (!must_converge()) {
77  return false;
78  }
79 
80  // if not calculated, or calculated for a different range of data,
81  // re-calculate
83  m_convergence_start_sample != equil_samples) {
84  _check_convergence(equil_samples);
85  }
86 
88  }
89 
91  double mean(size_type equil_samples) const {
92  // if not calculated, or calculated for a different range of data,
93  // re-calculate
95  m_convergence_start_sample != equil_samples) {
96  _check_convergence(equil_samples);
97  }
98 
99  return m_convergence.mean();
100  }
101 
103  double squared_norm(size_type equil_samples) const {
104  // if not calculated, or calculated for a different range of data,
105  // re-calculate
106  if (!m_convergence_uptodate ||
107  m_convergence_start_sample != equil_samples) {
108  _check_convergence(equil_samples);
109  }
110 
111  return m_convergence.squared_norm();
112  }
113 
116  double calculated_precision(size_type equil_samples) const {
117  // if not calculated, or calculated for a different range of data,
118  // re-calculate
119  if (!m_convergence_uptodate ||
120  m_convergence_start_sample != equil_samples) {
121  _check_convergence(equil_samples);
122  }
123 
125  }
126 
128  const MCData &data() const { return m_data; }
129 
131  std::string name() const { return m_name; }
132 
134  std::unique_ptr<MonteSampler> clone() const {
135  return std::unique_ptr<MonteSampler>(this->_clone());
136  }
137 
138  protected:
141  m_convergence_uptodate = false;
142  m_equilibration_uptodate = false;
143  return m_data;
144  }
145 
146  private:
147  virtual MonteSampler *_clone() const { return new MonteSampler(*this); }
148 
149  void _check_convergence(size_type equil_samples) const {
150  m_convergence_start_sample = equil_samples;
152  m_data.observations().segment(
153  equil_samples, m_data.observations().size() - equil_samples),
154  m_conf);
155  m_convergence_uptodate = true;
156  }
157 
161 
163  double m_prec;
164 
166  double m_conf;
167 
170 
172  std::string m_name;
173 
174  // enable storing equilibration and convergence info
175 
176  mutable bool m_equilibration_uptodate = false;
178  mutable bool m_convergence_uptodate = false;
181 };
182 
186  public:
188  ScalarMonteSampler(std::string _property_name, std::string print_name,
189  double data_confidence, size_type data_initsize);
190 
192  ScalarMonteSampler(std::string _property_name, std::string print_name,
193  double data_prec, double data_confidence,
194  size_type data_initsize);
195 
197  void sample(const MonteCarlo &mc, const MonteCounter &counter);
198 
200  std::unique_ptr<ScalarMonteSampler> clone() const {
201  return std::unique_ptr<ScalarMonteSampler>(this->_clone());
202  }
203 
204  private:
205  virtual ScalarMonteSampler *_clone() const {
206  return new ScalarMonteSampler(*this);
207  }
208 
209  std::string m_property_name;
210 };
211 
215  public:
217  VectorMonteSampler(std::string _property_name, size_type _index,
218  std::string print_name, double data_confidence,
219  size_type data_initsize);
220 
222  VectorMonteSampler(std::string _property_name, size_type _index,
223  std::string print_name, double data_prec,
224  double data_confidence, size_type data_initsize);
225 
227  void sample(const MonteCarlo &mc, const MonteCounter &counter);
228 
230  std::unique_ptr<VectorMonteSampler> clone() const {
231  return std::unique_ptr<VectorMonteSampler>(this->_clone());
232  }
233 
234  private:
235  virtual VectorMonteSampler *_clone() const {
236  return new VectorMonteSampler(*this);
237  }
238 
239  std::string m_property_name;
240 
242 };
243 
247  public:
250  class Formatter {
251  public:
253  Formatter(const DataFormatter<Configuration> &formatter);
254 
256 
257  const DataFormatter<Configuration> &get() const { return m_formatter; }
258 
260  const Eigen::VectorXd &sample(const MonteCarlo &mc,
261  const MonteCounter &counter);
262 
263  private:
266  std::pair<size_type, size_type> m_last_sample;
267  };
268 
270  QueryMonteSampler(std::shared_ptr<QueryMonteSampler::Formatter> formatter,
271  size_type _index, std::string print_name,
272  double data_confidence, size_type data_initsize);
273 
275  QueryMonteSampler(std::shared_ptr<QueryMonteSampler::Formatter> formatter,
276  size_type _index, std::string print_name, double data_prec,
277  double data_confidence, size_type data_initsize);
278 
280  void sample(const MonteCarlo &mc, const MonteCounter &counter);
281 
283  std::unique_ptr<QueryMonteSampler> clone() const {
284  return std::unique_ptr<QueryMonteSampler>(this->_clone());
285  }
286 
287  private:
288  virtual QueryMonteSampler *_clone() const {
289  return new QueryMonteSampler(*this);
290  }
291 
293  std::shared_ptr<QueryMonteSampler::Formatter> m_formatter;
294 };
295 
301  public:
304  const CompositionConverter &_comp_converter,
305  std::string print_name, double data_confidence,
306  size_type data_initsize);
307 
310  const CompositionConverter &_comp_converter,
311  std::string print_name, double data_prec,
312  double data_confidence, size_type data_initsize);
313 
315  void sample(const MonteCarlo &mc, const MonteCounter &counter);
316 
318  std::unique_ptr<CompMonteSampler> clone() const {
319  return std::unique_ptr<CompMonteSampler>(this->_clone());
320  }
321 
322  private:
323  virtual CompMonteSampler *_clone() const {
324  return new CompMonteSampler(*this);
325  }
326 
328 
330 };
331 
337  public:
339  SiteFracMonteSampler(size_type _index, size_type _basis_size,
340  std::string print_name, double data_confidence,
341  size_type data_initsize);
342 
344  SiteFracMonteSampler(size_type _index, size_type _basis_size,
345  std::string print_name, double data_prec,
346  double data_confidence, size_type data_initsize);
347 
349  void sample(const MonteCarlo &mc, const MonteCounter &counter);
350 
352  std::unique_ptr<SiteFracMonteSampler> clone() const {
353  return std::unique_ptr<SiteFracMonteSampler>(this->_clone());
354  }
355 
356  private:
357  virtual SiteFracMonteSampler *_clone() const {
358  return new SiteFracMonteSampler(*this);
359  }
360 
362 
364 };
365 
371  public:
373  AtomFracMonteSampler(size_type _index, size_type _vacancy_index,
374  std::string print_name, double data_confidence,
375  size_type data_initsize);
376 
378  AtomFracMonteSampler(size_type _index, size_type _vacancy_index,
379  std::string print_name, double data_prec,
380  double data_confidence, size_type data_initsize);
381 
383  void sample(const MonteCarlo &mc, const MonteCounter &counter);
384 
386  std::unique_ptr<AtomFracMonteSampler> clone() const {
387  return std::unique_ptr<AtomFracMonteSampler>(this->_clone());
388  }
389 
390  private:
391  virtual AtomFracMonteSampler *_clone() const {
392  return new AtomFracMonteSampler(*this);
393  }
394 
396 
398 };
399 
400 } // namespace Monte
401 } // namespace CASM
402 
403 #endif
Convert between number of species per unit cell and parametric composition.
Extract data from objects of 'DataObject' class.
Sampler for atom fraction.
void sample(const MonteCarlo &mc, const MonteCounter &counter)
Sample data from a MonteCarlo calculation.
virtual AtomFracMonteSampler * _clone() const
AtomFracMonteSampler(size_type _index, size_type _vacancy_index, std::string print_name, double data_confidence, size_type data_initsize)
Construct sampler that does not need to converge.
std::unique_ptr< AtomFracMonteSampler > clone() const
Clone this object.
Sampler for parametric composition.
virtual CompMonteSampler * _clone() const
void sample(const MonteCarlo &mc, const MonteCounter &counter)
Sample data from a MonteCarlo calculation.
std::unique_ptr< CompMonteSampler > clone() const
Clone this object.
CompositionConverter m_comp_converter
CompMonteSampler(size_type _index, const CompositionConverter &_comp_converter, std::string print_name, double data_confidence, size_type data_initsize)
Construct sampler that does not need to converge.
Checks if a range of observations have converged.
Definition: MCData.hh:79
bool is_converged(double prec) const
Returns true if converged to the requested level.
Definition: MCData.hh:98
double calculated_precision() const
Calculated precision of <X>
Definition: MCData.hh:114
double squared_norm() const
<X*X>
Definition: MCData.hh:104
double mean() const
<X>
Definition: MCData.hh:101
Checks if a range of observations have equilibrated.
Definition: MCData.hh:58
size_type equilibration_samples() const
Definition: MCData.hh:70
MCData stores observations of properties.
Definition: MCData.hh:12
void clear()
Forget all the observed values (does not resize reserved space)
Definition: MCData.hh:24
Eigen::VectorBlock< const Eigen::VectorXd > observations() const
Return all observations.
Definition: MCData.hh:40
size_type size() const
Number of observations.
Definition: MCData.hh:45
Interface base class for all types of Monte Carlo simulations (not meant to be used polymorphically)
Definition: MonteCarlo.hh:44
Track the number of passes, steps and samples taken in a Monte Carlo calculation.
Definition: MonteCounter.hh:23
An abstract base class for sampling and storing data observations.
Definition: MonteSampler.hh:26
MonteSampler(const std::string &print_name, double data_confidence, size_type data_initsize)
Construct sampler that does not need to converge.
Definition: MonteSampler.cc:11
bool m_must_converge
If false, data observations are recorded but shouldn't be used to determine if Monte Carlo run has co...
bool is_converged(size_type equil_samples) const
Returns true if convergence criteria have been met for data sampled in range [equil_samples,...
Definition: MonteSampler.hh:75
std::string name() const
Property name for printing.
size_type m_convergence_start_sample
std::unique_ptr< MonteSampler > clone() const
Clone this object.
virtual MonteSampler * _clone() const
std::string m_name
Property name for printing.
void _check_convergence(size_type equil_samples) const
const MCData & data() const
const Access the raw data observation container
MCData m_data
An array of observations, one for every so many steps or passes.
double calculated_precision(size_type equil_samples) const
Returns calculated precision on the mean for data sampled in range [equil_samples,...
bool must_converge() const
Returns true if convergence criteria must be met for Monte Carlo calculation to be complete.
Definition: MonteSampler.hh:66
std::pair< bool, size_type > is_equilibrated() const
Returns pair(true, equil_steps) if equilibration has occured to required precision.
Definition: MonteSampler.hh:50
virtual void sample(const MonteCarlo &mc, const MonteCounter &counter)
Definition: MonteSampler.hh:38
double requested_precision() const
Returns requested precision on the mean.
Definition: MonteSampler.hh:69
double mean(size_type equil_samples) const
Returns <X> for data sampled in range [equil_samples, end)
Definition: MonteSampler.hh:91
MCDataEquilibration m_equilibration
MCDataConvergence m_convergence
void clear()
Clear all data observations.
Definition: MonteSampler.hh:43
MCData & data()
Access the raw data observation container.
double squared_norm(size_type equil_samples) const
Returns <X*X> for data sampled in range [equil_samples, end)
double m_prec
Requested precision, if must_converge.
double m_conf
Requested confidence, if must_converge.
Data structure to make queries occur once each sample time.
Formatter(const DataFormatter< Configuration > &formatter)
Construct sampler that does not need to converge.
const DataFormatter< Configuration > & get() const
DataFormatter< Configuration > & get()
DataFormatter< Configuration > m_formatter
const Eigen::VectorXd & sample(const MonteCarlo &mc, const MonteCounter &counter)
Evaluate datum formatters, if necessary, and return result.
std::pair< size_type, size_type > m_last_sample
Sampler for individual elements of a vector property.
std::shared_ptr< QueryMonteSampler::Formatter > m_formatter
QueryMonteSampler(std::shared_ptr< QueryMonteSampler::Formatter > formatter, size_type _index, std::string print_name, double data_confidence, size_type data_initsize)
Construct sampler that does not need to converge.
void sample(const MonteCarlo &mc, const MonteCounter &counter)
Sample data from a MonteCarlo calculation.
virtual QueryMonteSampler * _clone() const
std::unique_ptr< QueryMonteSampler > clone() const
Clone this object.
Sampler for a scalar property.
ScalarMonteSampler(std::string _property_name, std::string print_name, double data_confidence, size_type data_initsize)
Construct sampler that does not need to converge.
Definition: MonteSampler.cc:35
void sample(const MonteCarlo &mc, const MonteCounter &counter)
Sample data from a MonteCarlo calculation.
Definition: MonteSampler.cc:58
std::unique_ptr< ScalarMonteSampler > clone() const
Clone this object.
virtual ScalarMonteSampler * _clone() const
Sampler for site fraction.
std::unique_ptr< SiteFracMonteSampler > clone() const
Clone this object.
virtual SiteFracMonteSampler * _clone() const
SiteFracMonteSampler(size_type _index, size_type _basis_size, std::string print_name, double data_confidence, size_type data_initsize)
Construct sampler that does not need to converge.
void sample(const MonteCarlo &mc, const MonteCounter &counter)
Sample data from a MonteCarlo calculation.
Sampler for individual elements of a vector property.
std::unique_ptr< VectorMonteSampler > clone() const
Clone this object.
VectorMonteSampler(std::string _property_name, size_type _index, std::string print_name, double data_confidence, size_type data_initsize)
Construct sampler that does not need to converge.
Definition: MonteSampler.cc:72
virtual VectorMonteSampler * _clone() const
void sample(const MonteCarlo &mc, const MonteCounter &counter)
Sample data from a MonteCarlo calculation.
Definition: MonteSampler.cc:97
Main CASM namespace.
Definition: APICommand.hh:8
Eigen::VectorXd VectorXd