From: hackbard Date: Thu, 20 Aug 2009 09:14:32 +0000 (+0200) Subject: added trafo c code X-Git-Url: https://hackdaworld.org/gitweb/?a=commitdiff_plain;h=be1d19844c16b2d49a376c8afc574fe468cbb2fb;p=physik%2Fposic.git added trafo c code --- diff --git a/vasp_tools/tXp.c b/vasp_tools/tXp.c new file mode 100644 index 0000000..54fe5e2 --- /dev/null +++ b/vasp_tools/tXp.c @@ -0,0 +1,214 @@ +/* + * tXp.c + * + * author: frank.zirkelbach@physik.uni-augsburg.de + * + */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include + +int get_line(int fd,char *line,int max) { + + int count,ret; + + count=0; + + while(1) { + if(count==max) return count; + ret=read(fd,line+count,1); + if(ret<=0) return ret; + if(line[count]=='\n') { + memset(line+count,0,max-count-1); + //line[count]='\0'; + return count+1; + } + count+=1; + } +} + + +int main(int argc,char **argv) { + + double x1,x2,x3,y1,y2,y3,z1,z2,z3; + double x_1,x_2,x_3,y_1,y_2,y_3,z_1,z_2,z_3; + double X1,X2,X3,Y1,Y2,Y3,Z1,Z2,Z3; + double theta; + double costheta,sintheta; + double normx,normy,normz; + int posr,poso; + char file[128],buf[256]; + char *wptr; + char t1,t2,t3; + int cnt; + + posr=open("POSCAR",O_RDONLY); + if(posr<0) { + perror("open POSCAR (read) file\n"); + return posr; + } + + theta=(atof(argv[1])/180.0*M_PI); + costheta=cos(theta); + sintheta=sin(theta); + + snprintf(file,127,"POSCAR.X%f",theta); + poso=open(file,O_WRONLY|O_CREAT); + if(poso<0) { + perror("open POSCAR (write) file\n"); + return poso; + } + + // first line + cnt=get_line(posr,buf,256); + buf[cnt-1]='\n'; + write(poso,buf,cnt); + + // second line + cnt=get_line(posr,buf,256); + buf[cnt-1]='\n'; + write(poso,buf,cnt); + + // basis + cnt=get_line(posr,buf,256); + wptr=strtok(buf," "); + x1=atof(wptr); + wptr=strtok(NULL," "); + x2=atof(wptr); + wptr=strtok(NULL," "); + x3=atof(wptr); + + cnt=get_line(posr,buf,256); + wptr=strtok(buf," "); + y1=atof(wptr); + wptr=strtok(NULL," "); + y2=atof(wptr); + wptr=strtok(NULL," "); + y3=atof(wptr); + + cnt=get_line(posr,buf,256); + wptr=strtok(buf," "); + z1=atof(wptr); + wptr=strtok(NULL," "); + z2=atof(wptr); + wptr=strtok(NULL," "); + z3=atof(wptr); + + /* norm */ + normx=sqrt(x1*x1+x2*x2+x3*x3); + normy=sqrt(y1*y1+y2*y2+y3*y3); + normz=sqrt(z1*z1+z2*z2+z3*z3); + + /* basis in given basis */ + X1=(x1*x1+x2*x2+x3*x3)/normx; + X2=(y1*x1+y2*x2+y3*x3)/normy; + X3=(z1*x1+z2*x2+z3*x3)/normz; + + Y1=(x1*y1+x2*y2+x3*y3)/normx; + Y2=(y1*y1+y2*y2+y3*y3)/normy; + Y3=(z1*y1+z2*y2+z3*y3)/normz; + + Z1=(x1*z1+x2*z2+x3*z3)/normx; + Z2=(y1*z1+y2*z2+y3*z3)/normy; + Z3=(z1*z1+z2*z2+z3*z3)/normz; + + printf("Basis expressed by itself:\n"); + printf(" %f %f %f\n",X1,Y1,Z1); + printf(" x = %f y = %f z = %f\n",X2,Y2,Z2); + printf(" %f %f %f\n",X3,Y3,Z3); + + /* transformed basis */ + x_1=X1; + x_2=costheta*X2-sintheta*X3; + x_3=sintheta*X2+costheta*X3; + + y_1=Y1; + y_2=costheta*Y2-sintheta*Y3; + y_3=sintheta*Y2+costheta*Y3; + + z_1=Z1; + z_2=costheta*Z2-sintheta*Z3; + z_3=sintheta*Z2+costheta*Z3; + + printf("Transformed basis in the given basis:\n"); + printf(" %f %f %f\n",x_1,y_1,z_1); + printf(" x = %f y = %f z = %f\n",x_2,y_2,z_2); + printf(" %f %f %f\n",x_3,y_3,z_3); + + /* transformed basis in cartesian coordinates */ + X1=(x1/normx*x_1+y1/normy*x_2+z1/normz*x_3); + X2=(x2/normx*x_1+y2/normy*x_2+z2/normz*x_3); + X3=(x3/normx*x_1+y3/normy*x_2+z3/normz*x_3); + + Y1=(x1/normx*y_1+y1/normy*y_2+z1/normz*y_3); + Y2=(x2/normx*y_1+y2/normy*y_2+z2/normz*y_3); + Y3=(x3/normx*y_1+y3/normy*y_2+z3/normz*y_3); + + Z1=(x1/normx*z_1+y1/normy*z_2+z1/normz*z_3); + Z2=(x2/normx*z_1+y2/normy*z_2+z2/normz*z_3); + Z3=(x3/normx*z_1+y3/normy*z_2+z3/normz*z_3); + + printf("Transformed basis in cartesian coordinates:\n"); + printf(" %f %f %f\n",X1,Y1,Z1); + printf(" x = %f y = %f z = %f\n",X2,Y2,Z2); + printf(" %f %f %f\n",X3,Y3,Z3); + + dprintf(poso," %f %f %f\n",X1,X2,X3); + dprintf(poso," %f %f %f\n",Y1,Y2,Y3); + dprintf(poso," %f %f %f\n",Z1,Z2,Z3); + + // 6th line + cnt=get_line(posr,buf,256); + buf[cnt-1]='\n'; + write(poso,buf,cnt); + + // 7th line + cnt=get_line(posr,buf,256); + buf[cnt-1]='\n'; + write(poso,buf,cnt); + + // 8th line + cnt=get_line(posr,buf,256); + buf[cnt-1]='\n'; + write(poso,buf,cnt); + + while(1) { + cnt=get_line(posr,buf,256); + if(cnt<=0) + break; + wptr=strtok(buf," "); + x1=atof(wptr); + wptr=strtok(NULL," "); + x2=atof(wptr); + wptr=strtok(NULL," "); + x3=atof(wptr); + + wptr=strtok(NULL," "); + t1=wptr[0]; + wptr=strtok(NULL," "); + t2=wptr[0]; + wptr=strtok(NULL," "); + t3=wptr[0]; + + X1=x1; + X2=costheta*x2+sintheta*x3; + X3=costheta*x3-sintheta*x2; + + dprintf(poso," %f %f %f %c %c %c\n",X1,X2,X3,t1,t2,t3); + } + + close(posr); + close(poso); + + printf("done!\n"); + + return 0; +} +