QMCPACK
SpinDensityNew Class Reference

Class that collects density per species of particle. More...

+ Inheritance diagram for SpinDensityNew:
+ Collaboration diagram for SpinDensityNew:

Public Types

using POLT = PtclOnLatticeTraits
 
using Lattice = POLT::ParticleLayout
 
using QMCT = QMCTraits
 
using FullPrecRealType = QMCT::FullPrecRealType
 
- Public Types inherited from OperatorEstBase
using QMCT = QMCTraits
 
using FullPrecRealType = QMCT::FullPrecRealType
 
using MCPWalker = Walker< QMCTraits, PtclOnLatticeTraits >
 
using Data = std::vector< QMCT::RealType >
 

Public Member Functions

 SpinDensityNew (SpinDensityInput &&sdi, const SpeciesSet &species, DataLocality dl=DataLocality::crowd)
 Constructor for SpinDensityNew that contains an explicitly defined cell part of legacy input handling, Deprecated. More...
 
 SpinDensityNew (SpinDensityInput &&sdi, const Lattice &, const SpeciesSet &species, const DataLocality dl=DataLocality::crowd)
 Constructor. More...
 
 SpinDensityNew (const SpinDensityNew &sdn, DataLocality dl)
 Constructor used when spawing crowd clones needs to be public so std::make_unique can call it. More...
 
void startBlock (int steps) override
 This allows us to allocate the necessary data for the DataLocality::queue. More...
 
std::unique_ptr< OperatorEstBasespawnCrowdClone () const override
 standard interface More...
 
void accumulate (const RefVector< MCPWalker > &walkers, const RefVector< ParticleSet > &psets, const RefVector< TrialWaveFunction > &wfns, const RefVector< QMCHamiltonian > &hams, RandomBase< FullPrecRealType > &rng) override
 accumulate 1 or more walkers of SpinDensity samples More...
 
void collect (const RefVector< OperatorEstBase > &operator_estimators) override
 this allows the EstimatorManagerNew to reduce without needing to know the details of SpinDensityNew's data. More...
 
void registerOperatorEstimator (hdf_archive &file) override
 this allows the EstimatorManagerNew to reduce without needing to know the details of SpinDensityNew's data. More...
 
- Public Member Functions inherited from OperatorEstBase
 OperatorEstBase (DataLocality dl)
 constructor More...
 
 OperatorEstBase (const OperatorEstBase &oth)
 Shallow copy constructor! This alows us to keep the default copy constructors for derived classes which is quite useful to the spawnCrowdClone design. More...
 
virtual ~OperatorEstBase ()=default
 virtual destructor More...
 
virtual void normalize (QMCT::RealType invToWgt)
 
std::vector< QMCT::RealType > & get_data ()
 
void write (hdf_archive &file)
 Write to previously registered observable_helper hdf5 wrapper. More...
 
void zero ()
 zero data appropriately for the DataLocality More...
 
QMCT::FullPrecRealType get_walkers_weight () const
 Return the total walker weight for this block. More...
 
const std::string & get_my_name () const
 
virtual void registerListeners (QMCHamiltonian &ham_leader)
 Register 0-many listeners with a leading QMCHamiltonian instance i.e. More...
 
bool isListenerRequired ()
 
DataLocality get_data_locality () const
 

Private Member Functions

 SpinDensityNew (const SpinDensityNew &sdn)=default
 
size_t getFullDataSize ()
 derived_parameters_ must be valid i.e. More...
 
void accumulateToData (size_t point, QMCT::RealType weight)
 
void reset ()
 
void report (const std::string &pad)
 

Static Private Member Functions

static std::vector< int > getSpeciesSize (const SpeciesSet &species)
 

Private Attributes

const SpinDensityInput input_
 
const SpeciesSetspecies_
 
const std::vector< int > species_size_
 
Lattice lattice_
 

they should be limited to values that can be changed from input or are not present explicitly in the SpinDensityInput More...
 
SpinDensityInput::DerivedParameters derived_parameters_
 
class testing::SpinDensityNewTests
 }@ More...
 

Additional Inherited Members

- Protected Attributes inherited from OperatorEstBase
DataLocality data_locality_
 locality for accumulation of estimator data. More...
 
