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