CASM
AClustersApproachtoStatisticalMechanics
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
OccLocation.cc
Go to the documentation of this file.
5 #include "casm/external/MersenneTwister/MersenneTwister.h"
6 
7 
8 namespace CASM {
9  namespace Monte {
10 
11  OccLocation::OccLocation(const Conversions &_convert, const OccCandidateList &_cand) :
12  m_convert(_convert),
13  m_cand(_cand),
14  m_loc(_cand.size()),
15  m_kmc(false) {}
16 
18  void OccLocation::initialize(const Configuration &config) {
19 
20  m_mol.clear();
21  m_species.clear();
22  m_l_to_mol.clear();
23  for(auto &vec : m_loc) {
24  vec.clear();
25  }
26 
27  Index Nmut = 0;
28  for(Index l = 0; l < config.size(); ++l) {
29  Index asym = m_convert.l_to_asym(l);
30  if(m_convert.occ_size(asym) > 1) {
31  Nmut++;
32  }
33  }
34 
35  m_mol.resize(Nmut);
36  m_l_to_mol.reserve(config.size());
37  Index mol_id = 0;
38  for(Index l = 0; l < config.size(); ++l) {
39  Index asym = m_convert.l_to_asym(l);
40  if(m_convert.occ_size(asym) > 1) {
41  Index species_index = m_convert.species_index(asym, config.occ(l));
42  Index cand_index = m_cand.index(asym, species_index);
43 
44  Mol &mol = m_mol[mol_id];
45  mol.id = mol_id;
46  mol.l = l;
47  mol.asym = asym;
48  mol.species_index = species_index;
49  mol.loc = m_loc[cand_index].size();
50 
51  if(m_kmc) {
52  // only atoms now
53  for(Index j = 0; j < 1; j++) {
54  Species spec;
55  spec.species_index = species_index;
56  spec.id = m_species.size();
57  spec.bijk_begin = m_convert.l_to_bijk(l);
58  mol.component.push_back(spec.id);
59 
60  m_species.push_back(spec);
61  }
62  }
63 
64  m_loc[cand_index].push_back(mol_id);
65  m_l_to_mol.push_back(mol_id);
66  mol_id++;
67  }
68  else {
69  m_l_to_mol.push_back(Nmut);
70  }
71  }
72  if(m_kmc) {
73  m_tmol = m_mol;
74  }
75  }
76 
79  return m_mol.size();
80  }
81 
83  return m_mol[mol_id];
84  }
85 
86  const Mol &OccLocation::mol(Index mol_id) const {
87  return m_mol[mol_id];
88  }
89 
92  return m_loc[cand_index].size();
93  }
94 
97  return cand_size(m_cand.index(cand));
98  }
99 
101  Index OccLocation::mol_id(Index cand_index, Index loc) const {
102  return m_loc[cand_index][loc];
103  }
104 
106  Index OccLocation::mol_id(const OccCandidate &cand, Index loc) const {
107  return mol_id(m_cand.index(cand), loc);
108  }
109 
112  return m_l_to_mol[l];
113  }
114 
117  OccEvent &e,
118  const std::vector<OccSwap> &canonical_swap,
119  MTRand &mtrand) const {
120 
121  Index tsize = canonical_swap.size();
122  m_tsum.resize(tsize + 1);
123 
124  m_tsum[0] = 0.;
125  for(Index i = 0; i < tsize; ++i) {
126  m_tsum[i + 1] = m_tsum[i] +
127  ((double) cand_size(canonical_swap[i].cand_a)) *
128  ((double) cand_size(canonical_swap[i].cand_b));
129  }
130 
131  double rand = mtrand.randExc(m_tsum.back());
132 
133  for(Index i = 0; i < tsize; ++i) {
134  if(rand < m_tsum[i + 1]) {
135  return _propose(e, canonical_swap[i], mtrand);
136  }
137  }
138 
139  throw std::runtime_error("OccLocation::propose_canonical error");
140  }
141 
144  e.occ_transform.resize(1);
145  e.species_traj.resize(0);
146 
147  Index index_cand_a = m_cand.index(swap.cand_a);
148  Index index_cand_b = m_cand.index(swap.cand_b);
149 
150  OccTransform &f_a = e.occ_transform[0];
151  f_a.mol_id = m_loc[index_cand_a][mtrand.randInt(cand_size(swap.cand_a) - 1)];
152  f_a.l = m_mol[f_a.mol_id].l;
153  f_a.asym = m_cand[index_cand_a].asym;
154  f_a.from_species = m_cand[index_cand_a].species_index;
155  f_a.to_species = m_cand[index_cand_b].species_index;
156 
157  return e;
158  }
159 
161  void OccLocation::apply(const OccEvent &e, ConfigDoF &configdof) {
162 
163  // copy original Mol.component
164  if(m_kmc) {
165  for(const auto &occ : e.occ_transform) {
166  m_tmol[occ.mol_id].component = m_mol[occ.mol_id].component;
167  }
168  }
169 
170  // update Mol and config occupation
171  for(const auto &occ : e.occ_transform) {
172  auto &mol = m_mol[occ.mol_id];
173 
174  // set config occupation
175  configdof.occ(mol.l) = m_convert.occ_index(mol.asym, occ.to_species);
176 
177  // remove from m_loc
178  Index cand_index = m_cand.index(mol.asym, mol.species_index);
179  Index back = m_loc[cand_index].back();
180  m_loc[cand_index][mol.loc] = back;
181  m_mol[back].loc = mol.loc;
182  m_loc[cand_index].pop_back();
183 
184  // set Mol.species index
185  mol.species_index = occ.to_species;
186 
187  if(m_kmc) {
189  }
190 
191  // add to m_loc
192  cand_index = m_cand.index(mol.asym, mol.species_index);
193  mol.loc = m_loc[cand_index].size();
194  m_loc[cand_index].push_back(mol.id);
195 
196  }
197 
198  if(m_kmc) {
199  // update Mol.component
200  for(const auto &traj : e.species_traj) {
201  m_mol[traj.to.mol_id].component[traj.to.mol_comp] = m_tmol[traj.from.mol_id].component[traj.from.mol_comp];
202  }
203  }
204  }
205 
207  OccEvent &OccLocation::_propose(OccEvent &e, const OccSwap &swap, MTRand &mtrand, Index cand_a, Index cand_b, Index size_a, Index size_b) const {
208  e.occ_transform.resize(2);
209  e.species_traj.resize(0);
210 
211  OccTransform &f_a = e.occ_transform[0];
212  f_a.mol_id = m_loc[cand_a][mtrand.randInt(size_a - 1)];
213  f_a.l = m_mol[f_a.mol_id].l;
214  f_a.asym = m_cand[cand_a].asym;
215  f_a.from_species = m_cand[cand_a].species_index;
216  f_a.to_species = m_cand[cand_b].species_index;
217  //std::cout << "size_a: " << size_a << " loc: " << m_mol[f_a.mol_id].loc << std::endl;
218 
219  OccTransform &f_b = e.occ_transform[1];
220  f_b.mol_id = m_loc[cand_b][mtrand.randInt(size_b - 1)];
221  f_b.l = m_mol[f_b.mol_id].l;
222  f_b.asym = m_cand[cand_b].asym;
223  f_b.from_species = m_cand[cand_b].species_index;
224  f_b.to_species = m_cand[cand_a].species_index;
225  //std::cout << "size_b: " << size_b << " loc: " << m_mol[f_b.mol_id].loc << std::endl;
226 
227  return e;
228  }
229 
231  OccEvent &OccLocation::_propose(OccEvent &e, const OccSwap &swap, MTRand &mtrand) const {
232  Index cand_a = m_cand.index(swap.cand_a);
233  Index cand_b = m_cand.index(swap.cand_b);
234  Index size_a = m_loc[cand_a].size();
235  Index size_b = m_loc[cand_b].size();
236  return _propose(e, swap, mtrand, cand_a, cand_b, size_a, size_b);
237  }
238  }
239 }
Index l
Config occupant that is being transformed.
Definition: OccLocation.hh:43
void initialize(const Configuration &config)
Fill tables with occupation info.
Definition: OccLocation.cc:18
const OccCandidateList & m_cand
Definition: OccLocation.hh:120
std::vector< Mol > m_mol
Holds Mol objects, one for each mutating site in the configuration.
Definition: OccLocation.hh:130
int & occ(Index i)
Definition: ConfigDoF.hh:61
Mol & mol(Index mol_id)
Definition: OccLocation.cc:82
const int & occ(Index site_l) const
Occupant variable on site l.
Index species_index
Species type index (must be consistent with config.occ(l))
Definition: OccLocation.hh:37
std::vector< double > m_tsum
Data used by propose_canonical.
Definition: OccLocation.hh:142
std::vector< SpecieTraj > species_traj
Definition: OccLocation.hh:63
OccEvent & propose_grand_canonical(OccEvent &e, const OccSwap &swap, MTRand &mtrand) const
Propose grand canonical OccEvent.
Definition: OccLocation.cc:143
Index asym
Asym index.
Definition: OccLocation.hh:45
OccCandidate cand_b
Definition: OccCandidate.hh:60
Index l_to_mol_id(Index l) const
Convert from config index to variable site index.
Definition: OccLocation.cc:111
Main CASM namespace.
Definition: complete.cpp:8
OccCandidate cand_a
Definition: OccCandidate.hh:59
Index mol_id
Location in OccLocation.m_mol.
Definition: OccLocation.hh:44
Index l
Location in config.
Definition: OccLocation.hh:35
UnitCellCoord bijk_begin
Saves initial position.
Definition: OccLocation.hh:26
Index asym
Asym unit index (must be consistent with l)
Definition: OccLocation.hh:36
Index mol_id(Index cand_index, Index loc) const
Mol.id of a particular OccCandidate type.
Definition: OccLocation.cc:101
std::vector< OccTransform > occ_transform
Definition: OccLocation.hh:62
const Conversions & m_convert
Definition: OccLocation.hh:118
std::vector< Index > component
Location of component Specie in OccLocation.m_species.
Definition: OccLocation.hh:38
size_type cand_size(Index cand_index) const
Total number of mutating sites, of OccCandidate type, specified by index.
Definition: OccLocation.cc:91
Index l_to_asym(Index l) const
Definition: Conversions.cc:83
void swap(ConfigDoF &A, ConfigDoF &B)
Definition: ConfigDoF.cc:195
std::vector< Species > m_species
Holds Monte::Species objects.
Definition: OccLocation.hh:127
Index id
Location in OccLocation.m_species.
Definition: OccLocation.hh:25
std::vector< std::vector< Index > > m_loc
Definition: OccLocation.hh:124
EigenIndex Index
For long integer indexing:
Store swap type, mutating sites, and info for keeping OccLocation up-to-date.
Definition: OccCandidate.hh:51
A container class for the different degrees of freedom a Configuration might have.
Definition: ConfigDoF.hh:27
UnitCellCoord l_to_bijk(Index l) const
Definition: Conversions.cc:77
Represents the occupant on a site.
Definition: OccLocation.hh:33
void apply(const OccEvent &e, ConfigDoF &configdof)
Update configdof and this to reflect that event 'e' occurred.
Definition: OccLocation.cc:161
Index id
Location in OccLocation.m_mol.
Definition: OccLocation.hh:34
Represents an indivisible molecule component.
Definition: OccLocation.hh:23
Index occ_index(Index asym, Index species_index) const
Definition: Conversions.cc:130
Index size() const
Returns number of sites, NOT the number of primitives that fit in here.
OccEvent & _propose(OccEvent &e, const OccSwap &swap, MTRand &mtrand, Index cand_a, Index cand_b, Index size_a, Index size_b) const
Canonical propose.
Definition: OccLocation.cc:207
List of asym / species_index pairs indicating allowed variable occupation dof.
Index loc
Location in OccLocation.m_loc.
Definition: OccLocation.hh:39
OccEvent & propose_canonical(OccEvent &e, const std::vector< OccSwap > &canonical_swap, MTRand &mtrand) const
Propose canonical OccEvent.
Definition: OccLocation.cc:116
bool m_kmc
If true, update Species location during apply.
Definition: OccLocation.hh:136
Index to_species
Species index after transformation.
Definition: OccLocation.hh:47
Index components_size(Index species_index) const
Definition: Conversions.cc:150
size_type size() const
Total number of mutating sites.
Definition: OccLocation.cc:78
std::vector< Mol > m_tmol
Data structure used store temporaries during apply.
Definition: OccLocation.hh:139
Index index(const OccCandidate &cand) const
Return index into std::vector, or _candidate.size() if not allowed. ...
Index species_index(Index asym, Index occ_index) const
Definition: Conversions.cc:127
Index species_index
Species type index.
Definition: OccLocation.hh:24
Index from_species
Species index before transformation.
Definition: OccLocation.hh:46
Index occ_size(Index asym) const
Definition: Conversions.cc:124
A Configuration represents the values of all degrees of freedom in a Supercell.
OccLocation(const Conversions &_convert, const OccCandidateList &_cand)
Definition: OccLocation.cc:11
std::vector< Index > m_l_to_mol
l_to_mol[l] -> Mol.id, m_mol.size() otherwise
Definition: OccLocation.hh:133