CASM
AClustersApproachtoStatisticalMechanics
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
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 
13  class MonteCarlo;
14 
22  class MonteSampler {
23 
24  public:
25 
27 
29  MonteSampler(const std::string &print_name,
30  double data_confidence,
31  size_type data_initsize);
32 
34  MonteSampler(const std::string &print_name,
35  double data_prec,
36  double data_confidence,
37  size_type data_initsize);
38 
39  virtual ~MonteSampler() {}
40 
41 
42  virtual void sample(const MonteCarlo &mc, const MonteCounter &counter) {
43  throw std::runtime_error("Error: MonteSampler base class used to sample");
44  }
45 
47  void clear() {
48  m_data.clear();
49  }
50 
55  std::pair<bool, size_type> is_equilibrated() const {
56 
57  if(!must_converge()) {
58  return std::make_pair(false, m_data.size());
59  }
60 
64  }
65 
67  }
68 
70  bool must_converge() const {
71  return m_must_converge;
72  }
73 
75  double requested_precision() const {
76  return m_prec;
77  }
78 
82  bool is_converged(size_type equil_samples) const {
83 
84  if(!must_converge()) {
85  return false;
86  }
87 
88  // if not calculated, or calculated for a different range of data, re-calculate
89  if(!m_convergence_uptodate || m_convergence_start_sample != equil_samples) {
90  _check_convergence(equil_samples);
91  }
92 
94  }
95 
97  double mean(size_type equil_samples) const {
98 
99  // if not calculated, or calculated for a different range of data, re-calculate
100  if(!m_convergence_uptodate || m_convergence_start_sample != equil_samples) {
101  _check_convergence(equil_samples);
102  }
103 
104  return m_convergence.mean();
105  }
106 
108  double squared_norm(size_type equil_samples) const {
109 
110  // if not calculated, or calculated for a different range of data, re-calculate
111  if(!m_convergence_uptodate || m_convergence_start_sample != equil_samples) {
112  _check_convergence(equil_samples);
113  }
114 
115  return m_convergence.squared_norm();
116  }
117 
119  double calculated_precision(size_type equil_samples) const {
120 
121  // if not calculated, or calculated for a different range of data, re-calculate
122  if(!m_convergence_uptodate || m_convergence_start_sample != equil_samples) {
123  _check_convergence(equil_samples);
124  }
125 
127  }
128 
130  const MCData &data() const {
131  return m_data;
132  }
133 
135  std::string name() const {
136  return m_name;
137  }
138 
140  std::unique_ptr<MonteSampler> clone() const {
141  return std::unique_ptr<MonteSampler>(this->_clone());
142  }
143 
144 
145  protected:
146 
149  m_convergence_uptodate = false;
150  m_equilibration_uptodate = false;
151  return m_data;
152  }
153 
154  private:
155 
156  virtual MonteSampler *_clone() const {
157  return new MonteSampler(*this);
158  }
159 
160  void _check_convergence(size_type equil_samples) const {
161 
162  m_convergence_start_sample = equil_samples;
163  m_convergence = MCDataConvergence(m_data.observations().segment(equil_samples, m_data.observations().size() - equil_samples), m_conf);
164  m_convergence_uptodate = true;
165  }
166 
169 
171  double m_prec;
172 
174  double m_conf;
175 
178 
180  std::string m_name;
181 
182  // enable storing equilibration and convergence info
183 
184  mutable bool m_equilibration_uptodate = false;
186  mutable bool m_convergence_uptodate = false;
187  mutable size_type m_convergence_start_sample = 0;
189  };
190 
191 
195 
196  public:
197 
199  ScalarMonteSampler(std::string _property_name,
200  std::string print_name,
201  double data_confidence,
202  size_type data_initsize);
203 
205  ScalarMonteSampler(std::string _property_name,
206  std::string print_name,
207  double data_prec,
208  double data_confidence,
209  size_type data_initsize);
210 
211 
213  void sample(const MonteCarlo &mc, const MonteCounter &counter);
214 
216  std::unique_ptr<ScalarMonteSampler> clone() const {
217  return std::unique_ptr<ScalarMonteSampler>(this->_clone());
218  }
219 
220  private:
221 
222  virtual ScalarMonteSampler *_clone() const {
223  return new ScalarMonteSampler(*this);
224  }
225 
226  std::string m_property_name;
227 
228  };
229 
233 
234  public:
235 
236  typedef Index size_type;
237 
239  VectorMonteSampler(std::string _property_name,
240  size_type _index,
241  std::string print_name,
242  double data_confidence,
243  size_type data_initsize);
244 
246  VectorMonteSampler(std::string _property_name,
247  size_type _index,
248  std::string print_name,
249  double data_prec,
250  double data_confidence,
251  size_type data_initsize);
252 
253 
255  void sample(const MonteCarlo &mc, const MonteCounter &counter);
256 
258  std::unique_ptr<VectorMonteSampler> clone() const {
259  return std::unique_ptr<VectorMonteSampler>(this->_clone());
260  }
261 
262  private:
263 
264  virtual VectorMonteSampler *_clone() const {
265  return new VectorMonteSampler(*this);
266  }
267 
268  std::string m_property_name;
269 
270  size_type m_index;
271 
272  };
273 
277 
278  public:
279 
282  class Formatter {
283 
284  public:
285 
286  typedef Index size_type;
287 
289  Formatter(const DataFormatter<Configuration> &formatter);
290 
292  return m_formatter;
293  }
294 
295  const DataFormatter<Configuration> &get() const {
296  return m_formatter;
297  }
298 
300  const Eigen::VectorXd &sample(const MonteCarlo &mc, const MonteCounter &counter);
301 
302  private:
303 
306  std::pair<MonteCounter::size_type, MonteCounter::size_type> m_last_sample;
307  };
308 
309  typedef Index size_type;
310 
312  QueryMonteSampler(std::shared_ptr<QueryMonteSampler::Formatter> formatter,
313  size_type _index,
314  std::string print_name,
315  double data_confidence,
316  size_type data_initsize);
317 
319  QueryMonteSampler(std::shared_ptr<QueryMonteSampler::Formatter> formatter,
320  size_type _index,
321  std::string print_name,
322  double data_prec,
323  double data_confidence,
324  size_type data_initsize);
325 
326 
328  void sample(const MonteCarlo &mc, const MonteCounter &counter);
329 
331  std::unique_ptr<QueryMonteSampler> clone() const {
332  return std::unique_ptr<QueryMonteSampler>(this->_clone());
333  }
334 
335  private:
336 
337  virtual QueryMonteSampler *_clone() const {
338  return new QueryMonteSampler(*this);
339  }
340 
341  size_type m_index;
342  std::shared_ptr<QueryMonteSampler::Formatter> m_formatter;
343  };
344 
350 
351  public:
352 
353  typedef Index size_type;
354 
356  CompMonteSampler(size_type _index,
357  const CompositionConverter &_comp_converter,
358  std::string print_name,
359  double data_confidence,
360  size_type data_initsize);
361 
363  CompMonteSampler(size_type _index,
364  const CompositionConverter &_comp_converter,
365  std::string print_name,
366  double data_prec,
367  double data_confidence,
368  size_type data_initsize);
369 
370 
372  void sample(const MonteCarlo &mc, const MonteCounter &counter);
373 
375  std::unique_ptr<CompMonteSampler> clone() const {
376  return std::unique_ptr<CompMonteSampler>(this->_clone());
377  }
378 
379  private:
380 
381  virtual CompMonteSampler *_clone() const {
382  return new CompMonteSampler(*this);
383  }
384 
385  size_type m_index;
386 
388 
389  };
390 
391 
397 
398  public:
399 
400  typedef Index size_type;
401 
403  SiteFracMonteSampler(size_type _index,
404  size_type _basis_size,
405  std::string print_name,
406  double data_confidence,
407  size_type data_initsize);
408 
410  SiteFracMonteSampler(size_type _index,
411  size_type _basis_size,
412  std::string print_name,
413  double data_prec,
414  double data_confidence,
415  size_type data_initsize);
416 
417 
419  void sample(const MonteCarlo &mc, const MonteCounter &counter);
420 
422  std::unique_ptr<SiteFracMonteSampler> clone() const {
423  return std::unique_ptr<SiteFracMonteSampler>(this->_clone());
424  }
425 
426  private:
427 
428  virtual SiteFracMonteSampler *_clone() const {
429  return new SiteFracMonteSampler(*this);
430  }
431 
432  size_type m_index;
433 
434  size_type m_basis_size;
435 
436  };
437 
438 
444 
445  public:
446 
447  typedef Index size_type;
448 
450  AtomFracMonteSampler(size_type _index,
451  size_type _vacancy_index,
452  std::string print_name,
453  double data_confidence,
454  size_type data_initsize);
455 
457  AtomFracMonteSampler(size_type _index,
458  size_type _vacancy_index,
459  std::string print_name,
460  double data_prec,
461  double data_confidence,
462  size_type data_initsize);
463 
464 
466  void sample(const MonteCarlo &mc, const MonteCounter &counter);
467 
469  std::unique_ptr<AtomFracMonteSampler> clone() const {
470  return std::unique_ptr<AtomFracMonteSampler>(this->_clone());
471  }
472 
473  private:
474 
475  virtual AtomFracMonteSampler *_clone() const {
476  return new AtomFracMonteSampler(*this);
477  }
478 
479  size_type m_index;
480 
481  size_type m_vacancy_index;
482 
483  };
484 
485 }
486 
487 #endif
488 
489 
virtual VectorMonteSampler * _clone() const
Checks if a range of observations have converged.
Definition: MCData.hh:97
void _check_convergence(size_type equil_samples) const
void sample(const MonteCarlo &mc, const MonteCounter &counter)
Sample data from a MonteCarlo calculation.
Definition: MonteSampler.cc:67
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.
Eigen::VectorBlock< const Eigen::VectorXd > observations() const
Return all observations.
Definition: MCData.hh:44
Formatter(const DataFormatter< Configuration > &formatter)
Construct sampler that does not need to converge.
double mean() const
Definition: MCData.hh:123
bool must_converge() const
Returns true if convergence criteria must be met for Monte Carlo calculation to be complete...
Definition: MonteSampler.hh:70
Sampler for atom fraction.
virtual SiteFracMonteSampler * _clone() const
void sample(const MonteCarlo &mc, const MonteCounter &counter)
Sample data from a MonteCarlo calculation.
MonteSampler(const std::string &print_name, double data_confidence, size_type data_initsize)
Construct sampler that does not need to converge.
Definition: MonteSampler.cc:10
double squared_norm() const
Definition: MCData.hh:128
size_type m_convergence_start_sample
bool is_equilibrated() const
Definition: MCData.hh:79
std::unique_ptr< AtomFracMonteSampler > clone() const
Clone this object.
std::unique_ptr< QueryMonteSampler > clone() const
Clone this object.
Sampler for a scalar property.
An abstract base class for sampling and storing data observations.
Definition: MonteSampler.hh:22
Main CASM namespace.
Definition: complete.cpp:8
std::shared_ptr< QueryMonteSampler::Formatter > m_formatter
Sampler for individual elements of a vector property.
Data structure to make queries occur once each sample time.
std::string m_name
Property name for printing.
double m_prec
Requested precision, if must_converge.
Track the number of passes, steps and samples taken in a Monte Carlo calculation. ...
Definition: MonteCounter.hh:20
bool is_converged(double prec) const
Returns true if converged to the requested level.
Definition: MCData.hh:118
double m_conf
Requested confidence, if must_converge.
double requested_precision() const
Returns requested precision on the mean.
Definition: MonteSampler.hh:75
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.
std::unique_ptr< ScalarMonteSampler > clone() const
Clone this object.
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.
void sample(const MonteCarlo &mc, const MonteCounter &counter)
Sample data from a MonteCarlo calculation.
void clear()
Clear all data observations.
Definition: MonteSampler.hh:47
std::unique_ptr< VectorMonteSampler > clone() const
Clone this object.
std::unique_ptr< MonteSampler > clone() const
Clone this object.
std::string name() const
Property name for printing.
EigenIndex Index
For long integer indexing:
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.
virtual ~MonteSampler()
Definition: MonteSampler.hh:39
double squared_norm(size_type equil_samples) const
Returns for data sampled in range [equil_samples, end)
void sample(const MonteCarlo &mc, const MonteCounter &counter)
Sample data from a MonteCarlo calculation.
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:42
Sampler for individual elements of a vector property.
void sample(const MonteCarlo &mc, const MonteCounter &counter)
Sample data from a MonteCarlo calculation.
size_type equilibration_samples() const
Definition: MCData.hh:83
const MCData & data() const
const Access the raw data observation container
DataFormatter< Configuration > m_formatter
MCData & data()
Access the raw data observation container.
MCData m_data
An array of observations, one for every so many steps or passes.
Eigen::VectorXd VectorXd
virtual AtomFracMonteSampler * _clone() const
MCData stores observations of properties.
Definition: MCData.hh:9
Checks if a range of observations have equilibrated.
Definition: MCData.hh:66
virtual CompMonteSampler * _clone() const
MCData::size_type size_type
Definition: MonteSampler.hh:26
Sampler for parametric composition.
MCDataConvergence m_convergence
Interface base class for all types of Monte Carlo simulations (not meant to be used polymorphically) ...
Definition: MonteCarlo.hh:32
virtual MonteSampler * _clone() const
unsigned long int size_type
Definition: MCData.hh:13
size_type size() const
Number of observations.
Definition: MCData.hh:49
void clear()
Forget all the observed values (does not resize reserved space)
Definition: MCData.hh:25
Convert between number of species per unit cell and parametric composition.
virtual void sample(const MonteCarlo &mc, const MonteCounter &counter)
Definition: MonteSampler.hh:42
Extract data from objects of 'DataObject' class.
std::pair< MonteCounter::size_type, MonteCounter::size_type > m_last_sample
virtual ScalarMonteSampler * _clone() const
std::unique_ptr< SiteFracMonteSampler > clone() const
Clone this object.
void sample(const MonteCarlo &mc, const MonteCounter &counter)
Sample data from a MonteCarlo calculation.
CompositionConverter m_comp_converter
std::pair< bool, size_type > is_equilibrated() const
Returns pair(true, equil_steps) if equilibration has occured to required precision.
Definition: MonteSampler.hh:55
const Eigen::VectorXd & sample(const MonteCarlo &mc, const MonteCounter &counter)
Evaluate datum formatters, if necessary, and return result.
MCDataEquilibration m_equilibration
double mean(size_type equil_samples) const
Returns for data sampled in range [equil_samples, end)
Definition: MonteSampler.hh:97
double calculated_precision() const
Calculated precision of
Definition: MCData.hh:139
double calculated_precision(size_type equil_samples) const
Returns calculated precision on the mean for data sampled in range [equil_samples, end)
std::unique_ptr< CompMonteSampler > 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:82
virtual QueryMonteSampler * _clone() const
Sampler for site fraction.
bool is_converged(size_type equil_samples) const
Returns true if convergence criteria have been met for data sampled in range [equil_samples, end)
Definition: MonteSampler.hh:82
bool m_must_converge
If false, data observations are recorded but shouldn't be used to determine if Monte Carlo run has co...