fixed display bug (looped open of decker.ttf)
[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    // z relaxation, no pressure in z direction
138    // for(k=-range;k<=range;k++)
139    // {
140     if(!(i==0 && j==0)) //  && k==0))
141     {
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      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);
144     }
145    // }
146   }
147  }
148  pressure*=this_cell->conc;
149  if(this_cell->status&AMORPH)
150  {
151   /* wrong probability! just test by now ...*/
152   if(rand_get(URAND_2BYTE_MAX)>pressure)
153   {
154     make_cryst(this_cell);
155     *c_conc=*c_conc+1+this_cell->conc;
156   } else *c_conc+=1;
157  } else
158  {
159   if(rand_get(URAND_2BYTE_MAX)<=pressure)
160   {
161    make_amorph(this_cell);
162    *c_conc=*c_conc+1-this_cell->conc;
163   } else *c_conc+=1;
164  }
165  return 23;
166 }
167
168 int main(int argc,char **argv) 
169 {
170  u32 x_cell,y_cell,z_cell; /* amount of segments */
171  u32 x,y,z; /* cells */
172  int i; /* for counting */
173  int slope_nel,start_nel; /* nuclear energy loss: slope, constant */
174  int a_p_range; /* p. range of amorphous sic */
175  double a_p_faktor,a_p_p0; /* p0 and faktor of amorphous sic */
176  int c_dist_c0,c_dist_slope; /* c start concentration and linear slope of c distribution */
177  u32 c_conc;
178  int steps; /* # steps */
179  cell *cell_p;
180  struct __display display;
181  u32 display_x,display_y,display_z; /* intercept point of diplayed areas */
182  u32 display_refresh_rate; /* refresh rate for display */
183  int quit=0; /* continue/quit status */
184
185  printfd("debug: sizeof my u32 variable: %d\n",sizeof(u32));
186  printfd("debug: sizeof my cell struct: %d\n",sizeof(cell));
187
188  /* default values */
189  x_cell=DEFAULT_X_SEG;
190  y_cell=DEFAULT_Y_SEG;
191  z_cell=DEFAULT_Z_SEG;
192  slope_nel=DEFAULT_SLOPE_NEL;
193  start_nel=DEFAULT_START_NEL;
194  a_p_range=DEFAULT_A_P_RANGE;
195  a_p_faktor=DEFAULT_A_P_FAKTOR;
196  a_p_p0=DEFAULT_A_P_P0;
197  c_dist_c0=DEFAULT_C_DIST_START_CONC;
198  c_dist_slope=DEFAULT_C_DIST_SLOPE;
199  c_conc=DEFAULT_C_C0;
200  steps=DEFAULT_STEPS;
201  display_x=x_cell/2;
202  display_y=y_cell/2;
203  display_z=z_cell/2;
204  display_refresh_rate=DEFAULT_DISPLAY_REF_RATE;
205  strcpy(random_file,"");
206  
207  /* parse command args */
208  for(i=1;i<argc;i++)
209  {
210   if(argv[i][0]=='-')
211   {
212    switch(argv[i][1])
213    {
214     case 'h':
215      usage();
216      return 23;
217      break;
218     case 'a':
219      slope_nel=atoi(argv[++i]);
220      break;
221     case 'c':
222      start_nel=atoi(argv[++i]);
223      break;
224     case 'x':
225      x_cell=atoi(argv[++i]);
226      break;
227     case 'y':
228      y_cell=atoi(argv[++i]);
229      break;
230     case 'z':
231      z_cell=atoi(argv[++i]);
232      break;
233     case 's':
234      steps=atoi(argv[++i]);
235      break;
236     case 'X':
237      display_x=atoi(argv[++i]);
238      break;
239     case 'Y':
240      display_y=atoi(argv[++i]);
241      break;
242     case 'Z':
243      display_z=atoi(argv[++i]);
244      break;
245     case 'd':
246      display_refresh_rate=atoi(argv[++i]);
247      break;
248     case 'r':
249      a_p_range=atoi(argv[++i]);
250      break;
251     case 'f':
252      a_p_faktor=atof(argv[++i]);
253      break;
254     case 'p':
255      a_p_p0=atof(argv[++i]);
256      break;
257     case 'b':
258      c_conc=atoi(argv[++i]);
259      break;
260     case 'C':
261      c_dist_c0=atoi(argv[++i]);
262      break;
263     case 'S':
264      c_dist_slope=atoi(argv[++i]);
265      break;
266     case 'R':
267      strcpy(random_file,argv[++i]);
268      break;
269     default:
270      usage();
271      return -23;
272    }
273   } else usage();
274  }
275
276  /* open random fd */
277  if(!strcmp(random_file,""))
278  {
279   if((random_fd=open("/dev/urandom",O_RDONLY))<0)
280   {
281    puts("cannot open /dev/urandom");
282    return -23;
283   }
284  } else
285  {
286   if((random_fd=open(random_file,O_RDONLY))<0)
287   {
288    puts("cannot open random file");
289    return -23;
290   }
291  }
292  /* allocate random number buffer */
293  printf("malloc will free %d bytes now ...\n",RAND_BUF_SIZE);
294  if((rand_buf=(u32 *)malloc(RAND_BUF_SIZE))==NULL)
295  {
296   puts("failed allocating memory for random numbers");
297   return -23;
298  }
299  rand_current=rand_buf+RAND_BUF_SIZE;
300   
301  /* calculate gr one time! */
302  gr=0;
303  for(i=1;i<=z_cell;i++) gr+=i;
304  printfd("debug: gr = %d\n",gr);
305
306  /* allocate cells */
307  printf("malloc will free %d bytes now ...\n",x_cell*y_cell*z_cell*sizeof(cell));
308  if((cell_p=(cell *)malloc(x_cell*y_cell*z_cell*sizeof(cell)))==NULL)
309  {
310   puts("failed allocating memory for cells");
311   return -23;
312  }
313  memset(cell_p,0,x_cell*y_cell*z_cell*sizeof(cell));
314
315  /* init display */
316  printfd("debug: init display now ...\n");
317  display_init(x_cell,y_cell,z_cell,&display,cell_p,&argc,argv);
318
319  /* main routine */
320  printfd("debug: starting main routine ...\n");
321  for(display.step=0;display.step<steps;display.step++)
322  {
323   x=rand_get(x_cell);
324   y=rand_get(y_cell);
325   z=rand_get_lgp(slope_nel,start_nel,z_cell);
326
327   /* todo */
328   distrib_c_conc(cell_p,c_dist_c0,c_dist_slope,c_conc,x_cell,y_cell,z_cell);
329
330   process_cell(cell_p,x,y,z,x_cell,y_cell,z_cell,a_p_range,a_p_faktor,a_p_p0,&c_conc);
331
332   /* display stuff */
333   if((display.step%display_refresh_rate)==0)
334    display_draw(&display,display_x,display_y,display_z);
335  }
336  printfd("debug: main routine finished!\n");
337   
338  /* display again and listen for events */
339  display_draw(&display,display_x,display_y,display_z);
340  display_event_init(&display);
341
342  while(!quit)
343  {
344   display_scan_event(&display,&display_x,&display_y,&display_z,&quit);
345   display_draw(&display,display_x,display_y,display_z);
346  }
347
348  display_release(&display);
349
350  return 23;
351 }