Merge branch 'leadoff'
[physik/posic.git] / postproc.c
index 68e0918..18834af 100644 (file)
  *
  */
 
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
 #include <math.h>
  
 #include "moldyn.h"
+#include "math/math.h"
+
+#define FILEMAX        127
+#define BUFMAX 127
+
+#define DISPLACEMENT   (1<<0)
+#define VELOCITY       (1<<1)
+
+static char *pse[]={
+        "*",
+        "H",
+        "He",
+        "Li",
+        "Be",
+        "B",
+        "C",
+        "N",
+        "O",
+        "F",
+        "Ne",
+        "Na",
+        "Mg",
+        "Al",
+        "Si",
+        "P",
+        "S",
+        "Cl",
+        "Ar",
+};
+
+int usage(void) {
+       printf("usage:\n");
+       printf("  -i <infile> -o <outfile>\n");
+       printf("  -d/v (displacement,velocity)\n");
+       printf(" \n");
+       return 1;
+}
 
 int main(int argc,char **argv) {
 
        t_moldyn md;
+       t_atom *atom;
+       char infile[FILEMAX+1];
+       int infd;
+       char outfile[FILEMAX+1];
+       int outfd;
+       int ret,size;
+       unsigned char mode;
+       char buf[BUFMAX+1];
+       t_3dvec dist;
+       int i;
+
+       memset(infile,0,FILEMAX+1);
+       memset(outfile,0,FILEMAX+1);
+       memset(buf,0,BUFMAX+1);
+       mode=0;
+
+       /* parse argv */
+       for(i=1;i<argc;i++) {
+
+               if(argv[i][0]!='-') {
+                       usage();
+                       return -1;
+               }
+               
+               switch(argv[i][1]) {
+                       case 'h':
+                               usage();
+                               return 1;
+                       case 'i':
+                               strncpy(infile,argv[++i],FILEMAX);
+                               break;
+                       case 'o':
+                               strncpy(outfile,argv[++i],FILEMAX);
+                               break;
+                       case 'd':
+                               mode=DISPLACEMENT;
+                               break;
+                       case 'v':
+                               mode=VELOCITY;
+                               break;
+                       default:
+                               usage();
+                               return -1;
+               }
+
+       }
+
+       /* open infile */
 
+       if(!strcmp(infile,"")) {
+               printf("no infile specified!\n");
+               return -1;
+       }
+       printf("infile -> %s\n",infile);
+
+       infd=open(infile,O_RDONLY);
+       if(infd<3) {
+               perror("infile");
+               return infd;
+       }
+
+       /* open outfile */
        
+       if(!strcmp(outfile,"")) {
+               printf("no outfile specified!\n");
+               return -1;
+       }
+       printf("outfile -> %s\n",outfile);
+
+       outfd=open(outfile,O_WRONLY|O_CREAT);
+       if(outfd<3) {
+               perror("outfile");
+               return outfd;
+       }
+
+       /* read in data */
+
+       size=sizeof(t_moldyn);
+       ret=0;
+       while(size-ret)
+               ret+=read(infd,&md,size-ret);
+       printf("read %d bytes moldyn struct data\n",size);
+
+       /* allocate memory for atoms */
+       size=md.count*sizeof(t_atom);
+       md.atom=malloc(size);
+       if(md.atom==NULL) {
+               perror("malloc");
+               goto end;
+       }
+
+       /* read in atom data */
+
+       ret=0;
+       atom=md.atom;
+       while(size-ret)
+               ret+=read(infd,atom,size-ret);
+       printf("read %d bytes atom data\n",size);
+
+       /* postprocess ... */
+       dprintf(outfd,"%d\n",md.count+8);
+       dprintf(outfd,"atoms at time %f\n",md.time);
+       for(i=0;i<md.count;i++) {
+printf("%d ",i);
+               dprintf(outfd,"%s %f %f %f",pse[atom[i].element],
+                                          atom[i].r.x,atom[i].r.y,atom[i].r.z);
+               if(mode&DISPLACEMENT) {
+                       v3_sub(&dist,&(atom[i].r),&(atom[i].r_0));
+                       check_per_bound(&md,&dist);
+                       dprintf(outfd," %f",v3_norm(&dist));
+               }
+               if(mode&VELOCITY) {
+                       dprintf(outfd," %f",atom[i].ekin);
+               }
+               dprintf(outfd,"\n");
+       }
+
+       /* free memory & close file descriptors */
+
+end:
+       free(md.atom);
+       close(infd);
+       close(outfd);
 
        return 0;
 }