QMCPACK
BsplineReader.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) 2023 QMCPACK developers.
6 //
7 // File developed by: Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
8 // Paul R. C. Kent, kentpr@ornl.gov, Oak Ridge National Laboratory
9 // Mark A. Berrill, berrillma@ornl.gov, Oak Ridge National Laboratory
10 // Ye Luo, yeluo@anl.gov, Argonne National Laboratory
11 //
12 // File created by: Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
13 //////////////////////////////////////////////////////////////////////////////////////
14 
15 
16 /** @file BsplineReader.cpp
17  *
18  * Implement super function
19  */
20 #include "EinsplineSetBuilder.h"
21 #include "BsplineReader.h"
22 #include "OhmmsData/AttributeSet.h"
23 #include "Message/CommOperators.h"
24 
25 #include <array>
26 #include <filesystem>
27 
28 namespace qmcplusplus
29 {
31  : mybuilder(e), checkNorm(true), saveSplineCoefs(false), rotate(true)
32 {
34 }
35 
37 
38 inline std::string make_bandinfo_filename(const std::string& root,
39  int spin,
40  int twist,
41  const Tensor<int, 3>& tilematrix,
42  int gid)
43 {
44  std::ostringstream oo;
45  oo << root << ".tile_" << tilematrix(0, 0) << tilematrix(0, 1) << tilematrix(0, 2) << tilematrix(1, 0)
46  << tilematrix(1, 1) << tilematrix(1, 2) << tilematrix(2, 0) << tilematrix(2, 1) << tilematrix(2, 2) << ".spin_"
47  << spin << ".tw_" << twist;
48  if (gid >= 0)
49  oo << ".g" << gid;
50  return oo.str();
51 }
52 
53 
54 inline std::string make_bandgroup_name(const std::string& root,
55  int spin,
56  int twist,
57  const Tensor<int, 3>& tilematrix,
58  int first,
59  int last)
60 {
61  std::ostringstream oo;
62  oo << root << ".tile_" << tilematrix(0, 0) << tilematrix(0, 1) << tilematrix(0, 2) << tilematrix(1, 0)
63  << tilematrix(1, 1) << tilematrix(1, 2) << tilematrix(2, 0) << tilematrix(2, 1) << tilematrix(2, 2) << ".spin_"
64  << spin << ".tw_" << twist << ".l" << first << "u" << last;
65  return oo.str();
66 }
67 
68 void BsplineReader::setCommon(xmlNodePtr cur)
69 {
70  // check orbital normalization by default
71  std::string checkOrbNorm("yes");
72  std::string saveCoefs("no");
74  a.add(checkOrbNorm, "check_orb_norm");
75  a.add(saveCoefs, "save_coefs");
76  a.put(cur);
77 
78  // allow user to turn off norm check with a warning
79  if (checkOrbNorm == "no")
80  {
81  app_log() << "WARNING: disable orbital normalization check!" << std::endl;
82  checkNorm = false;
83  }
84  saveSplineCoefs = saveCoefs == "yes";
85 }
86 
87 std::unique_ptr<SPOSet> BsplineReader::create_spline_set(int spin, xmlNodePtr cur)
88 {
89  int ns(0);
90  std::string spo_object_name;
92  a.add(ns, "size");
93  a.add(spo_object_name, "name");
94  a.add(spo_object_name, "id");
95  a.put(cur);
96 
97  if (ns == 0)
98  APP_ABORT_TRACE(__FILE__, __LINE__, "parameter/@size missing");
99 
100  if (spo2band.empty())
101  spo2band.resize(mybuilder->states.size());
102 
103  std::vector<BandInfo>& fullband = (*(mybuilder->FullBands[spin]));
104 
105  if (spo2band[spin].empty())
106  {
107  spo2band[spin].reserve(fullband.size());
108  if (!mybuilder->states[spin])
109  mybuilder->states[spin] = std::make_unique<SPOSetInfo>();
110  mybuilder->clear_states(spin);
111  initialize_spo2band(spin, fullband, *mybuilder->states[spin], spo2band[spin]);
112  }
113 
114  BandInfoGroup vals;
115  vals.TwistIndex = fullband[0].TwistIndex;
116  vals.GroupID = 0;
118  vals.selectBands(fullband, 0, ns, false);
119 
120  return create_spline_set(spo_object_name, spin, vals);
121 }
122 
123 std::unique_ptr<SPOSet> BsplineReader::create_spline_set(int spin, xmlNodePtr cur, SPOSetInputInfo& input_info)
124 {
125  std::string spo_object_name;
127  a.add(spo_object_name, "name");
128  a.add(spo_object_name, "id");
129  a.put(cur);
130 
131  if (spo2band.empty())
132  spo2band.resize(mybuilder->states.size());
133 
134  std::vector<BandInfo>& fullband = (*(mybuilder->FullBands[spin]));
135 
136  if (spo2band[spin].empty())
137  {
138  spo2band[spin].reserve(fullband.size());
139  if (!mybuilder->states[spin])
140  mybuilder->states[spin] = std::make_unique<SPOSetInfo>();
141  mybuilder->clear_states(spin);
142  initialize_spo2band(spin, fullband, *mybuilder->states[spin], spo2band[spin]);
143  }
144 
145  BandInfoGroup vals;
146  vals.TwistIndex = fullband[0].TwistIndex;
147  vals.GroupID = 0;
149  input_info.min_index(), input_info.max_index());
150  vals.selectBands(fullband, spo2band[spin][input_info.min_index()], input_info.max_index() - input_info.min_index(),
151  false);
152 
153  return create_spline_set(spo_object_name, spin, vals);
154 }
155 
156 /** build index tables to map a state to band with k-point folidng
157  * @param bigspace full BandInfo constructed by EinsplineSetBuilder
158  * @param sposet SPOSetInfo owned by someone, most likely EinsplinseSetBuilder
159  * @param spo2band spo2band[i] is the index in bigspace
160  *
161  * At gamma or arbitrary kpoints with complex wavefunctions, spo2band[i]==i
162  */
164  const std::vector<BandInfo>& bigspace,
165  SPOSetInfo& sposet,
166  std::vector<int>& spo2band)
167 {
168  spo2band.reserve(bigspace.size());
169  int ns = 0;
170  for (int i = 0; i < bigspace.size(); ++i)
171  {
172  spo2band.push_back(i);
173  SPOInfo a(ns, bigspace[i].Energy);
174  sposet.add(a);
175  ns++;
176  if (bigspace[i].MakeTwoCopies)
177  {
178  spo2band.push_back(i);
179  SPOInfo b(ns, bigspace[i].Energy);
180  sposet.add(b);
181  ns++;
182  }
183  }
184 
185  //write to a file
186  const Communicate* comm = myComm;
187  if (comm->rank())
188  return;
189 
190  std::filesystem::path aname = make_bandinfo_filename(mybuilder->getName(), spin, mybuilder->twist_num_,
192  aname += ".bandinfo.dat";
193 
194  std::ofstream o(aname.c_str());
195  std::array<char, 1024> s;
196  ns = 0;
197  using PosType = QMCTraits::PosType;
198  o << "# Band State TwistIndex BandIndex Energy Kx Ky Kz K1 K2 K3 KmK "
199  << std::endl;
200  for (int i = 0; i < bigspace.size(); ++i)
201  {
202  int ti = bigspace[i].TwistIndex;
203  int bi = bigspace[i].BandIndex;
204  double e = bigspace[i].Energy;
205  int nd = (bigspace[i].MakeTwoCopies) ? 2 : 1;
207  int s_size = std::snprintf(s.data(), s.size(), "%8d %8d %8d %8d %12.6f %7.4f %7.4f %7.4f %7.4f %7.4f %7.4f %6d\n",
208  i, ns, ti, bi, e, k[0], k[1], k[2], mybuilder->primcell_kpoints[ti][0],
210  if (s_size < 0)
211  throw std::runtime_error("Error generating bandinfo");
212  o << s.data();
213  ns += nd;
214  }
215 }
216 } // namespace qmcplusplus
Tensor< int, OHMMS_DIM > TileMatrix
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
class to read state range information from sposet input
int rank() const
return the rank
Definition: Communicate.h:116
Communicate * myComm
communicator
Definition: BsplineReader.h:44
std::string make_bandinfo_filename(const std::string &root, int spin, int twist, const Tensor< int, 3 > &tilematrix, int gid)
std::ostream & app_log()
Definition: OutputManager.h:65
bool put(xmlNodePtr cur)
assign attributes to the set
Definition: AttributeSet.h:55
std::string myName
name of this band
Definition: BandInfo.h:81
Builder class for einspline-based SPOSet objects.
std::vector< std::unique_ptr< SPOSetInfo > > states
state info of all possible states available in the basis
Definition: SPOSetBuilder.h:57
std::vector< std::unique_ptr< std::vector< BandInfo > > > FullBands
Helper vector for sorting bands.
Wrapping information on parallelism.
Definition: Communicate.h:68
Communicate * getCommunicator() const
return myComm
Definition: MPIObjectBase.h:41
SingleParticlePos k_cart(const SingleParticlePos &kin) const
conversion of a reciprocal-vector
int getGroupID() const
return the group id
Definition: Communicate.h:121
int GroupID
index of the group
Definition: BandInfo.h:69
a group of bands
Definition: BandInfo.h:66
std::string make_bandgroup_name(const std::string &root, int spin, int twist, const Tensor< int, 3 > &tilematrix, int first, int last)
base class to describe a single orbital in an SPOSet
Definition: SPOInfo.h:23
void add(SPOInfo &state)
Definition: SPOSetInfo.cpp:34
Tensor<T,D> class for D by D tensor.
Definition: OhmmsTinyMeta.h:32
std::vector< std::vector< int > > spo2band
map from spo index to band index
Definition: BsplineReader.h:53
void clear_states(int index=0)
clear state information
Definition: SPOSetBuilder.h:69
base class to read data and manage spline tables
class to handle a set of attributes of an xmlNode
Definition: AttributeSet.h:24
void selectBands(const std::vector< BandInfo > &bigspace, double emin, double emax)
select the bands within an energy range [emin,emax)
Definition: BandInfo.cpp:22
int TwistIndex
twist index set by the full band not by the subset
Definition: BandInfo.h:77
collection of orbital info for SPOSet instance or builder
Definition: SPOSetInfo.h:25
QTBase::PosType PosType
Definition: Configuration.h:61
void setCommon(xmlNodePtr cur)
setting common parameters
BsplineReader(EinsplineSetBuilder *e)
virtual std::unique_ptr< SPOSet > create_spline_set(const std::string &my_name, int spin, const BandInfoGroup &bandgroup)=0
create the actual spline sets
void initialize_spo2band(int spin, const std::vector< BandInfo > &bigspace, SPOSetInfo &sposet, std::vector< int > &band2spo)
build index tables to map a state to band with k-point folidng
const std::string & getName() const
return the name
Definition: MPIObjectBase.h:54
std::vector< TinyVector< double, OHMMS_DIM > > primcell_kpoints
EinsplineSetBuilder * mybuilder
pointer to the EinsplineSetBuilder
Definition: BsplineReader.h:42
#define APP_ABORT_TRACE(f, l, msg)
Definition: AppAbort.h:34
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
bool checkNorm
mesh size check the norm of orbitals
Definition: BsplineReader.h:47
bool saveSplineCoefs
save spline coefficients to storage
Definition: BsplineReader.h:49