QMCPACK
SkAllEstimator.cpp
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////
2 // (c) Copyright 2008- by Jeongnim Kim
3 //////////////////////////////////////////////////////////////////
4 //////////////////////////////////////////////////////////////////
5 // National Center for Supercomputing Applications &
6 // Materials Computation Center
7 // University of Illinois, Urbana-Champaign
8 // Urbana, IL 61801
9 // e-mail: jnkim@ncsa.uiuc.edu
10 //
11 // Supported by
12 // National Center for Supercomputing Applications, UIUC
13 // Materials Computation Center, UIUC
14 //////////////////////////////////////////////////////////////////
15 // -*- C++ -*-
16 #include "SkAllEstimator.h"
17 #include "LongRange/StructFact.h"
19 #include "OhmmsData/AttributeSet.h"
20 #include "Configuration.h"
21 #include <fstream>
22 #include <sstream>
23 #include <string>
24 namespace qmcplusplus
25 {
27 {
28  elns = &target;
29  ions = &source;
32  update_mode_.set(COLLECTABLE, 1);
33 
34  NumK = source.getSimulationCell().getKLists().numk;
35  OneOverN = 1.0 / static_cast<RealType>(source.getTotalNum());
36  Kshell = source.getSimulationCell().getKLists().kshell;
37  MaxKshell = Kshell.size() - 1;
38 
41 
42  //for values, we are including e-e structure factor, and e-Ion. So a total of NumIonSpecies+1 structure factors.
43  //+2 for the real and imaginary parts of rho_k^e
44  values.resize(3 * NumK);
45  Kmag.resize(MaxKshell);
46  OneOverDnk.resize(MaxKshell);
47  for (int ks = 0; ks < MaxKshell; ks++)
48  {
49  Kmag[ks] = std::sqrt(source.getSimulationCell().getKLists().ksq[Kshell[ks]]);
50  OneOverDnk[ks] = 1.0 / static_cast<RealType>(Kshell[ks + 1] - Kshell[ks]);
51  }
52  hdf5_out = false;
53 }
54 
56 
57 
59 {
60  std::stringstream ss;
61  ss << " kx ky kz";
62 
63  std::ofstream skfile;
64  std::stringstream filebuffer;
65  skfile.open("rhok_IonIon.dat");
66  for (int i = 0; i < NumIonSpecies; i++)
67  ss << " rho_" << i << "_r"
68  << " rho_" << i << "_i";
69 
70  filebuffer << ss.str() << std::endl;
71 
72  for (int k = 0; k < NumK; k++)
73  {
74  PosType kvec = ions->getSimulationCell().getKLists().kpts_cart[k];
75 
76  filebuffer << kvec;
77  for (int i = 0; i < NumIonSpecies; i++)
78  {
79  double rho_i(0.0);
80  double rho_r(0.0);
81 
82  rho_r = ions->getSK().rhok_r[i][k];
83  rho_i = ions->getSK().rhok_i[i][k];
84  filebuffer << " " << rho_r << " " << rho_i;
85  }
86 
87  filebuffer << std::endl;
88  }
89 
90  skfile << filebuffer.str();
91 
92  skfile.close();
93 }
94 
95 
97 {
98  // Segfaults unless setHistories() is called prior
100  //sum over species
101  std::copy(P.getSK().rhok_r[0], P.getSK().rhok_r[0] + NumK, RhokTot_r.begin());
102  std::copy(P.getSK().rhok_i[0], P.getSK().rhok_i[0] + NumK, RhokTot_i.begin());
103  for (int i = 1; i < NumeSpecies; ++i)
105  for (int i = 1; i < NumeSpecies; ++i)
107 
108  for (int k = 0; k < NumK; k++)
109  values[k] = w * (RhokTot_r[k] * RhokTot_r[k] + RhokTot_i[k] * RhokTot_i[k]);
110 
111  // for (int ionSpec=0; ionSpec<NumIonSpecies; ionSpec++)
112  // {
113  // for(int k=0; k<NumK; k++)
114  // {
115  // RealType rhok_A_r(ions->getSK().rhok_r[ionSpec][k]), rhok_A_i(ions->getSK().rhok_i[ionSpec][k]);
116  // values[(ionSpec+1)*NumK+k]=RhokTot_r[k]*rhok_A_r+RhokTot_i[k]*rhok_A_i;
117  // }
118  // }
119 
120  if (hdf5_out)
121  {
122  int kc = my_index_;
123  for (int k = 0; k < NumK; k++, kc++)
124  P.Collectables[kc] += values[k];
125  for (int k = 0; k < NumK; k++, kc++)
126  P.Collectables[kc] += w * RhokTot_r[k];
127  for (int k = 0; k < NumK; k++, kc++)
128  P.Collectables[kc] += w * RhokTot_i[k];
129  }
130  else
131  {
132  for (int k = 0; k < NumK; k++)
133  {
134  values[NumK + 2 * k] = w * RhokTot_r[k];
135  values[NumK + 2 * k + 1] = w * RhokTot_i[k];
136  }
137  }
138  return 0.0;
139 }
140 
142 {
143  if (hdf5_out)
144  {
145  my_index_ = collectables.size();
146  std::vector<RealType> tmp(3 * NumK); // space for e-e modulus, e real, e imag
147  collectables.add(tmp.begin(), tmp.end());
148  }
149  else
150  {
151  my_index_ = plist.size();
152  //First the electron structure factor
153  for (int i = 0; i < NumK; i++)
154  {
155  std::stringstream sstr;
156  sstr << "rhok_e_e_" << i;
157  int id = plist.add(sstr.str());
158  }
159  //Now the e-Ion structure factors. IonIon are dumped to file, and not evaluated.
160  // for (int ionSpec=0; ionSpec<NumIonSpecies; ionSpec++)
161  // {
162  // for (int i=0; i<NumK; i++)
163  // {
164  // std::stringstream sstr;
165  // sstr << "rhok_e_" <<ionSpec<<"_"<<i;
166  // int id=plist.add(sstr.str());
167  // }
168  // }
169 
170  for (int k = 0; k < NumK; k++)
171  {
172  std::stringstream sstr1, sstr2;
173  sstr1 << "rhok_e_r_" << k;
174  sstr2 << "rhok_e_i_" << k;
175  int id = plist.add(sstr1.str());
176  int id2 = plist.add(sstr2.str());
177  }
178  }
179 }
180 
182 {
183  my_index_ = plist.size();
184  //First the electron structure factor
185  for (int i = 0; i < NumK; i++)
186  {
187  std::stringstream sstr;
188  sstr << "rhok_e_e_" << i;
189  int id = plist.add(sstr.str());
190  }
191  //Now the e-Ion structure factors. IonIon are dumped to file, and not evaluated.
192  // for (int ionSpec=0; ionSpec<NumIonSpecies; ionSpec++)
193  // {
194  // for (int i=0; i<NumK; i++)
195  // {
196  // std::stringstream sstr;
197  // sstr << "rhok_e_" <<ionSpec<<"_"<<i;
198  // int id=plist.add(sstr.str());
199  // }
200  // }
201  for (int k = 0; k < NumK; k++)
202  {
203  std::stringstream sstr1, sstr2;
204  sstr1 << "rhok_e_r_" << k;
205  sstr2 << "rhok_e_i_" << k;
206  int id = plist.add(sstr1.str());
207  int id2 = plist.add(sstr2.str());
208  }
209 }
210 
212 {
213  if (!hdf5_out)
214  std::copy(values.begin(), values.end(), plist.begin() + my_index_);
215 }
216 
218 {
219  if (!hdf5_out)
220  std::copy(values.begin(), values.end(), plist.begin() + my_index_ + offset);
221 }
222 
223 
224 void SkAllEstimator::registerCollectables(std::vector<ObservableHelper>& h5desc, hdf_archive& file) const
225 {
226  if (!hdf5_out)
227  return;
228 
229  // Add k-point information
230  hdf_path hdf_name{name_};
231  h5desc.emplace_back(hdf_name / "kpoints");
232  auto& ohKPoints = h5desc.back();
233  ohKPoints.addProperty(const_cast<std::vector<PosType>&>(ions->getSimulationCell().getKLists().kpts_cart), "value",
234  file);
235 
236  // Add electron-electron S(k)
237  std::vector<int> ng(1);
238  ng[0] = NumK;
239  // modulus
240  h5desc.emplace_back(hdf_name / "rhok_e_e");
241  auto& ohRhoKEE = h5desc.back();
242  ohRhoKEE.set_dimensions(ng, my_index_);
243  // real part
244  h5desc.emplace_back(hdf_name / "rhok_e_r");
245  auto& ohRhoKER = h5desc.back();
246  ohRhoKER.set_dimensions(ng, my_index_ + NumK);
247  // imaginary part
248  h5desc.emplace_back(hdf_name / "rhok_e_i");
249  auto& ohRhoKEI = h5desc.back();
250  ohRhoKEI.set_dimensions(ng, my_index_ + 2 * NumK);
251 }
252 
253 bool SkAllEstimator::put(xmlNodePtr cur)
254 {
255  OhmmsAttributeSet pAttrib;
256  std::string hdf5_flag = "no";
257  std::string write_ionion_flag = "no";
258 
259  pAttrib.add(hdf5_flag, "hdf5");
260  pAttrib.add(write_ionion_flag, "writeionion");
261  pAttrib.put(cur);
262  if (hdf5_flag == "yes")
263  hdf5_out = true;
264  else
265  hdf5_out = false;
266  app_log() << "hdf5_out= " << hdf5_out << "\n";
267 
268  if (write_ionion_flag == "Yes" || write_ionion_flag == "yes")
269  {
270  app_log() << "SkAll: evaluateIonIon()\n";
271  evaluateIonIon();
272  }
273  return true;
274 }
275 
276 
277 // Display some stuff
278 bool SkAllEstimator::get(std::ostream& os) const
279 {
280  app_log() << std::fixed;
281  app_log() << "\n";
282  app_log() << " SkAllEstimator Settings:\n";
283  app_log() << " ========================\n";
284  app_log() << " NumeSpecies " << std::setw(10) << std::setprecision(4) << NumeSpecies << "\n";
285  app_log() << " NumIonSpecies " << std::setw(10) << std::setprecision(4) << NumIonSpecies << "\n";
286  app_log() << " NumK " << std::setw(10) << std::setprecision(4) << NumK << "\n";
287  app_log() << " MaxKshell " << std::setw(10) << std::setprecision(4) << MaxKshell << "\n";
288 
289  return true;
290 }
291 
292 std::unique_ptr<OperatorBase> SkAllEstimator::makeClone(ParticleSet& qp, TrialWaveFunction& psi)
293 {
294  std::unique_ptr<SkAllEstimator> myclone = std::make_unique<SkAllEstimator>(*this);
295  myclone->hdf5_out = hdf5_out;
296  myclone->my_index_ = my_index_;
297  return myclone;
298 }
299 } // namespace qmcplusplus
std::vector< RealType > Kmag
instantaneous structure factor
void resize(size_type n, Type_t val=Type_t())
Resize the container.
Definition: OhmmsVector.h:166
void setParticlePropertyList(PropertySetType &plist, int offset) override
void resetTargetParticleSet(ParticleSet &P) override
Reset the data with the target ParticleSet.
size_type size() const
return the size of the data
Definition: PooledData.h:48
Matrix< RealType > rhok_r
2-D container for the phase
Definition: StructFact.h:51
Return_t evaluate(ParticleSet &P) override
Evaluate the local energy contribution of this component.
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
size_t getTotalNum() const
Definition: ParticleSet.h:493
Matrix< RealType > rhok_i
Definition: StructFact.h:51
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
Declare SkAllEstimator.
std::vector< T >::iterator begin()
iterators to use std algorithms
std::vector< RealType > OneOverDnk
1.0/degenracy for a kshell
class to handle hdf file
Definition: hdf_archive.h:51
Vectorized record engine for scalar properties.
bool put(xmlNodePtr cur) override
Read the input parameter.
Vector< RealType > RhokTot_i
std::vector< int > Kshell
kshell counters
for(int i=0;i< size_test;++i) CHECK(Approx(gauss_random_vals[offset_for_rs+i])
void copy(const Array< T1, 3 > &src, Array< T2, 3 > &dest)
Definition: Blitz.h:639
int getTotalNum() const
return the number of species
Definition: SpeciesSet.h:55
std::unique_ptr< OperatorBase > makeClone(ParticleSet &qp, TrialWaveFunction &psi) final
const auto & getSimulationCell() const
Definition: ParticleSet.h:250
std::string name_
name of this object
Definition: OperatorBase.h:527
Specialized paritlce class for atomistic simulations.
Definition: ParticleSet.h:55
void addObservables(PropertySetType &plist)
void add(T &x)
Definition: PooledData.h:88
int add(const std::string &aname)
class to handle a set of attributes of an xmlNode
Definition: AttributeSet.h:24
Vector< RealType > values
const StructFact & getSK() const
return Structure Factor
Definition: ParticleSet.h:216
Walker_t * t_walker_
reference to the current walker
Definition: OperatorBase.h:541
Vector< RealType > RhokTot_r
for species index
bool get(std::ostream &os) const override
write about the class
Buffer_t Collectables
observables in addition to those registered in Properties/PropertyList
Definition: ParticleSet.h:126
unsigned int NumK
number of kpoints
SpeciesSet & getSpeciesSet()
retrun the SpeciesSet of this particle set
Definition: ParticleSet.h:231
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)
void setObservables(PropertySetType &plist) override
Set the values evaluated by this object to plist Default implementation is to assign Value which is u...
Class to represent a many-body trial wave function.
int MaxKshell
number of kshells
FullPrecRealType Weight
Weight of the walker.
Definition: Walker.h:102
void registerCollectables(std::vector< ObservableHelper > &h5desc, hdf_archive &file) const override
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
SkAllEstimator(ParticleSet &ions, ParticleSet &elns)
void accumulate_elements(IT1 first, IT1 last, IT2 res)
RealType OneOverN
normalization factor
std::bitset< 8 > update_mode_
set the current update mode
Definition: OperatorBase.h:521