QMCPACK
kSpaceJastrowBuilder.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: Ken Esler, kpesler@gmail.com, University of Illinois at Urbana-Champaign
8 // Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
9 // Jeremy McMinnis, jmcminis@gmail.com, University of Illinois at Urbana-Champaign
10 // Mark A. Berrill, berrillma@ornl.gov, Oak Ridge National Laboratory
11 //
12 // File created by: Ken Esler, kpesler@gmail.com, University of Illinois at Urbana-Champaign
13 //////////////////////////////////////////////////////////////////////////////////////
14 
15 
16 #include "kSpaceJastrowBuilder.h"
17 #include "OhmmsData/AttributeSet.h"
18 #include "Utilities/qmc_common.h"
19 
20 namespace qmcplusplus
21 {
22 template<class T>
23 inline bool putContent2(std::vector<T>& a, xmlNodePtr cur)
24 {
25  std::istringstream stream(XMLNodeString{cur});
26  T temp;
27  a.clear();
28  while (!stream.eof())
29  {
30  stream >> temp;
31  if (stream.fail() || stream.bad())
32  break;
33  else
34  a.push_back(temp);
35  }
36  return a.size() > 0;
37 }
38 
39 
40 std::unique_ptr<WaveFunctionComponent> kSpaceJastrowBuilder::buildComponent(xmlNodePtr cur)
41 {
42  xmlNodePtr kids = cur->xmlChildrenNode;
43  kSpaceJastrow::SymmetryType oneBodySymm, twoBodySymm;
44  RealType kc1, kc2;
45  std::string symm1_opt, symm2_opt, id1_opt, id2_opt, spin1_opt("no"), spin2_opt("no");
46  std::vector<RealType> oneBodyCoefs, twoBodyCoefs;
47  // Initialize options
48  kc1 = kc2 = 0.0;
49  oneBodySymm = twoBodySymm = kSpaceJastrow::CRYSTAL;
50  id1_opt = "cG1";
51  id2_opt = "cG2";
52  while (kids != NULL)
53  {
54  std::string kidsname = (char*)kids->name;
55  std::vector<RealType>* coefs(NULL);
56  std::string* id_opt(NULL);
57  if (kidsname == "correlation")
58  {
59  std::string type_opt;
60  OhmmsAttributeSet attrib;
61  attrib.add(type_opt, "type");
62  attrib.put(kids);
63  if (type_opt == "One-Body")
64  {
65  attrib.add(symm1_opt, "symmetry");
66  attrib.add(kc1, "kc");
67  attrib.add(spin1_opt, "spinDependent");
68  coefs = &oneBodyCoefs;
69  id_opt = &id1_opt;
70  }
71  else if (type_opt == "Two-Body")
72  {
73  attrib.add(symm2_opt, "symmetry");
74  attrib.add(kc2, "kc");
75  attrib.add(spin2_opt, "spinDependent");
76  coefs = &twoBodyCoefs;
77  id_opt = &id2_opt;
78  }
79  else
80  app_warning() << " Unrecognized kSpace type \"" << type_opt
81  << "\" in kSpaceJastrowBuilder::put(xmlNotPtr cur).\n";
82  attrib.put(kids);
83  // Read the coefficients
84  if (coefs)
85  {
86  xmlNodePtr xmlCoefs = kids->xmlChildrenNode;
87  while (xmlCoefs != NULL)
88  {
89  std::string cname((const char*)xmlCoefs->name);
90  if (cname == "coefficients")
91  {
92  std::string type("0"), id("0");
93  OhmmsAttributeSet cAttrib;
94  cAttrib.add(*id_opt, "id");
95  cAttrib.add(type, "type");
96  cAttrib.put(xmlCoefs);
97  if (type != "Array")
98  {
99  app_error() << "Unknown coefficients type "
100  ""
101  << type
102  << ""
103  " in kSpaceJastrowBuilder.\n"
104  << "Resetting to "
105  "Array"
106  ".\n";
107  xmlNewProp(xmlCoefs, (const xmlChar*)"type", (const xmlChar*)"Array");
108  }
109  //vector<T> can be read by this
110  putContent2(*coefs, xmlCoefs);
111  app_log() << " Read " << coefs->size() << " coefficients for type " << type_opt << std::endl;
112  }
113  xmlCoefs = xmlCoefs->next;
114  }
115  }
116  }
117  else if (kidsname != "text")
118  {
119  app_warning() << "Unrecognized section \"" << kidsname << "\" in kSpaceJastrowBuilder.\n";
120  }
121  kids = kids->next;
122  }
123  // Now build the kSpaceJastrow
124  std::map<std::string, kSpaceJastrow::SymmetryType>::iterator symm1 = SymmMap.find(symm1_opt);
125  if (symm1 != SymmMap.end())
126  oneBodySymm = symm1->second;
127  std::map<std::string, kSpaceJastrow::SymmetryType>::iterator symm2 = SymmMap.find(symm2_opt);
128  if (symm2 != SymmMap.end())
129  twoBodySymm = symm2->second;
130  auto jastrow = std::make_unique<kSpaceJastrow>(sourcePtcl, targetPtcl, oneBodySymm, kc1, id1_opt, spin1_opt == "yes",
131  twoBodySymm, kc2, id2_opt, spin2_opt == "yes");
132  jastrow->setCoefficients(oneBodyCoefs, twoBodyCoefs);
133  if (qmc_common.io_node)
134  outputJastrow(jastrow.get());
135  //jastrow->addOptimizables(targetPsi.VarList);
136  return jastrow;
137 }
138 
139 
141 {
142  std::array<char, 32> fname;
143  int fname_len{0};
144  int taskid = is_manager() ? getGroupID() : -1;
145 
146  // output one-body jastrow
147  if (qmc_common.mpi_groups > 1)
148  fname_len = std::snprintf(fname.data(), fname.size(), "Jk1.g%03d.dat", taskid);
149  else
150  fname_len = std::snprintf(fname.data(), fname.size(), "Jk1.dat");
151  if (fname_len < 0)
152  throw std::runtime_error("Error generating filename");
153 
154  std::ofstream fout(std::string(fname.data(), fname_len));
155  fout << "# kx ky kz coeff_real coeff_imag" << std::endl;
156  jastrow->printOneBody(fout);
157  fout.close();
158 
159  // output two-body jastrow
160  if (qmc_common.mpi_groups > 1)
161  fname_len = std::snprintf(fname.data(), fname.size(), "Jk2.g%03d.dat", taskid);
162  else
163  fname_len = std::snprintf(fname.data(), fname.size(), "Jk2.dat");
164  if (fname_len < 0)
165  throw std::runtime_error("Error generating filename");
166  fout.open(std::string(fname.data(), fname_len));
167  fout << "# kx ky kz coeff_real coeff_imag" << std::endl;
168  jastrow->printTwoBody(fout);
169  fout.close();
170 }
171 } // namespace qmcplusplus
std::ostream & app_warning()
Definition: OutputManager.h:69
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
void printTwoBody(std::ostream &os)
std::ostream & app_log()
Definition: OutputManager.h:65
bool put(xmlNodePtr cur)
assign attributes to the set
Definition: AttributeSet.h:55
std::ostream & app_error()
Definition: OutputManager.h:67
int getGroupID() const
return the group id of the communicator
Definition: MPIObjectBase.h:38
std::map< std::string, kSpaceJastrow::SymmetryType > SymmMap
ParticleSet & targetPtcl
reference to the particle set on which targetPsi is defined
bool putContent2(std::vector< T > &a, xmlNodePtr cur)
class to handle a set of attributes of an xmlNode
Definition: AttributeSet.h:24
bool io_node
true, print out file
Definition: qmc_common.h:39
int mpi_groups
number of mpi groups
Definition: qmc_common.h:33
void printOneBody(std::ostream &os)
output jastrow coefficients
convert xmlNode contents into a std::string
void outputJastrow(kSpaceJastrow *jastrow)
bool is_manager() const
return true if the rank == 0
Definition: MPIObjectBase.h:51
QMCState qmc_common
a unique QMCState during a run
Definition: qmc_common.cpp:111
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
std::unique_ptr< WaveFunctionComponent > buildComponent(xmlNodePtr cur) override
process a xml node at cur