1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
|
/* Read some useful data from a SIESTA .fdf file
*
*/
/*
* This version does not parse quotation marks properly.
*/
/* Note that Siesta ignores any of the three characters -_. appearing
* in its keywords.
*/
/* Copyright (c) 2018-2020 MJ Rutter
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3
* of the Licence, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see http://www.gnu.org/licenses/
*/
#include<stdio.h>
#include<stdlib.h> /* malloc */
#include<string.h>
#include<ctype.h>
#include<errno.h>
#include "c2xsf.h"
#define LINE_SIZE 200
#define PARSE_ERROR fprintf(stderr,"Parse error on line %d %s\n",files->line,(files->name)?files->name:"")
static int fdfreadline(char *buffer, int len, char *dir,
struct infiles **filesp);
static int fdfreadlength(char *buff, double *x);
static int fdfreadenergy(char *buff, double *x);
static int fdfstrcasecmp(char **s1_in, char *s2);
static int fdftokenmatch(char **s1_in, char *s2);
static double siesta_pot_charge(char *prefix, char *label);
static char *ptr;
void fdf_read(FILE* in, struct unit_cell *c, struct contents *m,
struct kpts *kp, struct es *e){
int i,j,k,n,*atomspecies,mp_grid[3][3],okay;
int nspec,*species_to_atno;
double scale,shift[3],mp_off[3];
char *ptr2,*format;
static char buffer[LINE_SIZE+1];
double *vec,*abc,*atomcoords,*dmetol,latticeconstant,*atomspins;
double *species_to_charge;
char *siesta_misc[]={"ElectronicTemperature","MeshCutoff",
"MaxSCFIterations","MinSCFIterations",
"OccupationFunction","SaveDeltaRho",
"SaveElectrostaticPotential",
"SaveRho","SaveRhoXC",
"SaveTotalCharge","SaveTotalPotential",
"SlabDipoleCorrection","SolutionMethod",
"WriteDM","XCauthors","XCfunctional",
""};
char *spaces=" "; /* 30 spaces */
char *prefix;
char *dir;
struct infiles *files;
dir=dict_get(m->dict,"in_dir");
if (dir)
prefix=dir;
else
prefix="";
files=malloc(sizeof(struct infiles));
if (!files) error_exit("Malloc error for files in fdf_read");
files->f=in;
files->next=files->last=NULL;
files->name=NULL;
files->line=0;
m->n=0;
nspec=0;
vec=abc=NULL;
latticeconstant=0;
format=NULL;
species_to_atno=NULL;
species_to_charge=NULL;
atomspecies=NULL;
shift[0]=shift[1]=shift[2]=0;
dmetol=NULL;
atomspins=NULL;
atomcoords=NULL;
if (debug>2) fprintf(stderr,"fdf_read called\n");
if (!(c->basis=malloc(72)))
error_exit("Malloc error in fdfread for c->basis");
while(fdfreadline(buffer,LINE_SIZE,dir,&files)){
ptr=buffer;
if (!fdftokenmatch(&ptr,"SystemName")){
m->title=malloc(strlen(ptr)+1);
strcpy(m->title,ptr);
continue;
}
if (!fdftokenmatch(&ptr,"NumberOfAtoms")){
if (sscanf(ptr,"%d",&(m->n))!=1)
error_exit("Error parsing number of atoms");
continue;
}
if (!fdftokenmatch(&ptr,"NumberOfSpecies")){
if (sscanf(ptr,"%d",&nspec)!=1)
error_exit("Error parsing number of species");
continue;
}
if (!fdftokenmatch(&ptr,"LatticeConstant")){
if (!fdfreadlength(ptr,&latticeconstant))
error_exit("Error parsing lattice constant");
continue;
}
if (!fdftokenmatch(&ptr,"AtomicCoordinatesFormat")){
while (*ptr==' ') ptr++;
ptr2=ptr;
while (*ptr2&&(*ptr2!=' ')) ptr2++;
format=malloc(ptr2-ptr+1);
strncpy(format,ptr,(ptr2-ptr)+1);
continue;
}
if (!fdftokenmatch(&ptr,"AtomicCoordinatesOrigin")){
if (sscanf(ptr,"%lf %lf %lf",shift,shift+1,shift+2)!=3)
error_exit("Error parsing atomic coordinates origin");
continue;
}
if (!fdftokenmatch(&ptr,"DMEnergyTolerance")){
dmetol=malloc(sizeof(double));
if (!fdfreadenergy(ptr,dmetol)){
fprintf(stderr,"Error parsing DM.Energy.Tolerance\n");
free(dmetol);
dmetol=NULL;
}
continue;
}
if (!fdftokenmatch(&ptr,"NetCharge")){
e->charge=malloc(sizeof(double));
if (sscanf(ptr,"%lf",e->charge)!=1){
fprintf(stderr,"Warning: error parsing NetCharge\n");
free(e->charge);
e->charge=NULL;
}
continue;
}
if (!fdftokenmatch(&ptr,"SystemLabel")){
ptr2=malloc(strlen(ptr)+1);
strcpy(ptr2,ptr);
dict_add(m->dict,"Siesta_SystemLabel",ptr2);
}
for(i=0;siesta_misc[i][0];i++){
if (!fdftokenmatch(&ptr,siesta_misc[i])){
dict_strcat(m->dict,"Siesta_misc",siesta_misc[i]);
while ((*ptr)&&isspace(*ptr)) ptr++;
if (strlen(ptr)){
if (strlen(siesta_misc[i])<28) /* 28 < length spaces array */
dict_strcat(m->dict,"Siesta_misc",spaces+strlen(siesta_misc[i]));
else
dict_strcat(m->dict,"Siesta_misc"," ");
dict_strcat(m->dict,"Siesta_misc",ptr);
}
dict_strcat(m->dict,"Siesta_misc","\n");
}
}
/* What remains ought to be a block or a keyword. */
if (strncasecmp(ptr,"%block",6)) continue;
ptr+=6;
/* Eat leading spaces */
while(*ptr==' ') ptr++;
/* Kill trailing spaces and things after # */
ptr2=ptr;
while((*ptr2)&&(*ptr2!=' ')&&(*ptr2!='#')) ptr2++;
*ptr2=0; /* Either it was a null, or it should be one... */
if (debug>2) fprintf(stderr,"Found a %s block\n",ptr);
if (!fdfstrcasecmp(&ptr,"latticevectors")){
fdfreadline(buffer,LINE_SIZE,dir,&files);
vec=malloc(9*sizeof(double));
for(i=0;i<3;i++){
if (sscanf(buffer,"%lf %lf %lf",vec+3*i,vec+3*i+1,vec+3*i+2)!=3)
error_exit("Error reading lattice vectors");
fdfreadline(buffer,LINE_SIZE,dir,&files);
}
}else if (!fdfstrcasecmp(&ptr,"latticeparameters")){
fdfreadline(buffer,LINE_SIZE,dir,&files);
abc=malloc(6*sizeof(double));
if (sscanf(buffer,"%lf %lf %lf %lf %lf %lf",
abc,abc+1,abc+2,abc+3,abc+4,abc+5)!=6)
error_exit("Error reading lattice parameters");
fdfreadline(buffer,LINE_SIZE,dir,&files);
}else if (!fdfstrcasecmp(&ptr,"AtomicCoordinatesAndAtomicSpecies")){
if (m->n==0)
error_exit("AtomicCoordinatesAndAtomicSpecies preceeds NumberOfAtoms");
atomspecies=malloc(m->n*sizeof(int));
atomcoords=malloc(3*m->n*sizeof(double));
for(i=0;i<m->n;i++){
fdfreadline(buffer,LINE_SIZE,dir,&files);
if (sscanf(buffer,"%lf %lf %lf %d",
atomcoords+3*i,atomcoords+3*i+1,atomcoords+3*i+2,
atomspecies+i)!=4)
error_exit("Error reading atomic positions");
}
fdfreadline(buffer,LINE_SIZE,dir,&files);
}
else if (!fdfstrcasecmp(&ptr,"ChemicalSpeciesLabel")){
if (!nspec)
error_exit("ChemicalSpeciesLabel preceeds NumberOfSpecies");
species_to_atno=malloc((nspec+1)*sizeof(int));
species_to_charge=malloc((nspec+1)*sizeof(double));
for(i=0;i<nspec;i++){
fdfreadline(buffer,LINE_SIZE,dir,&files);
if (sscanf(buffer,"%d %d%n",&j,&k,&n)!=2)
error_exit("Error reading ChemicalSpeciesLabel");
if ((j<1)||(j>nspec)){
error_exit("Invalid species number in ChemicalSpeciesLabel");
}
species_to_atno[j]=k;
if (flags&CHDEN){ /* No point in attempting to find pseudo */
ptr=buffer+n; /* charges unless also processing elect charge */
while((*ptr)&&(isspace(*ptr))) ptr++;
ptr2=ptr;
while((*ptr2)&&(!isspace(*ptr2))) ptr2++;
*ptr2=0;
species_to_charge[j]=siesta_pot_charge(prefix,ptr);
}
}
fdfreadline(buffer,LINE_SIZE,dir,&files);
}
else if (!fdfstrcasecmp(&ptr,"DMInitSpin")){
if (m->n==0)
error_exit("DMInitSpin preceeds NumberOfAtoms");
atomspins=malloc(m->n*sizeof(double));
for(i=0;i<m->n;i++) atomspins[i]=0;
while(1){
fdfreadline(buffer,LINE_SIZE,dir,&files);
i=sscanf(buffer,"%d%n",&j,&k);
if (i==0) break;
if ((j<1)||(j>m->n))
error_exit("Atom index out of range in spin block");
ptr=buffer+k;
while (*ptr==' ') ptr++;
if (!tokenmatch(&ptr,"+")){
atomspins[j-1]=1;
}
else if (!tokenmatch(&ptr,"-")){
atomspins[j-1]=-1;
}
else{
if (sscanf(ptr,"%lf",atomspins+j-1)!=1)
error_exit("Error parsing spin");
}
}
}
else if ((kp)&&(!fdfstrcasecmp(&ptr,"kgridMonkhorstPack"))){
for(i=0;i<3;i++){
fdfreadline(buffer,LINE_SIZE,dir,&files);
j=sscanf(buffer,"%d %d %d %lf",mp_grid[i],mp_grid[i]+1,mp_grid[i]+2,
mp_off+i);
if (j!=4){
PARSE_ERROR;
break;
}
}
if (j!=4)
fprintf(stderr,"Ignoring MP block\n");
else{
okay=1;
/* Need grid to be diagonal */
for(i=0;i<3;i++)
for(j=0;j<3;j++)
if (i==j) continue;
else if (mp_grid[i][j]!=0) okay=0;
if (okay){
kp->mp=malloc(sizeof(struct mp_grid));
if (!kp->mp) error_exit("Malloc error for MP grid");
for(i=0;i<3;i++)
kp->mp->grid[i]=mp_grid[i][i];
for(i=0;i<3;i++){
if ((kp->mp->grid[i]&1)==0) mp_off[i]-=0.5;
mp_off[i]/=kp->mp->grid[i];
kp->mp->disp[i]=mp_off[i];
}
}
else{
fprintf(stderr,"c2x is unable to process non-diagonal MP grids\n");
}
}
}
else{
fprintf(stderr,"Ignoring %s\n",ptr);
while(fdfreadline(buffer,LINE_SIZE,dir,&files)){
ptr=buffer;
if (!strncasecmp(ptr,"%endblock",9)) break;
}
}
}
/* Extract unit cell */
if (vec){
for(i=0;i<3;i++)
for(j=0;j<3;j++)
c->basis[i][j]=vec[3*i+j]*latticeconstant;
free(vec);
vec=NULL;
}
else if (abc){
for(i=0;i<3;i++)
abc[i]*=latticeconstant;
abc2cart(abc,c);
free(abc);
abc=NULL;
}
else{
for(i=0;i<3;i++)
for(j=0;j<3;j++)
c->basis[i][j]=(i==j)?latticeconstant:0;
}
real2rec(c);
/* Extract atomic positions */
if (!atomcoords)
error_exit("No atomic coordinates found!");
m->atoms=malloc(m->n*sizeof(struct atom));
if (!m->atoms) error_exit("malloc error for atoms");
init_atoms(m->atoms,m->n);
scale=-1;
ptr=format;
if (!format) /* default */
scale=BOHR;
else if ((!fdftokenmatch(&ptr,"Bohr"))||
(!fdftokenmatch(&ptr,"NotScaledCartesianBohr")))
scale=BOHR;
else if ((!fdftokenmatch(&ptr,"Ang"))||
(!fdftokenmatch(&ptr,"NotScaledCartesianAng")))
scale=1;
else if (!fdftokenmatch(&ptr,"ScaledCartesian"))
scale=latticeconstant;
else if ((!fdftokenmatch(&ptr,"Fractional"))||
(!fdftokenmatch(&ptr,"ScaledByLatticeVectors")))
scale=0;
if (scale==-1){
fprintf(stderr,"Unrecognised atomic coord format: %s\n",format);
exit(1);
}
if (format) free(format);
if (scale){
for(i=0;i<m->n;i++)
for(j=0;j<3;j++)
m->atoms[i].abs[j]=(atomcoords[3*i+j]+shift[j])*scale;
addfrac(m->atoms,m->n,c->recip);
}
else{
for(i=0;i<m->n;i++)
for(j=0;j<3;j++)
m->atoms[i].frac[j]=atomcoords[3*i+j]+shift[j];
addabs(m->atoms,m->n,c->basis);
}
free(atomcoords);
/* Add atomic numbers */
if (!atomspecies)
error_exit("chemical_species_label block not found");
for(i=0;i<m->n;i++){
m->atoms[i].atno=species_to_atno[atomspecies[i]];
m->atoms[i].chg=species_to_charge[atomspecies[i]];
}
m->nspec=nspec;
m->spec=malloc(nspec*sizeof(struct species));
if (!m->spec) error_exit("malloc error for m->spec in fdf_read");
for(i=0;i<nspec;i++) m->spec[i].atno=species_to_atno[i+1];
free(atomspecies);
free(species_to_atno);
dict_add(m->dict,"Siesta_species_to_charge",species_to_charge);
/* Add spins */
if (atomspins){
for(i=0;i<m->n;i++)
m->atoms[i].spin=atomspins[i];
free(atomspins);
}
/* Miscellaneous */
if (dmetol) {
e->etol=(*dmetol)/m->n;
free(dmetol);
}
}
int fdfreadline(char *buffer, int len, char *dir, struct infiles **filesp){
int off,success2,inc;
char *ptr,*success,*p2;
while((success=fgets(buffer,len,(*filesp)->f))){ /* fgets() always
null terminates,
gcc likes extra brackets */
(*filesp)->line++;
/* Kill trailing spaces and newlines / carriage returns */
ptr=buffer+strlen(buffer)-1;
while((ptr>=buffer)&&((*ptr==' ')||(*ptr=='\n')||(*ptr=='\r'))) ptr--;
*(ptr+1)=0;
/* Eat leading spaces */
ptr=buffer;
while(*ptr==' ') ptr++;
/* Skip comments and blank lines */
if ((*ptr=='#')||(*ptr==0)) continue;
break;
}
if (!success){ /* Need to recurse out of nested include files */
success2=0;
while ((*filesp)->last){
fclose((*filesp)->f);
free((*filesp)->name);
inc=(*filesp)->include;
(*filesp)=(*filesp)->last;
free((*filesp)->next);
(*filesp)->next=NULL;
if (!inc){
strcpy(buffer,"%endblock\n");
return 1;
}
success2=fdfreadline(buffer,LINE_SIZE,dir,filesp);
if (success2) return success2;
}
if (!success2) return(0);
}
off=ptr-buffer;
if (off){
while(*ptr) {*(ptr-off)=*ptr;ptr++;}
*(ptr-off)=0;
}
if (!tokenmatch(&ptr,"%include")){
while (*ptr==' ') ptr++;
p2=ptr;
while ((*p2)&&(*p2!=' ')) p2++;
*p2=0;
include_file(filesp,dir,ptr);
return fdfreadline(buffer,LINE_SIZE,dir,filesp);
}
/* Might have "label < include" syntax */
/* consume label */
p2=ptr;
/* ought to add $%&@/^~ */
while ((isalnum(*p2))||(*p2=='.')||(*p2=='_')||(*p2=='-')||(*p2=='+')) p2++;
/* and following whitespace */
while (isspace(*p2)) p2++;
/* do we now have a < ? */
if (*p2=='<'){
ptr=p2+1;
while (*ptr==' ') ptr++;
p2=ptr;
while ((*p2)&&(*p2!=' ')) p2++;
*p2=0;
include_file(filesp,dir,ptr);
return fdfreadline(buffer,LINE_SIZE,dir,filesp);
}
if (!tokenmatch(&ptr,"%block")){ /* Need to scan for < includes */
while(*(++ptr)){
if (*ptr=='<'){
*ptr=0; /* stop rest of parser seeing the < part */
ptr++;
while (*ptr==' ') ptr++;
p2=ptr;
while ((*p2)&&(*p2!=' ')) p2++;
*p2=0;
include_file(filesp,dir,ptr);
(*filesp)->include=0;
return 1; /* returns buffer from old file with %block line
* next read is from new file
*/
}
}
}
return (1);
}
void include_file(struct infiles **file, char *dir, char *ptr){
struct infiles *fs;
char *name;
name=NULL;
fs=*file;
fs->next=malloc(sizeof(struct infiles));
if (!fs->next) error_exit("Malloc error for files_next in cell_read");
fs->next->last=fs;
fs->next->next=NULL;
fs=fs->next;
fs->f=NULL;
if (dir){
name=malloc(strlen(dir)+strlen(ptr)+1);
if (!name) error_exit("malloc error for filename");
strcpy(name,dir);
strcat(name,ptr);
fs->f=fopen(name,"r");
fs->name=name;
}
if (!fs->f) {
fs->f=fopen(ptr,"r");
fs->name=malloc(strlen(ptr)+1);
if (!fs->name) error_exit("malloc error for filename");
strcpy(fs->name,ptr);
}
if (!fs->f){
fprintf(stderr,"Error: failed to open include file ");
if (name) fprintf(stderr,"%s or",name);
fprintf(stderr,"%s\n",ptr);
}
if (debug)
fprintf(stderr,"Opened included file %s\n",fs->name);
fs->line=0;
fs->include=1; /* This gets set to zero if a block include which
* needs an %endblock added */
*file=fs;
}
static int fdfreadlength(char *buff, double *x){
char *p,*ptr;
int n,i;
while((*buff)&&((*buff==' '))) buff++;
/*
* n=sscanf(buff,"%lf %ms",x,&ptr);
*/
n=sscanf(buff,"%lf%n",x,&i);
n+=sscanfmsn(buff+i,&ptr,NULL);
p=ptr; /* tokenmatch may modify ptr, but we wish to free it */
if (n==0) return 0;
if (n==1) {
error_exit("No units specified for length");
}
if (!tokenmatch(&p,"bohr")){
(*x)*=BOHR;
free(ptr);
return 1;
}
if (!tokenmatch(&p,"ang")){
free(ptr);
return 1;
}
if (!tokenmatch(&p,"nm")){
(*x)*=10;
free(ptr);
return 1;
}
fprintf(stderr,"Unexpected length unit: %s\n",ptr);
free(ptr);
return 0;
}
static int fdfreadenergy(char *buff, double *x){
char *p,*ptr;
int n,i;
while((*buff)&&((*buff==' '))) buff++;
/*
* n=sscanf(buff,"%lf %ms",x,&ptr);
*/
n=sscanf(buff,"%lf%n",x,&i);
n+=sscanfmsn(buff+i,&ptr,NULL);
p=ptr; /* tokenmatch may modify ptr, but we wish to free it */
if (n==0) return 0;
if (n==1) {
error_exit("No units specified for energy");
}
if (!tokenmatch(&p,"Ry")){
(*x)*=0.5*H_eV;
free(ptr);
return 1;
}
if (!tokenmatch(&p,"mRy")){
(*x)*=0.5*0.001*H_eV;
free(ptr);
return 1;
}
if (!tokenmatch(&p,"eV")){
free(ptr);
return 1;
}
if (!tokenmatch(&p,"meV")){
(*x)*=0.001;
free(ptr);
return 1;
}
if (!tokenmatch(&p,"Hartree")){
(*x)*=H_eV;
free(ptr);
return 1;
}
if (!tokenmatch(&p,"mHartree")){
(*x)*=0.001*H_eV;
free(ptr);
return 1;
}
fprintf(stderr,"Unexpected length unit: %s\n",ptr);
free(ptr);
return 0;
}
/* Compare two strings up to the length of the second.
* If they compare equal, move the first pointer to the end of
* the equal section.
* Ignore the three characters -_. in s1 when comparing
*/
static int fdfstrcasecmp(char **s1_in, char *s2){
char *s1;
s1=*s1_in;
while((*s1)&&(*s2)){
if ((*s1=='_')||(*s1=='-')||(*s1=='.'))
s1++;
else if (toupper(*s1)==toupper(*s2)){
s1++;
s2++;
}
else break;
}
if (*s2==0){
*s1_in=s1;
return 0;
}
if ((*s1)>(*s2)) return 1;
return -1;
}
static int fdftokenmatch(char **s1_in, char *s2){
char *s1;
int tmp;
s1=*s1_in;
tmp=fdfstrcasecmp(&s1,s2);
if (tmp!=0) return tmp;
/* s1 will have been moved to end of the equal section */
if ((*s1==0)||(isspace(*s1))){
*s1_in=s1;
return 0;
}
return 1;
}
static double siesta_pot_charge(char *prefix, char *label){
char *name;
int i,idummy;
double dummy,charge;
FILE *pseudo;
char buffer[LINE_SIZE+1];
name=malloc(strlen(prefix)+strlen(label)+5);
name[0]=0;
strcpy(name,prefix);
strcat(name,label);
strcat(name,".psf");
pseudo=fopen(name,"rb");
if (!pseudo) {
fprintf(stderr,"Failed to open %s\n",name);
free(name);
return 0;
}
for(i=0;i<4;i++)
fgets(buffer,LINE_SIZE,pseudo);
i=sscanf(buffer,"%d %d %d %lf %lf %lf",&idummy,&idummy,&idummy,
&dummy,&dummy,&charge);
if (i!=6) {
fprintf(stderr,"Failed to read charge from %s\n",name);
charge=0;
}
fclose(pseudo);
free(name);
if (debug>1) fprintf(stderr,"Label %s charge %lf\n",label,charge);
return charge;
}
|