Merge branch 'leadoff'
[physik/posic.git] / postproc.c
1 /*
2  * postproc.c - post processing moldyn data
3  *
4  * author: Frank Zirkelbach <frank.zirkelbach@physik.uni-augsburg.de>
5  *
6  */
7
8 #define _GNU_SOURCE
9 #include <stdio.h>
10 #include <unistd.h>
11 #include <stdlib.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <fcntl.h>
15
16 #include <math.h>
17  
18 #include "moldyn.h"
19 #include "math/math.h"
20
21 #define FILEMAX 127
22 #define BUFMAX  127
23
24 #define DISPLACEMENT    (1<<0)
25 #define VELOCITY        (1<<1)
26
27 static char *pse[]={
28         "*",
29         "H",
30         "He",
31         "Li",
32         "Be",
33         "B",
34         "C",
35         "N",
36         "O",
37         "F",
38         "Ne",
39         "Na",
40         "Mg",
41         "Al",
42         "Si",
43         "P",
44         "S",
45         "Cl",
46         "Ar",
47 };
48
49 int usage(void) {
50         printf("usage:\n");
51         printf("  -i <infile> -o <outfile>\n");
52         printf("  -d/v (displacement,velocity)\n");
53         printf(" \n");
54         return 1;
55 }
56
57 int main(int argc,char **argv) {
58
59         t_moldyn md;
60         t_atom *atom;
61         char infile[FILEMAX+1];
62         int infd;
63         char outfile[FILEMAX+1];
64         int outfd;
65         int ret,size;
66         unsigned char mode;
67         char buf[BUFMAX+1];
68         t_3dvec dist;
69         int i;
70
71         memset(infile,0,FILEMAX+1);
72         memset(outfile,0,FILEMAX+1);
73         memset(buf,0,BUFMAX+1);
74         mode=0;
75
76         /* parse argv */
77         for(i=1;i<argc;i++) {
78
79                 if(argv[i][0]!='-') {
80                         usage();
81                         return -1;
82                 }
83                 
84                 switch(argv[i][1]) {
85                         case 'h':
86                                 usage();
87                                 return 1;
88                         case 'i':
89                                 strncpy(infile,argv[++i],FILEMAX);
90                                 break;
91                         case 'o':
92                                 strncpy(outfile,argv[++i],FILEMAX);
93                                 break;
94                         case 'd':
95                                 mode=DISPLACEMENT;
96                                 break;
97                         case 'v':
98                                 mode=VELOCITY;
99                                 break;
100                         default:
101                                 usage();
102                                 return -1;
103                 }
104
105         }
106
107         /* open infile */
108
109         if(!strcmp(infile,"")) {
110                 printf("no infile specified!\n");
111                 return -1;
112         }
113         printf("infile -> %s\n",infile);
114
115         infd=open(infile,O_RDONLY);
116         if(infd<3) {
117                 perror("infile");
118                 return infd;
119         }
120
121         /* open outfile */
122         
123         if(!strcmp(outfile,"")) {
124                 printf("no outfile specified!\n");
125                 return -1;
126         }
127         printf("outfile -> %s\n",outfile);
128
129         outfd=open(outfile,O_WRONLY|O_CREAT);
130         if(outfd<3) {
131                 perror("outfile");
132                 return outfd;
133         }
134
135         /* read in data */
136
137         size=sizeof(t_moldyn);
138         ret=0;
139         while(size-ret)
140                 ret+=read(infd,&md,size-ret);
141         printf("read %d bytes moldyn struct data\n",size);
142
143         /* allocate memory for atoms */
144         size=md.count*sizeof(t_atom);
145         md.atom=malloc(size);
146         if(md.atom==NULL) {
147                 perror("malloc");
148                 goto end;
149         }
150
151         /* read in atom data */
152
153         ret=0;
154         atom=md.atom;
155         while(size-ret)
156                 ret+=read(infd,atom,size-ret);
157         printf("read %d bytes atom data\n",size);
158
159         /* postprocess ... */
160         dprintf(outfd,"%d\n",md.count+8);
161         dprintf(outfd,"atoms at time %f\n",md.time);
162         for(i=0;i<md.count;i++) {
163 printf("%d ",i);
164                 dprintf(outfd,"%s %f %f %f",pse[atom[i].element],
165                                            atom[i].r.x,atom[i].r.y,atom[i].r.z);
166                 if(mode&DISPLACEMENT) {
167                         v3_sub(&dist,&(atom[i].r),&(atom[i].r_0));
168                         check_per_bound(&md,&dist);
169                         dprintf(outfd," %f",v3_norm(&dist));
170                 }
171                 if(mode&VELOCITY) {
172                         dprintf(outfd," %f",atom[i].ekin);
173                 }
174                 dprintf(outfd,"\n");
175         }
176
177         /* free memory & close file descriptors */
178
179 end:
180         free(md.atom);
181         close(infd);
182         close(outfd);
183
184         return 0;
185 }