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