CASM  1.1.0
A Clusters Approach to Statistical Mechanics
OccLocation.cc
Go to the documentation of this file.
2 
4 #include "casm/external/MersenneTwister/MersenneTwister.h"
7 
8 namespace CASM {
9 namespace Monte {
10 
12  const OccCandidateList &_cand)
13  : m_convert(_convert), m_cand(_cand), m_loc(_cand.size()), m_kmc(false) {}
14 
17  m_mol.clear();
18  m_species.clear();
19  m_l_to_mol.clear();
20  for (auto &vec : m_loc) {
21  vec.clear();
22  }
23 
24  Index Nmut = 0;
25  for (Index l = 0; l < config.size(); ++l) {
26  Index asym = m_convert.l_to_asym(l);
27  if (m_convert.occ_size(asym) > 1) {
28  Nmut++;
29  }
30  }
31 
32  m_mol.resize(Nmut);
33  m_l_to_mol.reserve(config.size());
34  Index mol_id = 0;
35  for (Index l = 0; l < config.size(); ++l) {
36  Index asym = m_convert.l_to_asym(l);
37  if (m_convert.occ_size(asym) > 1) {
38  Index species_index = m_convert.species_index(asym, config.occ(l));
39  Index cand_index = m_cand.index(asym, species_index);
40 
41  Mol &mol = m_mol[mol_id];
42  mol.id = mol_id;
43  mol.l = l;
44  mol.asym = asym;
45  mol.species_index = species_index;
46  mol.loc = m_loc[cand_index].size();
47 
48  if (m_kmc) {
49  // only atoms now
50  for (Index j = 0; j < 1; j++) {
51  Species spec(m_convert.l_to_bijk(l));
52  spec.species_index = species_index;
53  spec.id = m_species.size();
54  mol.component.push_back(spec.id);
55 
56  m_species.push_back(spec);
57  }
58  }
59 
60  m_loc[cand_index].push_back(mol_id);
61  m_l_to_mol.push_back(mol_id);
62  mol_id++;
63  } else {
64  m_l_to_mol.push_back(Nmut);
65  }
66  }
67  if (m_kmc) {
68  m_tmol = m_mol;
69  }
70 }
71 
74 
75 Mol &OccLocation::mol(Index mol_id) { return m_mol[mol_id]; }
76 
77 const Mol &OccLocation::mol(Index mol_id) const { return m_mol[mol_id]; }
78 
81  return m_loc[cand_index].size();
82 }
83 
86  return cand_size(m_cand.index(cand));
87 }
88 
90 Index OccLocation::mol_id(Index cand_index, Index loc) const {
91  return m_loc[cand_index][loc];
92 }
93 
95 Index OccLocation::mol_id(const OccCandidate &cand, Index loc) const {
96  return mol_id(m_cand.index(cand), loc);
97 }
98 
101 
104  OccEvent &e, const std::vector<OccSwap> &canonical_swap,
105  MTRand &mtrand) const {
106  Index tsize = canonical_swap.size();
107  m_tsum.resize(tsize + 1);
108 
109  m_tsum[0] = 0.;
110  for (Index i = 0; i < tsize; ++i) {
111  m_tsum[i + 1] =
112  m_tsum[i] + ((double)cand_size(canonical_swap[i].cand_a)) *
113  ((double)cand_size(canonical_swap[i].cand_b));
114  }
115 
116  double rand = mtrand.randExc(m_tsum.back());
117 
118  for (Index i = 0; i < tsize; ++i) {
119  if (rand < m_tsum[i + 1]) {
120  return _propose(e, canonical_swap[i], mtrand);
121  }
122  }
123 
124  throw std::runtime_error("OccLocation::propose_canonical error");
125 }
126 
129  MTRand &mtrand) const {
130  e.occ_transform.resize(1);
131  e.species_traj.resize(0);
132 
133  Index index_cand_a = m_cand.index(swap.cand_a);
134  Index index_cand_b = m_cand.index(swap.cand_b);
135 
136  OccTransform &f_a = e.occ_transform[0];
137  f_a.mol_id = m_loc[index_cand_a][mtrand.randInt(cand_size(swap.cand_a) - 1)];
138  f_a.l = m_mol[f_a.mol_id].l;
139  f_a.asym = m_cand[index_cand_a].asym;
140  f_a.from_species = m_cand[index_cand_a].species_index;
141  f_a.to_species = m_cand[index_cand_b].species_index;
142 
143  return e;
144 }
145 
147 void OccLocation::apply(const OccEvent &e, ConfigDoF &configdof) {
148  // copy original Mol.component
149  if (m_kmc) {
150  for (const auto &occ : e.occ_transform) {
151  m_tmol[occ.mol_id].component = m_mol[occ.mol_id].component;
152  }
153  }
154 
155  // update Mol and config occupation
156  for (const auto &occ : e.occ_transform) {
157  auto &mol = m_mol[occ.mol_id];
158 
159  // set config occupation
160  configdof.occ(mol.l) = m_convert.occ_index(mol.asym, occ.to_species);
161 
162  // remove from m_loc
163  Index cand_index = m_cand.index(mol.asym, mol.species_index);
164  Index back = m_loc[cand_index].back();
165  m_loc[cand_index][mol.loc] = back;
166  m_mol[back].loc = mol.loc;
167  m_loc[cand_index].pop_back();
168 
169  // set Mol.species index
170  mol.species_index = occ.to_species;
171 
172  if (m_kmc) {
174  }
175 
176  // add to m_loc
177  cand_index = m_cand.index(mol.asym, mol.species_index);
178  mol.loc = m_loc[cand_index].size();
179  m_loc[cand_index].push_back(mol.id);
180  }
181 
182  if (m_kmc) {
183  // update Mol.component
184  for (const auto &traj : e.species_traj) {
185  m_mol[traj.to.mol_id].component[traj.to.mol_comp] =
186  m_tmol[traj.from.mol_id].component[traj.from.mol_comp];
187  }
188  }
189 }
190 
193  MTRand &mtrand, Index cand_a, Index cand_b,
194  Index size_a, Index size_b) const {
195  e.occ_transform.resize(2);
196  e.species_traj.resize(0);
197 
198  OccTransform &f_a = e.occ_transform[0];
199  f_a.mol_id = m_loc[cand_a][mtrand.randInt(size_a - 1)];
200  f_a.l = m_mol[f_a.mol_id].l;
201  f_a.asym = m_cand[cand_a].asym;
202  f_a.from_species = m_cand[cand_a].species_index;
203  f_a.to_species = m_cand[cand_b].species_index;
204  // std::cout << "size_a: " << size_a << " loc: " << m_mol[f_a.mol_id].loc <<
205  // std::endl;
206 
207  OccTransform &f_b = e.occ_transform[1];
208  f_b.mol_id = m_loc[cand_b][mtrand.randInt(size_b - 1)];
209  f_b.l = m_mol[f_b.mol_id].l;
210  f_b.asym = m_cand[cand_b].asym;
211  f_b.from_species = m_cand[cand_b].species_index;
212  f_b.to_species = m_cand[cand_a].species_index;
213  // std::cout << "size_b: " << size_b << " loc: " << m_mol[f_b.mol_id].loc <<
214  // std::endl;
215 
216  return e;
217 }
218 
221  MTRand &mtrand) const {
222  Index cand_a = m_cand.index(swap.cand_a);
223  Index cand_b = m_cand.index(swap.cand_b);
224  Index size_a = m_loc[cand_a].size();
225  Index size_b = m_loc[cand_b].size();
226  return _propose(e, swap, mtrand, cand_a, cand_b, size_a, size_b);
227 }
228 
229 } // namespace Monte
230 } // namespace CASM
int & occ(Index i)
Reference occupation value on site i.
Definition: ConfigDoF.cc:34
Index species_index(Index asym, Index occ_index) const
Definition: Conversions.cc:126
Index occ_size(Index asym) const
Definition: Conversions.cc:123
Index occ_index(Index asym, Index species_index) const
Definition: Conversions.cc:129
Index l_to_asym(Index l) const
Definition: Conversions.cc:88
UnitCellCoord l_to_bijk(Index l) const
Definition: Conversions.cc:82
Index components_size(Index species_index) const
Definition: Conversions.cc:147
Index index(const OccCandidate &cand) const
std::vector< Index > m_l_to_mol
l_to_mol[l] -> Mol.id, m_mol.size() otherwise
Definition: OccLocation.hh:143
Index mol_id(Index cand_index, Index loc) const
Mol.id of a particular OccCandidate type.
Definition: OccLocation.cc:90
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:192
size_type cand_size(Index cand_index) const
Total number of mutating sites, of OccCandidate type, specified by index.
Definition: OccLocation.cc:80
std::vector< Mol > m_mol
Holds Mol objects, one for each mutating site in the configuration.
Definition: OccLocation.hh:140
bool m_kmc
If true, update Species location during apply.
Definition: OccLocation.hh:146
OccEvent & propose_grand_canonical(OccEvent &e, const OccSwap &swap, MTRand &mtrand) const
Propose grand canonical OccEvent.
Definition: OccLocation.cc:128
std::vector< Species > m_species
Holds Monte::Species objects.
Definition: OccLocation.hh:137
const OccCandidateList & m_cand
Definition: OccLocation.hh:130
void apply(const OccEvent &e, ConfigDoF &configdof)
Update configdof and this to reflect that event 'e' occurred.
Definition: OccLocation.cc:147
Index l_to_mol_id(Index l) const
Convert from config index to variable site index.
Definition: OccLocation.cc:100
Mol & mol(Index mol_id)
Definition: OccLocation.cc:75
OccLocation(const Conversions &_convert, const OccCandidateList &_cand)
Definition: OccLocation.cc:11
const Conversions & m_convert
Definition: OccLocation.hh:128
std::vector< std::vector< Index > > m_loc
Definition: OccLocation.hh:134
size_type size() const
Total number of mutating sites.
Definition: OccLocation.cc:73
OccEvent & propose_canonical(OccEvent &e, const std::vector< OccSwap > &canonical_swap, MTRand &mtrand) const
Propose canonical OccEvent.
Definition: OccLocation.cc:103
void initialize(const Configuration &config)
Fill tables with occupation info.
Definition: OccLocation.cc:16
std::vector< double > m_tsum
Data used by propose_canonical.
Definition: OccLocation.hh:152
std::vector< Mol > m_tmol
Data structure used store temporaries during apply.
Definition: OccLocation.hh:149
Store swap type, mutating sites, and info for keeping OccLocation up-to-date.
Definition: OccCandidate.hh:71
ConfigIO::GenericConfigFormatter< jsonParser > config()
Definition: ConfigIO.cc:777
Main CASM namespace.
Definition: APICommand.hh:8
void swap(ConfigDoF &A, ConfigDoF &B)
Definition: ConfigDoF.cc:260
INDEX_TYPE Index
For long integer indexing:
Definition: definitions.hh:39
Represents the occupant on a site.
Definition: OccLocation.hh:39
Index loc
Location in OccLocation.m_loc.
Definition: OccLocation.hh:47
Index id
Location in OccLocation.m_mol.
Definition: OccLocation.hh:40
std::vector< Index > component
Location of component Specie in OccLocation.m_species.
Definition: OccLocation.hh:46
Index asym
Asym unit index (must be consistent with l)
Definition: OccLocation.hh:42
Index l
Location in config.
Definition: OccLocation.hh:41
std::vector< SpeciesTraj > species_traj
Definition: OccLocation.hh:71
std::vector< OccTransform > occ_transform
Definition: OccLocation.hh:70
Index mol_id
Location in OccLocation.m_mol.
Definition: OccLocation.hh:52
Index from_species
Species index before transformation.
Definition: OccLocation.hh:54
Index asym
Asym index.
Definition: OccLocation.hh:53
Index l
Config occupant that is being transformed.
Definition: OccLocation.hh:51
Index to_species
Species index after transformation.
Definition: OccLocation.hh:55
Represents an indivisible molecule component.
Definition: OccLocation.hh:27
Index id
Location in OccLocation.m_species.
Definition: OccLocation.hh:31
Index species_index
Species type index.
Definition: OccLocation.hh:30