CASM  1.1.0
A Clusters Approach to Statistical Mechanics
MonteCarlo.hh
Go to the documentation of this file.
1 #ifndef CASM_MonteCarlo_HH
2 #define CASM_MonteCarlo_HH
3 
4 #include <string>
5 #include <vector>
6 
7 #include "casm/clex/ConfigDoF.hh"
9 #include "casm/clex/Supercell.hh"
10 #include "casm/external/MersenneTwister/MersenneTwister.h"
14 
15 namespace CASM {
16 
17 class Log;
18 class PrimClex;
19 class SuperNeighborList;
20 
21 } // namespace CASM
22 
23 namespace CASM {
24 namespace Monte {
25 
26 class MonteSampler;
27 class MonteSettings;
28 class MonteCounter;
29 
32 
33  bool operator()(const std::string &A, const std::string &B) const;
34 };
35 
44 class MonteCarlo {
45  public:
47 
51  typedef std::map<std::string, double> ScalarPropertyMap;
52 
56  typedef std::map<std::string, Eigen::VectorXd> VectorPropertyMap;
57 
63  typedef std::map<std::string, notstd::cloneable_ptr<MonteSampler>,
66 
69  typedef std::vector<std::pair<size_type, size_type> > SampleTimes;
70 
71  // ---- Accessors -----------------------------
72 
74  const MonteSettings &settings() const { return m_settings; }
75 
77  const PrimClex &primclex() const { return m_primclex; }
78 
80  const Supercell &supercell() const { return m_scel; }
81 
83  void set_nlist() { m_nlist = &(supercell().nlist()); }
84 
87  const SuperNeighborList &nlist() const { return *m_nlist; }
88 
90  const Configuration &config() const { return m_config; }
91 
93  const ConfigDoF &configdof() const { return m_configdof; }
94 
95  // ---- Accessors -----------------------------
96 
98  void reset(const ConfigDoF &dof) {
99  _configdof() = dof;
100  clear_samples();
101  }
102 
103  // ---- Properties ----------------
104 
107  return m_scalar_property;
108  }
109 
111  const double &scalar_property(std::string property_name) const {
112  return m_scalar_property.find(property_name)->second;
113  }
114 
117  return m_vector_property;
118  }
119 
121  const Eigen::VectorXd &vector_property(std::string property_name) const {
122  return m_vector_property.find(property_name)->second;
123  }
124 
125  // ---- Data sampling -------------
126 
129  void sample_data(const MonteCounter &counter);
130 
132  void clear_samples();
133 
135  bool must_converge() const { return m_must_converge; }
136 
139  std::pair<bool, size_type> is_equilibrated() const;
140 
142  bool check_convergence_time() const;
143 
146  bool is_converged() const;
147 
149  const SamplerMap &samplers() const { return m_sampler; }
150 
153  const SampleTimes &sample_times() const { return m_sample_time; }
154 
159  const std::vector<ConfigDoF> &trajectory() const { return m_trajectory; }
160 
162  bool debug() const { return m_debug; }
163 
164  protected:
167  template <typename MonteTypeSettings>
168  MonteCarlo(const PrimClex &primclex, const MonteTypeSettings &settings,
169  Log &_log);
170 
172  const PrimClex &_primclex() const { return m_primclex; }
173 
175  const Supercell &_supercell() const { return m_scel; }
176 
181  Configuration &_config() const { return m_config; }
182 
187  ConfigDoF &_configdof() const { return m_configdof; }
188 
189  Log &_log() const { return m_log; }
190 
191  MTRand &_mtrand() { return m_twister; }
192 
195 
197  double &_scalar_property(std::string property_name) {
198  return m_scalar_property.find(property_name)->second;
199  }
200 
203 
205  Eigen::VectorXd &_vector_property(std::string property_name) {
206  return m_vector_property.find(property_name)->second;
207  }
208 
209  private:
214 
219 
222 
225 
228 
231 
237 
241 
243  MTRand m_twister;
244 
246  bool m_write_trajectory = false;
247 
250  std::vector<ConfigDoF> m_trajectory;
251 
254 
257 
259  void _set_check_convergence_time() const;
260 
267 
271 
272  // Members to remember results of is_equilibrated and is_converged
273 
274  mutable std::pair<bool, size_type> m_is_equil;
275  mutable bool m_is_equil_uptodate = false;
276  mutable bool m_is_converged;
277  mutable bool m_is_converged_uptodate = false;
278 
279  // which sample to check convergence after
282 
283  // in debug mode, allow printing or checking extra things
284  bool m_debug;
285 };
286 
287 } // namespace Monte
288 } // namespace CASM
289 
290 #endif
Definition: Log.hh:48
Interface base class for all types of Monte Carlo simulations (not meant to be used polymorphically)
Definition: MonteCarlo.hh:44
double & _scalar_property(std::string property_name)
Access a particular scalar property.
Definition: MonteCarlo.hh:197
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
VectorPropertyMap & _vector_properties()
const Access vector properties map
Definition: MonteCarlo.hh:202
std::map< std::string, double > ScalarPropertyMap
a map of keyname to property value
Definition: MonteCarlo.hh:51
void clear_samples()
Clear all data from all samplers.
Definition: MonteCarlo.cc:50
std::map< std::string, notstd::cloneable_ptr< MonteSampler >, SamplerNameCompare > SamplerMap
a map of keyname to MonteSampler
Definition: MonteCarlo.hh:65
Supercell m_scel
Supercell for the calculation.
Definition: MonteCarlo.hh:227
SamplerMap m_sampler
a map of pair<keyname, index> to MonteSampler
Definition: MonteCarlo.hh:266
const SuperNeighborList * m_nlist
Pointer to SuperNeighborList.
Definition: MonteCarlo.hh:230
bool check_convergence_time() const
Returns true if a convergence check is due.
Definition: MonteCarlo.cc:190
ConfigDoF & _configdof() const
Access current microstate.
Definition: MonteCarlo.hh:187
const double & scalar_property(std::string property_name) const
const Access a particular scalar property
Definition: MonteCarlo.hh:111
Log & m_log
Target for messages.
Definition: MonteCarlo.hh:256
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
const VectorPropertyMap & vector_properties() const
const Access vector properties map
Definition: MonteCarlo.hh:116
const Supercell & _supercell() const
Access the Supercell that *this is based on.
Definition: MonteCarlo.hh:175
bool m_write_trajectory
Save trajectory?
Definition: MonteCarlo.hh:246
const PrimClex & _primclex() const
Access the PrimClex that *this is based on.
Definition: MonteCarlo.hh:172
std::vector< std::pair< size_type, size_type > > SampleTimes
a vector of std::pair(pass, step) indicating when samples were taken
Definition: MonteCarlo.hh:69
SampleTimes m_sample_time
a vector of std::pair(pass, step) indicating when samples were taken
Definition: MonteCarlo.hh:270
const ScalarPropertyMap & scalar_properties() const
const Access scalar properties map
Definition: MonteCarlo.hh:106
void set_nlist()
Set a pointer to the SuperNeighborList once it is ready.
Definition: MonteCarlo.hh:83
const PrimClex & m_primclex
PrimClex for this system.
Definition: MonteCarlo.hh:224
const SuperNeighborList & nlist() const
const Access the SuperNeighborList via pointer stored by 'set_nlist'
Definition: MonteCarlo.hh:87
ScalarPropertyMap m_scalar_property
a map of keyname to property value
Definition: MonteCarlo.hh:213
const MonteSettings & m_settings
Contains all input settings.
Definition: MonteCarlo.hh:221
ScalarPropertyMap & _scalar_properties()
Access scalar properties map.
Definition: MonteCarlo.hh:194
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
const Supercell & supercell() const
const Access the Supercell that *this is based on
Definition: MonteCarlo.hh:80
const MonteSettings & settings() const
const Access settings used for construction
Definition: MonteCarlo.hh:74
bool is_converged() const
Check to see if all the properties required to converge have converged.
Definition: MonteCarlo.cc:126
void reset(const ConfigDoF &dof)
Set current microstate and clear samplers.
Definition: MonteCarlo.hh:98
const Configuration & config() const
const Access current microstate
Definition: MonteCarlo.hh:90
const PrimClex & primclex() const
const Access the PrimClex that *this is based on
Definition: MonteCarlo.hh:77
Configuration & _config() const
Access current microstate.
Definition: MonteCarlo.hh:181
Monte::size_type size_type
Definition: MonteCarlo.hh:46
size_type m_convergence_check_period
Definition: MonteCarlo.hh:281
Eigen::VectorXd & _vector_property(std::string property_name)
const Access a particular vector property
Definition: MonteCarlo.hh:205
VectorPropertyMap m_vector_property
a map of keyname to property value Eigen::VectorXd
Definition: MonteCarlo.hh:218
const Eigen::VectorXd & vector_property(std::string property_name) const
const Access a particular vector property
Definition: MonteCarlo.hh:121
Configuration m_config
Stores all degrees of freedom of the current microstate.
Definition: MonteCarlo.hh:236
MonteCarlo(const PrimClex &primclex, const MonteTypeSettings &settings, Log &_log)
Construct with a starting ConfigDoF as specified the given MonteSettings and prepare data samplers.
const std::vector< ConfigDoF > & trajectory() const
const Access snapshots of the Monte Carlo calculation
Definition: MonteCarlo.hh:159
const ConfigDoF & configdof() const
const Access current microstate
Definition: MonteCarlo.hh:93
std::map< std::string, Eigen::VectorXd > VectorPropertyMap
a map of keyname to property value Eigen::VectorXd
Definition: MonteCarlo.hh:56
bool debug() const
return true if running in debug mode
Definition: MonteCarlo.hh:162
MTRand m_twister
Random number generator.
Definition: MonteCarlo.hh:243
const SamplerMap & samplers() const
const Access sampler map
Definition: MonteCarlo.hh:149
std::pair< bool, size_type > m_is_equil
Definition: MonteCarlo.hh:274
bool must_converge() const
Return true if convergence is requested.
Definition: MonteCarlo.hh:135
bool m_must_converge
True if any MonteSampler must converge.
Definition: MonteCarlo.hh:253
const SampleTimes & sample_times() const
const Access a vector of std::pair<pass, step> indicating when samples were taken
Definition: MonteCarlo.hh:153
Track the number of passes, steps and samples taken in a Monte Carlo calculation.
Definition: MonteCounter.hh:23
Settings for Monte Carlo calculations.
PrimClex is the top-level data structure for a CASM project.
Definition: PrimClex.hh:55
Represents a supercell of the primitive parent crystal structure.
Definition: Supercell.hh:51
const SuperNeighborList & nlist() const
Returns the SuperNeighborList.
Definition: Supercell.cc:244
Main CASM namespace.
Definition: APICommand.hh:8
Eigen::VectorXd VectorXd
bool operator()(const std::string &A, const std::string &B) const
Definition: MonteCarlo.cc:10