std::string my_name_
 name of this object – only used for debugging and h5 output More...
 
QMCT::FullPrecRealType walkers_weight_
 
std::vector< ObservableHelperh5desc_
 
Data data_
 
bool requires_listener_ = false
 

Detailed Description

Class that collects density per species of particle.

commonly used for spin up and down electrons

Definition at line 35 of file SpinDensityNew.h.

Member Typedef Documentation

◆ FullPrecRealType

Definition at line 41 of file SpinDensityNew.h.

◆ Lattice

Definition at line 39 of file SpinDensityNew.h.

◆ POLT

Definition at line 38 of file SpinDensityNew.h.

◆ QMCT

using QMCT = QMCTraits

Definition at line 40 of file SpinDensityNew.h.

Constructor & Destructor Documentation

◆ SpinDensityNew() [1/4]

SpinDensityNew ( SpinDensityInput &&  sdi,
const SpeciesSet species,
DataLocality  dl = DataLocality::crowd 
)

Constructor for SpinDensityNew that contains an explicitly defined cell part of legacy input handling, Deprecated.

Definition at line 23 of file SpinDensityNew.cpp.

References SpinDensityInput::calculateDerivedParameters(), qmcplusplus::crowd, OperatorEstBase::data_, OperatorEstBase::data_locality_, SpinDensityNew::derived_parameters_, CrystalLattice< T, D >::explicitly_defined, SpinDensityInput::get_cell(), SpinDensityInput::get_save_memory(), SpinDensityInput::get_write_report(), SpinDensityNew::getFullDataSize(), SpinDensityNew::input_, SpinDensityNew::lattice_, OperatorEstBase::my_name_, qmcplusplus::rank, and SpinDensityNew::report().

24  : OperatorEstBase(dl), input_(std::move(input)), species_(species), species_size_(getSpeciesSize(species))
25 {
26  my_name_ = "SpinDensity";
27 
29  if (input_.get_save_memory())
30  dl = DataLocality::rank;
31 
32  if (input_.get_cell().explicitly_defined == true)
34  else
35  throw std::runtime_error("If SpinDensityInput does not contain a cell definition you must call the constructor "
36  "with an explicit lattice defined");
37 
39 
40  data_.resize(getFullDataSize(), 0.0);
41 
43  report(" ");
44 }
DataLocality data_locality_
locality for accumulation of estimator data.
void report(const std::string &pad)
const std::vector< int > species_size_
static std::vector< int > getSpeciesSize(const SpeciesSet &species)
OperatorEstBase(DataLocality dl)
constructor
size_t getFullDataSize()
derived_parameters_ must be valid i.e.
const SpeciesSet & species_
Lattice lattice_
they should be limited to values that can be changed from input or are not present explicitly in t...
SpinDensityInput::DerivedParameters derived_parameters_
const SpinDensityInput input_
testing::ValidSpinDensityInput input
bool explicitly_defined
true, the lattice is defined by the input instead of an artificial default
DerivedParameters calculateDerivedParameters(const Lattice &lattice) const
Derived parameters of SpinDensity.
std::string my_name_
name of this object – only used for debugging and h5 output

◆ SpinDensityNew() [2/4]

SpinDensityNew ( SpinDensityInput &&  sdi,
const Lattice lattice,
const SpeciesSet species,
const DataLocality  dl = DataLocality::crowd 
)

Constructor.

If the sdi contains a cell definition the Lattice passed will be ignored.

Other wise the crystal lattice should come from the same particle set as the species set. in case you are tempted to just pass the ParticleSet don't. It clouds the data dependence of constructing the estimator and creates a strong coupling between the classes.

Ideally when validating input is built up enough there would be only one constructor with signature

SpinDensityNew(SpinDensityInput&& sdi, SpinDensityInput::DerivedParameters&& dev_par, SpeciesSet species, DataLocality dl);

Definition at line 46 of file SpinDensityNew.cpp.

References SpinDensityInput::calculateDerivedParameters(), OperatorEstBase::data_, OperatorEstBase::data_locality_, SpinDensityNew::derived_parameters_, CrystalLattice< T, D >::explicitly_defined, SpinDensityInput::get_cell(), SpinDensityInput::get_write_report(), SpinDensityNew::getFullDataSize(), SpinDensityNew::input_, SpinDensityNew::lattice_, OperatorEstBase::my_name_, and SpinDensityNew::report().

