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
|
/* Write a VASP-style output file, one density only
*/
/* Copyright (c) 2007, 2017 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>
#include<math.h>
#include<string.h>
#include "c2xsf.h"
void vasp_write(FILE* outfile, struct unit_cell *c, struct contents *m,
struct grid *g){
int i,j,k,count,line;
double *dptr1,scale,x;
int nspec,*natomsp;
char *fmt;
if (m->title) fprintf(outfile,"%s\n",m->title);
else fprintf(outfile,"comment\n");
fprintf(outfile," %20.16f\n",1.0); /* Scale factor */
if (flags&HIPREC)
fmt="% 19.15f % 19.15f % 19.15f\n";
else
/* Some VASP readers eschew free format, so are fussy about details */
fmt="% 12.6f % 12.6f % 12.6f\n";
for(i=0;i<3;i++) fprintf(outfile,fmt,
c->basis[i][0],
c->basis[i][1],
c->basis[i][2]);
if(!(flags&HIPREC)) fmt="% 8.6f % 8.6f % 8.6f\n";
if (!m->spec) addspec(m);
/* VASP now puts atomic symbols here */
for(i=0;i<m->nspec;i++) fprintf(outfile," %s",atno2sym(m->spec[i].atno));
fprintf(outfile,"\n");
/* Need number of atoms in each species */
nspec=m->nspec;
natomsp=malloc(nspec*sizeof(int));
if (!natomsp) error_exit("malloc error in vasp_write");
for(i=0;i<nspec;i++) natomsp[i]=0;
for(i=0;i<m->n;i++){
for(j=0;j<nspec;j++){
if (m->atoms[i].atno==m->spec[j].atno){
natomsp[j]++;
break;
}
}
}
/* Must not have trailing space on this line... */
for(i=0;i<nspec;i++) fprintf(outfile," %d",natomsp[i]);
fprintf(outfile,"\n");
free(natomsp);
natomsp=NULL;
fprintf(outfile,"Direct\n"); /* i.e. fractional co-ords */
/* Write out atoms, sorted by species */
for(i=0;i<nspec;i++)
for(j=0;j<m->n;j++)
if (m->atoms[j].atno==m->spec[i].atno)
fprintf(outfile,fmt,m->atoms[j].frac[0],
m->atoms[j].frac[1],m->atoms[j].frac[2]);
/* And now write density */
if (!g->data) return;
fprintf(outfile,"\n %d %d %d\n",g->size[0],g->size[1],g->size[2]);
if ((flags&HIPREC)||(flags&CHGCAR)){
/* Some VASP readers eschew free format, so are fussy about details */
fmt=" %17.11E";
line=3;
}
else{
fmt=" %g";
line=7;
}
scale=fabs(c->vol);
count=-1;
for(k=0;k<g->size[2];k++){
for(j=0;j<g->size[1];j++){
dptr1=g->data+k+j*g->size[2];
for(i=0;i<g->size[0];i++){
x=*(dptr1+i*g->size[1]*g->size[2])*scale;
if (fabs(x)>1e-99) fprintf(outfile,fmt,x);
else fprintf(outfile,fmt,0.0);
count++;
if ((count&line)==line) fprintf(outfile,"\n");
}
}
}
if ((count&line)!=line) fprintf(outfile,"\n");
/* Should we also write a spin density? */
g=g->next;
if ((!g)||(!g->data)) return;
if ((!g->name)||(strncasecmp(g->name,"Spin",4))) return;
/* Write out mag moments, same order as atoms */
k=0;
for(i=0;i<nspec;i++)
for(j=0;j<m->n;j++)
if (m->atoms[j].atno==m->spec[i].atno){
fprintf(outfile," %f",m->atoms[j].spin);
k++;
if (k%4==0) fprintf(outfile,"\n");
}
if (k%4) fprintf(outfile,"\n");
/* And now write spin */
fprintf(outfile," %d %d %d\n",g->size[0],g->size[1],g->size[2]);
count=-1;
for(k=0;k<g->size[2];k++){
for(j=0;j<g->size[1];j++){
dptr1=g->data+k+j*g->size[2];
for(i=0;i<g->size[0];i++){
x=*(dptr1+i*g->size[1]*g->size[2])*scale;
if (fabs(x)>1e-99) fprintf(outfile,fmt,x);
else fprintf(outfile,fmt,0.0);
count++;
if ((count&line)==line) fprintf(outfile,"\n");
}
}
}
if ((count&line)!=line) fprintf(outfile,"\n");
}
|