1 #include "converter.h"
2 #include "wf_writer.h"
3 #include "basis_writer.h"
4 #include "Pseudo_writer.h"
5 #include "elements.h"
6 #include <fstream>
7 #include <cstdio>
8 #include <cstdlib>
9 #include <cstring>
10 #include "vecmath.h"
11 #include "elements.h"
12 #include "sqd2qmc.h"
13 #include <libxml/parser.h>
14 #include <libxml/xmlmemory.h>
15 #include "hdf5.h"
16 
17 
18 
19 
20 using namespace std;
21 
usage(const char * name)22 void usage(const char * name) {
23   cout << "usage: " << name <<   " <options> <.out file> " << endl;
24   cout << "Where options can be: \n";
25   cout << "-o <string> Base name for your run case\n";
26   cout << "-debug to have more informative printout\n";
27   exit(1);
28 }
29 
30 
givesymmetry(int & i)31 string givesymmetry(int & i){
32   if(i==0)
33     return "S";
34   else if(i==1)
35     return "P";
36   else if(i==2)
37     return "5D";
38   else if(i==3)
39     return "7F";
40   else if(i==4)
41     return "9G";
42   else if(i==5)
43     return "11H";
44   else
45     return "what was that?";
46 }
47 
48 
read_parameter(xmlDocPtr doc,xmlNodePtr cur,group & g)49 void read_parameter(xmlDocPtr doc, xmlNodePtr cur, group  & g){
50   cur = cur->xmlChildrenNode;
51   while (cur != NULL) {
52     if ((!xmlStrcmp(cur->name, (const xmlChar *)"parameter"))){
53       if((!xmlStrcmp(xmlGetProp(cur, (const xmlChar *)"name"),(const xmlChar *)"charge"))){
54 	g.charge= atoi((const char*) xmlNodeListGetString(doc, cur->xmlChildrenNode, 1));
55 	//cout <<" charge "<<g.charge<<endl;
56       }
57 
58     }
59     cur = cur->next;
60   }
61 
62   return;
63 }
64 
65 
66 
67 
read_group(xmlDocPtr doc,xmlNodePtr cur,particleset & p)68 void read_group(xmlDocPtr doc, xmlNodePtr cur, particleset  & p){
69   cur = cur->xmlChildrenNode;
70   group tmpg;
71 
72   while (cur != NULL) {
73     if ((!xmlStrcmp(cur->name, (const xmlChar *)"group"))) {
74       //cout <<"found group "<<endl;
75       tmpg.name=((char *) xmlGetProp(cur, (const xmlChar *)"name"));\
76 
77       if(xmlGetProp(cur, (const xmlChar *)"size")!= NULL){
78 	tmpg.size=atoi((const char*)xmlGetProp(cur, (const xmlChar *)"size"));
79       }
80       read_parameter(doc,cur,tmpg);
81       p.g.push_back(tmpg);
82     }
83     cur = cur->next;
84   }
85 
86   return;
87 }
88 
89 
90 
91 
92 
read_particleset(xmlDocPtr doc,xmlNodePtr cur,xml_system_data & qmc_xml)93 void read_particleset(xmlDocPtr doc, xmlNodePtr cur, xml_system_data  & qmc_xml){
94   particleset ptmp;
95     if ((!xmlStrcmp(cur->name, (const xmlChar *)"particleset"))){
96       ptmp.name=((char *) xmlGetProp(cur, (const xmlChar *)"name"));
97       if(xmlGetProp(cur, (const xmlChar *)"size")!=NULL)
98 	ptmp.size=atoi((const char*)xmlGetProp(cur, (const xmlChar *)"size"));
99       read_group(doc,cur,ptmp);
100       qmc_xml.p.push_back(ptmp);
101     }
102 }
103 
104 
read_basisGroup(xmlDocPtr doc,xmlNodePtr cur,vector<basisGroup> & bg)105 void read_basisGroup(xmlDocPtr doc, xmlNodePtr cur, vector<basisGroup> & bg){
106   cur = cur->xmlChildrenNode;
107   while (cur != NULL) {
108     if ((!xmlStrcmp(cur->name, (const xmlChar *)"basisGroup"))){
109       basisGroup orbtmp;
110       //cout <<"found basisGroup "<<" for orbital "<<xmlGetProp(cur, (const xmlChar *)"rid") <<endl;
111       orbtmp.rid=(char *)xmlGetProp(cur, (const xmlChar *)"rid");
112       orbtmp.ds=(char *)xmlGetProp(cur, (const xmlChar *)"ds");
113       orbtmp.n=atoi((const char*)xmlGetProp(cur, (const xmlChar *)"n"));
114       orbtmp.m=atoi((const char*)xmlGetProp(cur, (const xmlChar *)"m"));
115       orbtmp.l=atoi((const char*)xmlGetProp(cur, (const xmlChar *)"l"));
116       orbtmp.s=atoi((const char*)xmlGetProp(cur, (const xmlChar *)"s"));
117       bg.push_back(orbtmp);
118      }
119     cur = cur->next;
120   }
121 }
122 
123 
124 
125 
read_atomicBasisSet(xmlDocPtr doc,xmlNodePtr cur,atomicBasisSet & abs)126 void read_atomicBasisSet(xmlDocPtr doc, xmlNodePtr cur, atomicBasisSet & abs){
127    cur = cur->xmlChildrenNode;
128    while (cur != NULL) {
129     if ((!xmlStrcmp(cur->name, (const xmlChar *)"atomicBasisSet"))){
130       //cout <<"found atomicBasisSet"<<endl;
131       abs.filename=(char *)xmlGetProp(cur, (const xmlChar *)"href");
132       abs.elementType=(char *)xmlGetProp(cur, (const xmlChar *)"elementType");
133       read_basisGroup(doc,cur,abs.orbs);
134     }
135     cur = cur->next;
136   }
137 }
138 
139 
140 
read_basisset(xmlDocPtr doc,xmlNodePtr cur,atomicBasisSet & abs)141 void read_basisset(xmlDocPtr doc, xmlNodePtr cur, atomicBasisSet & abs){
142   cur = cur->xmlChildrenNode;
143   while (cur != NULL) {
144     if ((!xmlStrcmp(cur->name, (const xmlChar *)"basisset"))){
145       //cout <<"found basisset"<<endl;
146       read_atomicBasisSet(doc,cur,abs);
147     }
148     cur = cur->next;
149   }
150 }
151 
152 
read_determinantset(xmlDocPtr doc,xmlNodePtr cur,wavefunction & wf)153 void read_determinantset(xmlDocPtr doc, xmlNodePtr cur, wavefunction  & wf){
154   cur = cur->xmlChildrenNode;
155   while (cur != NULL) {
156     // cout <<"cur->name "<<cur->name<<endl;
157      if ((!xmlStrcmp(cur->name, (const xmlChar *)"determinantset"))){
158        //cout <<"found determinantset of type "<< xmlGetProp(cur, (const xmlChar *)"type")
159        //    <<" for source "<<xmlGetProp(cur, (const xmlChar *)"source")<<endl;
160        read_basisset(doc,cur,wf.abs);
161      }
162      cur = cur->next;
163    }
164 }
165 
166 
read_wavefunction(xmlDocPtr doc,xmlNodePtr cur,wavefunction & wf)167 void read_wavefunction(xmlDocPtr doc, xmlNodePtr cur, wavefunction  & wf){
168   if ((!xmlStrcmp(cur->name, (const xmlChar *)"wavefunction"))){
169     //cout <<"found wavefunction of name "<< xmlGetProp(cur, (const xmlChar *)"name")<<endl;
170     read_determinantset(doc,cur,wf);
171   }
172 }
173 
174 
parse_from_xml(xmlDocPtr doc,xml_system_data & qmc_xml)175 static void parse_from_xml(xmlDocPtr doc, xml_system_data  & qmc_xml){
176   xmlNode *cur= NULL;
177   cur = xmlDocGetRootElement(doc);
178   if (xmlStrcmp(cur->name, (const xmlChar *) "qmcsystem")){
179     cout <<"This xml file does not contain qmcsystem!"<<endl; exit(-1);
180   }
181   cur = cur->xmlChildrenNode;
182   while (cur != NULL) {
183     read_particleset(doc,cur,qmc_xml);
184     read_wavefunction(doc,cur,qmc_xml.wf);
185     cur = cur->next;
186   }
187 
188 }
189 
read_into_array(hid_t grp,const char * name,vector<double> & ref)190 void read_into_array(hid_t grp, const char* name, vector <double> & ref){
191   // Turn off error printing
192   //H5E_auto_t func;
193   //void *client_data;
194   //H5Eget_auto (&func, &client_data);
195   //H5Eset_auto (NULL, NULL);
196 
197   hid_t dataset = H5Dopen(grp,name,H5P_DEFAULT);
198   if (dataset > -1) {
199     hid_t datatype=H5Dget_type(dataset);
200     hsize_t dim_out,dims_out;
201 
202     hid_t dataspace = H5Dget_space(dataset);
203     hid_t status = H5Sget_simple_extent_dims(dataspace, &dim_out, NULL);
204     H5Sclose(dataspace);
205     dims_out=H5Tget_size(datatype);
206     //  cout <<" dim_out "<<dim_out<<" dims_out"<<dims_out<<endl;
207     ref.resize(dim_out);
208     hid_t ret = H5Dread(dataset, datatype, H5S_ALL, H5S_ALL, H5P_DEFAULT,&(ref[0]));
209     H5Tclose(datatype);
210     H5Dclose(dataset);
211   }
212   // Turn error printing back on
213   //H5Eset_auto (func, client_data);
214 }
215 
read_into_array(hid_t grp,const char * name,vector<int> & ref)216 void read_into_array(hid_t grp, const char* name, vector <int> & ref){
217   // Turn off error printing
218   //H5E_auto_t func;
219   //void *client_data;
220   //H5Eget_auto (&func, &client_data);
221   //H5Eset_auto (NULL, NULL);
222 
223   hid_t dataset = H5Dopen(grp,name,H5P_DEFAULT);
224   if (dataset > -1) {
225     hid_t datatype=H5Dget_type(dataset);
226     hsize_t dim_out,dims_out;
227 
228     hid_t dataspace = H5Dget_space(dataset);
229     hid_t status = H5Sget_simple_extent_dims(dataspace, &dim_out, NULL);
230     H5Sclose(dataspace);
231     dims_out=H5Tget_size(datatype);
232     //  cout <<" dim_out "<<dim_out<<" dims_out"<<dims_out<<endl;
233     ref.resize(dim_out);
234     hid_t ret = H5Dread(dataset, datatype, H5S_ALL, H5S_ALL, H5P_DEFAULT,&(ref[0]));
235     H5Tclose(datatype);
236     H5Dclose(dataset);
237   }
238   // Turn error printing back on
239   //H5Eset_auto (func, client_data);
240 }
241 
242 
read_into_array(hid_t grp,const char * name,double & value)243 void read_into_array(hid_t grp, const char* name, double & value){
244   // Turn off error printing
245   //H5E_auto_t func;
246   //void *client_data;
247   //H5Eget_auto (&func, &client_data);
248   //H5Eset_auto (NULL, NULL);
249 
250   hid_t dataset = H5Dopen(grp,name,H5P_DEFAULT);
251   if (dataset > -1) {
252     hid_t datatype=H5Dget_type(dataset);
253     hsize_t dim_out,dims_out;
254 
255     hid_t dataspace = H5Dget_space(dataset);
256     hid_t status = H5Sget_simple_extent_dims(dataspace, &dim_out, NULL);
257     H5Sclose(dataspace);
258 
259     //dims_out=H5Tget_size(datatype);
260     //  cout <<" dim_out "<<dim_out<<" dims_out"<<dims_out<<endl;
261     vector <double> ref;
262     ref.resize(dim_out);
263     hid_t ret = H5Dread(dataset, datatype, H5S_ALL, H5S_ALL, H5P_DEFAULT,&(ref[0]));
264     value=ref[0];
265     H5Tclose(datatype);
266     H5Dclose(dataset);
267   }
268   // Turn error printing back on
269   //H5Eset_auto (func, client_data);
270 }
271 
272 
read_into_array(hid_t grp,const char * name,int & value)273 void read_into_array(hid_t grp, const char* name, int & value){
274   // Turn off error printing
275   //H5E_auto_t func;
276   //void *client_data;
277   //H5Eget_auto (&func, &client_data);
278   //H5Eset_auto (NULL, NULL);
279 
280   hid_t dataset = H5Dopen(grp,name,H5P_DEFAULT);
281   if (dataset > -1) {
282     hid_t datatype=H5Dget_type(dataset);
283     hsize_t dim_out,dims_out;
284 
285     hid_t dataspace = H5Dget_space(dataset);
286     hid_t status = H5Sget_simple_extent_dims(dataspace, &dim_out, NULL);
287     H5Sclose(dataspace);
288 
289     //dims_out=H5Tget_size(datatype);
290     //  cout <<" dim_out "<<dim_out<<" dims_out"<<dims_out<<endl;
291     vector <int> ref;
292     ref.resize(dim_out);
293     hid_t ret = H5Dread(dataset, datatype, H5S_ALL, H5S_ALL, H5P_DEFAULT,&(ref[0]));
294     value=ref[0];
295     H5Tclose(datatype);
296     H5Dclose(dataset);
297   }
298   // Turn error printing back on
299   //H5Eset_auto (func, client_data);
300 }
301 
302 
splinefit(vector<double> & x,vector<double> & y,double yp1,double ypn,vector<vector<double>> & coef,vector<double> & pos)303 void splinefit(vector <double>& x, vector <double> & y, double yp1, double ypn,
304 	       vector < vector <double> > & coef, vector <double> & pos)
305 {
306 
307   //following stolen from Numerical Recipes, more or less
308   int n=x.size();
309   pos.resize(n);
310   pos=x;
311 
312   vector <double> y2(n), u(n);
313   double sig, p, qn, un, hi;
314 
315   if(yp1 > .99e30) {
316     y2[0]=0;
317     u[0]=0;
318   }
319   else {
320     y2[0]=-.5;
321     u[0]=(3./(x[1]-x[0]))*((y[1]-y[0])/(x[1]-x[0])-yp1);
322   }
323 
324   for(int i=1; i<n-1; i++)
325   {
326     sig=(x[i]-x[i-1])/(x[i+1]-x[i-1]);
327     p=sig*y2[i-1]+2.;
328     y2[i]=(sig-1.)/p;
329     u[i]=(6.*((y[i+1]-y[i])/(x[i+1]-x[i])
330               -(y[i]-y[i-1])/(x[i]-x[i-1]))
331           /(x[i+1]-x[i-1])-sig*u[i-1])/p;
332   }
333 
334   if(ypn>.99e30)
335   {
336     qn=0;
337     un=0;
338   }
339   else
340   {
341     qn=.5;
342     un=(3./(x[n-1]-x[n-2]))*(ypn-(y[n-1]-y[n-2])/(x[n-1]-x[n-2]));
343   }
344 
345   y2[n-1]=(un-qn*u[n-2])/(qn*y2[n-2]+1.);
346 
347   for(int k=n-2; k>=0; k--)
348   {
349     y2[k]=y2[k]*y2[k+1]+u[k];
350   }
351 
352   for(int i=0; i<n-1; i++)
353   {
354     vector <double> coeff_tmp(4);
355     coeff_tmp[0]=y[i];
356     hi=x[i+1]-x[i];
357     coeff_tmp[1]=(y[i+1]-y[i])/hi - hi*(2.*y2[i]+y2[i+1])/6.;
358     coeff_tmp[2]=y2[i]/2.;
359     coeff_tmp[3]=(y2[i+1]-y2[i])/(6.*hi);
360     coef.push_back(coeff_tmp);
361   }
362   vector <double> coeff_tmp(4);
363   coeff_tmp[0]=y[n-1];
364   coeff_tmp[1]=0;
365   coeff_tmp[2]=0;
366   coeff_tmp[3]=0;
367   coef.push_back(coeff_tmp);
368 
369 }
370 
371 
getInterval(double r,vector<double> & pos)372 int getInterval(double r, vector <double>& pos ) {
373   int npts=pos.size();
374   int upper=npts;
375   int lower=0;
376   int guess=int(npts/2);
377 
378   //cout << "r= " << r << endl;
379 
380   while(true) {
381     if(r >= pos[guess]) {
382       //cout << "greater " << pos(guess) << endl;
383       if(r < pos[guess+1] ) {
384         //cout << "found it " << pos(guess+1) << endl;
385         return guess;
386       }
387       else {
388         lower=guess;
389         guess=(lower+upper)/2;
390         //cout << "didn't find it: new lower " << lower
391         //     << " new guess " << guess << endl;
392       }
393     }
394     if(r < pos[guess] ) {
395       upper=guess;
396       guess=(lower+upper)/2;
397       //cout << "less than " << pos(guess) << endl;
398       //cout << "new upper " << upper
399       //     << " new guess " << guess << endl;
400     }
401   }
402 
403 }
404 
405 
406 
getVal(double r,vector<double> & pos,vector<vector<double>> & coeff)407 double getVal(double r, vector <double>& pos, vector < vector <double> > & coeff) {
408   int i=getInterval(r,pos);
409   double height=r-pos[i];
410   return coeff[i][0]+height*(coeff[i][1]
411 			     +height*(coeff[i][2]
412 				      +height*coeff[i][3]));
413 }
414 
415 
416 
find_cuttoff(vector<double> & pos,vector<vector<double>> & coeff)417 double find_cuttoff(vector <double>& pos, vector < vector <double> > & coeff){
418   const double cutoff_threshold=1e-12;
419   for(int i=pos.size()-1; i >=0; i--) {
420 
421     if(fabs(coeff[i][0]) > cutoff_threshold) {
422       //cout << "cutoff " << x+step << endl;
423       if(i<pos.size()-1){
424 	coeff.resize(i+1);
425 	pos.resize(i+1);
426 	return pos[i+1];
427       }
428       else
429 	return pos[i];
430     }
431   }
432   return 0;
433 }
434 
435 
extrapolate_to_linear_grid(vector<double> & x,vector<double> & y,double & cusp,double & dismin2)436 void extrapolate_to_linear_grid(vector <double> & x, vector <double> & y , double & cusp, double & dismin2){
437   //copy the arrays
438 
439   vector <double> xtmp;
440   vector <double> ytmp;
441 
442   double xxi=-1;
443   double dismin=0.001;
444   if(dismin2<dismin){
445     cout <<"extrapolate_to_linear_grid: using to fine grid "<<endl;
446   }
447 
448 
449   for(int i=0;i<y.size();i++){
450     if(((x[i]-xxi) > dismin) && x[i] > 1.0e-4){
451       xtmp.push_back(x[i]);
452       ytmp.push_back(y[i]);
453       xxi=x[i];
454     }
455   }
456 
457   int ne=2;
458   double yp1=(ytmp[ne]-ytmp[0])/(xtmp[ne]-xtmp[1]);
459   double y0=ytmp[1]+(0.-xtmp[0])*yp1;
460   //cout <<" "<<ytmp[0]<<" "<<ytmp[ne]<<" "<<xtmp[0]<<" "<<xtmp[ne]<<endl;;
461   cout <<" derivative at the origin "<<yp1<<" yp1/y "<<yp1/ytmp[1]<<endl;
462 
463   double ypn=(ytmp[xtmp.size()-1] - ytmp[xtmp.size()-3])/(xtmp[xtmp.size()-1]-xtmp[xtmp.size()-3]);
464   //cout <<" derivative at the end " <<ypn<< " ypn/yn "<<ypn/ytmp[xtmp.size()-2]<<endl;
465 
466   vector < vector <double> > coef;
467   vector <double> pos;
468 
469   //fix the derivatives and values at the origin
470   yp1=cusp*ytmp[0];
471   //ypn=0.0
472   ytmp[0]=y0;
473   xtmp[0]=0.0;
474 
475 
476   splinefit(xtmp, ytmp, yp1, ypn, coef, pos);
477   double cutoff=find_cuttoff(pos,coef);
478   if(cutoff<x[x.size()-1])
479     cout <<" found cutoff "<<cutoff<<endl;
480 
481   //double dismin2=0.01;
482   double xmin=0.0;
483   double xmax=pos[pos.size()-1];
484   x.resize(0);
485   y.resize(0);
486   double r=xmin;
487   while(r<=xmax){
488     x.push_back(r);
489     y.push_back(getVal(r,pos,coef));
490     r+=dismin2;
491   }
492 
493   return;
494 }
495 
496 
497 
498 
writebasis(ostream & os,xml_system_data & qmc_xml,string output,string offset)499 void writebasis(ostream & os, xml_system_data  & qmc_xml, string output, string offset){
500   /*
501   cout <<"found the following atomicBasisSet: "<<endl;
502   for (int i=0; i<qmc_xml.wf.abs.orbs.size();i++){
503     cout <<qmc_xml.wf.abs.orbs[i].rid<<" "<<qmc_xml.wf.abs.orbs[i].ds<<" "
504 	 <<qmc_xml.wf.abs.orbs[i].n<<" "
505 	 <<qmc_xml.wf.abs.orbs[i].l<<" "
506 	 <<qmc_xml.wf.abs.orbs[i].m<<endl;
507   }
508   */
509 
510   hid_t   file1;
511   herr_t ret;
512 
513 
514   string filename=qmc_xml.wf.abs.filename;
515   cout <<" Using orbitals from file "<<filename<<endl;
516   file1 = H5Fopen (qmc_xml.wf.abs.filename, H5F_ACC_RDWR, H5P_DEFAULT);
517 
518   double rf,ri, log_mesh_spacing;
519   int npts;
520 
521   char grid_type[256];
522   hid_t dataset = H5Dopen(file1, "radial_basis_states/grid/type",H5P_DEFAULT);
523   hid_t datatype=H5Dget_type(dataset);
524   ret = H5Dread(dataset, datatype, H5S_ALL, H5S_ALL, H5P_DEFAULT, grid_type);
525   H5Tclose(datatype);
526   H5Dclose(dataset);
527 
528   if(strcmp(grid_type,"log")!=0){
529     cout <<" only log mesh is supported for now, exit"; exit(-1);
530   }
531 
532   read_into_array(file1,"radial_basis_states/grid/ri",ri);
533   read_into_array(file1,"radial_basis_states/grid/rf",rf);
534   read_into_array(file1,"radial_basis_states/grid/npts",npts);
535 
536   //cout <<" ri "<<ri<<" rf "<<rf<<" npts "<<npts<<endl;
537 
538   log_mesh_spacing=exp(log(rf/ri)/(npts-1));
539   /*
540   cout.precision(20);
541   cout.width(25);
542   cout <<" calculated log_mesh_spacing "<<log_mesh_spacing<<endl;
543   */
544   vector <double> r_log;
545   double x=ri;
546   for(int j=0;j<npts;j++){
547     r_log.push_back(x);
548     x*=log_mesh_spacing;
549   }
550 
551 
552   vector <radial_orbital> rad_orb_log_mesh;
553 
554 
555   char * unique_rid="";
556   vector <string> unique_rid_orb_name;
557 
558   for (int i=0; i<qmc_xml.wf.abs.orbs.size();i++){
559     if(strcmp(unique_rid,qmc_xml.wf.abs.orbs[i].ds)) {
560       radial_orbital orbtmp;
561       os << offset<<offset<<"SPLINE { "<<endl;
562       os << offset<<offset<<offset<<givesymmetry(qmc_xml.wf.abs.orbs[i].l)<<endl;
563       os << offset<<offset<<offset<<"INCLUDE "<<output<<"."<<qmc_xml.wf.abs.orbs[i].ds<<endl;
564       os << offset<<offset<<"       } "<<endl;
565       unique_rid=qmc_xml.wf.abs.orbs[i].ds;
566       orbtmp.orb_name=unique_rid;
567       string tmps=unique_rid;
568       string nameofthedataset;
569 
570       cout <<" reading data for spline "<<unique_rid<< " of "<< givesymmetry(qmc_xml.wf.abs.orbs[i].l)<<" symmetry"<<endl;
571 
572       nameofthedataset="radial_basis_states/"+tmps+"/eigenvalue";
573       //cout <<" reading in to array dataset "<<nameofthedataset<<endl;
574       read_into_array(file1,nameofthedataset.c_str(),orbtmp.eigenvalue);
575 
576       nameofthedataset="radial_basis_states/"+tmps+"/power";
577       //cout <<" reading in to array dataset "<<nameofthedataset<<endl;
578       read_into_array(file1,nameofthedataset.c_str(),orbtmp.power);
579 
580       nameofthedataset="radial_basis_states/"+tmps+"/quantum_numbers";
581       //cout <<" reading in to array dataset "<<nameofthedataset<<endl;
582       read_into_array(file1,nameofthedataset.c_str(),orbtmp.quantum_numbers);
583 
584       nameofthedataset="radial_basis_states/"+tmps+"/radial_orbital";
585       //cout <<" reading in to array dataset "<<nameofthedataset<<endl;
586       read_into_array(file1,nameofthedataset.c_str(),orbtmp.radial_part);
587 
588       nameofthedataset="radial_basis_states/"+tmps+"/uofr";
589       //cout <<" reading in to array dataset "<<nameofthedataset<<endl;
590       read_into_array(file1,nameofthedataset.c_str(),orbtmp.uofr);
591 
592       orbtmp.r_log=r_log;
593 
594       rad_orb_log_mesh.push_back(orbtmp);
595     }
596   }
597   //cout <<"closing file"<<endl;
598   ret = H5Fclose (file1);
599 
600 
601 
602 
603   double cusp=0.0;
604 
605 
606 
607   //print the orbitals to files
608   for(int i=0;i<rad_orb_log_mesh.size();i++){
609     //ofstream orbout(rad_orb_log_mesh[i].orb_name.c_str());
610      FILE * orbout;
611      string filename=output+"."+rad_orb_log_mesh[i].orb_name;
612      orbout = fopen (filename.c_str(),"w");
613      fprintf (orbout,"# eigenvalue %g \n",rad_orb_log_mesh[i].eigenvalue);
614      fprintf (orbout,"# power %d \n", rad_orb_log_mesh[i].power);
615      fprintf (orbout,"# n %d l %d \n",rad_orb_log_mesh[i].quantum_numbers[0], rad_orb_log_mesh[i].quantum_numbers[1]);
616 
617      /*
618      orbout<<"# eigenvalue "<<rad_orb_log_mesh[i].eigenvalue<<endl;
619      orbout<<"# power "<<rad_orb_log_mesh[i].power<<endl;
620      orbout<<"# n, l "<<rad_orb_log_mesh[i].quantum_numbers[0]<<" "<<rad_orb_log_mesh[i].quantum_numbers[1]<<endl;
621      */
622 
623 
624 
625      double spacing=0.005;
626      extrapolate_to_linear_grid(rad_orb_log_mesh[i].r_log, rad_orb_log_mesh[i].radial_part,cusp, spacing);
627 
628      //orbout.precision(16);
629      //orbout.width(20);
630      for(int j=0;j<rad_orb_log_mesh[i].radial_part.size();j++){
631        fprintf (orbout, "%15.12e %15.12e \n",rad_orb_log_mesh[i].r_log[j],rad_orb_log_mesh[i].radial_part[j]);
632        //orbout << rad_orb_log_mesh[i].r_log[j] <<" "<<rad_orb_log_mesh[i].radial_part[j]<<endl;
633      }
634      fclose (orbout);
635      //orbout.close();
636   }
637 }
638 
639 
getoccupations(xml_system_data & qmc_xml,std::string & calculatetype,vector<int> & occ_up,vector<int> & occ_down)640 void getoccupations(xml_system_data  & qmc_xml, std::string & calculatetype, vector <int> & occ_up, vector <int> & occ_down){
641   char * unique_rid="";
642   int n,l,m,s, nold, lold, mold, sold;
643   int nup=0;
644   int ndown=0;
645   vector < vector <int> > unique;
646   vector <int>  tmp;
647 
648   for (int i=0; i<qmc_xml.wf.abs.orbs.size();i++){
649     s=qmc_xml.wf.abs.orbs[i].s;
650     l=qmc_xml.wf.abs.orbs[i].l;
651     if(s>0)
652       nup++;
653     else
654       ndown++;
655 
656     if(strcmp(unique_rid,qmc_xml.wf.abs.orbs[i].ds)) {
657       unique_rid=qmc_xml.wf.abs.orbs[i].ds;
658       tmp.push_back(i);
659       for (int j=i+1; j<qmc_xml.wf.abs.orbs.size();j++){
660 	if(!strcmp(unique_rid,qmc_xml.wf.abs.orbs[j].ds)){
661 	  tmp.push_back(j);
662 	}
663       }
664       unique.push_back(tmp);
665       tmp.resize(0);
666     }
667   }
668 
669   // for(int i=0;i<unique.size();i++){
670   //  for(int j=0;j<unique[i].size();j++){
671   //   int k=unique[i][j];
672   //   cout << qmc_xml.wf.abs.orbs[k].ds <<" "<<qmc_xml.wf.abs.orbs[k].n<<" "<<qmc_xml.wf.abs.orbs[k].l<<" "<<qmc_xml.wf.abs.orbs[k].m<<" "<<qmc_xml.wf.abs.orbs[k].s<<endl;
673   // }
674   // cout <<endl;
675   //}
676 
677   //vector <int> occ_up;
678   //vector <int> occ_down;
679   int all_same=1;
680 
681   int occ=0;
682   for(int i=0;i<unique.size();i++){
683     int ss=qmc_xml.wf.abs.orbs[unique[i][0]].s;
684     int j=1;
685     while (j<unique[i].size() && all_same){
686       s=qmc_xml.wf.abs.orbs[unique[i][j]].s;
687       if(s!=ss)
688 	all_same=0;
689       j++;
690     }
691   }
692   //string calculatetype;
693   if (all_same){ //UHF
694     calculatetype="UHF";
695     int occ=1;
696     for(int i=0;i<unique.size();i++){
697       int ll=qmc_xml.wf.abs.orbs[unique[i][0]].l;
698       for(int j=0;j<unique[i].size();j++){
699 	int s=qmc_xml.wf.abs.orbs[unique[i][j]].s;
700 	if(s>0)
701 	  occ_up.push_back(occ++);
702 	else
703 	  occ_down.push_back(occ++);
704       }
705       occ+=(2*ll+1-unique[i].size());
706     }
707   }
708   else { //RHF or ROHF
709     int occup=1;
710     int occdown=1;
711     for(int i=0;i<unique.size();i++){
712       int ll=qmc_xml.wf.abs.orbs[unique[i][0]].l;
713       int tmpup=0;
714       int tmpdown=0;
715       for(int j=0;j<unique[i].size();j++){
716 	int s=qmc_xml.wf.abs.orbs[unique[i][j]].s;
717 	if(s>0){
718 	  occ_up.push_back(occup++);
719 	  tmpup++;
720 	}
721 	else{
722 	  occ_down.push_back(occdown++);
723 	  tmpdown++;
724 	}
725       }
726       occup+=(2*ll+1-tmpup);
727       occdown+=(2*ll+1-tmpdown);
728     }
729     if(occ_up.size()==occ_down.size())
730       calculatetype="RHF";
731     else
732       calculatetype="ROHF";
733   }
734 
735 
736   /*
737   for (int i=0;i<occ_up.size();i++)
738     cout <<occ_up[i]+1<<" ";
739   cout <<endl;
740 
741   for (int i=0;i<occ_down.size();i++)
742     cout <<occ_down[i]+1<<" ";
743   cout <<endl;
744   */
745 
746 }
747 
748 
749 
read_grid(xmlNodePtr cur,vector<double> & pos)750 void read_grid(xmlNodePtr cur, vector <double> & pos){
751   int npts= atoi((const char*) xmlGetProp(cur, (const xmlChar *)"npts"));
752   pos.resize(npts);
753   //cout <<"npts "<< npts<<endl;
754   xmlNodePtr cur1=cur->children;
755   while(cur1 != NULL)
756     {
757       string cname((const char*)cur1->name);
758       pos.resize(npts);
759       if(cname == "data"){
760 	putContent(pos,cur1);
761       }
762       cur1 = cur1->next;
763     }
764 }
765 
read_semilocal(xmlNodePtr cur,vector<double> & pos,double & cutoff)766 void read_semilocal(xmlNodePtr cur, vector <double> & pos, double & cutoff){
767   if(xmlStrcmp(xmlGetProp(cur, (const xmlChar *)"format"),(const xmlChar *)"r*V")){
768     cout <<"expected format in the semilocal should be r*V, exit"<<endl;exit(-1);
769   }
770   xmlNodePtr cur1=cur->children;
771   while(cur1 != NULL)
772     {
773       if(!xmlStrcmp(cur1->name, (const xmlChar *) "vps")){
774 	cutoff=atof((const char*) xmlGetProp(cur1, (const xmlChar *)"cutoff"));
775 	string symmetry=((const char*) xmlGetProp(cur1, (const xmlChar *)"l"));
776 	cout <<" cutoff "<<cutoff<<" L "<<symmetry<<endl;
777 	if(symmetry!="s"){
778 	  cout <<"need L=s in the semilocal section"<<endl; exit(-1);
779 	}
780 	xmlNodePtr cur2=cur1->children;
781 	while(cur2 != NULL)
782 	  {
783 	    xmlNodePtr cur3=cur2->children;
784 
785 	    while(cur3 != NULL)
786 	      {
787 		string cname((const char*)cur3->name);
788 		if(cname == "data"){
789 		  putContent(pos,cur3);
790 		}
791 		cur3 = cur3->next;
792 	      }
793 	    cur2 = cur2->next;
794 	  }
795       }
796       cur1 = cur1->next;
797     }
798 
799 
800 
801 
802 
803 }
804 
805 
806 
807 
get_pseudo_from_the_grid(string filename,Spline_pseudo_writer & pseudo)808 void get_pseudo_from_the_grid(string filename, Spline_pseudo_writer & pseudo){
809 
810   cout <<endl;
811   cout <<" Getting the external potential from the grid"<<endl;
812 
813   pseudo.psp_pos.resize(1);
814   pseudo.psp_val.resize(1);
815   double cutoff;
816 
817   //default values
818   /*
819   pseudo.psp_pos[0].resize(1);
820   pseudo.psp_pos[0][0]=0.0;
821   pseudo.psp_val[0].resize(1);
822   pseudo.psp_val[0][0]=0.0;
823   */
824 
825   // build an XML tree from a the file;
826   xmlDoc *doc = NULL;
827 
828   // LIBXML_TEST_VERSION
829   const char *psysfilename;
830   psysfilename=filename.c_str();
831   if ((doc = xmlReadFile(psysfilename, NULL, 0)) == NULL){
832     printf("error: could not parse file %s\n", psysfilename);
833     exit(-1);
834   }
835   else{
836     cout <<" Vext filename is "<<filename<<endl;
837   }
838 
839 
840   xmlNode *cur= NULL;
841   cur = xmlDocGetRootElement(doc);
842   if (xmlStrcmp(cur->name, (const xmlChar *) "pseudo")){
843     cout <<"The "<<filename<<" of type xml does not contain pseudo system!"<<endl; exit(-1);
844   }
845   cur = cur->xmlChildrenNode;
846   while (cur != NULL) {
847     //cout <<"cur->name "<<cur->name<<endl;
848     if(!xmlStrcmp(cur->name, (const xmlChar *) "grid")){
849       read_grid(cur, pseudo.psp_pos[0]);
850       pseudo.psp_val[0].resize(pseudo.psp_pos[0].size());
851     }
852     if(!xmlStrcmp(cur->name, (const xmlChar *) "semilocal"))
853       read_semilocal(cur, pseudo.psp_val[0], cutoff);
854     cur = cur->next;
855   }
856   xmlFreeDoc(doc);
857   xmlCleanupParser();
858 
859 
860   //adjust for V*r format
861   for(int i=0;i<pseudo.psp_val[0].size();i++)
862     pseudo.psp_val[0][i]/=pseudo.psp_pos[0][i];
863 
864 
865   //extrapolate
866   double cusp=0.0;
867   double spacing=0.02;
868   extrapolate_to_linear_grid(pseudo.psp_pos[0], pseudo.psp_val[0],cusp,spacing);
869 
870 
871 
872   //apply cuttoff
873   int i=0;
874   while(pseudo.psp_pos[0][i]<= cutoff){
875     //cout <<" pseudo.psp_pos[0][i] "<<pseudo.psp_pos[0][i]<<endl;
876     i++;
877   }
878   if(i+1<pseudo.psp_pos[0].size()){
879     pseudo.psp_pos[0].resize(i+1);
880     pseudo.psp_val[0].resize(i+1);
881   }
882 
883   //cout <<"i "<<i<<" pos "<<pseudo.psp_pos[0][i-1]<<" val "<<pseudo.psp_val[0][i-1]<<endl;
884 }
885 
886 
887 
888 
main(int argc,char ** argv)889 int main(int argc, char ** argv) {
890   string infilename, outputname;
891   int debug=0;
892 
893   if(argc >= 2) {
894     infilename=argv[argc-1];
895   }
896   else { usage(argv[0]); }
897 
898   for(int i=1; i< argc-1; i++) {
899     if(!strcmp(argv[i], "-o") && argc > i+1) {
900       outputname=argv[++i];
901     }
902     else if(!strcmp(argv[i], "-debug")) {
903      debug=1;
904     }
905     else {
906       usage(argv[0]);
907     }
908   }
909 
910 
911   if(outputname == "") {
912     outputname=infilename;
913   }
914 
915   vector <Spline_pseudo_writer> pseudo;
916   vector <Atom> atoms;
917   Slat_wf_writer slwriter;
918   vector <double> occupation; //electronic occupation
919 
920   string sysfilename=infilename+".qmc.xml";
921   string vextonthegrid=infilename+".Vext.xml";
922   string wffilename=infilename+".orb.dat";
923 
924 
925   //
926   int norbs;
927   int nelectrons=0;
928   int nup;
929   int ndown;
930   int basissize=0;
931   int natoms;
932   int total_charge=0;
933   int found_sqd=0;
934   double eref;
935 
936 
937   // build an XML tree from a the file;
938   xmlDoc *doc = NULL;
939 
940   // LIBXML_TEST_VERSION
941   const char *psysfilename;
942   psysfilename=sysfilename.c_str();
943   if ((doc = xmlReadFile(psysfilename, NULL, 0)) == NULL){
944     printf("error: could not parse file %s\n", psysfilename);
945     exit(-1);
946   }
947   else{
948     cout <<"######### SQD2QMC by M.B. ########"<<endl;
949     cout <<" converting SQD (qmcpack format) file "<<sysfilename<<" to QWalk format"<<endl;
950     cout <<endl;
951 
952   }
953 
954   xml_system_data  qmc_xml;
955   parse_from_xml(doc,qmc_xml);
956   xmlFreeDoc(doc);
957   xmlCleanupParser();
958 
959   //cout<<" stored charge variable "<<qmc_xml.p[0].g[0].charge<<endl;
960 
961 
962   for(int i=0;i<qmc_xml.p.size();i++){
963     //cout << " p.name "<< qmc_xml.p[i].name<<" p.size "<< qmc_xml.p[i].size <<endl;
964     for(int j=0;j<qmc_xml.p[i].g.size();j++){
965       //cout << " g.name "<< qmc_xml.p[i].g[j].name<<" g.size "
966       //   << qmc_xml.p[i].g[j].size <<" charge "<<qmc_xml.p[i].g[j].charge<<endl;
967 
968       if(strcmp(qmc_xml.p[i].g[j].name,"sqd")==0 && found_sqd==0){
969 	found_sqd=1;
970 	if(qmc_xml.p[i].size!=1)
971 	  {cout <<"sqd should have only one center, exit"<<endl; exit(-1);}
972 	natoms=1;
973 	Atom tmpatom;
974 	tmpatom.pos[0]=tmpatom.pos[1]=tmpatom.pos[2]=0.0;
975 	tmpatom.charge=qmc_xml.p[i].g[0].charge;
976 	tmpatom.name=outputname;
977 	tmpatom.name+="_origin";
978 	pseudo.resize(1);
979 	pseudo[0].label=tmpatom.name;
980 	atoms.push_back(tmpatom);
981 	slwriter.write_centers=false;
982 	slwriter.use_global_centers=false;
983       }
984       if(qmc_xml.p[i].g[j].size>0)
985 	total_charge+=qmc_xml.p[i].g[j].charge*qmc_xml.p[i].g[j].size;
986       else
987 	total_charge+=qmc_xml.p[i].g[j].charge;
988       if(strcmp(qmc_xml.p[i].name,"e")==0)
989 	if(strcmp(qmc_xml.p[i].g[j].name,"u")==0)
990 	  nup=qmc_xml.p[i].g[j].size;
991 	else if (strcmp(qmc_xml.p[i].g[j].name,"d")==0)
992 	  ndown=qmc_xml.p[i].g[j].size;
993     }
994   }
995 
996 
997   if(!found_sqd)
998     {cout <<"did not find sqd system, exit"<<endl; exit(-1);}
999 
1000   nelectrons=nup+ndown;
1001 
1002   cout <<" Sqd charge "<<atoms[0].charge<<", number of up e- "<<nup<<" and down e- "<<ndown
1003        <<" so the total charge is "<<total_charge<<endl;
1004 
1005   slwriter.mo_matrix_type="BASFUNC_MO";
1006   slwriter.magnification=1.0;
1007   slwriter.nup=nup;
1008   slwriter.ndown=ndown;
1009 
1010 
1011 
1012   vector < vector <int> > occ_up(1);
1013   vector < vector <int> > occ_down(1);
1014 
1015 
1016   string basisoutname=outputname+".basis";
1017   slwriter.basisname=basisoutname;
1018 
1019   ofstream basisout(basisoutname.c_str());
1020   string offset="  ";
1021   basisout<< "BASIS { "<<endl;
1022   basisout<< offset<<atoms[0].name <<endl;
1023   basisout<< offset<<"AOSPLINE" <<endl;
1024   basisout<< offset<<"NORENORMALIZE"<<endl;
1025   basisout<< offset<<"CUSP 0.0" <<endl;
1026   writebasis(basisout, qmc_xml, outputname, offset);
1027   basisout<< "      } "<<endl;
1028   basisout.close();
1029 
1030 
1031 
1032 
1033 
1034   //get the pseudopotential
1035   get_pseudo_from_the_grid(vextonthegrid,pseudo[0]);
1036 
1037   string slateroutname=outputname+".slater";
1038   getoccupations(qmc_xml,slwriter.calctype, occ_up[0], occ_down[0]);
1039 
1040 
1041   double weight=1.0;
1042   slwriter.detwt.push_back(weight);
1043   slwriter.occ_up.push_back(occ_up[0]);
1044   slwriter.occ_down.push_back(occ_down[0]);
1045 
1046   int largest_orb=0;
1047   cout <<" Spin-up occupation "<<endl;
1048   for (int i=0;i<occ_up[0].size();i++){
1049     cout <<"  "<<occ_up[0][i]<<" ";
1050     if(occ_up[0][i]> largest_orb)
1051       largest_orb=occ_up[0][i];
1052   }
1053   cout <<endl;
1054 
1055    cout <<" Spin-down occupation "<<endl;
1056   for (int i=0;i<occ_down[0].size();i++){
1057     cout <<"  "<<occ_down[0][i]<<" ";
1058     if(occ_down[0][i]> largest_orb)
1059       largest_orb=occ_down[0][i];
1060   }
1061   cout <<endl;
1062 
1063   if(slwriter.calctype=="UHF"){
1064     slwriter.spin_dwn_start=largest_orb-occ_down[0].size();
1065   }
1066 
1067   //all the outputs will be writen here
1068   string orboutname=outputname+".orb";
1069   slwriter.orbname=orboutname;
1070 
1071   ofstream orbout(orboutname.c_str());
1072   for(int orbs=0;orbs<largest_orb;orbs++)
1073     orbout<<orbs+1<<"  1"<<endl;
1074   orbout.close();
1075 
1076 
1077   ofstream slaterout(slateroutname.c_str());
1078   slwriter.print_wavefunction(slaterout);
1079   slaterout.close();
1080 
1081 
1082   //--------------------------Jastrow 2 output
1083 
1084 
1085 
1086   string jast2outname=outputname+".jast2";
1087   Jastrow2_wf_writer jast2writer;
1088   jast2writer.set_atoms(atoms);
1089 
1090   double basis_cutoff=7.5; //arbitrary cutoff
1091   ofstream jast2out(jast2outname.c_str());
1092   print_std_jastrow2(jast2writer, jast2out, basis_cutoff);
1093   jast2out.close();
1094 
1095 
1096   //-----------------------------------System output
1097 
1098   string sysoutname=outputname+".sys";
1099   ofstream sysout(sysoutname.c_str());
1100   sysout << "SYSTEM { MOLECULE \n";
1101   sysout << "  NSPIN { " << slwriter.nup << "  "
1102 	 << slwriter.ndown << " } \n";
1103 
1104 
1105   for(vector <Atom>::iterator at=atoms.begin(); at != atoms.end(); at++) {
1106     at->print_atom(sysout);
1107   }
1108   sysout << "}\n\n\n";
1109 
1110 
1111   int npsp=pseudo.size();
1112   for(int psp=0; psp < npsp; psp++) {
1113     pseudo[psp].print_pseudo(sysout);
1114   }
1115 
1116   sysout.close();
1117 
1118   //---------------------------HF and OPT outputs
1119 
1120   string hfoutname=outputname+".hf";
1121   ofstream hfout(hfoutname.c_str());
1122   print_vmc_section(hfout, outputname, eref);
1123   hfout << "\n\n";
1124   hfout << "INCLUDE " << sysoutname << "  \n";
1125   hfout << "TRIALFUNC { INCLUDE " << slateroutname << "}\n\n";
1126   hfout.close();
1127 
1128   string optoutname=outputname+".opt";
1129   ofstream optout(optoutname.c_str());
1130   optout << "\n\n";
1131   optout << "INCLUDE " << sysoutname << " \n";
1132   optout << "TRIALFUNC { \n  SLATER-JASTROW \n"
1133 	 << "  WF1 { INCLUDE " << slateroutname << " } \n"
1134 	 << "  WF2 { INCLUDE " << jast2outname   << " } \n"
1135 	 << "}\n\n";
1136   print_vmc_section(optout, outputname, eref);
1137   print_opt_section(optout, outputname, eref);
1138   print_vmc_section(optout, outputname, eref);
1139   print_dmc_section(optout, outputname, eref);
1140   optout.close();
1141 
1142 
1143 }
1144 
1145