50  : OperatorEstBase(dl),
51  input_(std::move(input)),
52  species_(species),
53  species_size_(getSpeciesSize(species)),
55 {
56  my_name_ = "SpinDensity";
57  data_locality_ = dl;
58  if (input_.get_cell().explicitly_defined == true)
61  data_.resize(getFullDataSize());
63  report(" ");
64 }
DataLocality data_locality_
locality for accumulation of estimator data.
void report(const std::string &pad)
CrystalLattice< OHMMS_PRECISION, OHMMS_DIM > lattice
const std::vector< int > species_size_
static std::vector< int > getSpeciesSize(const SpeciesSet &species)
OperatorEstBase(DataLocality dl)
constructor
size_t getFullDataSize()
derived_parameters_ must be valid i.e.
const SpeciesSet & species_
Lattice lattice_
they should be limited to values that can be changed from input or are not present explicitly in t...
SpinDensityInput::DerivedParameters derived_parameters_
const SpinDensityInput input_
testing::ValidSpinDensityInput input
bool explicitly_defined
true, the lattice is defined by the input instead of an artificial default
DerivedParameters calculateDerivedParameters(const Lattice &lattice) const
Derived parameters of SpinDensity.
std::string my_name_
name of this object – only used for debugging and h5 output

◆ SpinDensityNew() [3/4]

SpinDensityNew ( const SpinDensityNew sdn,
DataLocality  dl 
)

Constructor used when spawing crowd clones needs to be public so std::make_unique can call it.

Do not use directly unless you've really thought it through.

Definition at line 66 of file SpinDensityNew.cpp.

References OperatorEstBase::data_locality_.

67 {
68  data_locality_ = dl;
69 }
DataLocality data_locality_
locality for accumulation of estimator data.
SpinDensityNew(SpinDensityInput &&sdi, const SpeciesSet &species, DataLocality dl=DataLocality::crowd)
Constructor for SpinDensityNew that contains an explicitly defined cell part of legacy input handling...
SpinDensityNew sdn(std::move(sdi), lattice, species_set)

◆ SpinDensityNew() [4/4]

SpinDensityNew ( const SpinDensityNew sdn)
privatedefault

Member Function Documentation

◆ accumulate()

void accumulate ( const RefVector< MCPWalker > &  walkers,
const RefVector< ParticleSet > &  psets,
const RefVector< TrialWaveFunction > &  wfns,
const RefVector< QMCHamiltonian > &  hams,
RandomBase< FullPrecRealType > &  rng 
)
overridevirtual

accumulate 1 or more walkers of SpinDensity samples

Gets called every step and writes to thread local data.

I tried for readable and not doing the optimizers job. The offsets into bare data are already bad enough.

Implements OperatorEstBase.

Definition at line 117 of file SpinDensityNew.cpp.

References SpinDensityNew::accumulateToData(), SpinDensityNew::derived_parameters_, QMCTraits::DIM, qmcplusplus::floor(), SpinDensityNew::lattice_, qmcplusplus::Units::time::ps, qmcplusplus::pset, qmcplusplus::Units::time::s, SpeciesSet::size(), SpinDensityNew::species_, SpinDensityNew::species_size_, CrystalLattice< T, D >::toUnit(), qmcplusplus::walker, qmcplusplus::hdf::walkers, and OperatorEstBase::walkers_weight_.

Referenced by qmcplusplus::accumulateFromPsets(), qmcplusplus::randomUpdateAccumulate(), and qmcplusplus::TEST_CASE().

