MADNESS  0.10.1
PNOGuessFunctions.h
Go to the documentation of this file.
1 /*
2  * BasisFunctions.h
3  *
4  * Created on: Apr 18, 2018
5  * Author: kottmanj
6  */
7 
8 #ifndef PAPER_CODE_BASISFUNCTIONS_H_
9 #define PAPER_CODE_BASISFUNCTIONS_H_
10 
11 
12 #include <exception>
13 #include <stdlib.h>
14 #include <cassert>
15 #include <cmath>
16 #include <numeric>
17 #include <valarray>
18 #include <stdio.h>
19 #include <madness.h>
24 
25 #include <tuple>
26 
27 
28 namespace madness {
29 
30 
31 /// class which provides guess functions for pnos and cabs basis sets
32 /// the guess functions are not orthonormalized or Q-projected
34 public:
35  typedef std::vector<std::tuple<int, std::vector<double>, std::vector<double> > > cbfT;
36  BasisFunctions(World& world,const Molecule& mol, const size_t& l): world(world), molecule(mol), lmax(l) {}
39  const size_t lmax;
40 
41  // print out a basis of contracted orbitals
42  void print_contracted_basis(std::map<std::string, cbfT >& molbas)const{
43  for(const auto& tmp:molbas){
44  cbfT abf=tmp.second;
45  for(const auto& bf:abf){
46  const int type = std::get<0>(bf);
47  const std::vector<double> ex=std::get<1>(bf);
48  const std::vector<double> c=std::get<2>(bf);
49  std::cout << ex.size() << " " << type << "\n";
50  for(size_t k=0;k<ex.size();++k) std::cout << ex[k] << " " << c[k] << "\n";
51 
52  }
53  }
54  }
55 
58  MyTimer time_1 = MyTimer(world).start();
59  // Read Exponents from file
60  std::map<std::string, cbfT > molbas = read_contracted_basis_from_file("bas", molecule.get_atoms());
61  print("Exponents from file bas:");
62  print_contracted_basis(molbas);
63  time_1.stop().print("Read Exponents From File");
64  vector_real_function_3d virtuals;
65 
66  MyTimer time_2 = MyTimer(world).start();
67  for (const madness::Atom& atom : molecule.get_atoms()) {
68  cbfT abf = molbas.at(atomic_number_to_symbol(atom.atomic_number));
69  for(const auto& bf:abf){
70  const int type = std::get<0>(bf);
71  const std::vector<double> ex=std::get<1>(bf);
72  const std::vector<double> c=std::get<2>(bf);
73  MADNESS_ASSERT(ex.size()==c.size());
74  vector_real_function_3d contracted_function;
75  for(size_t k=0;k<ex.size();++k){
77  print_size(world,gshell,"ex="+std::to_string(ex[k]));
78  normalize(world,gshell);
80  print_size(world,gshell,"ex="+std::to_string(ex[k]));
81  if(contracted_function.empty()) contracted_function=c[k]*gshell;
82  else contracted_function += c[k]*gshell;
83  }
84  normalize(world,contracted_function);
85  virtuals=append(virtuals,contracted_function);
86 
87  }
88 // for (size_t l = 0; l < exp_atom.size(); ++l) {
89 // for (const double e : exp_atom[l]) {
90 // virtuals=append(virtuals, guess_virtual_gaussian_shell(atom, l, e));
91 // }
92 // }
93  }
94  time_2.stop().print("Creating Contracted Guess Basis");
95  return virtuals;
96  }
97 
99  MADNESS_EXCEPTION("Not there for new madness version",1);
100  }
101 
102  /// make guess virtuals by exciting with polynomials: v = poly*f
103  vector_real_function_3d guess_with_exop(const vector_real_function_3d& f, const std::string& type="dipole+", const bool trigo=true)const{
104  if(world.rank()==0) std::cout << "Create guess functions by multiplying plane-waves to " << f.size() << " functions \n";
105  MADNESS_ASSERT(not f.empty());
106 
107  std::vector<coord_3d> centers = guessfactory::compute_centroids(f);
108  std::vector<std::string> exop_list = guessfactory::make_predefined_exop_strings(type);
109 
110 
111 
112  // make the list with seeds and excitation operators
113  std::vector<std::pair<vector_real_function_3d,std::string> > init_list;
114 
115  for(const auto& exop:exop_list){
117  init_list.push_back(std::make_pair(cs,exop));
118  }
119  world.gop.fence();
120 
121  vector_real_function_3d virtuals;
122  if(trigo) for(auto& it:init_list) virtuals=append(virtuals, guessfactory::apply_trigonometric_exop(it.first,it.second,centers,false));
123  else for(auto& it:init_list){
124  if(world.rank()==0) std::cout << "polynomial guess!\n";
125  virtuals=append(virtuals, guessfactory::apply_polynomial_exop(it.first,it.second,centers,false));
126  }
127  world.gop.fence();
128 
129  return (virtuals);
130  }
131 
132  vector_real_function_3d guess_virtuals_internal(const std::map<std::string, std::vector<int> > guess_map) const;
134  // determine start zeta value
135  size_t start=0;
136  bool empty=false;
137  if(name=="pvdz") start=2;
138  else if(name=="pvtz") start=3;
139  else if(name=="pvqz") start=4;
140  else if(name=="pv5z") start=5;
141  else if(name=="pv6z") start=6;
142  else if(name=="pv7z") start=7;
143  else if(name=="zero" or name=="0" or name=="void" or name=="none" or name=="empty") empty=true;
144  else MADNESS_EXCEPTION(("unknown predefined guess:"+name).c_str(),1);
145 
146  std::map<std::string, std::vector<int> > guess_map;
147  for(const auto& atom :molecule.get_atoms()){// (int i = 0; i < nemo.molecule().natom(); ++i) {
148  const std::string symbol = atomic_number_to_symbol(atom.atomic_number);
149  const std::size_t n=atom.atomic_number;
150  if(empty) guess_map[symbol]=std::vector<int>(lmax,0);
151  else{
152  if(n<3) guess_map[symbol]=fill_peterson(start); // first row
153  else if(n<11) guess_map[symbol]=fill_peterson(start+1); // first row
154  else if(n<19) guess_map[symbol]=fill_peterson(start+2); // third row
155  else MADNESS_EXCEPTION("Predefined guesses only up to third row",1);
156  }
157 
158  }
159  return guess_virtuals_internal(guess_map);
160  }
161 
162  // helper to fill up l numbers (Peterson Style)
163  std::vector<int> fill_peterson(const size_t& s)const{
164  std::vector<int> result(lmax,0);
165  for(size_t i=0;i<lmax;i++){
166  const int tmp=(s-i);
167  if(tmp>0) result[i]=tmp;
168  else result[i]=0;
169  }
170  return result;
171  }
172 
173  // helper to get vectors of right size
174  std::vector<int> fill_up(std::vector<int> v)const{
175  std::vector<int> result(lmax,0);
176  for(size_t i=0;i<(lmax>v.size() ? v.size() : lmax);++i) result[i]=v[i];
177  return result;
178  }
179 
180  /// return a shell of l-quantum l and exponent e, including all cartesian
181  /// components
182  vector_real_function_3d guess_virtual_gaussian_shell(const Atom& atom, const int l, const double e) const;
183 
184  /// read external CABS
185  std::map<std::string, std::vector<std::vector<double> > > read_basis_from_file(const std::string& filename, const std::vector<madness::Atom> atoms) const;
186  std::vector<std::vector<double> > read_basis_from_file(const std::string& filename, const std::string& atom) const;
187 
188 
189 
190 
191  std::map<std::string, cbfT > read_contracted_basis_from_file(const std::string& filename, const std::vector<madness::Atom> atoms) const {
192  std::map<std::string, cbfT > molbas;
193  for (const madness::Atom& atom : atoms) {
194  const std::string symbol = atomic_number_to_symbol(atom.atomic_number);
195  if (molbas.find(symbol) == molbas.end()) {
197  molbas[symbol] = tmpbas;
198  }
199  }
200  return molbas;
201  }
202 
203  cbfT read_contracted_basis_from_file(const std::string& filename, const std::string& atom) const {
204  std::ifstream f(filename.c_str());
205  position_stream(f, atom);
206  std::string s;
207  int n=-1;
208  std::string str_pw_guess, symbol;
209  cbfT result;
210  std::size_t stars = 0;
211  while (f >> s) {
212  if (s == "*") {
213  f>>n; // initial step
214  ++stars;
215  if (stars == 2)
216  break;
217  }else{
218  n=std::stoi(s);
219  MADNESS_ASSERT(n>0);
220  }
221  if(n>0){
222  f>>s;
223  if (s == "s" || s == "p" || s == "d" || s == "f" || s == "g" || s == "h" || s == "i" || s == "k") {
224  std::vector<double> exponents;
225  std::vector<double> coeff;
226  for(int i=0;i<n;++i){
227  double ex,c;
228  f>>ex;
229  f>>c;
230  exponents.push_back(ex);
231  coeff.push_back(c);
232  }
233 
234  result.push_back(std::make_tuple(lqtoint(s),exponents,coeff));
235  } else MADNESS_EXCEPTION("UNKNOWN ANGULAR QUANTUM NUMBER",1);
236  }
237  }
238 
239  return result;
240  }
241 
242  /// little helper function for l-quantum numbers
243  size_t lqtoint(const std::string& l) const;
244 
245  class CartesianGaussian : public FunctionFunctorInterface<double, 3> {
246  public:
247  CartesianGaussian(const Atom& atom, const double e, const int i, const int j, const int k);
248 
249  double x, y, z; ///< origin
250  double exponent; ///< exponent
251  int i, j, k; ///< cartesian exponents
252 
253  double operator()(const coord_3d& xyz) const {
254  double xx = xyz[0] - x;
255  double yy = xyz[1] - y;
256  double zz = xyz[2] - z;
257  const double e = exponent * (xx * xx + yy * yy + zz * zz);
258  return pow(xx, i) * pow(yy, j) * pow(zz, k) * exp(-e);
259  }
260  };
261 
262  /// make a set of Cartesian Gaussian functions located on atom
263 
264  /// @param[in] atom where the Cartesian Gaussian function is located
265  /// @param[in] l the l-quantum number
266  /// @param[in] exponent exponent of the Cartesian Gaussian function
267  std::vector<CartesianGaussian> make_cartesian_guess(const Atom& atom, int l,
268  const double exponent) {
269  std::vector<CartesianGaussian> gg;
270 
271  // loop over all Cartesian components of l
272  for (int kx = l; kx >= 0; kx--) {
273  for (int ky = l - kx; ky >= 0; ky--) {
274  int kz = l - kx - ky;
275  gg.push_back(CartesianGaussian(atom, exponent, kx, ky, kz));
276  }
277  }
278 
279  return gg;
280  }
281 
282  /// real solid harmonic Gaussian
283  /// \note see https://en.wikipedia.org/wiki/Spherical_harmonics and https://en.wikipedia.org/wiki/Solid_harmonics
285  public:
286  SolidHarmonicGaussian(const Atom& atom, const double e, int l, int m)
287  : x(atom.x), y(atom.y), z(atom.z), exponent(e), Z(atom.atomic_number), l(l), m(m) {}
288 
289  double x, y, z; ///< origin
290  double exponent; ///< exponent
291  double Z;
292  int l, m; ///< (real) solid harmonic quanta
293 
294  template <typename T> int sgn(T val) const {
295  return (T(0) < val) - (val < T(0));
296  }
297 
298  double operator ()(const coord_3d& xyz) const;
299 
300  std::vector<coord_3d> special_points()const{
301  coord_3d coord;
302  coord[0]=x;
303  coord[1]=y;
304  coord[2]=z;
305  return std::vector<coord_3d>(1,coord);
306  }
307 
309  const double width = 1.0/sqrt(2.0*exponent); // with width of the gaussian
310  const int level = FunctionDefaults<3>::set_length_scale(width); // length scale for special points (center)
311  return level;
312  }
313  };
314 
315  /// make a set of Cartesian Gaussian functions located on atom
316 
317  /// @param[in] atom where the Cartesian Gaussian function is located
318  /// @param[in] l the l-quantum number
319  /// @param[in] exponent exponent of the Cartesian Gaussian function
320  std::vector<SolidHarmonicGaussian> make_solidharmonic_guess(const Atom& atom, int l,
321  const double exponent)const {
322  std::vector<SolidHarmonicGaussian> gg;
323 
324  // loop over all angular components of l
325  for(int m=-l; m<=l; ++m) {
326  gg.push_back(SolidHarmonicGaussian(atom, exponent, l, m));
327  }
328 
329  return gg;
330  }
331 
332 };
333 
334 }// namespace madness
335 
336 #endif /* PAPER_CODE_BASISFUNCTIONS_H_ */
Definition: molecule.h:58
Definition: PNOGuessFunctions.h:245
int j
Definition: PNOGuessFunctions.h:251
int k
cartesian exponents
Definition: PNOGuessFunctions.h:251
CartesianGaussian(const Atom &atom, const double e, const int i, const int j, const int k)
double y
Definition: PNOGuessFunctions.h:249
double z
origin
Definition: PNOGuessFunctions.h:249
int i
Definition: PNOGuessFunctions.h:251
double x
Definition: PNOGuessFunctions.h:249
double exponent
exponent
Definition: PNOGuessFunctions.h:250
double operator()(const coord_3d &xyz) const
Definition: PNOGuessFunctions.h:253
Definition: PNOGuessFunctions.h:284
double operator()(const coord_3d &xyz) const
Definition: PNOGuessFunctions.cpp:160
Level special_level()
Override this change level refinement for special points (default is 6)
Definition: PNOGuessFunctions.h:308
double exponent
exponent
Definition: PNOGuessFunctions.h:290
std::vector< coord_3d > special_points() const
Override this to return list of special points to be refined more deeply.
Definition: PNOGuessFunctions.h:300
SolidHarmonicGaussian(const Atom &atom, const double e, int l, int m)
Definition: PNOGuessFunctions.h:286
double z
origin
Definition: PNOGuessFunctions.h:289
int m
(real) solid harmonic quanta
Definition: PNOGuessFunctions.h:292
int sgn(T val) const
Definition: PNOGuessFunctions.h:294
int l
Definition: PNOGuessFunctions.h:292
double Z
Definition: PNOGuessFunctions.h:291
double y
Definition: PNOGuessFunctions.h:289
double x
Definition: PNOGuessFunctions.h:289
Definition: PNOGuessFunctions.h:33
void print_contracted_basis(std::map< std::string, cbfT > &molbas) const
Definition: PNOGuessFunctions.h:42
std::vector< int > fill_peterson(const size_t &s) const
Definition: PNOGuessFunctions.h:163
std::vector< std::tuple< int, std::vector< double >, std::vector< double > > > cbfT
Definition: PNOGuessFunctions.h:35
std::vector< SolidHarmonicGaussian > make_solidharmonic_guess(const Atom &atom, int l, const double exponent) const
make a set of Cartesian Gaussian functions located on atom
Definition: PNOGuessFunctions.h:320
std::map< std::string, cbfT > read_contracted_basis_from_file(const std::string &filename, const std::vector< madness::Atom > atoms) const
Definition: PNOGuessFunctions.h:191
vector_real_function_3d guess_virtuals_internal(const std::map< std::string, std::vector< int > > guess_map) const
Definition: PNOGuessFunctions.cpp:46
vector_real_function_3d guess_with_psi4(const vector_real_function_3d &mos) const
Definition: PNOGuessFunctions.h:98
size_t lqtoint(const std::string &l) const
little helper function for l-quantum numbers
Definition: PNOGuessFunctions.cpp:133
std::vector< int > fill_up(std::vector< int > v) const
Definition: PNOGuessFunctions.h:174
vector_real_function_3d guess_contracted_virtuals_from_file() const
Definition: PNOGuessFunctions.h:57
vector_real_function_3d predefined_guess(const std::string name) const
Definition: PNOGuessFunctions.h:133
const size_t lmax
Definition: PNOGuessFunctions.h:39
std::map< std::string, std::vector< std::vector< double > > > read_basis_from_file(const std::string &filename, const std::vector< madness::Atom > atoms) const
read external CABS
Definition: PNOGuessFunctions.cpp:121
World & world
Definition: PNOGuessFunctions.h:37
const Molecule & molecule
Definition: PNOGuessFunctions.h:38
BasisFunctions(World &world, const Molecule &mol, const size_t &l)
Definition: PNOGuessFunctions.h:36
cbfT read_contracted_basis_from_file(const std::string &filename, const std::string &atom) const
Definition: PNOGuessFunctions.h:203
vector_real_function_3d guess_virtuals_from_file() const
Definition: PNOGuessFunctions.cpp:16
vector_real_function_3d guess_with_exop(const vector_real_function_3d &f, const std::string &type="dipole+", const bool trigo=true) const
make guess virtuals by exciting with polynomials: v = poly*f
Definition: PNOGuessFunctions.h:103
std::vector< CartesianGaussian > make_cartesian_guess(const Atom &atom, int l, const double exponent)
make a set of Cartesian Gaussian functions located on atom
Definition: PNOGuessFunctions.h:267
vector_real_function_3d guess_virtual_gaussian_shell(const Atom &atom, const int l, const double e) const
Definition: PNOGuessFunctions.cpp:89
FunctionDefaults holds default paramaters as static class members.
Definition: funcdefaults.h:204
static int set_length_scale(const double lo, const size_t k=get_k())
adapt the special level to resolve the smallest length scale
Definition: funcdefaults.h:437
Abstract base class interface required for functors used as input to Functions.
Definition: function_interface.h:68
Definition: molecule.h:124
const std::vector< Atom > & get_atoms() const
Definition: molecule.h:433
void fence(bool debug=false)
Synchronizes all processes in communicator AND globally ensures no pending AM or tasks.
Definition: worldgop.cc:161
A parallel world class.
Definition: world.h:132
ProcessID rank() const
Returns the process rank in this World (same as MPI_Comm_rank()).
Definition: world.h:318
WorldGopInterface & gop
Global operations.
Definition: world.h:205
const double m
Definition: gfit.cc:199
auto T(World &world, response_space &f) -> response_space
Definition: global_functions.cc:34
static const double v
Definition: hatom_sf_dirac.cc:20
static double pow(const double *a, const double *b)
Definition: lda.h:74
General header file for using MADNESS.
#define MADNESS_EXCEPTION(msg, value)
Macro for throwing a MADNESS exception.
Definition: madness_exception.h:119
#define MADNESS_ASSERT(condition)
Assert a condition that should be free of side-effects since in release builds this might be a no-op.
Definition: madness_exception.h:134
std::vector< std::string > make_predefined_exop_strings(const std::string what)
Makes an excitation operator string based on predefined keywords.
Definition: GuessFactory.cc:252
vector_real_function_3d apply_polynomial_exop(vector_real_function_3d &vf, const std::string &exop_input, std::vector< coord_3d > centers, const bool &fence)
Definition: GuessFactory.cc:56
std::vector< Function< T, NDIM > > apply_trigonometric_exop(std::vector< Function< T, NDIM > > &vf, const std::string &exop_input, std::vector< coord_3d > centers, const bool &fence)
Definition: GuessFactory.h:182
std::vector< coord_3d > compute_centroids(const std::vector< Function< T, NDIM > > &vf)
Definition: GuessFactory.cc:27
File holds all helper structures necessary for the CC_Operator and CC2 class.
Definition: DFParameters.h:10
static const char * filename
Definition: legendre.cc:96
std::vector< Function< T, NDIM > > append(const std::vector< Function< T, NDIM > > &lhs, const std::vector< Function< T, NDIM > > &rhs)
combine two vectors
Definition: vmra.h:649
std::istream & position_stream(std::istream &f, const std::string &tag, bool rewind=true)
Definition: position_stream.cc:37
void truncate(World &world, response_space &v, double tol, bool fence)
Definition: basic_operators.cc:30
Function< T, NDIM > copy(const Function< T, NDIM > &f, const std::shared_ptr< WorldDCPmapInterface< Key< NDIM > > > &pmap, bool fence=true)
Create a new copy of the function with different distribution and optional fence.
Definition: mra.h:2002
std::vector< real_function_3d > vector_real_function_3d
Definition: functypedefs.h:79
int Level
Definition: key.h:55
void print(const T &t, const Ts &... ts)
Print items to std::cout (items separated by spaces) and terminate with a new line.
Definition: print.h:225
std::string atomic_number_to_symbol(const unsigned int atomic_number)
return the lower-case element symbol corresponding to the atomic number
Definition: atomutil.cc:188
void print_size(World &world, const std::vector< Function< T, NDIM > > &v, const std::string &msg="vectorfunction")
Definition: vmra.h:1622
NDIM & f
Definition: mra.h:2416
std::string type(const PairType &n)
Definition: PNOParameters.h:18
void normalize(World &world, std::vector< Function< T, NDIM > > &v, bool fence=true)
Normalizes a vector of functions — v[i] = v[i].scale(1.0/v[i].norm2())
Definition: vmra.h:1614
std::string name(const FuncType &type, const int ex=-1)
Definition: ccpairfunction.h:28
static const double c
Definition: relops.cc:10
static const long k
Definition: rk.cc:44
Timer structure.
Definition: PNOStructures.h:75
MyTimer stop()
Definition: PNOStructures.h:89
MyTimer start() const
Definition: PNOStructures.h:83
MyTimer print(const std::string &msg) const
Definition: PNOStructures.h:96
void e()
Definition: test_sig.cc:75
static const double ky
Definition: testcosine.cc:16
static const double kz
Definition: testcosine.cc:16
static const double kx
Definition: testcosine.cc:16