2 * pc_calc.c - calculate the pc
4 * author: frank.zirkelbach@physik.uni-augsburg.de
11 #include <sys/types.h>
22 typedef struct s_atom {
27 int get_line(int fd,char *line,int max) {
34 if(count==max) return count;
35 ret=read(fd,line+count,1);
36 if(ret<=0) return ret;
37 if(line[count]=='\n') {
38 memset(line+count,0,max-count-1);
46 int get_max3_vals(char *buf,int amount,double *v1, double *v2,double *v3) {
53 for(k=0;k<amount;k++) {
57 while((buf[i+j]!=' ')&&(buf[i+j]!=0))
61 if(k==0) *v1=atof(temp);
62 if(k==1) *v2=atof(temp);
63 if(k==2) *v3=atof(temp);
70 int get_2_ints(char *buf,int *i1,int *i2) {
81 while((buf[i+j]!=' ')&&(buf[i+j]!=0))
85 if(k==0) *i1=atoi(temp);
86 if(k==1) *i2=atoi(temp);
93 int main(int argc,char **argv) {
96 double *aslot,*bslot,*cslot;
97 int cnt,count,fcnt,slots;
102 double dx,dy,dz,dist,norm;
104 double X1,X2,X3,Y1,Y2,Y3,Z1,Z2,Z3;
110 printf("usage: %s file1 file2 ...\n",
117 aslot=malloc(slots*sizeof(double));
122 memset(aslot,0,slots*sizeof(double));
123 bslot=malloc(slots*sizeof(double));
128 memset(bslot,0,slots*sizeof(double));
129 cslot=malloc(slots*sizeof(double));
134 memset(cslot,0,slots*sizeof(double));
135 printf("i: allocated 3 times %d slots ...\n",slots);
137 // use all given files ...
138 printf("using files:\n");
139 for(fcnt=1;fcnt<argc;fcnt++)
140 printf(" %d: %s\n",fcnt,argv[fcnt]);
142 for(fcnt=1;fcnt<argc;fcnt++) {
144 fd=open(argv[fcnt],O_RDONLY);
146 perror("open file\n");
151 cnt=get_line(fd,buf,256);
153 // second line -> scale
154 cnt=get_line(fd,buf,256);
155 get_max3_vals(buf,1,&scale,NULL,NULL);
156 printf("scale: %f\n",scale);
159 cnt=get_line(fd,buf,256);
160 get_max3_vals(buf,3,&X1,&X2,&X3);
161 printf("X: %f %f %f\n",X1,X2,X3);
164 cnt=get_line(fd,buf,256);
165 get_max3_vals(buf,3,&Y1,&Y2,&Y3);
166 printf("Y: %f %f %f\n",Y1,Y2,Y3);
169 cnt=get_line(fd,buf,256);
170 get_max3_vals(buf,3,&Z1,&Z2,&Z3);
171 printf("Z: %f %f %f\n",Z1,Z2,Z3);
173 // 6th line -> nsi + nc = ntot
174 cnt=get_line(fd,buf,256);
175 get_2_ints(buf,&nsi,&nc);
177 printf("Si: %d - C: %d - tot: %d\n",nsi,nc,ntot);
180 cnt=get_line(fd,buf,256);
183 cnt=get_line(fd,buf,256);
188 cnt=get_line(fd,buf,256);
190 printf("Should never happen!\n");
194 atom=realloc(atom,(count+1)*sizeof(t_atom));
196 atom[count].type='S';
198 atom[count].type='C';
208 printf("i: read in %d atoms ...\n",count);
211 for(i=0;i<count;i++) {
213 dx=atom[i].x-atom[j].x;
219 dy=atom[i].y-atom[j].y;
225 dz=atom[i].z-atom[j].z;
231 dxt=dx*X1+dy*Y1+dz*Z1;
232 dyt=dx*X2+dy*Y2+dz*Z2;
233 dzt=dx*X3+dy*Y3+dz*Z3;
235 dist=sqrt(dxt*dxt+dyt*dyt+dzt*dzt);
241 if((atom[i].type=='S')&&(atom[j].type=='S')) {
244 else if((atom[i].type=='C')&&(atom[j].type=='C')) {
257 // normalization and output
258 for(i=1;i<slots;i++) {
259 norm=4*M_PI*(i*DELTA)*(i*DELTA)*DELTA;
261 printf("pc: %f %f %f %f\n",i*DELTA,