QMCPACK
LCAOHDFParser.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: Anouar Benali, benali@anl.gov, Argonne National Laboratory
8 //
9 // File created by: Anouar Benali, benali@anl.gov, Argonne National Laboratory
10 //////////////////////////////////////////////////////////////////////////////////////
11 
12 
13 #include "LCAOHDFParser.h"
14 #include <algorithm>
15 #include <fstream>
16 #include <iterator>
17 #include <algorithm>
18 #include <set>
19 #include <map>
20 #include <sstream>
21 
22 
24 {
25  basisName = "Gaussian";
26  Normalized = "no";
27  BohrUnit = true;
28  MOtype = "Canonical";
29  angular_type = "cartesian";
30  readtype = 0;
31  NFZC = 0;
32 }
33 
34 LCAOHDFParser::LCAOHDFParser(int argc, char** argv) : QMCGaussianParserBase(argc, argv)
35 {
36  basisName = "Gaussian";
37  Normalized = "no";
38  BohrUnit = true;
39  MOtype = "Canonical";
40  angular_type = "cartesian";
41  SpinRestricted = true;
42  readtype = 0;
43  NFZC = 0;
44 }
45 
46 void LCAOHDFParser::parse(const std::string& fname)
47 {
48  hdf_archive hin;
49 
50  if (!hin.open(fname.c_str(), H5F_ACC_RDONLY))
51  {
52  std::cerr << "Could not open H5 file" << std::endl;
53  abort();
54  }
55 
56  hin.push("application");
57  hin.read(CodeName, "code");
58  hin.pop();
59  std::cout << "Converting Wavefunction from the " << CodeName << " Code" << std::endl;
60 
61  hin.push("PBC");
62  hin.read(PBC, "PBC");
63  hin.pop();
64 
65  std::cout << "Periodic Boundary Conditions: " << (PBC ? ("yes") : ("no")) << std::endl;
66  std::cout.flush();
67 
68 
69  hin.push("parameters");
70  hin.read(ECP, "ECP");
71  std::cout << "usingECP: " << (ECP ? ("yes") : ("no")) << std::endl;
72  std::cout.flush();
73 
74  std::cout << "Multideterminants: " << (multideterminant ? ("yes") : ("no")) << std::endl;
75  std::cout.flush();
76 
77  hin.read(SpinRestricted, "SpinRestricted");
78  hin.read(numAO, "numAO");
79  hin.read(numMO, "numMO");
80 
81 
82  std::cout << "NUMBER OF AOs: " << numAO << std::endl;
84  std::cout << "Size of Basis Set: " << SizeOfBasisSet << std::endl;
85  std::cout << "NUMBER OF MOs: " << numMO << std::endl;
86 
87  bool bohr = true;
88  hin.read(bohr, "Unit");
89  std::cout << "Unit in Bohr=" << bohr << std::endl;
90  BohrUnit = bohr;
91 
92  hin.read(NumberOfAlpha, "NbAlpha");
93  hin.read(NumberOfBeta, "NbBeta");
94  hin.read(NumberOfEls, "NbTotElec");
95  int ds;
96  hin.read(ds, "spin");
97  if (CodeName == "PySCF")
98  SpinMultiplicity = ds + 1;
99  else
100  SpinMultiplicity = ds;
101 
102  std::cout << "Number of alpha electrons: " << NumberOfAlpha << std::endl;
103  std::cout << "Number of beta electrons: " << NumberOfBeta << std::endl;
104  std::cout << "Number of electrons: " << NumberOfEls << std::endl;
105  std::cout << "SPIN MULTIPLICITY: " << SpinMultiplicity << std::endl;
106 
107  hin.pop();
108  hin.push("atoms");
109  hin.read(NumberOfAtoms, "number_of_atoms");
110  std::cout << "NUMBER OF ATOMS: " << NumberOfAtoms << std::endl;
111  hin.pop();
112 
113  EigVal_alpha.resize(numMO);
114  EigVal_beta.resize(numMO);
115  Matrix<double> myvec(1, numMO);
116 
117  hin.push("Super_Twist");
118  if (hin.readEntry(myvec, "eigenval_0"))
119  {
120  // eigenval_0 exists on file
121  for (int i = 0; i < numMO; i++)
122  EigVal_alpha[i] = myvec[0][i];
123 
124  //Reading Eigenvals for Spin unRestricted calculation. This section is needed to set the occupation numbers
125  if (!SpinRestricted)
126  {
127  hin.read(myvec, "eigenval_1");
128  for (int i = 0; i < numMO; i++)
129  EigVal_beta[i] = myvec[0][i];
130  }
131  }
132  else
133  {
134  app_warning() << "eigenval_0 doesn't exist in h5 file. Treat all values zero." << std::endl;
135  }
136  hin.close();
137 
138 
140  GroupName.resize(NumberOfAtoms);
141  if (PBC)
142  {
143  getCell(fname);
144  getSuperTwist(fname);
145  if (debug)
146  {
147  getGaussianCenters(fname);
148  getMO(fname);
149  }
150  }
151  getGeometry(fname);
152 
153  if (multideterminant)
154  {
155  hdf_archive hin;
156 
157  if (!hin.open(outputFile.c_str(), H5F_ACC_RDONLY))
158  {
159  std::cerr << "Could not open H5 file" << std::endl;
160  abort();
161  }
162 
163  hin.push("MultiDet");
164 
165  hin.read(ci_size, "NbDet");
166  hin.read(ci_nstates, "nstate");
167  hin.read(nbexcitedstates, "nexcitedstate");
168  CIcoeff.clear();
169  CIalpha.clear();
170  CIbeta.clear();
171  CIcoeff.resize(ci_size);
172  CIalpha.resize(ci_size);
173  CIbeta.resize(ci_size);
174 
175  int ds = SpinMultiplicity - 1;
176  int neb = (NumberOfEls - ds) / 2;
177  int nea = NumberOfEls - NumberOfBeta;
180  ci_nca = nea - ci_nea;
181  ci_ncb = neb - ci_neb;
182  std::cout << " Done reading CIs!!" << std::endl;
183  hin.close();
184  }
185 }
186 
187 void LCAOHDFParser::getCell(const std::string& fname)
188 {
189  X.resize(3);
190  Y.resize(3);
191  Z.resize(3);
192 
193  hdf_archive hin;
194 
195  if (!hin.open(fname.c_str(), H5F_ACC_RDONLY))
196  {
197  std::cerr << "Could not open H5 file" << std::endl;
198  abort();
199  }
200  hin.push("Cell");
201  Matrix<double> LatticeVec(3, 3);
202  hin.read(LatticeVec, "LatticeVectors");
203  X[0] = LatticeVec[0][0];
204  X[1] = LatticeVec[0][1];
205  X[2] = LatticeVec[0][2];
206  Y[0] = LatticeVec[1][0];
207  Y[1] = LatticeVec[1][1];
208  Y[2] = LatticeVec[1][2];
209  Z[0] = LatticeVec[2][0];
210  Z[1] = LatticeVec[2][1];
211  Z[2] = LatticeVec[2][2];
212  hin.close();
213  std::cout << "Lattice parameters in Bohr:" << std::endl;
214  std::cout << X[0] << " " << X[1] << " " << X[2] << std::endl;
215  std::cout << Y[0] << " " << Y[1] << " " << Y[2] << std::endl;
216  std::cout << Z[0] << " " << Z[1] << " " << Z[2] << std::endl;
217 }
218 void LCAOHDFParser::getGeometry(const std::string& fname)
219 {
220  hdf_archive hin;
221 
222  if (!hin.open(fname.c_str(), H5F_ACC_RDONLY))
223  {
224  std::cerr << "Could not open H5 file" << std::endl;
225  abort();
226  }
227  hin.push("atoms");
228 
229  //atomic numbers
230  std::vector<int> atomic_number;
231  std::vector<double> q, pos;
232  tags.clear();
233  //read atomic info
234  //MAP: (Atom Number, Species Index)
235  std::vector<int> idx(NumberOfAtoms);
236  std::map<int, int> AtomIndexmap;
237  hin.read(idx, "species_ids");
238  for (int i = 0; i < NumberOfAtoms; i++)
239  {
240  AtomIndexmap.insert(std::pair<int, int>(i, idx[i]));
241  }
242  for (int i = 0; i < NumberOfAtoms; i++)
243  {
244  std::string speciesName("species_");
245  speciesName = speciesName + std::to_string(AtomIndexmap[i]);
246  hin.push(speciesName);
247  int zint;
248  double z;
249  std::string Name;
250  hin.read(zint, "atomic_number");
251  atomic_number.push_back(zint);
252  hin.read(z, "charge");
253  q.push_back(z);
254  hin.read(Name, "name");
255  tags.push_back(Name);
256  hin.pop();
257  }
258 
259  Matrix<double> IonPos(NumberOfAtoms, 3);
260  hin.read(IonPos, "positions");
261 
262  SpeciesSet& species(IonSystem.getSpeciesSet());
263  for (int i = 0; i < NumberOfAtoms; i++)
264  {
265  IonSystem.R[i][0] = IonPos[i][0];
266  IonSystem.R[i][1] = IonPos[i][1];
267  IonSystem.R[i][2] = IonPos[i][2];
268  GroupName[i] = IonName[atomic_number[i]];
269  int speciesID = species.addSpecies(GroupName[i]);
270  IonSystem.GroupID[i] = speciesID;
271  species(AtomicNumberIndex, speciesID) = atomic_number[i];
272  species(IonChargeIndex, speciesID) = q[i];
273  }
274  hin.close();
275 }
276 
277 void LCAOHDFParser::getSuperTwist(const std::string& fname)
278 {
279  Matrix<double> MyVec(1, 3);
280  hdf_archive hin;
281 
282  if (!hin.open(fname.c_str(), H5F_ACC_RDONLY))
283  {
284  std::cerr << "Could not open H5 file" << std::endl;
285  abort();
286  }
287 
288  hin.push("Super_Twist");
289 
290  STwist_Coord.resize(3);
291 
292  hin.read(MyVec, "Coord");
293 
294  hin.pop();
295  STwist_Coord[0] = MyVec[0][0];
296  STwist_Coord[1] = MyVec[0][1];
297  STwist_Coord[2] = MyVec[0][2];
298 
299  hin.close();
300 }
301 
302 void LCAOHDFParser::getMO(const std::string& fname)
303 {
304  EigVal_alpha.resize(numMO);
305  EigVal_beta.resize(numMO);
306  EigVec.resize(2 * SizeOfBasisSet * numMO);
307 
309 
310  hdf_archive hin;
311 
312  if (!hin.open(fname.c_str(), H5F_ACC_RDONLY))
313  {
314  std::cerr << "Could not open H5 file" << std::endl;
315  abort();
316  }
317 
318  std::string setname = "/Super_Twist/eigenset_0";
319  hin.read(CartMat, setname);
320  setname = "/Super_Twist/eigenval_0";
321  hin.read(EigVal_alpha, setname);
322  std::copy(CartMat.begin(), CartMat.end(), EigVec.begin());
323 
324  if (!SpinRestricted)
325  {
326  setname = "/Super_Twist/eigenset_1";
327  hin.read(CartMat, setname);
328  setname = "/Super_Twist/eigenval_1";
329  hin.read(EigVal_beta, setname);
330  }
331  std::copy(CartMat.begin(), CartMat.end(), EigVec.begin() + SizeOfBasisSet * numMO);
332 
333  hin.close();
334  int btot = numMO * SizeOfBasisSet;
335  int n = btot / 4, b = 0;
336  int dn = btot - n * 4;
337  std::ostringstream eig;
338 
339  eig.setf(std::ios::scientific, std::ios::floatfield);
340  eig.setf(std::ios::right, std::ios::adjustfield);
341  eig.precision(14);
342  eig << "\n";
343  for (int k = 0; k < n; k++)
344  {
345  eig << std::setw(22) << EigVec[b] << std::setw(22) << EigVec[b + 1] << std::setw(22) << EigVec[b + 2]
346  << std::setw(22) << EigVec[b + 3] << "\n";
347  b += 4;
348  }
349  for (int k = 0; k < dn; k++)
350  {
351  eig << std::setw(22) << EigVec[b++];
352  }
353  std::cout << eig.str() << std::endl;
354  std::cout << "Finished reading MO." << std::endl;
355  hin.close();
356 }
357 
358 void LCAOHDFParser::getGaussianCenters(const std::string fname) {}
Container_t::iterator begin()
Definition: OhmmsMatrix.h:89
std::vector< value_type > EigVal_beta
std::string MOtype
Definition: LCAOHDFParser.h:33
std::ostream & app_warning()
Definition: OutputManager.h:69
bool open(const std::filesystem::path &fname, unsigned flags=H5F_ACC_RDWR)
open a file
void getSuperTwist(const std::string &fname)
std::vector< double > Z
std::vector< double > X
void close()
close all the open groups and file
Definition: hdf_archive.cpp:38
std::vector< double > CIcoeff
std::vector< std::string > tags
Definition: LCAOHDFParser.h:32
class to handle hdf file
Definition: hdf_archive.h:51
std::vector< std::string > GroupName
ParticleIndex GroupID
Species ID.
Definition: ParticleSet.h:77
void copy(const Array< T1, 3 > &src, Array< T2, 3 > &dest)
Definition: Blitz.h:639
std::vector< value_type > EigVal_alpha
std::vector< double > STwist_Coord
std::vector< value_type > EigVec
ParticlePos R
Position.
Definition: ParticleSet.h:79
static std::map< int, std::string > IonName
std::vector< double > Y
void getMO(const std::string &fname)
SpeciesSet & getSpeciesSet()
retrun the SpeciesSet of this particle set
Definition: ParticleSet.h:231
std::vector< std::string > CIalpha
void create(const std::vector< int > &agroup)
create grouped particles
void parse(const std::string &fname) override
void push(const std::string &gname, bool createit=true)
push a group to the group stack
void getCell(const std::string &fname)
void read(T &data, const std::string &aname)
read the data from the group aname and check status runtime error is issued on I/O error ...
Definition: hdf_archive.h:306
void getGaussianCenters(const std::string fname)
Custom container for set of attributes for a set of species.
Definition: SpeciesSet.h:33
void getGeometry(const std::string &fname)
bool readEntry(T &data, const std::string &aname)
read the data from the group aname and return status use read() for inbuilt error checking ...
Definition: hdf_archive.h:293
std::vector< std::string > CIbeta
Container_t::iterator end()
Definition: OhmmsMatrix.h:90