122 {
123  auto& dp_ = derived_parameters_;
124  for (int iw = 0; iw < walkers.size(); ++iw)
125  {
126  MCPWalker& walker = walkers[iw];
127  ParticleSet& pset = psets[iw];
128  QMCT::RealType weight = walker.Weight;
129  assert(weight >= 0);
130  // for testing
131  walkers_weight_ += weight;
132  int p = 0;
133  size_t offset = 0;
134  for (int s = 0; s < species_.size(); ++s, offset += dp_.npoints)
135  for (int ps = 0; ps < species_size_[s]; ++ps, ++p)
136  {
137  QMCT::PosType u = lattice_.toUnit(pset.R[p] - dp_.corner);
138  size_t point = offset;
139  for (int d = 0; d < QMCT::DIM; ++d)
140  point += dp_.gdims[d] * ((int)(dp_.grid[d] * (u[d] - std::floor(u[d])))); //periodic only
141  accumulateToData(point, weight);
142  }
143  }
144 }
QTBase::RealType RealType
Definition: Configuration.h:58
const char walkers[]
Definition: HDFVersion.h:36
int size() const
return the number of species
Definition: SpeciesSet.h:53
QMCT::FullPrecRealType walkers_weight_
const std::vector< int > species_size_
void accumulateToData(size_t point, QMCT::RealType weight)
QTBase::PosType PosType
Definition: Configuration.h:61
SingleParticlePos toUnit(const TinyVector< T1, D > &r) const
Convert a cartesian vector to a unit vector.
const SpeciesSet & species_
Lattice lattice_
they should be limited to values that can be changed from input or are not present explicitly in t...
SpinDensityInput::DerivedParameters derived_parameters_
MakeReturn< UnaryNode< FnFloor, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t > >::Expression_t floor(const Vector< T1, C1 > &l)
Walker< QMCTraits, PtclOnLatticeTraits > MCPWalker

◆ accumulateToData()

void accumulateToData ( size_t  point,
QMCT::RealType  weight 
)
private

Definition at line 146 of file SpinDensityNew.cpp.

References qmcplusplus::crowd, OperatorEstBase::data_, OperatorEstBase::data_locality_, and qmcplusplus::queue.

Referenced by SpinDensityNew::accumulate().

147 {
149  {
150  data_[point] += weight;
151  }
153  {
154  data_.push_back(point);
155  data_.push_back(weight);
156  }
157  else
158  {
159  throw std::runtime_error("You cannot accumulate to a SpinDensityNew with datalocality of this type");
160  }
161 }
DataLocality data_locality_
locality for accumulation of estimator data.

◆ collect()

void collect ( const RefVector< OperatorEstBase > &  operator_estimators)
overridevirtual

this allows the EstimatorManagerNew to reduce without needing to know the details of SpinDensityNew's data.

can use base class default until crowd level SpinDensity estimators don't have a copy of the density grid.

Reimplemented from OperatorEstBase.

Definition at line 163 of file SpinDensityNew.cpp.

References OperatorEstBase::collect(), qmcplusplus::crowd, OperatorEstBase::data_, OperatorEstBase::data_locality_, OperatorEstBase::get_data(), qmcplusplus::rank, and OperatorEstBase::walkers_weight_.

164 {
166  {
167  for (OperatorEstBase& crowd_oeb : type_erased_operator_estimators)
168  {
169  // This will throw a std::bad_cast in debug if the calling code hands the
170  // wrong type erased operator_estimator type into here.
171  // In release we don't want that overhead.
172 #ifndef NDEBUG
173  auto& oeb = dynamic_cast<SpinDensityNew&>(crowd_oeb);
174 #else
175  auto& oeb = static_cast<SpinDensityNew&>(crowd_oeb);
176 #endif
177  auto& data = oeb.get_data();
178  for (int id = 0; id < data.size(); id += 2)
179  {
180  // This is a smell
181  size_t point{static_cast<size_t>(data[id])};
182  const QMCT::RealType weight{data[id + 1]};
183  data_[point] += weight;
184  walkers_weight_ += weight;
185  }
186  oeb.zero();
187  }
188  }
190  {
191  OperatorEstBase::collect(type_erased_operator_estimators);
192  }
193  else
194  {
195  throw std::runtime_error("You cannot call collect on a SpinDensityNew with this DataLocality");
196  }
197 }
QTBase::RealType RealType
Definition: Configuration.h:58
DataLocality data_locality_
locality for accumulation of estimator data.
QMCT::FullPrecRealType walkers_weight_
SpinDensityNew(SpinDensityInput &&sdi, const SpeciesSet &species, DataLocality dl=DataLocality::crowd)
Constructor for SpinDensityNew that contains an explicitly defined cell part of legacy input handling...
OperatorEstBase(DataLocality dl)
constructor
virtual void collect(const RefVector< OperatorEstBase > &oebs)
Reduce estimator result data from crowds to rank.

