added c0 in usage()
[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 #define _GNU_SOURCE
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <fcntl.h>
21 #include <unistd.h>
22
23 /* important defines */
24 #include "defines.h"
25
26 /* function prototypes */
27 #include "random.h"
28 #include "display.h"
29
30 /* global variables */
31 u32 *rand_buf,*rand_current;
32 char random_file[MAX_CHARS_RANDOM_FILE];
33 u32 gr;
34 int random_fd; /* /dev/urandom file descriptor */
35
36 int usage()
37 {
38  puts("usage:");
39  puts("-h: help");
40  printf("-a <value> \t slope of nuclear energy loss (default %d)\n",DEFAULT_SLOPE_NEL);
41  printf("-c <value> \t nuclear enery loss at depths 0 (default %d)\n",DEFAULT_START_NEL);
42  printf("-x <value> \t # x cells (default %d)\n",DEFAULT_X_SEG);
43  printf("-y <value> \t # y cells (default %d)\n",DEFAULT_Y_SEG);
44  printf("-z <value> \t # z cells (default %d)\n",DEFAULT_Z_SEG);
45  printf("-s <value> \t # steps to calculate (default %d)\n",DEFAULT_STEPS);
46  puts("-X <value> \t display area intercept point x (default # x celss / 2)");
47  puts("-Y <value> \t display area intercept point y (default # y cells / 2)");
48  puts("-Z <value> \t display area intercept point z (default # z cells / 2)");
49  printf("-d <value> \t refresh every <value> loops (default %d)\n",DEFAULT_DISPLAY_REF_RATE);
50  printf("-r <value> \t pressure range from amorphous SiCx (default %d)\n",DEFAULT_A_P_RANGE);
51  printf("-f <value> \t faktor for pressure from amorphous SiCx (default %f)\n",DEFAULT_A_P_FAKTOR);
52  printf("-p <value> \t p0 for probability of cell getting amorph (default %f)\n",DEFAULT_A_P_P0);
53  printf("-C <value> \t C disribution start concentration (default %d)\n",DEFAULT_C_DIST_START_CONC);
54  printf("-S <value> \t slope of linear C distribution (default %d)\n",DEFAULT_C_DIST_SLOPE);
55  printf("-b <value> \t initial C concentration (default %d)\n",DEFAULT_C_C0);
56  puts("-R <file> \t read random data from file (default not used)");
57  puts("-D <file> \t dump cell info into <file> (default not used)");
58  puts("-L <file> \t load cell info and display it (default no)");
59  puts("-n \t do not wait for user interaction (default no)");
60  return -23;
61 }
62
63 int make_amorph(cell *cell)
64 {
65  cell->status|=AMORPH;
66  return 23;
67 }
68
69 int make_cryst(cell *cell)
70 {
71  cell->status&=~AMORPH;
72  return 23;
73 }
74
75 int load_from_file(char *file,display *display)
76 {
77  int load_file_fd;
78
79  if((load_file_fd=open(file,O_RDONLY))<0)
80  {
81   puts("cannot open load file");
82   return -23;
83  }
84  if(read(load_file_fd,&(display->max_x),sizeof(u32))<sizeof(u32))
85  {
86   puts("failed reading max x");
87   return-23;
88  }
89  if(read(load_file_fd,&(display->max_y),sizeof(u32))<sizeof(u32))
90  {
91   puts("failed reading max y");
92   return -23;
93  }
94  if(read(load_file_fd,&(display->max_z),sizeof(u32))<sizeof(u32))
95  {
96   puts("failed reading max z");
97   return -23;
98  }
99  if(read(load_file_fd,display->cell_p,display->max_x*display->max_y*display->max_z*sizeof(cell))<display->max_x*display->max_y*display->max_z*sizeof(cell))
100  {
101   puts("failed reading cell info from file");
102   return -23;
103  }
104  close(load_file_fd);
105  puts("loaded file");
106  return 23;
107 }
108
109 int save_to_file(char *file,display *display)
110 {
111  int save_file_fd;
112
113  if((save_file_fd=open(file,O_CREAT|O_WRONLY|O_TRUNC))<0)
114  {
115   puts("cannot open save file");
116   return -23;
117  }
118  if(write(save_file_fd,&(display->max_x),sizeof(u32))<sizeof(u32))
119  {
120   puts("failed saving max x");
121   return -23;
122  }
123  if(write(save_file_fd,&(display->max_y),sizeof(u32))<sizeof(u32))
124  {
125   puts("failed saving max y");
126   return -23;
127  }
128  if(write(save_file_fd,&(display->max_z),sizeof(u32))<sizeof(u32))
129  {
130   puts("failed saving max z");
131   return -23;
132  }
133  if(write(save_file_fd,display->cell_p,display->max_x*display->max_y*display->max_z*sizeof(cell))!=display->max_x*display->max_y*display->max_z*sizeof(cell))
134  {
135   puts("saving file failed");
136   return -23;
137  }
138  close(save_file_fd);
139  puts("saved file");
140  return 23;
141 }
142
143 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)
144 {
145  int i,j;
146  u32 area,c_area,total,count;
147  u32 x,y,z,sum_c_z;
148
149  total=0;
150  area=x_max*y_max;
151  sum_c_z=c_c0*z_max+c_slope*gr;
152
153  for(i=0;i<z_max;i++)
154  {
155   count=0;
156   c_area=(c_conc*(c_c0+(i+1)*c_slope))/sum_c_z;
157   for(j=0;j<area;j++)
158   {
159    if(!(cell_p+j+i*area)->status&AMORPH) count++; 
160   }
161   for(j=0;j<area;j++)
162   {
163    if(!(cell_p+j+i*area)->status&AMORPH)
164    { 
165     (cell_p+j+i*area)->conc=c_area/count;
166     total+=c_area/count;
167    }
168   }
169   j=0;
170   while(j<c_area%count)
171   {
172    x=rand_get(x_max);
173    y=rand_get(y_max);
174    if(!(cell_p+x+y*x_max+i*area)->status&AMORPH)
175    {
176     (cell_p+x+y*x_max+i*area)->conc+=1;
177     total++;
178     j++;
179    }
180   }
181  }
182  i=0;
183  while(i<c_conc-total)
184  {
185   x=rand_get(x_max);
186   y=rand_get(y_max);
187   z=rand_get_lgp(c_slope,c_c0,z_max);
188   if(!(cell_p+x+y*x_max+z*area)->status&AMORPH)
189   {
190    (cell_p+x+y*x_max+z*area)->conc+=1;
191    i++;
192   }
193  }
194  return 23;
195 }
196
197 /* look at cell ... */
198 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)
199 {
200  cell *this_cell;
201  int i,j,k;
202  double pressure;
203
204  this_cell=cell_p+x+y*x_max+z*x_max*y_max;
205  pressure=p0*URAND_2BYTE_MAX;
206
207  for(i=-range;i<=range;i++)
208  {
209   for(j=-range;j<=range;j++)
210   {
211    // z relaxation, no pressure in z direction
212    // for(k=-range;k<=range;k++)
213    // {
214     if(!(i==0 && j==0)) //  && k==0))
215     {
216      // 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);
217      if((cell_p+((x+x_max+i)%x_max)+((y+j+y_max)%y_max)*x_max+z*x_max*y_max)->status&AMORPH) pressure+=faktor*URAND_2BYTE_MAX/(i*i+j*j);
218     }
219    // }
220   }
221  }
222  pressure*=this_cell->conc;
223  if(this_cell->status&AMORPH)
224  {
225   /* wrong probability! just test by now ...*/
226   if(rand_get(URAND_2BYTE_MAX)>pressure)
227   {
228     make_cryst(this_cell);
229     *c_conc=*c_conc+1+this_cell->conc;
230   } else *c_conc+=1;
231  } else
232  {
233   if(rand_get(URAND_2BYTE_MAX)<=pressure)
234   {
235    make_amorph(this_cell);
236    *c_conc=*c_conc+1-this_cell->conc;
237   } else *c_conc+=1;
238  }
239  return 23;
240 }
241
242 int main(int argc,char **argv) 
243 {
244  u32 x_cell,y_cell,z_cell; /* amount of segments */
245  u32 x,y,z; /* cells */
246  int i; /* for counting */
247  int slope_nel,start_nel; /* nuclear energy loss: slope, constant */
248  int a_p_range; /* p. range of amorphous sic */
249  double a_p_faktor,a_p_p0; /* p0 and faktor of amorphous sic */
250  int c_dist_c0,c_dist_slope; /* c start concentration and linear slope of c distribution */
251  u32 c_conc;
252  int steps; /* # steps */
253  cell *cell_p;
254  struct __display display;
255  u32 display_x,display_y,display_z; /* intercept point of diplayed areas */
256  u32 display_refresh_rate; /* refresh rate for display */
257  int quit=0; /* continue/quit status */
258  char save_file[MAX_CHARS_SAVE_FILE];
259  char load_file[MAX_CHARS_LOAD_FILE];
260  unsigned char no_wait;
261
262  printfd("debug: sizeof my u32 variable: %d\n",sizeof(u32));
263  printfd("debug: sizeof my cell struct: %d\n",sizeof(cell));
264
265  /* default values */
266  x_cell=DEFAULT_X_SEG;
267  y_cell=DEFAULT_Y_SEG;
268  z_cell=DEFAULT_Z_SEG;
269  slope_nel=DEFAULT_SLOPE_NEL;
270  start_nel=DEFAULT_START_NEL;
271  a_p_range=DEFAULT_A_P_RANGE;
272  a_p_faktor=DEFAULT_A_P_FAKTOR;
273  a_p_p0=DEFAULT_A_P_P0;
274  c_dist_c0=DEFAULT_C_DIST_START_CONC;
275  c_dist_slope=DEFAULT_C_DIST_SLOPE;
276  c_conc=DEFAULT_C_C0;
277  steps=DEFAULT_STEPS;
278  display_x=x_cell/2;
279  display_y=y_cell/2;
280  display_z=z_cell/2;
281  display_refresh_rate=DEFAULT_DISPLAY_REF_RATE;
282  strcpy(random_file,"");
283  strcpy(save_file,"");
284  strcpy(load_file,"");
285  no_wait=0;
286  
287  /* parse command args */
288  for(i=1;i<argc;i++)
289  {
290   if(argv[i][0]=='-')
291   {
292    switch(argv[i][1])
293    {
294     case 'h':
295      usage();
296      return 23;
297      break;
298     case 'a':
299      slope_nel=atoi(argv[++i]);
300      break;
301     case 'c':
302      start_nel=atoi(argv[++i]);
303      break;
304     case 'x':
305      x_cell=atoi(argv[++i]);
306      break;
307     case 'y':
308      y_cell=atoi(argv[++i]);
309      break;
310     case 'z':
311      z_cell=atoi(argv[++i]);
312      break;
313     case 's':
314      steps=atoi(argv[++i]);
315      break;
316     case 'X':
317      display_x=atoi(argv[++i]);
318      break;
319     case 'Y':
320      display_y=atoi(argv[++i]);
321      break;
322     case 'Z':
323      display_z=atoi(argv[++i]);
324      break;
325     case 'd':
326      display_refresh_rate=atoi(argv[++i]);
327      break;
328     case 'r':
329      a_p_range=atoi(argv[++i]);
330      break;
331     case 'f':
332      a_p_faktor=atof(argv[++i]);
333      break;
334     case 'p':
335      a_p_p0=atof(argv[++i]);
336      break;
337     case 'b':
338      c_conc=atoi(argv[++i]);
339      break;
340     case 'C':
341      c_dist_c0=atoi(argv[++i]);
342      break;
343     case 'S':
344      c_dist_slope=atoi(argv[++i]);
345      break;
346     case 'R':
347      strcpy(random_file,argv[++i]);
348      break;
349     case 'D':
350      strcpy(save_file,argv[++i]);
351      break;
352     case 'L':
353      strcpy(load_file,argv[++i]);
354      break;
355     case 'n':
356      no_wait=1;
357      break;
358     default:
359      usage();
360      return -23;
361    }
362   } else usage();
363  }
364
365  /* open random fd */
366  if(!strcmp(random_file,""))
367  {
368   if((random_fd=open("/dev/urandom",O_RDONLY))<0)
369   {
370    puts("cannot open /dev/urandom");
371    return -23;
372   }
373  } else
374  {
375   if((random_fd=open(random_file,O_RDONLY))<0)
376   {
377    puts("cannot open random file");
378    return -23;
379   }
380  }
381  /* allocate random number buffer */
382  printf("malloc will free %d bytes now ...\n",RAND_BUF_SIZE);
383  if((rand_buf=(u32 *)malloc(RAND_BUF_SIZE))==NULL)
384  {
385   puts("failed allocating memory for random numbers");
386   return -23;
387  }
388  rand_current=rand_buf+RAND_BUF_SIZE;
389
390  /* calculate gr one time! */
391  gr=0;
392  for(i=1;i<=z_cell;i++) gr+=i;
393  printfd("debug: gr = %d\n",gr);
394
395  /* allocate cells */
396  printf("malloc will free %d bytes now ...\n",x_cell*y_cell*z_cell*sizeof(cell));
397  if((cell_p=(cell *)malloc(x_cell*y_cell*z_cell*sizeof(cell)))==NULL)
398  {
399   puts("failed allocating memory for cells");
400   return -23;
401  }
402  memset(cell_p,0,x_cell*y_cell*z_cell*sizeof(cell));
403
404  /* load from file */
405  if(strcmp(load_file,""))
406  {
407   display.cell_p=cell_p;
408   load_from_file(load_file,&display);
409   x_cell=display.max_x;
410   y_cell=display.max_y;
411   z_cell=display.max_z;
412  }
413
414  /* init display */
415  printfd("debug: init display now ...\n");
416  display_init(x_cell,y_cell,z_cell,&display,cell_p,&argc,argv);
417
418  /* main routine */
419  
420  /* did we load from file? */
421  if(!strcmp(load_file,""))
422  {
423
424  printfd("debug: starting main routine ...\n");
425  for(display.step=0;display.step<steps;display.step++)
426  {
427   x=rand_get(x_cell);
428   y=rand_get(y_cell);
429   z=rand_get_lgp(slope_nel,start_nel,z_cell);
430
431   /* distribute c concentration */
432   distrib_c_conc(cell_p,c_dist_c0,c_dist_slope,c_conc,x_cell,y_cell,z_cell);
433
434   process_cell(cell_p,x,y,z,x_cell,y_cell,z_cell,a_p_range,a_p_faktor,a_p_p0,&c_conc);
435
436   /* display stuff */
437   if((display.step%display_refresh_rate)==0)
438    display_draw(&display,display_x,display_y,display_z);
439  }
440
441  }
442
443  if(strcmp(save_file,"")) save_to_file(save_file,&display);
444
445  printfd("debug: main routine finished!\n");
446   
447  /* display again and listen for events */
448  display_draw(&display,display_x,display_y,display_z);
449
450  if(!no_wait)
451  {
452   display_event_init(&display);
453
454   while(!quit)
455   {
456    display_scan_event(&display,&display_x,&display_y,&display_z,&quit);
457    display_draw(&display,display_x,display_y,display_z);
458   }
459  }
460
461  display_release(&display);
462
463  return 23;
464 }