QMCPACK
WalkerReconfiguration.cpp
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////////
2 // This file is distributed under the University of Illinois/NCSA Open Source License.
3 // See LICENSE file in top directory for details.
4 //
5 // Copyright (c) 2016 Jeongnim Kim and QMCPACK developers.
6 //
7 // File developed by: Jeremy McMinnis, jmcminis@gmail.com, University of Illinois at Urbana-Champaign
8 // Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
9 // Mark A. Berrill, berrillma@ornl.gov, Oak Ridge National Laboratory
10 // Andrew D. Baczewski, adbacze@sandia.gov, Sandia National Laboratories
11 //
12 // File created by: Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
13 //////////////////////////////////////////////////////////////////////////////////////
14 
16 #include "WalkerReconfiguration.h"
18 #include "Utilities/FairDivide.h"
20 
21 namespace qmcplusplus
22 {
23 
25 /** default constructor
26  *
27  * set SwapMode
28  */
30 {
31  SwapMode = 1;
32  //ofstream fout("check.dat");
33 }
34 
36 {
37  int nw(W.getActiveWalkers());
38  if (Zeta.size() != nw)
39  {
40  Zeta.resize(nw + 1);
41  IndexCopy.resize(nw);
42  wConf.resize(nw);
43  }
44  //accumulate the energies
45  FullPrecRealType esum = 0.0, e2sum = 0.0, wtot = 0.0, ecum = 0.0;
46  FullPrecRealType r2_accepted = 0.0, r2_proposed = 0.0;
47  for (int iw = 0; iw < nw; iw++)
48  {
49  const auto &walker = W[iw];
50  r2_accepted += walker->Properties(WP::R2ACCEPTED);
51  r2_proposed += walker->Properties(WP::R2PROPOSED);
52  FullPrecRealType wgt(walker->Weight);
53  FullPrecRealType e(walker->Properties(WP::LOCALENERGY));
54  esum += wgt * e;
55  e2sum += wgt * e * e;
56  ecum += e;
57  wtot += wConf[iw] = wgt;
58  }
59  curData[ENERGY_INDEX] = esum;
60  curData[ENERGY_SQ_INDEX] = e2sum;
62  curData[WEIGHT_INDEX] = wtot;
63  curData[EREF_INDEX] = ecum;
64  curData[R2ACCEPTED_INDEX] = r2_accepted;
65  curData[R2PROPOSED_INDEX] = r2_proposed;
66  FullPrecRealType nwInv = 1.0 / static_cast<FullPrecRealType>(nw);
67  UnitZeta = Random();
68  FullPrecRealType dstep = UnitZeta * nwInv;
69  for (int iw = 0; iw < nw; iw++)
70  {
71  Zeta[iw] = wtot * (dstep + static_cast<FullPrecRealType>(iw) * nwInv);
72  }
73  Zeta[nw] = wtot + 1.0;
74  //for(int iw=0; iw<nw; iw++) {
75  // fout << iw << " " << Zeta[iw+1]-Zeta[iw] << " " << wConf[iw] << std::endl;
76  //}
77  //assign negative
78  //std::fill(IndexCopy.begin(),IndexCopy.end(),-1);
79  int ind = 0;
80  FullPrecRealType wCur = 0.0;
81  //surviving walkers
82  int icdiff = 0;
83  std::vector<int> ipip(nw, 0);
84  for (int iw = 0; iw < nw; iw++)
85  {
86  FullPrecRealType tryp = wCur + std::abs(wConf[iw]);
87  int ni = 0;
88  while (Zeta[ind] < tryp && Zeta[ind] >= wCur)
89  {
90  //IndexCopy[ind]=iw;
91  ind++;
92  ni++;
93  }
94  wCur += std::abs(wConf[iw]);
95  if (ni)
96  {
97  icdiff++;
98  }
99  ipip[iw] = ni;
100  }
101  //ofstream fout("check.dat", std::ios::app);
102  //fout << wtot << " " << icdiff << std::endl;
103  std::vector<int> plus, minus;
104  for (int iw = 0; iw < nw; iw++)
105  {
106  int m = ipip[iw];
107  if (m > 1)
108  plus.insert(plus.end(), m - 1, iw);
109  else if (m == 0)
110  minus.push_back(iw);
111  }
112  curData[FNSIZE_INDEX] = nw - minus.size();
113  curData[RNONESIZE_INDEX] = minus.size();
114  for (int i = 0; i < plus.size(); i++)
115  {
116  int im = minus[i], ip = plus[i];
117  W[im]->makeCopy(*(W[ip]));
118  W[im]->setParentID(W[ip]->getWalkerID());
119  W[im]->setWalkerID((++NumWalkersCreated) * num_contexts_ + MyContext);
120  }
121  //int killed = shuffleIndex(nw);
122  //fout << "# Total weight " << wtot << " " << killed << std::endl;
123  //cout << "<<<< CopyIndex " << std::endl;
124  //std::copy(IndexCopy.begin(), IndexCopy.end(), std::ostream_iterator<int>(std::cout, " "));
125  //cout << std::endl << "<<<<<<" << std::endl;
126  //for(int iw=0; iw<nw; iw++) {
127  // if(IndexCopy[iw] != iw) {
128  // W[iw]->assign(*(W[IndexCopy[iw]]));
129  // }
130  //}
131  return icdiff;
132 }
133 
135 {
136  std::vector<int> ipip(nw, 0);
137  for (int iw = 0; iw < nw; iw++)
138  ipip[IndexCopy[iw]] += 1;
139  std::vector<int> indz;
140  for (int iw = 0; iw < nw; iw++)
141  {
142  if (ipip[iw] == 0)
143  {
144  indz.push_back(iw);
145  }
146  }
147  int ikilled = 0;
148  for (int iw = 0; iw < nw; iw++)
149  {
150  if (ipip[iw] != 0)
151  {
152  IndexCopy[iw] = iw;
153  for (int i = 1; i < ipip[iw]; i++)
154  {
155  IndexCopy[indz[ikilled++]] = iw;
156  }
157  }
158  }
159  return indz.size();
160 }
161 
163 {
164  int nwkept = getIndexPermutation(W);
165  //update EnsembleProperty
166  measureProperties(iter);
168  //W.EnsembleProperty.NumSamples=curData[WALKERSIZE_INDEX];
169  //W.EnsembleProperty.Weight=curData[WEIGHT_INDEX];
170  //RealType wgtInv(1.0/curData[WEIGHT_INDEX]);
171  //accumData[ENERGY_INDEX] += curData[ENERGY_INDEX]*wgtInv;
172  //accumData[ENERGY_SQ_INDEX] += curData[ENERGY_SQ_INDEX]*wgtInv;
173  //accumData[WALKERSIZE_INDEX] += nwkept;
174  ////accumData[WALKERSIZE_INDEX] += curData[WALKERSIZE_INDEX];
175  //accumData[WEIGHT_INDEX] += curData[WEIGHT_INDEX];
176  //set Weight and Multiplicity to default values
177  for (auto& walker : W)
178  {
179  walker->Weight = 1.0;
180  walker->Multiplicity = 1.0;
181  }
182  //curData[WALKERSIZE_INDEX]=nwkept;
183  return nwkept;
184 }
185 } // namespace qmcplusplus
HamiltonianRef::FullPrecRealType FullPrecRealType
A set of walkers that are to be advanced by Metropolis Monte Carlo.
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
size_t getActiveWalkers() const
return the number of active walkers
MakeReturn< UnaryNode< FnFabs, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t > >::Expression_t abs(const Vector< T1, C1 > &l)
#define Random
std::vector< FullPrecRealType > Zeta
int branch(int iter, MCWalkerConfiguration &W, FullPrecRealType trigger) override
perform branch and swap walkers as required
Wrapping information on parallelism.
Definition: Communicate.h:68
A collection of functions for dividing fairly.
WalkerReconfiguration(Communicate *c)
default constructor
WalkerProperties::Indexes WP
Definition: ParticleSet.cpp:34
std::vector< FullPrecRealType > wConf
void measureProperties(int iter)
take averages and writes to a file
std::vector< FullPrecRealType > curData
any temporary data includes many ridiculous conversions of integral types to and from fp ...
IndexType NumWalkersCreated
Number of walkers created by this rank.
int getIndexPermutation(MCWalkerConfiguration &W)
return the surviving Walkers
IndexType num_contexts_
number of contexts
Indexes
an enum denoting index of physical properties
Base class to control the walkers for DMC simulations.
MCDataType< FullPrecRealType > EnsembleProperty
MCDataType< FullPrecRealType > ensemble_property_
ensemble properties
Declare a global Random Number Generator.
IndexType SwapMode
0 is default