4 * author: frank.zirkelbach@physik.uni-augsburg.de
11 #include <sys/types.h>
18 int get_line(int fd,char *line,int max) {
25 if(count==max) return count;
26 ret=read(fd,line+count,1);
27 if(ret<=0) return ret;
28 if(line[count]=='\n') {
29 memset(line+count,0,max-count-1);
38 int main(int argc,char **argv) {
40 double x1,x2,x3,y1,y2,y3,z1,z2,z3;
41 double x_1,x_2,x_3,y_1,y_2,y_3,z_1,z_2,z_3;
42 double X1,X2,X3,Y1,Y2,Y3,Z1,Z2,Z3;
44 double costheta,sintheta;
45 double normx,normy,normz;
47 char file[128],buf[256];
52 posr=open("POSCAR",O_RDONLY);
54 perror("open POSCAR (read) file\n");
58 theta=(atof(argv[1])/180.0*M_PI);
62 snprintf(file,127,"POSCAR.X%f",theta);
63 poso=open(file,O_WRONLY|O_CREAT);
65 perror("open POSCAR (write) file\n");
70 cnt=get_line(posr,buf,256);
75 cnt=get_line(posr,buf,256);
80 cnt=get_line(posr,buf,256);
83 wptr=strtok(NULL," ");
85 wptr=strtok(NULL," ");
88 cnt=get_line(posr,buf,256);
91 wptr=strtok(NULL," ");
93 wptr=strtok(NULL," ");
96 cnt=get_line(posr,buf,256);
99 wptr=strtok(NULL," ");
101 wptr=strtok(NULL," ");
105 normx=sqrt(x1*x1+x2*x2+x3*x3);
106 normy=sqrt(y1*y1+y2*y2+y3*y3);
107 normz=sqrt(z1*z1+z2*z2+z3*z3);
109 /* basis in given basis */
110 X1=(x1*x1+x2*x2+x3*x3)/normx;
111 X2=(y1*x1+y2*x2+y3*x3)/normy;
112 X3=(z1*x1+z2*x2+z3*x3)/normz;
114 Y1=(x1*y1+x2*y2+x3*y3)/normx;
115 Y2=(y1*y1+y2*y2+y3*y3)/normy;
116 Y3=(z1*y1+z2*y2+z3*y3)/normz;
118 Z1=(x1*z1+x2*z2+x3*z3)/normx;
119 Z2=(y1*z1+y2*z2+y3*z3)/normy;
120 Z3=(z1*z1+z2*z2+z3*z3)/normz;
122 printf("Basis expressed by itself:\n");
123 printf(" %f %f %f\n",X1,Y1,Z1);
124 printf(" x = %f y = %f z = %f\n",X2,Y2,Z2);
125 printf(" %f %f %f\n",X3,Y3,Z3);
127 /* transformed basis */
129 x_2=costheta*X2-sintheta*X3;
130 x_3=sintheta*X2+costheta*X3;
133 y_2=costheta*Y2-sintheta*Y3;
134 y_3=sintheta*Y2+costheta*Y3;
137 z_2=costheta*Z2-sintheta*Z3;
138 z_3=sintheta*Z2+costheta*Z3;
140 printf("Transformed basis in the given basis:\n");
141 printf(" %f %f %f\n",x_1,y_1,z_1);
142 printf(" x = %f y = %f z = %f\n",x_2,y_2,z_2);
143 printf(" %f %f %f\n",x_3,y_3,z_3);
145 /* transformed basis in cartesian coordinates */
146 X1=(x1/normx*x_1+y1/normy*x_2+z1/normz*x_3);
147 X2=(x2/normx*x_1+y2/normy*x_2+z2/normz*x_3);
148 X3=(x3/normx*x_1+y3/normy*x_2+z3/normz*x_3);
150 Y1=(x1/normx*y_1+y1/normy*y_2+z1/normz*y_3);
151 Y2=(x2/normx*y_1+y2/normy*y_2+z2/normz*y_3);
152 Y3=(x3/normx*y_1+y3/normy*y_2+z3/normz*y_3);
154 Z1=(x1/normx*z_1+y1/normy*z_2+z1/normz*z_3);
155 Z2=(x2/normx*z_1+y2/normy*z_2+z2/normz*z_3);
156 Z3=(x3/normx*z_1+y3/normy*z_2+z3/normz*z_3);
158 printf("Transformed basis in cartesian coordinates:\n");
159 printf(" %f %f %f\n",X1,Y1,Z1);
160 printf(" x = %f y = %f z = %f\n",X2,Y2,Z2);
161 printf(" %f %f %f\n",X3,Y3,Z3);
163 dprintf(poso," %f %f %f\n",X1,X2,X3);
164 dprintf(poso," %f %f %f\n",Y1,Y2,Y3);
165 dprintf(poso," %f %f %f\n",Z1,Z2,Z3);
168 cnt=get_line(posr,buf,256);
173 cnt=get_line(posr,buf,256);
178 cnt=get_line(posr,buf,256);
183 cnt=get_line(posr,buf,256);
186 wptr=strtok(buf," ");
188 wptr=strtok(NULL," ");
190 wptr=strtok(NULL," ");
193 wptr=strtok(NULL," ");
195 wptr=strtok(NULL," ");
197 wptr=strtok(NULL," ");
201 X2=costheta*x2+sintheta*x3;
202 X3=costheta*x3-sintheta*x2;
205 double tmp1,tmp2,tmp3;
206 tmp1=(X1*x_1+X2*y_1+X3*z_1)/normx;
207 tmp2=(X1*x_2+X2*y_2+X3*z_2)/normy;
208 tmp3=(X1*x_3+X2*y_3+X3*z_3)/normz;
209 printf("%f %f %f - %f %f %f | %f %f %f\n",
210 x1,x2,x3,tmp1,tmp2,tmp3,x1-tmp1,x2-tmp2,x3-tmp3);
212 dprintf(poso," %f %f %f %c %c %c\n",X1,X2,X3,t1,t2,t3);