QMCPACK
SpinDensity.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: Jaron T. Krogel, krogeljt@ornl.gov, Oak Ridge National Laboratory
8 // Ye Luo, yeluo@anl.gov, Argonne National Laboratory
9 // Mark A. Berrill, berrillma@ornl.gov, Oak Ridge National Laboratory
10 //
11 // File created by: Jaron T. Krogel, krogeljt@ornl.gov, Oak Ridge National Laboratory
12 //////////////////////////////////////////////////////////////////////////////////////
13 
14 
15 #include "SpinDensity.h"
16 #include "OhmmsData/AttributeSet.h"
18 
19 namespace qmcplusplus
20 {
22 {
23  // get particle information
24  SpeciesSet& species = P.getSpeciesSet();
25  nspecies = species.size();
26  for (int s = 0; s < nspecies; ++s)
27  species_size.push_back(P.groupsize(s));
28  for (int s = 0; s < nspecies; ++s)
29  species_name.push_back(species.speciesName[s]);
30  reset();
31 
32  //jtk: spin density only works for periodic bc's for now
33  // abort if using open boundary conditions
34  bool open_bcs = (P.getLattice().SuperCellEnum == SUPERCELL_OPEN);
35  if (open_bcs)
36  {
37  APP_ABORT("SpinDensity is not implemented for open boundary conditions at present\n please contact the developers "
38  "if you need this feature");
39  }
40 
41  Ptmp = &P;
42 }
43 
44 
46 {
47  name_ = "SpinDensity";
48  update_mode_.set(COLLECTABLE, 1);
49  corner = 0.0;
50 }
51 
52 
53 std::unique_ptr<OperatorBase> SpinDensity::makeClone(ParticleSet& P, TrialWaveFunction& Psi)
54 {
55  return std::make_unique<SpinDensity>(*this);
56 }
57 
58 
59 bool SpinDensity::put(xmlNodePtr cur)
60 {
61  using std::ceil;
62  using std::sqrt;
63  reset();
64  std::string write_report = "no";
65  OhmmsAttributeSet attrib;
66  attrib.add(name_, "name");
67  attrib.add(write_report, "report");
68  attrib.put(cur);
69 
70  bool have_dr = false;
71  bool have_grid = false;
72  bool have_center = false;
73  bool have_corner = false;
74  bool have_cell = false;
75 
76  PosType dr;
77  PosType center;
79 
80  int test_moves = 0;
81 
82  xmlNodePtr element = cur->xmlChildrenNode;
83  while (element != NULL)
84  {
85  std::string ename((const char*)element->name);
86  if (ename == "parameter")
87  {
88  const std::string name(getXMLAttributeValue(element, "name"));
89  if (name == "dr")
90  {
91  have_dr = true;
92  putContent(dr, element);
93  }
94  else if (name == "grid")
95  {
96  have_grid = true;
97  putContent(grid, element);
98  }
99  else if (name == "corner")
100  {
101  have_corner = true;
102  putContent(corner, element);
103  }
104  else if (name == "center")
105  {
106  have_center = true;
107  putContent(center, element);
108  }
109  else if (name == "cell")
110  {
111  have_cell = true;
112  putContent(axes, element);
113  }
114  else if (name == "test_moves")
115  putContent(test_moves, element);
116  }
117  element = element->next;
118  }
119 
120  if (have_dr && have_grid)
121  {
122  APP_ABORT("SpinDensity::put dr and grid are provided, this is ambiguous");
123  }
124  else if (!have_dr && !have_grid)
125  APP_ABORT("SpinDensity::put must provide dr or grid");
126 
127  if (have_corner && have_center)
128  APP_ABORT("SpinDensity::put corner and center are provided, this is ambiguous");
129  if (have_cell)
130  {
131  cell.set(axes);
132  if (!have_corner && !have_center)
133  APP_ABORT("SpinDensity::put must provide corner or center");
134  }
135  else
136  cell = Ptmp->getLattice();
137 
138  if (have_center)
139  corner = center - cell.Center;
140 
141  if (have_dr)
142  for (int d = 0; d < DIM; ++d)
143  grid[d] = (int)ceil(sqrt(dot(cell.Rv[d], cell.Rv[d])) / dr[d]);
144 
145  npoints = 1;
146  for (int d = 0; d < DIM; ++d)
147  npoints *= grid[d];
148  gdims[0] = npoints / grid[0];
149  for (int d = 1; d < DIM; ++d)
150  gdims[d] = gdims[d - 1] / grid[d];
151 
152  if (write_report == "yes")
153  report(" ");
154  if (test_moves > 0)
155  test(test_moves, *Ptmp);
156 
157  return true;
158 }
159 
160 
161 void SpinDensity::report(const std::string& pad)
162 {
163  app_log() << pad << "SpinDensity report" << std::endl;
164  app_log() << pad << " dim = " << DIM << std::endl;
165  app_log() << pad << " npoints = " << npoints << std::endl;
166  app_log() << pad << " grid = " << grid << std::endl;
167  app_log() << pad << " gdims = " << gdims << std::endl;
168  app_log() << pad << " corner = " << corner << std::endl;
169  app_log() << pad << " center = " << corner + cell.Center << std::endl;
170  app_log() << pad << " cell " << std::endl;
171  for (int d = 0; d < DIM; ++d)
172  app_log() << pad << " " << d << " " << cell.Rv[d] << std::endl;
173  app_log() << pad << " end cell " << std::endl;
174  app_log() << pad << " nspecies = " << nspecies << std::endl;
175  for (int s = 0; s < nspecies; ++s)
176  app_log() << pad << " species[" << s << "]"
177  << " = " << species_name[s] << " " << species_size[s] << std::endl;
178  app_log() << pad << "end SpinDensity report" << std::endl;
179 }
180 
181 
183 {
184  my_index_ = collectables.current();
185  std::vector<RealType> tmp(nspecies * npoints);
186  collectables.add(tmp.begin(), tmp.end());
187 }
188 
189 
190 void SpinDensity::registerCollectables(std::vector<ObservableHelper>& h5desc, hdf_archive& file) const
191 {
192  std::vector<int> ng(1);
193  ng[0] = npoints;
194 
195  hdf_path hdf_name{name_};
196  for (int s = 0; s < nspecies; ++s)
197  {
198  h5desc.emplace_back(hdf_name / species_name[s]);
199  auto& oh = h5desc.back();
201  }
202 }
203 
204 
206 {
208  int p = 0;
209  int offset = my_index_;
210  for (int s = 0; s < nspecies; ++s, offset += npoints)
211  for (int ps = 0; ps < species_size[s]; ++ps, ++p)
212  {
213  PosType u = cell.toUnit(P.R[p] - corner);
214  //bool inside = true;
215  //for(int d=0;d<DIM;++d)
216  // inside &= u[d]>0.0 && u[d]<1.0;
217  //if(inside)
218  //{
219  int point = offset;
220  for (int d = 0; d < DIM; ++d)
221  point += gdims[d] * ((int)(grid[d] * (u[d] - std::floor(u[d])))); //periodic only
222  P.Collectables[point] += w;
223  //}
224  }
225  return 0.0;
226 }
227 
228 
229 void SpinDensity::test(int moves, ParticleSet& P)
230 {
231  app_log() << " SpinDensity test" << std::endl;
232  RandomGenerator rng;
233  int particles = P.getTotalNum();
234  int pmin = std::numeric_limits<int>::max();
235  int pmax = std::numeric_limits<int>::min();
236  for (int m = 0; m < moves; ++m)
237  {
238  for (int p = 0; p < particles; ++p)
239  {
240  PosType u;
241  for (int d = 0; d < DIM; ++d)
242  u[d] = rng();
243  P.R[p] = P.getLattice().toCart(u);
244  }
245  test_evaluate(P, pmin, pmax);
246  }
247  app_log() << " end SpinDensity test" << std::endl;
248  APP_ABORT("SpinDensity::test test complete");
249 }
250 
251 
253 {
254  int p = 0;
255  int offset = 0;
256  for (int s = 0; s < nspecies; ++s, offset += npoints)
257  for (int ps = 0; ps < species_size[s]; ++ps, ++p)
258  {
259  PosType u = cell.toUnit(P.R[p] - corner);
260  bool inside = true;
261  for (int d = 0; d < DIM; ++d)
262  inside &= u[d] > 0.0 && u[d] < 1.0;
263  if (inside)
264  {
265  int point = offset;
266  for (int d = 0; d < DIM; ++d)
267  point += gdims[d] * ((int)(u[d] * grid[d]));
268  pmin = std::min(pmin, point - offset);
269  pmax = std::max(pmax, point - offset);
270  }
271  }
272  app_log() << " pmin = " << pmin << " pmax = " << pmax << " npoints = " << npoints << std::endl;
273  return 0.0;
274 }
275 
276 } // namespace qmcplusplus
void set(const Tensor< TT, D > &lat)
set the lattice vector from the command-line options
SingleParticlePos Center
Center of the cell sum(Rv[i])/2.
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
bool put(xmlNodePtr cur) override
Read the input parameter.
Definition: SpinDensity.cpp:59
QTBase::RealType RealType
Definition: Configuration.h:58
std::vector< int > species_size
Definition: SpinDensity.h:33
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
bool put(xmlNodePtr cur)
assign attributes to the set
Definition: AttributeSet.h:55
class to handle hdf file
Definition: hdf_archive.h:51
Vectorized record engine for scalar properties.
Return_t test_evaluate(ParticleSet &P, int &pmin, int &pmax)
int size() const
return the number of species
Definition: SpeciesSet.h:53
SpinDensity(ParticleSet &P)
Definition: SpinDensity.cpp:21
T min(T a, T b)
void addObservables(PropertySetType &plist, BufferType &olist) override
named values to the property list Default implementaton uses addValue(plist_)
TinyVector< SingleParticlePos, D > Rv
Real-space unit vectors.
TinyVector< int, DIM > gdims
Definition: SpinDensity.h:38
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 &P, TrialWaveFunction &psi) final
Definition: SpinDensity.cpp:53
Return_t evaluate(ParticleSet &P) override
Evaluate the local energy contribution of this component.
void add(T &x)
Definition: PooledData.h:88
MakeReturn< UnaryNode< FnCeil, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t > >::Expression_t ceil(const Vector< T1, C1 > &l)
class to handle a set of attributes of an xmlNode
Definition: AttributeSet.h:24
int groupsize(int igroup) const
return the size of a group
Definition: ParticleSet.h:527
void report(const std::string &pad)
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
SingleParticlePos toUnit(const TinyVector< T1, D > &r) const
Convert a cartesian vector to a unit vector.
size_type current() const
Definition: PooledData.h:51
Walker_t * t_walker_
reference to the current walker
Definition: OperatorBase.h:541
std::string getXMLAttributeValue(const xmlNodePtr cur, const std::string_view name)
get the value string for attribute name if name is unfound in cur you get an empty string back this i...
Buffer_t Collectables
observables in addition to those registered in Properties/PropertyList
Definition: ParticleSet.h:126
SpeciesSet & getSpeciesSet()
retrun the SpeciesSet of this particle set
Definition: ParticleSet.h:231
void registerCollectables(std::vector< ObservableHelper > &h5desc, hdf_archive &file) const override
std::vector< std::string > speciesName
Species name list.
Definition: SpeciesSet.h:44
bool putContent(T &a, xmlNodePtr cur)
replaces a&#39;s value with the first "element" in the "string" returned by XMLNodeString{cur}.
Definition: libxmldefs.h:88
FullPrecRealType Return_t
type of return value of evaluate
Definition: OperatorBase.h:64
MakeReturn< UnaryNode< FnSqrt, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t > >::Expression_t sqrt(const Vector< T1, C1 > &l)
Class to represent a many-body trial wave function.
void set_dimensions(const std::vector< int > &dims, int first)
set the shape of this observable
void test(int moves, ParticleSet &P)
FullPrecRealType Weight
Weight of the walker.
Definition: Walker.h:102
const auto & getLattice() const
Definition: ParticleSet.h:251
Tensor< typename BinaryReturn< T1, T2, OpMultiply >::Type_t, D > dot(const AntiSymTensor< T1, D > &lhs, const AntiSymTensor< T2, D > &rhs)
std::vector< std::string > species_name
Definition: SpinDensity.h:34
Custom container for set of attributes for a set of species.
Definition: SpeciesSet.h:33
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.
std::bitset< 8 > update_mode_
set the current update mode
Definition: OperatorBase.h:521
ObservableHelper oh
MakeReturn< UnaryNode< FnFloor, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t > >::Expression_t floor(const Vector< T1, C1 > &l)
TinyVector< int, DIM > grid
Definition: SpinDensity.h:37