QMCPACK
SHOSetBuilder.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: Jaron T. Krogel, krogeljt@ornl.gov, Oak Ridge National Laboratory
8 // Mark A. Berrill, berrillma@ornl.gov, Oak Ridge National Laboratory
9 //
10 // File created by: Jaron T. Krogel, krogeljt@ornl.gov, Oak Ridge National Laboratory
11 //////////////////////////////////////////////////////////////////////////////////////
12 
13 
14 #include "SHOSetBuilder.h"
16 #include "OhmmsData/AttributeSet.h"
18 #include "Utilities/string_utils.h"
19 #include "CPU/math.hpp"
20 
21 namespace qmcplusplus
22 {
24 {
25  ClassName = "SHOSetBuilder";
26  legacy = false;
27  app_log() << "Constructing SHOSetBuilder" << std::endl;
28  reset();
29 }
30 
31 
33 
34 
36 {
37  nstates = 0;
38  mass = -1.0;
39  energy = -1.0;
40  length = -1.0;
41  center = 0.0;
42 }
43 
44 
45 std::unique_ptr<SPOSet> SHOSetBuilder::createSPOSetFromXML(xmlNodePtr cur)
46 {
47  APP_ABORT("SHOSetBuilder::createSPOSetFromXML SHOSetBuilder should not use legacy interface");
48 
49  app_log() << "SHOSetBuilder::createSHOSet(xml) " << std::endl;
50 
52 
53  return createSPOSet(cur, input);
54 }
55 
56 
57 std::unique_ptr<SPOSet> SHOSetBuilder::createSPOSet(xmlNodePtr cur, SPOSetInputInfo& input)
58 {
59  app_log() << "SHOSetBuilder::createSHOSet(indices) " << std::endl;
60 
61  using std::ceil;
62  using std::sqrt;
63 
64  reset();
65 
66  // read parameters
67  std::string spo_name = "sho";
68  OhmmsAttributeSet attrib;
69  attrib.add(spo_name, "name");
70  attrib.add(spo_name, "id");
71  attrib.add(mass, "mass");
72  attrib.add(energy, "energy");
73  attrib.add(energy, "frequency");
74  attrib.add(length, "length");
75  attrib.add(center, "center");
76  attrib.add(nstates, "size");
77  attrib.put(cur);
78 
79  if (energy < 0.0)
80  energy = 1.0;
81  if (mass < 0.0 && length < 0.0)
82  length = 1.0;
83  if (mass < 0.0)
84  mass = 1.0 / (energy * length * length);
85  else if (length < 0.0)
86  length = 1.0 / sqrt(mass * energy);
87 
88  // initialize states and/or adjust basis
89  int smax = -1;
90  if (input.has_index_info)
91  smax = std::max(smax, input.max_index());
92  if (input.has_energy_info)
93  {
94  smax = std::max(smax, (int)ceil(input.max_energy() / energy));
95  }
96  if (smax < 0)
97  APP_ABORT("SHOSetBuilder::Initialize\n invalid basis size");
98  update_basis_states(smax);
99 
100  // create sho state request
101  indices_t& indices = input.get_indices(states);
102  std::vector<SHOState*> sho_states;
103  for (int i = 0; i < indices.size(); ++i)
104  sho_states.push_back(basis_states[indices[i]]);
105 
106  // make the sposet
107  auto sho = std::make_unique<SHOSet>(spo_name, length, center, sho_states);
108 
109  sho->report(" ");
110  //sho->test_derivatives();
111  //sho->test_overlap();
112  //APP_ABORT("SHOSetBuilder check");
113 
114  return sho;
115 }
116 
117 
119 {
120  using std::ceil;
121  using std::exp;
122  using std::log;
123  using std::sort;
124  using std::sqrt;
125 
126  int states_required = smax - basis_states.size() + 1;
127  if (states_required > 0)
128  {
129  RealType N = smax + 1;
130  if (DIM == 1)
131  nmax = smax;
132  else if (DIM == 2)
133  nmax = ceil(.5 * sqrt(8. * N + 1.) - 1.5);
134  else if (DIM == 3)
135  {
136  RealType f = exp(1.0 / 3.0 * log(81. * N + 3. * sqrt(729. * N * N - 3.)));
137  nmax = ceil(f / 3. + 1. / f - 2.);
138  }
139  else
140  APP_ABORT("SHOSetBuilder::update_basis_states dimensions other than 1, 2, or 3 are not supported");
141  int ndim = nmax + 1;
142  ind_dims[DIM - 1] = 1;
143  for (int d = DIM - 2; d > -1; --d)
144  ind_dims[d] = ind_dims[d + 1] * ndim;
145  int s = 0;
146  int ntot = pow(ndim, DIM);
147  TinyVector<int, DIM> qnumber;
148  for (int m = 0; m < ntot; ++m)
149  {
150  int n = 0; // principal quantum number
151  int nrem = m;
152  for (int d = 0; d < DIM; ++d)
153  {
154  int i = nrem / ind_dims[d];
155  nrem -= i * ind_dims[d];
156  qnumber[d] = i;
157  n += i;
158  }
159  if (n <= nmax)
160  {
161  SHOState* st;
162  if (s < basis_states.size())
163  st = basis_states[s];
164  else
165  {
166  st = new SHOState();
167  basis_states.add(st);
168  }
169  RealType e = energy * (n + .5 * DIM);
170  st->set(qnumber, e);
171  s++;
172  }
173  }
174  basis_states.energy_sort(1e-6, true);
175  }
176 
177  // reset energy scale even if no states need to be added
178  for (int i = 0; i < basis_states.size(); ++i)
179  {
180  SHOState& state = *basis_states[i];
181  const TinyVector<int, DIM>& qnumber = state.quantum_number;
182  int n = 0;
183  for (int d = 0; d < DIM; ++d)
184  n += qnumber[d];
185  state.energy = energy * (n + .5 * DIM);
186  }
187 
188  //somewhat redundant, but necessary
189  clear_states(0);
190  states[0]->finish(basis_states.states);
191 
192  if (basis_states.size() <= smax)
193  APP_ABORT("SHOSetBuilder::update_basis_states failed to make enough states");
194 }
195 
196 
197 void SHOSetBuilder::report(const std::string& pad)
198 {
199  app_log() << pad << "SHOSetBuilder report" << std::endl;
200  app_log() << pad << " dimension = " << DIM << std::endl;
201  app_log() << pad << " mass = " << mass << std::endl;
202  app_log() << pad << " frequency = " << energy << std::endl;
203  app_log() << pad << " energy = " << energy << std::endl;
204  app_log() << pad << " length = " << length << std::endl;
205  app_log() << pad << " center = " << center << std::endl;
206  app_log() << pad << " nstates = " << nstates << std::endl;
207  app_log() << pad << " nmax = " << nmax << std::endl;
208  app_log() << pad << " ind_dims = " << ind_dims << std::endl;
209  app_log() << pad << " # basis states = " << basis_states.size() << std::endl;
210  app_log() << pad << " basis_states" << std::endl;
211  for (int s = 0; s < basis_states.size(); ++s)
212  basis_states[s]->report(pad + " " + int2string(s) + " ");
213  app_log() << pad << "end SHOSetBuilder report" << std::endl;
214  app_log().flush();
215 }
216 
217 } // namespace qmcplusplus
std::vector< int > indices_t
Definition: SPOSetBuilder.h:50
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
class to read state range information from sposet input
QTBase::RealType RealType
Definition: Configuration.h:58
std::ostream & app_log()
Definition: OutputManager.h:65
bool put(xmlNodePtr cur)
assign attributes to the set
Definition: AttributeSet.h:55
TinyVector< int, DIM > ind_dims
Definition: SHOSetBuilder.h:36
std::string int2string(const int &i)
Definition: string_utils.h:119
void update_basis_states(int smax)
void set(TinyVector< int, DIM > qn, RealType e)
Definition: SHOSet.h:35
SPOSetInfoSimple< SHOState > basis_states
Definition: SHOSetBuilder.h:38
std::vector< std::unique_ptr< SPOSetInfo > > states
state info of all possible states available in the basis
Definition: SPOSetBuilder.h:57
TinyVector< int, DIM > quantum_number
Definition: SHOSet.h:25
void report(const std::string &pad="")
RealType energy
energy of the orbital (in Hartree units)
Definition: SPOInfo.h:43
Wrapping information on parallelism.
Definition: Communicate.h:68
std::unique_ptr< SPOSet > createSPOSet(xmlNodePtr cur, SPOSetInputInfo &input) override
create an sposet from a general xml request
MakeReturn< BinaryNode< FnPow, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t, typename CreateLeaf< Vector< T2, C2 > >::Leaf_t > >::Expression_t pow(const Vector< T1, C1 > &l, const Vector< T2, C2 > &r)
Specialized paritlce class for atomistic simulations.
Definition: ParticleSet.h:55
base class for the real SPOSet builder
Definition: SPOSetBuilder.h:47
MakeReturn< UnaryNode< FnCeil, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t > >::Expression_t ceil(const Vector< T1, C1 > &l)
void clear_states(int index=0)
clear state information
Definition: SPOSetBuilder.h:69
class to handle a set of attributes of an xmlNode
Definition: AttributeSet.h:24
std::string ClassName
class Name
Definition: MPIObjectBase.h:65
#define APP_ABORT(msg)
Widely used but deprecated fatal error macros from legacy code.
Definition: AppAbort.h:27
MakeReturn< UnaryNode< FnExp, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t > >::Expression_t exp(const Vector< T1, C1 > &l)
MakeReturn< UnaryNode< FnLog, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t > >::Expression_t log(const Vector< T1, C1 > &l)
MakeReturn< UnaryNode< FnSqrt, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t > >::Expression_t sqrt(const Vector< T1, C1 > &l)
SHOSetBuilder(ParticleSet &P, Communicate *comm)
std::unique_ptr< SPOSet > createSPOSetFromXML(xmlNodePtr cur) override
create an sposet from xml (legacy)
testing::ValidSpinDensityInput input
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 legacy
whether implementation conforms only to legacy standard
Definition: SPOSetBuilder.h:54