added cbox creation tool
[physik/nlsop.git] / nlsop_create_cbox.c
1 #define _GNU_SOURCE
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8 #include <unistd.h>
9
10 #include <math.h>
11
12 #include <signal.h>
13
14 #include "nlsop.h"
15 #include "dfbapi.h"
16
17 int main(int argc,char **argv) {
18
19         int fd;
20         double atp;
21         int area,size,x,y,z,d;
22         int i,j,k;
23         d3_lattice d3l;
24         info info;
25         unsigned char *status;
26         int *conc;
27         double sigma;
28         int C,C0;
29
30         if(argc!=5) {
31                 printf("usage: %s <x> <y> <z> <conc>\n",argv[0]);
32                 return -23;
33         }
34
35         if((fd=open("cbox.save",O_WRONLY))<0) {
36                 perror("open");
37                 printf("unable to open save file!\n");
38                 return -23;
39         }
40
41         x=atoi(argv[1]);
42         y=atoi(argv[2]);
43         z=atoi(argv[3]);
44         d=500/3; /* maximum 180keV, so ... */
45         atp=atof(argv[4]);
46         area=x*y;
47         size=area*z;
48         C=(1.0*atp*SI_PER_VOLUME)/(1-atp);
49         sigma=(1.0*((700-500)/3)*((700-500)/3))/(log(C));
50
51         printf("sanity checks ...\n");
52
53         if(z<d) {
54                 printf("stupid!\n");
55                 return -23;
56         }
57
58         if(atp>=1) {
59                 printf("even more stupid!\n");
60                 return -23;
61         }
62
63         printf("malloc ...\n");
64
65         status=malloc(size*sizeof(unsigned char));
66         memset(status,0,size*sizeof(unsigned char));
67         conc=malloc(size*sizeof(int));
68
69         printf("computing c profile ...\n");
70
71         i=area*(d+1);
72         C0=C;
73         printf("  first constant part -> %d\n",C0);
74         for(k=0;k<i;k++) *(conc+k)=C0;
75         printf("  last gauss part:\n");
76         for(k=d+1;k<z;k++) {
77                 C=C0*exp(-1.0*(k-167)*(k-167)/sigma);
78                 printf("    c = %d\n",C);
79                 for(j=0;j<area;j++) *(conc+k*area+j)=C;
80         }
81
82         printf("nlsp header stuff ...\n");
83
84         d3l.max_x=x;
85         d3l.max_y=y;
86         d3l.max_z=z;
87         memset(&info,0,sizeof(info));
88
89         printf("writing file ...\n");
90
91         write(fd,&d3l,sizeof(d3_lattice));
92         write(fd,&info,sizeof(info));
93         write(fd,status,size*sizeof(unsigned char));
94         write(fd,conc,size*sizeof(int));
95
96         close(fd);
97
98         printf("done!\n");
99
100         return 1;
101 }
102