first c conc distribution implementation (slow and commented out)
[physik/morpheus.git] / main.c
1 /*
2  * morpheus - main.c
3  *
4  * this program tries helping to understand the amorphous depuration
5  * and recrystallization of SiCx while ion implanation. hopefully the program 
6  * will simulate the stabilization of the selforganizing structure in the
7  * observed behaviour.
8  *
9  * refs: 
10  *  - J. K. N. Lindner. Habilationsschrift, Universitaet Augsburg.
11  *  - Maik Haeberlen. Diplomarbeit, Universitaet Augsburg.
12  */
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <fcntl.h>
20
21 /* important defines */
22 #include "defines.h"
23
24 /* function prototypes */
25 #include "random.h"
26 #include "display.h"
27
28 /* global variables */
29 u32 gr;
30 int random_fd; /* /dev/urandom file descriptor */
31
32 int usage()
33 {
34  puts("usage:");
35  puts("-h: help");
36  printf("-a <value> \t slope of nuclear energy loss (default %d)\n",DEFAULT_SLOPE_NEL);
37  printf("-c <value> \t nuclear enery loss at depths 0 (default %d)\n",DEFAULT_START_NEL);
38  printf("-x <value> \t # x cells (default %d)\n",DEFAULT_X_SEG);
39  printf("-y <value> \t # y cells (default %d)\n",DEFAULT_Y_SEG);
40  printf("-z <value> \t # z cells (default %d)\n",DEFAULT_Z_SEG);
41  printf("-s <value> \t # steps to calculate (default %d)\n",DEFAULT_STEPS);
42  puts("-X <value> \t display area intercept point x (default # x celss / 2)");
43  puts("-Y <value> \t display area intercept point y (default # y cells / 2)");
44  puts("-Z <value> \t display area intercept point z (default # z cells / 2)");
45  printf("-d <value> \t refresh every <value> loops (default %d)\n",DEFAULT_DISPLAY_REF_RATE);
46  printf("-r <value> \t pressure range from amorphous SiCx (default %d)\n",DEFAULT_A_P_RANGE);
47  printf("-f <value> \t faktor for pressure from amorphous SiCx (default %f)\n",DEFAULT_A_P_FAKTOR);
48  printf("-p <value> \t p0 for probability of cell getting amorph (default %f)\n",DEFAULT_A_P_P0);
49  printf("-C <value> \t C start concentration (default %d)\n",DEFAULT_C_DIST_START_CONC);
50  printf("-S <value> \t slope of linear C distribution (default %d)\n",DEFAULT_C_DIST_SLOPE);
51  return -23;
52 }
53
54 int make_amorph(cell *cell)
55 {
56  cell->status|=AMORPH;
57  return 23;
58 }
59
60 int make_cryst(cell *cell)
61 {
62  cell->status&=~AMORPH;
63  return 23;
64 }
65
66 int distrib_c_conc(cell *cell_p,int c_c0,int c_slope,u32 c_conc,u32 x_max,u32 y_max,u32 z_max)
67 {
68  /* cryst. c to distribute */
69
70  int i,j;
71  u32 area,c_area,total;
72  u32 x,y,z,sum_c_z;
73
74  total=0;
75  area=x_max*y_max;
76  sum_c_z=c_c0*z_max+c_slope*gr;
77
78  for(i=0;i<z_max;i++)
79  {
80   c_area=(c_conc*(c_c0+(i+1)*c_slope))/sum_c_z;
81   for(j=0;j<area;j++)
82   { 
83    if(!(cell_p+j+i*area)->status&AMORPH)
84    { 
85     (cell_p+j+i*area)->conc=c_area/area;
86     total+=c_area/area;
87    }
88   }
89   j=0;
90   while(j<c_area%area)
91   {
92    x=rand_get(x_max);
93    y=rand_get(y_max);
94    if(!(cell_p+x+y*x_max+i*area)->status&AMORPH)
95    {
96     (cell_p+x+y*x_max+i*area)->conc+=1;
97     total++;
98     j++;
99    }
100   }
101  }
102  i=0;
103  while(i<c_conc-total)
104  {
105   x=rand_get(x_max);
106   y=rand_get(y_max);
107   z=rand_get_lgp(c_slope,c_c0,z_max);
108   if(!(cell_p+x+y*x_max+z*area)->status&AMORPH)
109   {
110    (cell_p+x+y*x_max+z*area)->conc+=1;
111    i++;
112   }
113  }
114  return 23;
115 }
116
117 /* look at cell ... */
118 int process_cell(cell *cell_p,u32 x,u32 y,u32 z,u32 x_max,u32 y_max,u32 z_max,int range,double faktor,double p0,u32 *c_conc)
119 {
120  cell *this_cell;
121  int i,j,k;
122  double pressure;
123  this_cell=cell_p+x+y*x_max+z*x_max*y_max;
124  pressure=p0*URAND_2BYTE_MAX;
125  for(i=-range;i<=range;i++)
126  {
127   for(j=-range;j<=range;j++)
128   {
129    for(k=-range;k<=range;k++)
130    {
131     if(!(i==0 && j==0 && k==0))
132     {
133      // if((cell_p+((x+x_max+i)%x_max)+((y+j+y_max)%y_max)*x_max+((z+k+z_max)%z_max)*x_max*y_max)->status&AMORPH) pressure+=(cell_p+((x+x_max+i)%x_max)+((y+j+y_max)%y_max)*x_max+((z+k+z_max)%z_max)*x_max*y_max)->conc*faktor*URAND_2BYTE_MAX/(i*i+j*j+k*k);
134      if((cell_p+((x+x_max+i)%x_max)+((y+j+y_max)%y_max)*x_max+((z+k+z_max)%z_max)*x_max*y_max)->status&AMORPH) pressure+=faktor*URAND_2BYTE_MAX/(i*i+j*j+k*k);
135     }
136    }
137   }
138  }
139  if(this_cell->status&AMORPH)
140  {
141   /* wrong probability! just test by now ...*/
142   if(rand_get(URAND_2BYTE_MAX)>pressure)
143   {
144     make_cryst(this_cell);
145     *c_conc=*c_conc+1+this_cell->conc;
146   } else *c_conc+=1;
147  } else
148  {
149   if(rand_get(URAND_2BYTE_MAX)<=pressure)
150   {
151    make_amorph(this_cell);
152    *c_conc=*c_conc+1-this_cell->conc;
153   } else *c_conc+=1;
154  }
155  return 23;
156 }
157
158 int main(int argc,char **argv) 
159 {
160  u32 x_cell,y_cell,z_cell; /* amount of segments */
161  u32 x,y,z; /* cells */
162  int i; /* for counting */
163  int slope_nel,start_nel; /* nuclear energy loss: slope, constant */
164  int a_p_range; /* p. range of amorphous sic */
165  double a_p_faktor,a_p_p0; /* p0 and faktor of amorphous sic */
166  int c_dist_c0,c_dist_slope; /* c start concentration and linear slope of c distribution */
167  u32 c_conc;
168  int steps; /* # steps */
169  cell *cell_p;
170  struct __display display;
171  u32 display_x,display_y,display_z; /* intercept point of diplayed areas */
172  u32 display_refresh_rate; /* refresh rate for display */
173  int quit=0; /* continue/quit status */
174
175  printfd("debug: sizeof my u32 variable: %d\n",sizeof(u32));
176  printfd("debug: sizeof my cell struct: %d\n",sizeof(cell));
177
178  /* default values */
179  x_cell=DEFAULT_X_SEG;
180  y_cell=DEFAULT_Y_SEG;
181  z_cell=DEFAULT_Z_SEG;
182  slope_nel=DEFAULT_SLOPE_NEL;
183  start_nel=DEFAULT_START_NEL;
184  a_p_range=DEFAULT_A_P_RANGE;
185  a_p_faktor=DEFAULT_A_P_FAKTOR;
186  a_p_p0=DEFAULT_A_P_P0;
187  c_dist_c0=DEFAULT_C_DIST_START_CONC;
188  c_dist_slope=DEFAULT_C_DIST_SLOPE;
189  c_conc=DEFAULT_C_C0;
190  steps=DEFAULT_STEPS;
191  display_x=x_cell/2;
192  display_y=y_cell/2;
193  display_z=z_cell/2;
194  display_refresh_rate=DEFAULT_DISPLAY_REF_RATE;
195  
196  /* parse command args */
197  for(i=1;i<argc;i++)
198  {
199   if(argv[i][0]=='-')
200   {
201    switch(argv[i][1])
202    {
203     case 'h':
204      usage();
205      return 23;
206      break;
207     case 'a':
208      slope_nel=atoi(argv[++i]);
209      break;
210     case 'c':
211      start_nel=atoi(argv[++i]);
212      break;
213     case 'x':
214      x_cell=atoi(argv[++i]);
215      break;
216     case 'y':
217      y_cell=atoi(argv[++i]);
218      break;
219     case 'z':
220      z_cell=atoi(argv[++i]);
221      break;
222     case 's':
223      steps=atoi(argv[++i]);
224      break;
225     case 'X':
226      display_x=atoi(argv[++i]);
227      break;
228     case 'Y':
229      display_y=atoi(argv[++i]);
230      break;
231     case 'Z':
232      display_z=atoi(argv[++i]);
233      break;
234     case 'd':
235      display_refresh_rate=atoi(argv[++i]);
236      break;
237     case 'r':
238      a_p_range=atoi(argv[++i]);
239      break;
240     case 'f':
241      a_p_faktor=atof(argv[++i]);
242      break;
243     case 'p':
244      a_p_p0=atof(argv[++i]);
245      break;
246     case 'b':
247      c_conc=atoi(argv[++i]);
248      break;
249     case 'C':
250      c_dist_c0=atoi(argv[++i]);
251      break;
252     case 'S':
253      c_dist_slope=atoi(argv[++i]);
254      break;
255     default:
256      usage();
257      return -23;
258    }
259   } else usage();
260  }
261
262  /* open random fd */
263  if((random_fd=open("/dev/urandom",O_RDONLY))<0)
264  {
265   puts("cannot open /dev/urandom\n");
266   return -23;
267  }
268
269  /* calculate gr one time! */
270  gr=0;
271  for(i=1;i<=z_cell;i++) gr+=i;
272  printfd("debug: gr = %d\n",gr);
273
274  /* allocate cells */
275  printf("malloc will free %d bytes now ...\n",x_cell*y_cell*z_cell*sizeof(cell));
276  if((cell_p=(cell *)malloc(x_cell*y_cell*z_cell*sizeof(cell)))==NULL)
277  {
278   puts("failed allocating memory for cells\n");
279   return -23;
280  }
281  memset(cell_p,0,x_cell*y_cell*z_cell*sizeof(cell));
282
283  /* init display */
284  printfd("debug: init display now ...\n");
285  display_init(x_cell,y_cell,z_cell,&display,cell_p,&argc,argv);
286
287  /* main routine */
288  printfd("debug: starting main routine ...\n");
289  for(i=0;i<steps;i++)
290  {
291   x=rand_get(x_cell);
292   y=rand_get(y_cell);
293   z=rand_get_lgp(slope_nel,start_nel,z_cell);
294
295   /* todo */
296   // distrib_c_conc(cell_p,c_dist_c0,c_dist_slope,c_conc,x_cell,y_cell,z_cell);
297
298   // process_cell(cell_p+x+y*x_cell+z*x_cell*y_cell);
299   process_cell(cell_p,x,y,z,x_cell,y_cell,z_cell,a_p_range,a_p_faktor,a_p_p0,&c_conc);
300
301   /* display stuff */
302   if((i%display_refresh_rate)==0)
303    display_draw(&display,display_x,display_y,display_z);
304  }
305  printfd("debug: main routine finished!\n");
306   
307  /* display again and listen for events */
308  display_draw(&display,display_x,display_y,display_z);
309  display_event_init(&display);
310
311  while(!quit)
312  {
313   display_scan_event(&display,&display_x,&display_y,&display_z,&quit);
314   display_draw(&display,display_x,display_y,display_z);
315  }
316
317  display_release(&display);
318
319  return 23;
320 }