QMCPACK
LCAOSpinorBuilder.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) 2020 QMCPACK developers.
6 //
7 // File developed by: Cody A. Melton, cmelton@sandia.gov, Sandia National Laboratories
8 //
9 // File created by: Cody A. Melton, cmelton@sandia.gov, Sandia National Laboratories
10 //////////////////////////////////////////////////////////////////////////////////////
11 
12 #include "LCAOSpinorBuilder.h"
14 #include "OhmmsData/AttributeSet.h"
16 #include "hdf/hdf_archive.h"
17 #include "Message/CommOperators.h"
18 
19 namespace qmcplusplus
20 {
22  : LCAOrbitalBuilder(els, ions, comm, cur)
23 {
24  ClassName = "LCAOSpinorBuilder";
25 
26  if (h5_path == "")
27  myComm->barrier_and_abort("LCAOSpinorBuilder only works with href");
28 }
29 
30 std::unique_ptr<SPOSet> LCAOSpinorBuilder::createSPOSetFromXML(xmlNodePtr cur)
31 {
32  ReportEngine PRE(ClassName, "createSPO(xmlNodePtr)");
33  std::string spo_name(""), optimize("no");
34  std::string basisset_name("LCAOBSet");
35  size_t norbs(0);
36  OhmmsAttributeSet spoAttrib;
37  spoAttrib.add(spo_name, "name");
38  spoAttrib.add(optimize, "optimize");
39  spoAttrib.add(basisset_name, "basisset");
40  spoAttrib.add(norbs, "size");
41  spoAttrib.put(cur);
42 
43  BasisSet_t* myBasisSet = nullptr;
44  if (basisset_map_.find(basisset_name) == basisset_map_.end())
45  myComm->barrier_and_abort("basisset \"" + basisset_name + "\" cannot be found\n");
46  else
47  myBasisSet = basisset_map_[basisset_name].get();
48 
49  if (optimize == "yes")
50  app_log() << " SPOSet " << spo_name << " is optimizable\n";
51 
52  std::unique_ptr<LCAOrbitalSet> upspo =
53  std::make_unique<LCAOrbitalSet>(spo_name + "_up", std::unique_ptr<BasisSet_t>(myBasisSet->makeClone()), norbs,
54  false, false);
55  std::unique_ptr<LCAOrbitalSet> dnspo =
56  std::make_unique<LCAOrbitalSet>(spo_name + "_dn", std::unique_ptr<BasisSet_t>(myBasisSet->makeClone()), norbs,
57  false, false);
58  loadMO(*upspo, *dnspo, cur);
59 
60  //create spinor and register up/dn
61  auto spinor_set = std::make_unique<SpinorSet>(spo_name);
62  spinor_set->set_spos(std::move(upspo), std::move(dnspo));
63  return spinor_set;
64 }
65 
67 {
68  bool PBC = false;
69  std::string debugc("no");
70  OhmmsAttributeSet aAttrib;
71  aAttrib.add(debugc, "debug");
72  aAttrib.put(cur);
73 
74  xmlNodePtr occ_ptr = nullptr;
75  cur = cur->xmlChildrenNode;
76  while (cur != nullptr)
77  {
78  std::string cname((const char*)(cur->name));
79  if (cname == "occupation")
80  {
81  occ_ptr = cur;
82  }
83  cur = cur->next;
84  }
85 
86  hdf_archive hin(myComm);
87  if (myComm->rank() == 0)
88  {
89  if (!hin.open(h5_path, H5F_ACC_RDONLY))
90  myComm->barrier_and_abort("LCAOSpinorBuilder::loadMO missing or incorrect path to H5 file.");
91  hin.push("PBC");
92  PBC = false;
93  hin.read(PBC, "PBC");
94  hin.close();
95  }
96  myComm->bcast(PBC);
97  if (PBC)
98  myComm->barrier_and_abort("LCAOSpinorBuilder::loadMO lcao spinors not implemented in PBC");
99 
100  bool success = putFromH5(up, dn, occ_ptr);
101 
102 
103  if (debugc == "yes")
104  {
105  app_log() << "UP: Single-particle orbital coefficients dims=" << up.C->rows() << " x " << up.C->cols()
106  << std::endl;
107  app_log() << *up.C << std::endl;
108  app_log() << "DN: Single-particle orbital coefficients dims=" << dn.C->rows() << " x " << dn.C->cols()
109  << std::endl;
110  app_log() << *dn.C << std::endl;
111  }
112  return success;
113 }
114 
115 bool LCAOSpinorBuilder::putFromH5(LCAOrbitalSet& up, LCAOrbitalSet& dn, xmlNodePtr occ_ptr)
116 {
117 #ifdef QMC_COMPLEX
118  if (up.getBasisSetSize() == 0 || dn.getBasisSetSize() == 0)
119  {
120  myComm->barrier_and_abort("LCASpinorBuilder::loadMO detected ZERO BasisSetSize");
121  return false;
122  }
123 
124  bool success = true;
125  hdf_archive hin(myComm);
126  if (myComm->rank() == 0)
127  {
128  if (!hin.open(h5_path, H5F_ACC_RDONLY))
129  myComm->barrier_and_abort("LCAOSpinorBuilder::putFromH5 missing or incorrect path to H5 file");
130 
131  Matrix<RealType> upReal;
132  Matrix<RealType> upImag;
133  std::string setname = "/Super_Twist/eigenset_0";
134  readRealMatrixFromH5(hin, setname, upReal);
135  setname += "_imag";
136  readRealMatrixFromH5(hin, setname, upImag);
137 
138  assert(upReal.rows() == upImag.rows());
139  assert(upReal.cols() == upImag.cols());
140 
141  Matrix<ValueType> upTemp(upReal.rows(), upReal.cols());
142  for (int i = 0; i < upTemp.rows(); i++)
143  {
144  for (int j = 0; j < upTemp.cols(); j++)
145  {
146  upTemp[i][j] = ValueType(upReal[i][j], upImag[i][j]);
147  }
148  }
149 
150  Matrix<RealType> dnReal;
151  Matrix<RealType> dnImag;
152  setname = "/Super_Twist/eigenset_1";
153  readRealMatrixFromH5(hin, setname, dnReal);
154  setname += "_imag";
155  readRealMatrixFromH5(hin, setname, dnImag);
156 
157  assert(dnReal.rows() == dnImag.rows());
158  assert(dnReal.cols() == dnImag.cols());
159 
160  Matrix<ValueType> dnTemp(dnReal.rows(), dnReal.cols());
161  for (int i = 0; i < dnTemp.rows(); i++)
162  {
163  for (int j = 0; j < dnTemp.cols(); j++)
164  {
165  dnTemp[i][j] = ValueType(dnReal[i][j], dnImag[i][j]);
166  }
167  }
168 
169  assert(upReal.rows() == dnReal.rows());
170  assert(upReal.cols() == dnReal.cols());
171 
172  Occ.resize(upReal.rows());
173  success = putOccupation(up, occ_ptr);
174 
175  int norbs = up.getOrbitalSetSize();
176 
177  int n = 0, i = 0;
178  while (i < norbs)
179  {
180  if (Occ[n] > 0.0)
181  {
182  std::copy(upTemp[n], upTemp[n + 1], (*up.C)[i]);
183  std::copy(dnTemp[n], dnTemp[n + 1], (*dn.C)[i]);
184  i++;
185  }
186  n++;
187  }
188 
189  hin.close();
190  }
191 
192 #ifdef HAVE_MPI
193  myComm->comm.broadcast_n(up.C->data(), up.C->size());
194  myComm->comm.broadcast_n(dn.C->data(), dn.C->size());
195 #endif
196 
197 #else
198  myComm->barrier_and_abort("LCAOSpinorBuilder::putFromH5 Must build with QMC_COMPLEX");
199 #endif
200 
201  return success;
202 }
203 
204 } // namespace qmcplusplus
void resize(size_type n, Type_t val=Type_t())
Resize the container.
Definition: OhmmsVector.h:166
bool open(const std::filesystem::path &fname, unsigned flags=H5F_ACC_RDWR)
open a file
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
int rank() const
return the rank
Definition: Communicate.h:116
int getBasisSetSize() const
return the size of the basis set
Definition: LCAOrbitalSet.h:78
std::ostream & app_log()
Definition: OutputManager.h:65
bool put(xmlNodePtr cur)
assign attributes to the set
Definition: AttributeSet.h:55
void close()
close all the open groups and file
Definition: hdf_archive.cpp:38
Vector< RealType > Occ
occupation number
LCAOSpinorBuilder(ParticleSet &els, ParticleSet &ions, Communicate *comm, xmlNodePtr cur)
constructor
SPOSetBuilder using new LCAOrbitalSet and Soa versions.
class to handle hdf file
Definition: hdf_archive.h:51
std::unique_ptr< SPOSet > createSPOSetFromXML(xmlNodePtr cur) override
creates and returns SpinorSet
size_type cols() const
Definition: OhmmsMatrix.h:78
void copy(const Array< T1, 3 > &src, Array< T2, 3 > &dest)
Definition: Blitz.h:639
Wrapping information on parallelism.
Definition: Communicate.h:68
Specialized paritlce class for atomistic simulations.
Definition: ParticleSet.h:55
std::map< std::string, std::unique_ptr< BasisSet_t > > basisset_map_
localized basis set map
QTBase::ValueType ValueType
Definition: Configuration.h:60
Final class and should not be derived.
Communicate * myComm
pointer to Communicate
Definition: MPIObjectBase.h:62
class to handle a set of attributes of an xmlNode
Definition: AttributeSet.h:24
declaration of ProgressReportEngine
std::shared_ptr< OffloadValueMatrix > C
pointer to matrix containing the coefficients
Definition: LCAOrbitalSet.h:42
int getOrbitalSetSize() const
return the size of the orbitals
Definition: SPOSet.h:88
std::string ClassName
class Name
Definition: MPIObjectBase.h:65
bool putOccupation(LCAOrbitalSet &spo, xmlNodePtr occ_ptr)
size_type rows() const
Definition: OhmmsMatrix.h:77
std::string h5_path
Path to HDF5 Wavefunction.
bool loadMO(LCAOrbitalSet &up, LCAOrbitalSet &dn, xmlNodePtr cur)
load the up and down MO sets
void push(const std::string &gname, bool createit=true)
push a group to the group stack
void readRealMatrixFromH5(hdf_archive &hin, const std::string &setname, Matrix< LCAOrbitalBuilder::RealType > &Creal) const
read matrix from h5 file
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
virtual SoaBasisSetBase< T > * makeClone() const =0
bool putFromH5(LCAOrbitalSet &up, LCAOrbitalSet &dn, xmlNodePtr)
parse h5 file for spinor info
void bcast(T &)
class to handle linear combinations of basis orbitals used to evaluate the Dirac determinants.
Definition: LCAOrbitalSet.h:30
void barrier_and_abort(const std::string &msg) const
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
Base for real basis set.
Definition: BasisSetBase.h:132