CASM  1.1.0
A Clusters Approach to Statistical Mechanics
ConfigEnumRandomLocal.cc
Go to the documentation of this file.
2 
4 #include "casm/external/MersenneTwister/MersenneTwister.h"
5 
6 namespace CASM {
7 
9  MTRand &_mtrand, DoFKey _dof_key, Index _n_config, double _mag,
10  bool _normal_distribution)
11  : mtrand(_mtrand),
12  dof_key(_dof_key),
13  n_config(_n_config),
14  mag(_mag),
15  normal_distribution(_normal_distribution) {}
16 
18  ConfigEnumInput const &_in_config,
19  ConfigEnumRandomLocalParams const &params)
20  : ConfigEnumRandomLocal(_in_config, params.dof_key, params.n_config,
21  params.mag, params.normal_distribution,
22  params.mtrand) {}
23 
25  DoFKey const &_dof_key,
26  Index _n_config, double _mag,
27  bool _normal, MTRand &_mtrand)
28  : m_n_config(_n_config),
29  m_mtrand(_mtrand),
30  m_mag(_mag),
31  m_normal(_normal),
32  m_unit_length(DoF::BasicTraits(_dof_key).unit_length()),
33  m_site_selection(_in_config.sites().begin(), _in_config.sites().end()) {
34  if (m_unit_length) m_normal = false;
35 
36  if (m_n_config < 0) {
37  throw std::runtime_error("Error in ConfigEnumRandomLocal: n_config < 0");
38  }
39  if (m_n_config == 0) {
40  this->_invalidate();
41  return;
42  }
43 
44  m_current = notstd::make_cloneable<Configuration>(_in_config.configuration());
45 
47  this->_initialize(&(*m_current));
48 
49  m_dof_vals = &(m_current->configdof().local_dof(_dof_key));
50 
51  auto const &dof_info = m_dof_vals->info();
52  for (Index l : m_site_selection)
53  m_dof_dims.push_back(dof_info[m_current->sublat(l)].dim());
54 
55  // Make initial random config
56  this->randomize();
57  _set_step(0);
58  m_current->set_source(this->source(step()));
59 
60  // std::cout << "Selection: " << m_site_selection <<"\n"
61  // << "dofkey: " << _dof_key << "\n"
62  // << "mag: " << m_mag << "\n"
63  // << "dof_dims: " << m_dof_dims << "\n"
64  // << "unit_length: " << m_unit_length << "\n"
65  // << "m_normal: " << m_normal << "\n";
66 }
67 
68 std::string ConfigEnumRandomLocal::name() const { return enumerator_name; }
69 
71  "ConfigEnumRandomLocal";
72 
76  this->_increment_step();
77  if (step() < m_n_config) {
78  this->randomize();
79  m_current->set_source(this->source(step()));
80  } else {
81  this->_invalidate();
82  }
83 }
84 
86  double tnorm;
87  for (Index i = 0; i < m_site_selection.size(); ++i) {
88  for (Index j = 0; j < m_dof_dims[i]; ++j) {
90  m_mtrand.randNorm(0., m_mag);
91  }
92  if (!m_normal) {
93  tnorm = m_dof_vals->site_value(m_site_selection[i]).norm();
94  if (!m_unit_length) {
95  tnorm += -m_mag * std::log(m_mtrand.rand());
96  }
97  (m_dof_vals->site_value(m_site_selection[i])) /= tnorm;
98  }
99  }
100  // std::cout << "Randomized: \n" << m_dof_vals->values() << "\n";
101 }
102 
103 } // namespace CASM
Enumerate random values for continuous degrees of freedom.
std::string name() const override
Derived enumerators must implement name, via ENUM_MEMBERS.
notstd::cloneable_ptr< Configuration > m_current
static const std::string enumerator_name
LocalContinuousConfigDoFValues * m_dof_vals
void increment() override
Implements increment.
std::vector< Index > m_site_selection
ConfigEnumRandomLocal(ConfigEnumInput const &_in_config, ConfigEnumRandomLocalParams const &params)
std::vector< DoFSetInfo > const & info() const
DoFSetInfo provides the basis and symmetry representations for values
SiteReference site_value(Index l)
Access site DoF value vector.
virtual jsonParser source(step_type step) const
Definition: Enumerator.hh:129
void _invalidate()
Call if enumeration complete.
Definition: Enumerator.hh:159
void _increment_step()
Increment current step value.
Definition: Enumerator.hh:153
step_type step() const
Increments with each enumerated object.
Definition: Enumerator.hh:115
void _set_step(step_type val)
Set current step value.
Definition: Enumerator.hh:150
Configuration const & configuration() const
AnisoValTraits BasicTraits
Definition: DoF.hh:34
Main CASM namespace.
Definition: APICommand.hh:8
void reset_properties(ConfigType &config)
Definition: Calculable.cc:147
std::string DoFKey
Definition: DoFDecl.hh:7
INDEX_TYPE Index
For long integer indexing:
Definition: definitions.hh:39
Log & log
Definition: settings.cc:139
Parameters controlling ConfigEnumRandomLocal.
ConfigEnumRandomLocalParams(MTRand &_mtrand, DoFKey _dof_key, Index _n_config, double _mag, bool _normal_distribution)