CASM
AClustersApproachtoStatisticalMechanics
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
MonteCarlo.hh
Go to the documentation of this file.
1 #ifndef CASM_MonteCarlo_HH
2 #define CASM_MonteCarlo_HH
3 
4 #include <vector>
6 #include "casm/external/MersenneTwister/MersenneTwister.h"
7 #include "casm/casm_io/Log.hh"
8 #include "casm/clex/PrimClex.hh"
9 #include "casm/clex/Supercell.hh"
10 #include "casm/clex/ConfigDoF.hh"
14 
15 namespace CASM {
16 
18 
20 
21  bool operator()(const std::string &A, const std::string &B) const;
22 
23  };
24 
25 
32  class MonteCarlo {
33 
34 
35  public:
36 
38 
42  typedef std::map<std::string, double> ScalarPropertyMap;
43 
47  typedef std::map<std::string, Eigen::VectorXd> VectorPropertyMap;
48 
54  typedef std::map<std::string, notstd::cloneable_ptr<MonteSampler>, SamplerNameCompare> SamplerMap;
55 
57  typedef std::vector<std::pair<MonteCounter::size_type, MonteCounter::size_type> > SampleTimes;
58 
59 
60  // ---- Accessors -----------------------------
61 
63  const MonteSettings &settings() const {
64  return m_settings;
65  }
66 
68  const PrimClex &primclex() const {
69  return m_primclex;
70  }
71 
73  const Supercell &supercell() const {
74  return m_scel;
75  }
76 
78  void set_nlist() {
79  m_nlist = &(supercell().nlist());
80  }
81 
83  const SuperNeighborList &nlist() const {
84  return *m_nlist;
85  }
86 
88  const Configuration &config() const {
89  return m_config;
90  }
91 
93  const ConfigDoF &configdof() const {
94  return m_configdof;
95  }
96 
97 
98  // ---- Accessors -----------------------------
99 
101  void reset(const ConfigDoF &dof) {
102  _configdof() = dof;
103  clear_samples();
104  }
105 
106 
107  // ---- Properties ----------------
108 
110  const ScalarPropertyMap &scalar_properties() const {
111  return m_scalar_property;
112  }
113 
115  const double &scalar_property(std::string property_name) const {
116  return m_scalar_property.find(property_name)->second;
117  }
118 
120  const VectorPropertyMap &vector_properties() const {
121  return m_vector_property;
122  }
123 
125  const Eigen::VectorXd &vector_property(std::string property_name) const {
126  return m_vector_property.find(property_name)->second;
127  }
128 
129 
130  // ---- Data sampling -------------
131 
133  void sample_data(const MonteCounter &counter);
134 
136  void clear_samples();
137 
139  bool must_converge() const {
140  return m_must_converge;
141  }
142 
144  std::pair<bool, MonteSampler::size_type> is_equilibrated() const;
145 
147  bool check_convergence_time() const;
148 
150  bool is_converged() const;
151 
153  const SamplerMap &samplers() const {
154  return m_sampler;
155  }
156 
158  const SampleTimes &sample_times() const {
159  return m_sample_time;
160  }
161 
166  const std::vector<ConfigDoF> &trajectory() const {
167  return m_trajectory;
168  }
169 
171  bool debug() const {
172  return m_debug;
173  }
174 
175 
176  protected:
177 
179  template<typename MonteTypeSettings>
180  MonteCarlo(PrimClex &primclex, const MonteTypeSettings &settings, Log &_log);
181 
183  PrimClex &_primclex() const {
184  return m_primclex;
185  }
186 
189  return m_scel;
190  }
191 
197  return m_config;
198  }
199 
205  return m_configdof;
206  }
207 
208  Log &_log() const {
209  return m_log;
210  }
211 
212  MTRand &_mtrand() {
213  return m_twister;
214  }
215 
217  ScalarPropertyMap &_scalar_properties() {
218  return m_scalar_property;
219  }
220 
222  double &_scalar_property(std::string property_name) {
223  return m_scalar_property.find(property_name)->second;
224  }
225 
227  VectorPropertyMap &_vector_properties() {
228  return m_vector_property;
229  }
230 
232  Eigen::VectorXd &_vector_property(std::string property_name) {
233  return m_vector_property.find(property_name)->second;
234  }
235 
236  private:
237 
241  ScalarPropertyMap m_scalar_property;
242 
243 
247  VectorPropertyMap m_vector_property;
248 
251 
254 
256  mutable Supercell m_scel;
257 
260 
266 
269 
271  MTRand m_twister;
272 
273 
275  bool m_write_trajectory = false;
276 
278  std::vector<ConfigDoF> m_trajectory;
279 
282 
285 
287  void _set_check_convergence_time() const;
288 
294  SamplerMap m_sampler;
295 
297  SampleTimes m_sample_time;
298 
299 
300  // Members to remember results of is_equilibrated and is_converged
301 
302  mutable std::pair<bool, MonteSampler::size_type> m_is_equil;
303  mutable bool m_is_equil_uptodate = false;
304  mutable bool m_is_converged;
305  mutable bool m_is_converged_uptodate = false;
306 
307  // which sample to check convergence after
310 
311  // in debug mode, allow printing or checking extra things
312  bool m_debug;
313 
314  };
315 
317  template<typename MonteTypeSettings>
318  MonteCarlo::MonteCarlo(PrimClex &primclex, const MonteTypeSettings &settings, Log &_log) :
319  m_settings(settings),
320  m_primclex(primclex),
321  m_scel(&primclex, settings.simulation_cell_matrix()),
322  m_config(m_scel),
323  m_configdof(m_config.configdof()),
324  m_write_trajectory(settings.write_trajectory()),
325  m_log(_log),
326  m_debug(m_settings.debug()) {
327 
328  settings.samplers(primclex, std::inserter(m_sampler, m_sampler.begin()));
329 
330  m_must_converge = false;
331  for(auto it = m_sampler.cbegin(); it != m_sampler.cend(); ++it) {
332  if(it->second->must_converge()) {
333  m_must_converge = true;
334  break;
335  }
336  }
337  }
338 
339 }
340 #endif
void clear_samples()
Clear all data from all samplers.
Definition: MonteCarlo.cc:46
const SuperNeighborList & nlist() const
const Access the SuperNeighborList via pointer stored by 'set_nlist'
Definition: MonteCarlo.hh:83
std::pair< bool, MonteSampler::size_type > m_is_equil
Definition: MonteCarlo.hh:302
bool m_is_converged_uptodate
Definition: MonteCarlo.hh:305
std::vector< std::pair< MonteCounter::size_type, MonteCounter::size_type > > SampleTimes
a vector of std::pair(pass, step) indicating when samples were taken
Definition: MonteCarlo.hh:57
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
VectorPropertyMap & _vector_properties()
const Access vector properties map
Definition: MonteCarlo.hh:227
const MonteSettings & m_settings
Contains all input settings.
Definition: MonteCarlo.hh:250
ConfigDoF & _configdof() const
Access current microstate.
Definition: MonteCarlo.hh:204
SampleTimes m_sample_time
a vector of std::pair(pass, step) indicating when samples were taken
Definition: MonteCarlo.hh:297
bool m_must_converge
True if any Sampler must converge.
Definition: MonteCarlo.hh:281
PrimClex * primclex
Definition: settings.cc:101
const double & scalar_property(std::string property_name) const
const Access a particular scalar property
Definition: MonteCarlo.hh:115
const Configuration & config() const
const Access current microstate
Definition: MonteCarlo.hh:88
const ConfigDoF & configdof() const
const Access current microstate
Definition: MonteCarlo.hh:93
void reset(const ConfigDoF &dof)
Set current microstate and clear samplers.
Definition: MonteCarlo.hh:101
Main CASM namespace.
Definition: complete.cpp:8
Represents a supercell of the primitive parent crystal structure.
Definition: Supercell.hh:37
bool m_is_equil_uptodate
Definition: MonteCarlo.hh:303
MonteCarlo(PrimClex &primclex, const MonteTypeSettings &settings, Log &_log)
Construct with a starting ConfigDoF as specified the given MonteSettings and prepare data samplers...
Definition: MonteCarlo.hh:318
bool debug() const
return true if running in debug mode
Definition: MonteCarlo.hh:171
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
MonteSampler::size_type m_next_convergence_check
Definition: MonteCarlo.hh:308
Configuration & _config() const
Access current microstate.
Definition: MonteCarlo.hh:196
The SuperNeighborList gives the linear indices of neighboring sites and unitcells in a particular Sup...
ScalarPropertyMap m_scalar_property
a map of keyname to property value
Definition: MonteCarlo.hh:241
Log & _log() const
Definition: MonteCarlo.hh:208
PrimClex & m_primclex
PrimClex for this system.
Definition: MonteCarlo.hh:253
PrimClex & _primclex() const
Access the PrimClex that *this is based on.
Definition: MonteCarlo.hh:183
Configuration m_config
Stores all degrees of freedom of the current microstate.
Definition: MonteCarlo.hh:265
void set_nlist()
Set a pointer to the SuperNeighborList once it is ready.
Definition: MonteCarlo.hh:78
const SuperNeighborList & nlist() const
Returns the SuperNeighborList.
Definition: Supercell.cc:79
SamplerMap m_sampler
a map of pair to MonteSampler
Definition: MonteCarlo.hh:294
const SampleTimes & sample_times() const
const Access a vector of std::pair indicating when samples were taken ...
Definition: MonteCarlo.hh:158
const Supercell & supercell() const
const Access the Supercell that *this is based on
Definition: MonteCarlo.hh:73
void write_trajectory(const MonteSettings &settings, const MonteCarlo &mc, Index cond_index, Log &_log)
Will create (and possibly overwrite) new file with all observations from run with conditions...
Definition: MonteIO.cc:269
MTRand m_twister
Random number generator.
Definition: MonteCarlo.hh:271
A container class for the different degrees of freedom a Configuration might have.
Definition: ConfigDoF.hh:27
Supercell m_scel
Supercell for the calculation.
Definition: MonteCarlo.hh:256
const PrimClex & primclex() const
const Access the PrimClex that *this is based on
Definition: MonteCarlo.hh:68
const std::vector< ConfigDoF > & trajectory() const
const Access snapshots of the Monte Carlo calculation
Definition: MonteCarlo.hh:166
Eigen::VectorXd & _vector_property(std::string property_name)
const Access a particular vector property
Definition: MonteCarlo.hh:232
Eigen::VectorXd VectorXd
std::map< std::string, notstd::cloneable_ptr< MonteSampler >, SamplerNameCompare > SamplerMap
a map of keyname to MonteSampler
Definition: MonteCarlo.hh:54
PrimClex is the top-level data structure for a CASM project.
Definition: PrimClex.hh:52
bool is_converged() const
Check to see if all the properties required to converge have converged.
Definition: MonteCarlo.cc:122
ConfigDoF & m_configdof
Reference to m_config.configdof(), to avoid invalidating id every time used.
Definition: MonteCarlo.hh:268
const VectorPropertyMap & vector_properties() const
const Access vector properties map
Definition: MonteCarlo.hh:120
MCData::size_type size_type
Definition: MonteSampler.hh:26
std::map< std::string, Eigen::VectorXd > VectorPropertyMap
a map of keyname to property value Eigen::VectorXd
Definition: MonteCarlo.hh:47
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
const Eigen::VectorXd & vector_property(std::string property_name) const
const Access a particular vector property
Definition: MonteCarlo.hh:125
Interface base class for all types of Monte Carlo simulations (not meant to be used polymorphically) ...
Definition: MonteCarlo.hh:32
Log & m_log
Target for messages.
Definition: MonteCarlo.hh:284
MonteSampler::size_type size_type
Definition: MonteCarlo.hh:37
const SuperNeighborList * m_nlist
Pointer to SuperNeighborList.
Definition: MonteCarlo.hh:259
std::map< std::string, double > ScalarPropertyMap
a map of keyname to property value
Definition: MonteCarlo.hh:42
Supercell & _supercell() const
Access the Supercell that *this is based on.
Definition: MonteCarlo.hh:188
const ScalarPropertyMap & scalar_properties() const
const Access scalar properties map
Definition: MonteCarlo.hh:110
bool must_converge() const
Return true if convergence is requested.
Definition: MonteCarlo.hh:139
void _set_check_convergence_time() const
Set the next time convergence is due to be checked.
Definition: MonteCarlo.cc:199
double & _scalar_property(std::string property_name)
Access a particular scalar property.
Definition: MonteCarlo.hh:222
Settings for Monte Carlo calculations.
Definition: Log.hh:9
MTRand & _mtrand()
Definition: MonteCarlo.hh:212
bool check_convergence_time() const
Returns true if a convergence check is due.
Definition: MonteCarlo.cc:188
ScalarPropertyMap & _scalar_properties()
Access scalar properties map.
Definition: MonteCarlo.hh:217
MonteSampler::size_type m_convergence_check_period
Definition: MonteCarlo.hh:309
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
const SamplerMap & samplers() const
const Access sampler map
Definition: MonteCarlo.hh:153
bool m_write_trajectory
Save trajectory?
Definition: MonteCarlo.hh:275
const MonteSettings & settings() const
const Access settings used for construction
Definition: MonteCarlo.hh:63
VectorPropertyMap m_vector_property
a map of keyname to property value Eigen::VectorXd
Definition: MonteCarlo.hh:247
A Configuration represents the values of all degrees of freedom in a Supercell.