QMCPACK
MCPopulation.h
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) 2020 QMCPACK developers.
6 //
7 // File developed by: Peter Doak, doakpw@ornl.gov, Oak Ridge National Laboratory
8 //
9 // File refactored from VMC.h
10 //////////////////////////////////////////////////////////////////////////////////////
11 
12 #ifndef QMCPLUSPLUS_MCPOPULATION_H
13 #define QMCPLUSPLUS_MCPOPULATION_H
14 
15 #include <iterator>
16 #include <vector>
17 #include <memory>
18 
19 #include "Configuration.h"
20 #include "Particle/ParticleSet.h"
22 #include "Particle/Walker.h"
25 #include "OhmmsPETE/OhmmsVector.h"
26 #include "Utilities/FairDivide.h"
27 
28 // forward declaration
29 namespace optimize
30 {
31 struct VariableSet;
32 }
33 
34 namespace qmcplusplus
35 {
36 // forward declaration
37 class QMCHamiltonian;
39 {
40 public:
48 
49 private:
50  // Potential thread safety issue
52 
58  //Properties properties_;
59 
60  // By making this a linked list and creating the crowds at the same time we could get first touch.
63  std::vector<RealType> ptclgrp_mass_;
64  ///1/Mass per species
65  std::vector<RealType> ptclgrp_inv_mass_;
66  ///1/Mass per particle
67  std::vector<RealType> ptcl_inv_mass_;
68 
69  // This is necessary MCPopulation is constructed in a simple call scope in QMCDriverFactory from the legacy MCWalkerConfiguration
70  // MCPopulation should have QMCMain scope eventually and the driver will just have a reference to it.
71  // Then these too can be references.
75  // At the moment these are "clones" but I think this design pattern smells.
79 
80  // We still haven't cleaned up the dependence between different walker elements so they all need to be tracked
81  // as in the legacy implementation.
85 
86  // MCPopulation immutables
87  // would be nice if they were const but we'd lose the default move assignment
89  int rank_;
90 
91 public:
92  /** Temporary constructor to deal with MCWalkerConfiguration be the only source of some information
93  * in QMCDriverFactory.
94  */
96  int this_rank,
97  ParticleSet* elecs,
98  TrialWaveFunction* trial_wf,
100 
101  ~MCPopulation();
102  MCPopulation(MCPopulation&) = delete;
103  MCPopulation& operator=(MCPopulation&) = delete;
104  MCPopulation(MCPopulation&&) = default;
105 
106  /** @ingroup PopulationControl
107  *
108  * State Requirement:
109  * * createWalkers must have been called
110  * @{
111  */
113  void killWalker(MCPWalker&);
114  void killLastWalker();
115  /** }@ */
116 
117  /** Creates walkers with a clone of the golden electron particle set and golden trial wavefunction
118  *
119  * \param[in] num_walkers number of living walkers in initial population
120  * \param[in] reserve multiple above that to reserve >=1.0
121  */
122  void createWalkers(IndexType num_walkers, const WalkerConfigurations& walker_configs, RealType reserve = 1.0);
123 
124  /** distributes walkers and their "cloned" elements to the elements of a vector
125  * of unique_ptr to "walker_consumers".
126  *
127  * a valid "walker_consumer" has a member function of
128  * void addWalker(MCPWalker& walker, ParticleSet& elecs, TrialWaveFunction& twf, QMCHamiltonian& hamiltonian);
129  */
130  template<typename WTTV>
131  void redistributeWalkers(WTTV& walker_consumers)
132  {
133  // The type returned here is dependent on the integral type that the walker_consumers
134  // use to return there size.
135  auto walkers_per_crowd = fairDivide(walkers_.size(), walker_consumers.size());
136 
137  auto walker_index = 0;
138  for (int i = 0; i < walker_consumers.size(); ++i)
139  {
140  walker_consumers[i]->clearWalkers();
141  for (int j = 0; j < walkers_per_crowd[i]; ++j)
142  {
143  walker_consumers[i]->addWalker(*walkers_[walker_index], *walker_elec_particle_sets_[walker_index],
144  *walker_trial_wavefunctions_[walker_index], *walker_hamiltonians_[walker_index]);
145  ++walker_index;
146  }
147  }
148  }
149 
152 
153  /**@ingroup Accessors
154  * @{
155  */
156 
157  /** The number of cases in which this and get_num_local_walkers is so few that
158  * I strongly suspect it is a design issue.
159  *
160  * get_active_walkers is onlyh useful between the setting of num_local_walkers_ and
161  * creation of walkers. I would reason this is actually a time over which the MCPopulation object
162  * is invalid. Ideally MCPopulation not process any calls in this state, next best would be to only
163  * process calls to become valid.
164  */
165  //IndexType get_active_walkers() const { return walkers_.size(); }
166  int get_num_ranks() const { return num_ranks_; }
167  int get_rank() const { return rank_; }
173  //const Properties& get_properties() const { return properties_; }
174 
175  // accessor to the gold copy
178  const TrialWaveFunction& get_golden_twf() const { return *trial_wf_; }
180  // TODO: the fact this is needed is sad remove need for its existence.
182 
183  void set_num_global_walkers(IndexType num_global_walkers) { num_global_walkers_ = num_global_walkers; }
184  void set_num_local_walkers(IndexType num_local_walkers) { num_local_walkers_ = num_local_walkers; }
185 
187  void set_target_samples(IndexType samples) { target_samples_ = samples; }
188 
190  {
191  ensemble_property_ = ensemble_property;
192  }
193 
195  const UPtrVector<MCPWalker>& get_walkers() const { return walkers_; }
197 
199 
202 
205 
206  /** Non threadsafe access to walkers and their elements
207  *
208  * Prefer to distribute the walker elements and access
209  * through a crowd to support the concurrency design.
210  *
211  * You should not use this unless absolutely necessary.
212  * That doesn't include that you would rather just use
213  * omp parallel and ignore concurrency.
214  */
215  WalkerElementsRef getWalkerElementsRef(const size_t walker_index);
216 
217  /** As long as walker WalkerElements is used we need this for unit tests
218  *
219  * As operator[] don't use it to ignore the concurrency design.
220  */
221  std::vector<WalkerElementsRef> get_walker_elements();
222 
223  const std::vector<RealType>& get_ptclgrp_mass() const { return ptclgrp_mass_; }
224  const std::vector<RealType>& get_ptclgrp_inv_mass() const { return ptclgrp_inv_mass_; }
225  const std::vector<RealType>& get_ptcl_inv_mass() const { return ptcl_inv_mass_; }
226 
227  /** }@ */
228 
229  /// check if all the internal vector contain consistent sizes;
230  void checkIntegrity() const;
231 
232  /// save walker configurations to walker_configs_ref_
233  void saveWalkerConfigurations(WalkerConfigurations& walker_configs);
234 };
235 
236 } // namespace qmcplusplus
237 
238 #endif
std::vector< WalkerElementsRef > get_walker_elements()
As long as walker WalkerElements is used we need this for unit tests.
QMCTraits::RealType RealType
Definition: MCPopulation.h:43
UPtrVector< TrialWaveFunction > & get_twfs()
Definition: MCPopulation.h:200
HamiltonianRef::FullPrecRealType FullPrecRealType
UPtrVector< QMCHamiltonian > & get_dead_hamiltonians()
Definition: MCPopulation.h:204
UPtrVector< TrialWaveFunction > walker_trial_wavefunctions_
Definition: MCPopulation.h:77
void set_ensemble_property(const MCDataType< QMCTraits::FullPrecRealType > &ensemble_property)
Definition: MCPopulation.h:189
WalkerElementsRef spawnWalker()
State Requirement:
const ParticleSet & get_golden_electrons() const
Definition: MCPopulation.h:176
std::vector< RealType > ptcl_inv_mass_
1/Mass per particle
Definition: MCPopulation.h:67
QMCHamiltonian & get_golden_hamiltonian()
Definition: MCPopulation.h:181
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
QMCTraits::FullPrecRealType FullPrecRealType
Definition: MCPopulation.h:46
QTBase::RealType RealType
Definition: Configuration.h:58
void set_target(IndexType pop)
Definition: MCPopulation.h:186
UPtrVector< MCPWalker > & get_walkers()
Definition: MCPopulation.h:194
type for returning the walker and its elements from MCPopulation
A set of light weight walkers that are carried between driver sections and restart.
UPtrVector< QMCHamiltonian > dead_walker_hamiltonians_
Definition: MCPopulation.h:84
const char num_walkers[]
Definition: HDFVersion.h:37
IndexType get_max_samples() const
Definition: MCPopulation.h:170
Collection of Local Energy Operators.
int get_num_ranks() const
The number of cases in which this and get_num_local_walkers is so few that I strongly suspect it is a...
Definition: MCPopulation.h:166
QMCHamiltonian * hamiltonian_
Definition: MCPopulation.h:74
std::vector< std::unique_ptr< T > > UPtrVector
UPtrVector< QMCHamiltonian > & get_hamiltonians()
Definition: MCPopulation.h:203
MCPopulation(int num_ranks, int this_rank, ParticleSet *elecs, TrialWaveFunction *trial_wf, QMCHamiltonian *hamiltonian_)
Temporary constructor to deal with MCWalkerConfiguration be the only source of some information in QM...
ConstantSizeMatrix< FullPrecRealType, std::allocator< FullPrecRealType > > PropertyContainer_t
typedef for the property container, fixed size
Definition: Walker.h:75
const std::vector< RealType > & get_ptcl_inv_mass() const
Definition: MCPopulation.h:225
UPtrVector< QMCHamiltonian > walker_hamiltonians_
Definition: MCPopulation.h:78
void measureGlobalEnergyVariance(Communicate &comm, FullPrecRealType &ener, FullPrecRealType &variance) const
void checkIntegrity() const
}@
Wrapping information on parallelism.
Definition: Communicate.h:68
void set_target_samples(IndexType samples)
Definition: MCPopulation.h:187
PooledMemory< FullPrecRealType > WFBuffer_t
Definition: Walker.h:80
UPtrVector< TrialWaveFunction > dead_walker_trial_wavefunctions_
Definition: MCPopulation.h:83
Specialized paritlce class for atomistic simulations.
Definition: ParticleSet.h:55
A collection of functions for dividing fairly.
TrialWaveFunction & get_golden_twf()
Definition: MCPopulation.h:179
std::vector< RealType > ptclgrp_inv_mass_
1/Mass per species
Definition: MCPopulation.h:65
UPtrVector< ParticleSet > dead_walker_elec_particle_sets_
Definition: MCPopulation.h:82
void syncWalkersPerRank(Communicate *comm)
const std::vector< RealType > & get_ptclgrp_mass() const
Definition: MCPopulation.h:223
std::vector< RealType > ptclgrp_mass_
Definition: MCPopulation.h:63
MCPopulation & operator=(MCPopulation &)=delete
class to handle a set of variables that can be modified during optimizations
Definition: VariableSet.h:49
IndexType get_target_population() const
Definition: MCPopulation.h:171
void set_num_global_walkers(IndexType num_global_walkers)
Definition: MCPopulation.h:183
QMCTraits::IndexType IndexType
Definition: MCPopulation.h:45
const std::vector< RealType > & get_ptclgrp_inv_mass() const
Definition: MCPopulation.h:224
void createWalkers(IndexType num_walkers, const WalkerConfigurations &walker_configs, RealType reserve=1.0)
}@
OHMMS_INDEXTYPE IndexType
define other types
Definition: Configuration.h:65
void saveWalkerConfigurations(WalkerConfigurations &walker_configs)
save walker configurations to walker_configs_ref_
void set_num_local_walkers(IndexType num_local_walkers)
Definition: MCPopulation.h:184
const UPtrVector< MCPWalker > & get_dead_walkers() const
Definition: MCPopulation.h:196
const TrialWaveFunction & get_golden_twf() const
Definition: MCPopulation.h:178
Declaration of a TrialWaveFunction.
Declaraton of ParticleAttrib<T> derived from Vector.
Declaraton of Vector<T,Alloc> Manage memory through Alloc directly and allow referencing an existing ...
Class to represent a many-body trial wave function.
IndexType get_num_global_walkers() const
Definition: MCPopulation.h:168
UPtrVector< TrialWaveFunction > & get_dead_twfs()
Definition: MCPopulation.h:201
TrialWaveFunction * trial_wf_
Definition: MCPopulation.h:72
UPtrVector< ParticleSet > walker_elec_particle_sets_
Definition: MCPopulation.h:76
UPtrVector< MCPWalker > walkers_
Definition: MCPopulation.h:61
QTFull::RealType FullPrecRealType
Definition: Configuration.h:66
WalkerElementsRef getWalkerElementsRef(const size_t walker_index)
Non threadsafe access to walkers and their elements.
UPtrVector< MCPWalker > dead_walkers_
Definition: MCPopulation.h:62
IndexType get_target_samples() const
Definition: MCPopulation.h:172
IndexType get_num_local_walkers() const
Definition: MCPopulation.h:169
UPtrVector< ParticleSet > & get_elec_particle_sets()
Definition: MCPopulation.h:198
void killWalker(MCPWalker &)
Kill a walker (just barely)
std::vector< IV > fairDivide(IV ntot, IV npart)
return the occupation vector for ntot entities partitioned npart ways.
Definition: FairDivide.h:77
A container class to represent a walker.
Definition: Walker.h:49
ParticleSet & get_golden_electrons()
Definition: MCPopulation.h:177
MCDataType< QMCTraits::FullPrecRealType > ensemble_property_
Definition: MCPopulation.h:51
void redistributeWalkers(WTTV &walker_consumers)
distributes walkers and their "cloned" elements to the elements of a vector of unique_ptr to "walker_...
Definition: MCPopulation.h:131
void killLastWalker()
Kill last walker (just barely)
ParticleSet * elec_particle_set_
Definition: MCPopulation.h:73
const UPtrVector< MCPWalker > & get_walkers() const
Definition: MCPopulation.h:195