◆ getFullDataSize()

size_t getFullDataSize ( )
private

derived_parameters_ must be valid i.e.

initialized with call to input_.calculateDerivedParameters

Definition at line 82 of file SpinDensityNew.cpp.

References SpinDensityNew::derived_parameters_, SpinDensityInput::DerivedParameters::npoints, SpeciesSet::size(), and SpinDensityNew::species_.

Referenced by SpinDensityNew::SpinDensityNew().

int size() const
return the number of species
Definition: SpeciesSet.h:53
const SpeciesSet & species_
SpinDensityInput::DerivedParameters derived_parameters_

◆ getSpeciesSize()

std::vector< int > getSpeciesSize ( const SpeciesSet species)
staticprivate

Definition at line 71 of file SpinDensityNew.cpp.

References SpeciesSet::findAttribute(), qmcplusplus::Units::time::s, and SpeciesSet::size().

72 {
73  std::vector<int> species_size;
74  int index = species.findAttribute("membersize");
75  if (index < 0)
76  throw std::runtime_error("SpinDensity(P) Species set does not have the required attribute 'membersize'");
77  for (int s = 0; s < species.size(); ++s)
78  species_size.push_back(species(index, s));
79  return species_size;
80 }

◆ registerOperatorEstimator()

void registerOperatorEstimator ( hdf_archive file)
overridevirtual

this allows the EstimatorManagerNew to reduce without needing to know the details of SpinDensityNew's data.

can use base class default until crowd level SpinDensity estimators don't have a copy of the density grid. this gets us into the hdf5 file

Just parroting for now don't fully understand. , needs to be unraveled and simplified the hdf5 output is another big state big coupling design.

Reimplemented from OperatorEstBase.

Definition at line 220 of file SpinDensityNew.cpp.

References SpinDensityNew::derived_parameters_, OperatorEstBase::h5desc_, OperatorEstBase::my_name_, SpinDensityInput::DerivedParameters::npoints, qmcplusplus::oh, qmcplusplus::Units::time::s, ObservableHelper::set_dimensions(), SpeciesSet::size(), SpinDensityNew::species_, and SpeciesSet::speciesName.

221 {
222  std::vector<size_t> my_indexes;
223 
224  std::vector<int> ng(1, derived_parameters_.npoints);
225 
226  hdf_path hdf_name{my_name_};
227  for (int s = 0; s < species_.size(); ++s)
228  {
229  h5desc_.emplace_back(hdf_name / species_.speciesName[s]);
230  auto& oh = h5desc_.back();
232  }
233 }
std::vector< ObservableHelper > h5desc_
int size() const
return the number of species
Definition: SpeciesSet.h:53
const SpeciesSet & species_
std::vector< std::string > speciesName
Species name list.
Definition: SpeciesSet.h:44
void set_dimensions(const std::vector< int > &dims, int first)
set the shape of this observable
SpinDensityInput::DerivedParameters derived_parameters_
ObservableHelper oh
std::string my_name_
name of this object – only used for debugging and h5 output

◆ report()

void report ( const std::string &  pad)
private

Definition at line 199 of file SpinDensityNew.cpp.

References qmcplusplus::app_log(), CrystalLattice< T, D >::Center, SpinDensityNew::derived_parameters_, QMCTraits::DIM, SpinDensityNew::lattice_, CrystalLattice< T, D >::Rv, qmcplusplus::Units::time::s, SpeciesSet::size(), SpinDensityNew::species_, SpinDensityNew::species_size_, and SpeciesSet::speciesName.

Referenced by SpinDensityNew::SpinDensityNew().

