]> hackdaworld.org Git - physik/posic.git/blob - vasp_tools/tXp.c
mirror feature + new std vis
[physik/posic.git] / vasp_tools / tXp.c
1 /*
2  * tXp.c
3  *
4  * author: frank.zirkelbach@physik.uni-augsburg.de
5  *
6  */
7
8 #define _GNU_SOURCE
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <fcntl.h>
14 #include <string.h>
15 #include <unistd.h>
16 #include <math.h>
17
18 int get_line(int fd,char *line,int max) {
19
20         int count,ret;
21
22         count=0;
23
24         while(1) {
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);
30                         //line[count]='\0';
31                         return count+1;
32                 }
33                 count+=1;
34         }
35 }
36
37
38 int main(int argc,char **argv) {
39
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;
43         double theta;
44         double costheta,sintheta;
45         double normx,normy,normz;
46         int posr,poso;
47         char file[128],buf[256];
48         char *wptr;
49         char t1,t2,t3;
50         int cnt;
51
52         posr=open("POSCAR",O_RDONLY);
53         if(posr<0) {
54                 perror("open POSCAR (read) file\n");
55                 return posr;
56         }
57
58         theta=(atof(argv[1])/180.0*M_PI);
59         costheta=cos(theta);
60         sintheta=sin(theta);
61
62         snprintf(file,127,"POSCAR.X%f",theta);
63         poso=open(file,O_WRONLY|O_CREAT);
64         if(poso<0) {
65                 perror("open POSCAR (write) file\n");
66                 return poso;
67         }
68         
69         // first line
70         cnt=get_line(posr,buf,256);
71         buf[cnt-1]='\n';
72         write(poso,buf,cnt);
73
74         // second line
75         cnt=get_line(posr,buf,256);
76         buf[cnt-1]='\n';
77         write(poso,buf,cnt);
78
79         // basis
80         cnt=get_line(posr,buf,256);
81         wptr=strtok(buf," ");
82         x1=atof(wptr);
83         wptr=strtok(NULL," ");
84         x2=atof(wptr);
85         wptr=strtok(NULL," ");
86         x3=atof(wptr);
87
88         cnt=get_line(posr,buf,256);
89         wptr=strtok(buf," ");
90         y1=atof(wptr);
91         wptr=strtok(NULL," ");
92         y2=atof(wptr);
93         wptr=strtok(NULL," ");
94         y3=atof(wptr);
95
96         cnt=get_line(posr,buf,256);
97         wptr=strtok(buf," ");
98         z1=atof(wptr);
99         wptr=strtok(NULL," ");
100         z2=atof(wptr);
101         wptr=strtok(NULL," ");
102         z3=atof(wptr);
103
104         /* norm */
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);
108
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;
113
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;
117
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;
121
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);
126
127         /* transformed basis */
128         x_1=X1;
129         x_2=costheta*X2-sintheta*X3;
130         x_3=sintheta*X2+costheta*X3;
131
132         y_1=Y1;
133         y_2=costheta*Y2-sintheta*Y3;
134         y_3=sintheta*Y2+costheta*Y3;
135
136         z_1=Z1;
137         z_2=costheta*Z2-sintheta*Z3;
138         z_3=sintheta*Z2+costheta*Z3;
139
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);
144
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);
149
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);
153
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);
157
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);
162
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);
166
167         // 6th line
168         cnt=get_line(posr,buf,256);
169         buf[cnt-1]='\n';
170         write(poso,buf,cnt);
171
172         // 7th line
173         cnt=get_line(posr,buf,256);
174         buf[cnt-1]='\n';
175         write(poso,buf,cnt);
176
177         // 8th line
178         cnt=get_line(posr,buf,256);
179         buf[cnt-1]='\n';
180         write(poso,buf,cnt);
181
182         while(1) {
183                 cnt=get_line(posr,buf,256);
184                 if(cnt<=0)
185                         break;
186                 wptr=strtok(buf," ");
187                 x1=atof(wptr);
188                 wptr=strtok(NULL," ");
189                 x2=atof(wptr);
190                 wptr=strtok(NULL," ");
191                 x3=atof(wptr);
192
193                 wptr=strtok(NULL," ");
194                 t1=wptr[0];
195                 wptr=strtok(NULL," ");
196                 t2=wptr[0];
197                 wptr=strtok(NULL," ");
198                 t3=wptr[0];
199
200                 X1=x1;
201                 X2=costheta*x2+sintheta*x3;
202                 X3=costheta*x3-sintheta*x2;
203
204                 /* check! */
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);
211
212                 dprintf(poso," %f %f %f %c %c %c\n",X1,X2,X3,t1,t2,t3);
213         }
214
215         close(posr);
216         close(poso);
217
218         printf("done!\n");
219
220         return 0;
221 }
222