QMCPACK
DensityEstimator.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: Bryan Clark, bclark@Princeton.edu, Princeton University
8 // John R. Gergely, University of Illinois at Urbana-Champaign
9 // Ken Esler, kpesler@gmail.com, University of Illinois at Urbana-Champaign
10 // Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
11 // Jeremy McMinnis, jmcminis@gmail.com, University of Illinois at Urbana-Champaign
12 // Ye Luo, yeluo@anl.gov, Argonne National Laboratory
13 // Mark A. Berrill, berrillma@ornl.gov, Oak Ridge National Laboratory
14 //
15 // File created by: Bryan Clark, bclark@Princeton.edu, Princeton University
16 //////////////////////////////////////////////////////////////////////////////////////
17 
18 
19 #include "DensityEstimator.h"
20 #include "OhmmsData/AttributeSet.h"
22 #include "Particle/DistanceTable.h"
24 
25 #include <stdexcept> // std::invalid_argument
26 
27 namespace qmcplusplus
28 {
32 
33 
35 {
36  update_mode_.set(COLLECTABLE, 1);
37  periodic_ = (elns.getLattice().SuperCellEnum != SUPERCELL_OPEN);
38  for (int dim = 0; dim < OHMMS_DIM; ++dim)
39  {
40  density_max_[dim] = elns.getLattice().Length[dim];
41  scale_factor_[dim] = 1.0 / elns.getLattice().Length[dim];
42  }
43 }
44 
45 std::string DensityEstimator::getClassName() const { return "DensityEstimator"; }
46 
48 
50 {
51  if (t_walker_ == nullptr)
52  {
53  throw std::invalid_argument("calling DensityEstimator::evaluate needs the weight of the walkers");
54  }
55 
56  const RealType wgt = t_walker_->Weight;
57 
58  if (periodic_)
59  {
60  for (int iat = 0; iat < P.getTotalNum(); ++iat)
61  {
62  PosType ru;
63  ru = P.getLattice().toUnit(P.R[iat]);
64  int i = static_cast<int>(delta_inv_[0] * (ru[0] - std::floor(ru[0])));
65  int j = static_cast<int>(delta_inv_[1] * (ru[1] - std::floor(ru[1])));
66  int k = static_cast<int>(delta_inv_[2] * (ru[2] - std::floor(ru[2])));
67  P.Collectables[getGridIndex(i, j, k)] += wgt; //1.0;
68  }
69  }
70  else
71  {
72  for (int iat = 0; iat < P.getTotalNum(); ++iat)
73  {
74  PosType ru;
75  for (int dim = 0; dim < OHMMS_DIM; dim++)
76  {
77  ru[dim] = (P.R[iat][dim] - density_min_[dim]) * scale_factor_[dim];
78  }
79  if (ru[0] > 0.0 && ru[1] > 0.0 && ru[2] > 0.0 && ru[0] < 1.0 && ru[1] < 1.0 && ru[2] < 1.0)
80  {
81  int i = static_cast<int>(delta_inv_[0] * (ru[0] - std::floor(ru[0])));
82  int j = static_cast<int>(delta_inv_[1] * (ru[1] - std::floor(ru[1])));
83  int k = static_cast<int>(delta_inv_[2] * (ru[2] - std::floor(ru[2])));
84  P.Collectables[getGridIndex(i, j, k)] += wgt; //1.0;
85  }
86  }
87  }
88  return 0.0;
89 }
90 
92 
94 {
95  //current index
96  my_index_ = collectables.current();
97  std::vector<RealType> tmp(num_grids_[OHMMS_DIM]);
98  collectables.add(tmp.begin(), tmp.end());
99 }
100 
101 void DensityEstimator::registerCollectables(std::vector<ObservableHelper>& h5desc, hdf_archive& file) const
102 {
103  int loc = h5desc.size();
104  std::vector<int> ng(OHMMS_DIM);
105  for (int i = 0; i < OHMMS_DIM; ++i)
106  ng[i] = num_grids_[i];
107  h5desc.emplace_back(hdf_path{name_});
108  auto& h5o = h5desc.back();
109  h5o.set_dimensions(ng, my_index_);
110 }
111 
113 
115 
116 /** check xml elements
117  *
118  * <estimator name="density" debug="no" delta="0.1 0.1 0.1"/>
119  */
120 bool DensityEstimator::put(xmlNodePtr cur)
121 {
122  delta_ = 0.1;
123  std::vector<double> delta;
124  std::string debug("no");
125  std::string potential("no");
126  OhmmsAttributeSet attrib;
127  attrib.add(debug, "debug");
128  attrib.add(potential, "potential");
129  attrib.add(density_min_[0], "x_min");
130  attrib.add(density_min_[1], "y_min");
131  attrib.add(density_min_[2], "z_min");
132  attrib.add(density_max_[0], "x_max");
133  attrib.add(density_max_[1], "y_max");
134  attrib.add(density_max_[2], "z_max");
135  attrib.add(delta_, "delta");
136  attrib.put(cur);
137  if (!periodic_)
138  {
139  for (int dim = 0; dim < OHMMS_DIM; ++dim)
140  scale_factor_[dim] = 1.0 / (density_max_[dim] - density_min_[dim]);
141  }
142  resize();
143  return true;
144 }
145 
146 bool DensityEstimator::get(std::ostream& os) const
147 {
148  os << name_ << " bin =" << delta_ << " bohrs " << std::endl;
149  return true;
150 }
151 
152 std::unique_ptr<OperatorBase> DensityEstimator::makeClone(ParticleSet& qp, TrialWaveFunction& psi)
153 {
154  //default constructor is sufficient
155  return std::make_unique<DensityEstimator>(*this);
156 }
157 
159 {
160  for (int i = 0; i < OHMMS_DIM; ++i)
161  {
162  delta_inv_[i] = 1.0 / delta_[i];
163  num_grids_[i] = static_cast<int>(delta_inv_[i]);
164  if (num_grids_[i] < 2)
165  {
166  APP_ABORT("DensityEstimator::resize invalid bin size");
167  }
168  }
169  app_log() << " DensityEstimator bin_size= " << num_grids_ << " delta = " << delta_ << std::endl;
171 }
172 
173 int DensityEstimator::getGridIndex(int i, int j, int k) const noexcept
174 {
175  return my_index_ + k + num_grids_[2] * (j + num_grids_[1] * i);
176 }
177 
178 } // namespace qmcplusplus
void setParticlePropertyList(PropertySetType &plist, int offset) override
One-Dimensional linear-grid.
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
int getGridIndex(int i, int j, int k) const noexcept
Get the linearized grid Index object from 3D coordinates.
std::string getClassName() const override
return class name
size_t getTotalNum() const
Definition: ParticleSet.h:493
int my_index_
starting index of this object
Definition: OperatorBase.h:535
std::ostream & app_log()
Definition: OutputManager.h:65
void setObservables(PropertySetType &plist) override
Set the values evaluated by this object to plist Default implementation is to assign Value which is u...
bool put(xmlNodePtr cur)
assign attributes to the set
Definition: AttributeSet.h:55
bool put(xmlNodePtr cur) override
check xml elements
class to handle hdf file
Definition: hdf_archive.h:51
TinyVector< RealType, OHMMS_DIM > density_min_
lower bound
Vectorized record engine for scalar properties.
TinyVector< int, OHMMS_DIM+1 > num_grids_
number of grids
#define OHMMS_DIM
Definition: config.h:64
void registerCollectables(std::vector< ObservableHelper > &h5desc, hdf_archive &file) const override
std::string name_
name of this object
Definition: OperatorBase.h:527
Specialized paritlce class for atomistic simulations.
Definition: ParticleSet.h:55
std::unique_ptr< OperatorBase > makeClone(ParticleSet &qp, TrialWaveFunction &psi) final
void add(T &x)
Definition: PooledData.h:88
class to handle a set of attributes of an xmlNode
Definition: AttributeSet.h:24
TinyVector< RealType, OHMMS_DIM > density_max_
upper bound
OneDimCubicSpline< pRealType > RadFunctorType
void addObservables(PropertySetType &plist)
ParticlePos R
Position.
Definition: ParticleSet.h:79
void resize()
resize the internal data
#define APP_ABORT(msg)
Widely used but deprecated fatal error macros from legacy code.
Definition: AppAbort.h:27
size_type current() const
Definition: PooledData.h:51
DensityEstimator(ParticleSet &elns)
Walker_t * t_walker_
reference to the current walker
Definition: OperatorBase.h:541
void resetTargetParticleSet(ParticleSet &P) override
Reset the data with the target ParticleSet.
Buffer_t Collectables
observables in addition to those registered in Properties/PropertyList
Definition: ParticleSet.h:126
FullPrecRealType Return_t
type of return value of evaluate
Definition: OperatorBase.h:64
Class to represent a many-body trial wave function.
TinyVector< RealType, OHMMS_DIM > delta_
bin size
TinyVector< RealType, OHMMS_DIM > scale_factor_
scaling factor for conversion
FullPrecRealType Weight
Weight of the walker.
Definition: Walker.h:102
const auto & getLattice() const
Definition: ParticleSet.h:251
Define a LRHandler with two template parameters.
base class for LRHandlerTemp<FUNC,BASIS> and DummyLRHanlder<typename Func>
Definition: LRHandlerBase.h:30
bool get(std::ostream &os) const override
write about the class
LinearGrid< pRealType > GridType
void add(PDT &aparam, const std::string &aname, std::vector< PDT > candidate_values={}, TagStatus status=TagStatus::OPTIONAL)
add a new attribute
Definition: AttributeSet.h:42
Declaration of a MCWalkerConfiguration.
Return_t evaluate(ParticleSet &P) override
Evaluate the local energy contribution of this component.
std::bitset< 8 > update_mode_
set the current update mode
Definition: OperatorBase.h:521
bool periodic_
true if any direction of a supercell is periodic
MakeReturn< UnaryNode< FnFloor, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t > >::Expression_t floor(const Vector< T1, C1 > &l)
TinyVector< RealType, OHMMS_DIM > delta_inv_
inverse