QMCPACK
MCWalkerConfiguration.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: Jordan E. Vincent, University of Illinois at Urbana-Champaign
8 // Bryan Clark, bclark@Princeton.edu, Princeton University
9 // Ken Esler, kpesler@gmail.com, University of Illinois at Urbana-Champaign
10 // Jeremy McMinnis, jmcminis@gmail.com, University of Illinois at Urbana-Champaign
11 // Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
12 // Cynthia Gu, zg1@ornl.gov, Oak Ridge National Laboratory
13 // Ye Luo, yeluo@anl.gov, Argonne National Laboratory
14 // Mark A. Berrill, berrillma@ornl.gov, Oak Ridge National Laboratory
15 //
16 // File created by: Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
17 //////////////////////////////////////////////////////////////////////////////////////
18 
19 
20 #include "MCWalkerConfiguration.h"
22 #include "Message/Communicate.h"
23 #include "Message/CommOperators.h"
25 #include "LongRange/StructFact.h"
27 #include "Particle/MCSample.h"
28 #include "Particle/Reptile.h"
29 #include "hdf/hdf_hyperslab.h"
30 #include "hdf/HDFVersion.h"
31 #include <map>
32 
33 namespace qmcplusplus
34 {
36  : ParticleSet(simulation_cell, kind), ReadyForPbyP(false), UpdateMode(Update_Walker), reptile(0), Polymer(0)
37 {}
38 
40  : ParticleSet(mcw), ReadyForPbyP(false), UpdateMode(Update_Walker), Polymer(0)
41 {
45  Properties = mcw.Properties;
46 }
47 
49 
51 {
52  const int old_nw = getActiveWalkers();
54  // no pre-existing walkers, need to initialized based on particleset.
55  if (old_nw == 0)
56  for (auto& awalker : walker_list_)
57  {
58  awalker->R = R;
59  awalker->spins = spins;
60  }
62 }
63 
64 
65 void MCWalkerConfiguration::resize(int numWalkers, int numPtcls)
66 {
67  if (TotalNum && walker_list_.size())
68  app_warning() << "MCWalkerConfiguration::resize cleans up the walker list." << std::endl;
69  const int old_nw = getActiveWalkers();
70  ParticleSet::resize(unsigned(numPtcls));
72  // no pre-existing walkers, need to initialized based on particleset.
73  if (old_nw == 0)
74  for (auto& awalker : walker_list_)
75  {
76  awalker->R = R;
77  awalker->spins = spins;
78  }
79 }
80 
81 /** Make Metropolis move to the walkers and save in a temporary array.
82  * @param it the iterator of the first walker to work on
83  * @param tauinv inverse of the time step
84  *
85  * R + D + X
86  */
88 {
89  throw std::runtime_error("MCWalkerConfiguration::sample obsolete");
90  // makeGaussRandom(R);
91  // R *= tauinv;
92  // R += (*it)->R + (*it)->Drift;
93 }
94 
95 /** reset the Property container of all the walkers
96  */
98 {
99  int m(PropertyList.size());
100  app_log() << " Resetting Properties of the walkers " << ncopy << " x " << m << std::endl;
101  try
102  {
103  Properties.resize(ncopy, m);
104  }
105  catch (std::domain_error& de)
106  {
107  app_error() << de.what() << '\n'
108  << "This is likely because some object has attempted to add walker properties\n"
109  << " in excess of WALKER_MAX_PROPERTIES.\n"
110  << "build with cmake ... -DWALKER_MAX_PROPERTIES=at_least_properties_required" << std::endl;
111  APP_ABORT("Fatal Exception");
112  }
113 
114  for (auto& walker : walker_list_)
115  {
116  walker->resizeProperty(ncopy, m);
117  walker->Weight = 1.0;
118  }
120 }
121 
123 {
124  //using std::vector<std::vector<RealType> > is too costly.
125  int np = PropertyHistory.size();
126  if (np)
127  for (int iw = 0; iw < walker_list_.size(); ++iw)
129  np = PHindex.size();
130  if (np)
131  for (int iw = 0; iw < walker_list_.size(); ++iw)
133  ;
134 }
135 
136 /** allocate the SampleStack
137  * @param n number of samples per thread
138  */
140 {
143 }
144 
145 /** save the current walkers to SampleStack
146  */
148 
149 /** save the [first,last) walkers to SampleStack
150  */
152 {
153  for (; first != last; first++)
154  {
156  }
157 }
158 /** load a single sample from SampleStack
159  */
161 
162 /** load SampleStack to walker_list_
163  */
165 {
167  int nsamples = std::min(samples.getMaxSamples(), samples.getNumSamples());
168  if (samples.empty() || nsamples == 0)
169  return;
170  Walker_t::PropertyContainer_t prop(1, PropertyList.size(), 1, WP::MAXPROPERTIES);
171  walker_list_.resize(nsamples);
172  for (int i = 0; i < nsamples; ++i)
173  {
174  auto awalker = std::make_unique<Walker_t>(TotalNum);
175  awalker->Properties.copy(prop);
176  samples.getSample(i).convertToWalker(*awalker);
177  walker_list_[i] = std::move(awalker);
178  }
181 }
182 
183 bool MCWalkerConfiguration::dumpEnsemble(std::vector<MCWalkerConfiguration*>& others,
184  HDFWalkerOutput& out,
185  int np,
186  int nBlock)
187 {
188  WalkerConfigurations wctemp;
189  for (auto* mcwc : others)
190  {
191  const auto& astack(mcwc->getSampleStack());
192  const size_t sample_size = std::min(mcwc->getMaxSamples(), mcwc->numSamples());
193  for (int j = 0; j < sample_size; ++j)
194  {
195  const auto& sample = astack.getSample(j);
196  const size_t num_ptcls = sample.getNumPtcls();
197  auto awalker = std::make_unique<Walker_t>(num_ptcls);
198  sample.convertToWalker(*awalker);
199  wctemp.push_back(std::move(awalker));
200  }
201  }
202  const int w = wctemp.getActiveWalkers();
203  if (w == 0)
204  return false;
205 
206  // The following code assumes the same amount of active walkers on all the MPI ranks
207  std::vector<int> nwoff(np + 1, 0);
208  for (int ip = 0; ip < np; ++ip)
209  nwoff[ip + 1] = nwoff[ip] + w;
210  wctemp.setWalkerOffsets(nwoff);
211  out.dump(wctemp, nBlock);
212  return true;
213 }
214 
216 
217 void MCWalkerConfiguration::loadEnsemble(std::vector<MCWalkerConfiguration*>& others, bool doclean)
218 {
220  std::vector<int> off(others.size() + 1, 0);
221  for (int i = 0; i < others.size(); ++i)
222  {
223  off[i + 1] = off[i] + std::min(others[i]->getMaxSamples(), others[i]->numSamples());
224  }
225  int nw_tot = off.back();
226  if (nw_tot)
227  {
228  Walker_t::PropertyContainer_t prop(1, PropertyList.size(), 1, WP::MAXPROPERTIES);
229  while (walker_list_.size())
230  pop_back();
231  walker_list_.resize(nw_tot);
232  for (int i = 0; i < others.size(); ++i)
233  {
234  SampleStack& astack(others[i]->getSampleStack());
235  for (int j = 0, iw = off[i]; iw < off[i + 1]; ++j, ++iw)
236  {
237  auto awalker = std::make_unique<Walker_t>(TotalNum);
238  awalker->Properties.copy(prop);
239  astack.getSample(j).convertToWalker(*awalker);
240  walker_list_[iw] = std::move(awalker);
241  }
242  if (doclean)
243  others[i]->clearEnsemble();
244  }
245  }
246  if (doclean)
248 }
249 
251 
252 } // namespace qmcplusplus
bool dump(const WalkerConfigurations &w, int block)
dump configurations
DynamicCoordinateKind
enumerator for DynamicCoordinates kinds
void saveEnsemble()
save the position of current walkers to SampleStack
void sample(iterator it, RealType tauinv)
make random moves for all the walkers
Stores particle configurations for later use in DMC and wavefunction optimization.
A set of walkers that are to be advanced by Metropolis Monte Carlo.
std::ostream & app_warning()
Definition: OutputManager.h:69
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
size_t getMaxSamples() const
Definition: SampleStack.h:34
ParticleScalar spins
internal spin variables for dynamical spin calculations
Definition: ParticleSet.h:81
PropertyContainer_t Properties
properties of the current walker
Definition: ParticleSet.h:119
void resizeWalkerHistories()
resize Walker::PropertyHistory and Walker::PHindex:
QTBase::RealType RealType
Definition: Configuration.h:58
size_t getActiveWalkers() const
return the number of active walkers
std::ostream & app_log()
Definition: OutputManager.h:65
size_t TotalNum
total number of particles
Definition: ParticleSet.h:642
Writes a set of walker configurations to an HDF5 file.
A set of light weight walkers that are carried between driver sections and restart.
std::ostream & app_error()
Definition: OutputManager.h:67
std::vector< std::vector< FullPrecRealType > > PropertyHistory
Property history vector.
Definition: ParticleSet.h:129
void pop_back()
delete the last Walker_t*
int getMaxSamples() const
Transitional forwarding methods.
size_t getNumSamples() const
Definition: SampleStack.h:41
int first(int igroup) const
return the first index of a group i
Definition: ParticleSet.h:514
void setWalkerOffsets(const std::vector< int > &o)
return the total number of active walkers among a MPI group
void push_back(std::unique_ptr< Walker_t > awalker)
add Walker_t* at the end
MCWalkerConfiguration(const SimulationCell &simulation_cell, const DynamicCoordinateKind kind=DynamicCoordinateKind::DC_POS)
default constructor
void loadEnsemble()
load SampleStack data to the current list of walker configurations
T min(T a, T b)
void clearEnsemble()
clear the ensemble
Definition: SampleStack.cpp:51
PropertySetType PropertyList
name-value map of Walker Properties
Definition: ParticleSet.h:112
store minimum Walker data
Definition: MCSample.h:28
void loadSample(ParticleSet &pset, size_t iw) const
load a single sample from SampleStack
Definition: SampleStack.cpp:45
Specialized paritlce class for atomistic simulations.
Definition: ParticleSet.h:55
WalkerList_t::iterator iterator
FIX: a type alias of iterator for an object should not be for just one of many objects it holds...
void createWalkers(int numWalkers, size_t numPtcls)
create numWalkers Walkers
const MCSample & getSample(size_t i) const
Definition: SampleStack.cpp:31
void resize(size_t numPtcl)
resize internal storage
Definition: ParticleSet.h:683
std::vector< int > PHindex
Definition: ParticleSet.h:130
ParticlePos R
Position.
Definition: ParticleSet.h:79
#define APP_ABORT(msg)
Widely used but deprecated fatal error macros from legacy code.
Definition: AppAbort.h:27
void setNumSamples(int n)
set the number of max samples
int last(int igroup) const
return the last index of a group i
Definition: ParticleSet.h:517
void resize(int numWalkers, int numPtcls)
clean up the walker list and make a new list
void setMaxSamples(size_t n, size_t number_of_ranks=1)
set the number of max samples per rank.
Definition: SampleStack.cpp:23
const std::vector< int > & getWalkerOffsets() const
static bool dumpEnsemble(std::vector< MCWalkerConfiguration *> &others, HDFWalkerOutput &out, int np, int nBlock)
dump Samples to a file
void resize(int numWalkers, size_t numPtcls)
clean up the walker list and make a new list
void convertToWalker(Walker_t &w) const
Definition: MCSample.h:61
Indexes
an enum denoting index of physical properties
void appendSample(MCSample &&sample)
Definition: SampleStack.cpp:33
void loadSample(ParticleSet &pset, size_t iw) const
load a single sample from SampleStack
const SampleStack & getSampleStack() const
void createWalkers(int numWalkers)
create numWalkers Walkers
walker_list__t walker_list_
a collection of walkers
Declaration of a MCWalkerConfiguration.
void resetWalkerProperty(int ncopy=1)
reset the Property container of all the walkers