200 {
201  auto& dp_ = derived_parameters_;
202  app_log() << pad << "SpinDensity report" << std::endl;
203  app_log() << pad << " dim = " << QMCT::DIM << std::endl;
204  app_log() << pad << " npoints = " << dp_.npoints << std::endl;
205  app_log() << pad << " grid = " << dp_.grid << std::endl;
206  app_log() << pad << " gdims = " << dp_.gdims << std::endl;
207  app_log() << pad << " corner = " << dp_.corner << std::endl;
208  app_log() << pad << " center = " << dp_.corner + lattice_.Center << std::endl;
209  app_log() << pad << " cell " << std::endl;
210  for (int d = 0; d < QMCT::DIM; ++d)
211  app_log() << pad << " " << d << " " << lattice_.Rv[d] << std::endl;
212  app_log() << pad << " end cell " << std::endl;
213  app_log() << pad << " nspecies = " << species_.size() << std::endl;
214  for (int s = 0; s < species_.size(); ++s)
215  app_log() << pad << " species[" << s << "]"
216  << " = " << species_.speciesName[s] << " " << species_size_[s] << std::endl;
217  app_log() << pad << "end SpinDensity report" << std::endl;
218 }
SingleParticlePos Center
Center of the cell sum(Rv[i])/2.
std::ostream & app_log()
Definition: OutputManager.h:65
int size() const
return the number of species
Definition: SpeciesSet.h:53
TinyVector< SingleParticlePos, D > Rv
Real-space unit vectors.
const std::vector< int > species_size_
const SpeciesSet & species_
Lattice lattice_
they should be limited to values that can be changed from input or are not present explicitly in t...
std::vector< std::string > speciesName
Species name list.
Definition: SpeciesSet.h:44
SpinDensityInput::DerivedParameters derived_parameters_

◆ reset()

void reset ( )
private

◆ spawnCrowdClone()

std::unique_ptr< OperatorEstBase > spawnCrowdClone ( ) const
overridevirtual

standard interface

Implements OperatorEstBase.

Definition at line 84 of file SpinDensityNew.cpp.

References OperatorEstBase::data_, OperatorEstBase::data_locality_, qmcplusplus::queue, qmcplusplus::rank, and SpinDensityNew::species_size_.

Referenced by qmcplusplus::accumulateFromPsets().

85 {
86  std::size_t data_size = data_.size();
87  auto spawn_data_locality = data_locality_;
89  {
90  spawn_data_locality = DataLocality::queue;
91  // at construction we don't know what the data requirement is going to be
92  // since its steps per block dependent. so start with 10 steps worth.
93  int num_particles = std::accumulate(species_size_.begin(), species_size_.end(), 0);
94  data_size = num_particles * 20;
95  }
96  UPtr<SpinDensityNew> spawn(std::make_unique<SpinDensityNew>(*this, spawn_data_locality));
97  spawn->get_data().resize(data_size);
98  return spawn;
99 }
DataLocality data_locality_
locality for accumulation of estimator data.
const std::vector< int > species_size_

◆ startBlock()

void startBlock ( int  steps)
overridevirtual

This allows us to allocate the necessary data for the DataLocality::queue.

Implements OperatorEstBase.

Definition at line 101 of file SpinDensityNew.cpp.

References OperatorEstBase::data_, OperatorEstBase::data_locality_, qmcplusplus::rank, and SpinDensityNew::species_size_.

102 {
104  {
105  int num_particles = std::accumulate(species_size_.begin(), species_size_.end(), 0);
106  size_t data_size = num_particles * steps * 2;
107  data_.reserve(data_size);
108  data_.resize(0);
109  }
110 }
DataLocality data_locality_
locality for accumulation of estimator data.
const std::vector< int > species_size_

Friends And Related Function Documentation

◆ testing::SpinDensityNewTests

friend class testing::SpinDensityNewTests
friend

}@

Definition at line 143 of file SpinDensityNew.h.

Member Data Documentation

◆ derived_parameters_

◆ input_

const SpinDensityInput input_
private

Definition at line 126 of file SpinDensityNew.h.

Referenced by SpinDensityNew::SpinDensityNew().

◆ lattice_

Lattice lattice_
private



they should be limited to values that can be changed from input or are not present explicitly in the SpinDensityInput

Lattice is always local since it is either in the input or a constructor argument.

Definition at line 139 of file SpinDensityNew.h.

Referenced by SpinDensityNew::accumulate(), SpinDensityNew::report(), and SpinDensityNew::SpinDensityNew().

◆ species_

◆ species_size_


The documentation for this class was generated from the following files: