Merge branch 'leadoff'
[physik/posic.git] / pair_correlation_calc.c
1 /*
2  * calcultae pair correlation function
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 <unistd.h>
12 #include <string.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <fcntl.h>
16
17 #include "moldyn.h"
18
19 int usage(char *prog) {
20
21         printf("\nusage:\n");
22         printf("  %s <dr> <save file 1> [<save file 2> ...]\n\n",prog);
23
24         return -1;
25 }
26
27 int main(int argc,char **argv) {
28
29         t_moldyn moldyn;
30         int ret;
31         double *stat,*total;
32         int slots;
33         int i,j;
34         double dr;
35         int fd;
36         unsigned char first;
37
38         if(argc<3) {
39                 usage(argv[0]);
40                 return -1;
41         }
42
43         dr=atof(argv[1]);
44
45         first=1;
46         stat=NULL;
47         total=NULL;
48
49         for(j=2;j<argc;j++) {
50
51                 memset(&moldyn,0,sizeof(t_moldyn));
52
53                 printf("[pair corr calc] reading save file ...\n");
54                 ret=moldyn_read_save_file(&moldyn,argv[j]);
55                 if(ret) {
56                         printf("[pair corr calc] exit!\n");
57                         return ret;
58                 }
59
60                 //moldyn.cutoff*=2;
61                 //moldyn.cutoff_square*=4;
62                 moldyn.cutoff=6.0;
63                 moldyn.cutoff_square=36.0;
64
65                 slots=moldyn.cutoff/dr;
66                 printf("[pair corr calc]\n");
67                 printf("  slots: %d\n",slots);
68                 printf("  cutoff: %f\n",moldyn.cutoff);
69                 printf("  dr: %f\n",dr);
70
71                 if(first) {
72                         stat=(double *)malloc(3*slots*sizeof(double));
73                         total=(double *)malloc(3*slots*sizeof(double));
74                         first=0;
75                         if(stat==NULL) {
76                                 perror("[pair corr calc] alloc mem (stat)");
77                                 return -1;
78                         }
79                         if(total==NULL) {
80                                 perror("[pair corr calc] alloc mem (toal)");
81                                 return -1;
82                         }
83                         memset(total,0,3*slots*sizeof(double));
84                 }
85
86                 /* link cell init */
87                 link_cell_init(&moldyn,VERBOSE);
88
89                 calculate_pair_correlation(&moldyn,dr,stat);
90
91                 for(i=0;i<3*slots;i++)
92                         total[i]+=stat[i];
93
94         }
95
96         fd=open("pair_corr_func.txt",
97                 O_WRONLY|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR);
98         dprintf(fd,"#r #ab #aa #bb\n");
99         for(i=0;i<slots;i++)
100                 dprintf(fd,"%f %f %f %f\n",
101                         i*dr,total[i],total[slots+i],total[2*slots+i]);
102         close(fd);
103
104         free(stat);
105         free(total);
106
107         moldyn_free_save_file(&moldyn);
108
109         return 0;
110 }