-
[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 sum_z_cells,sum_c_dist;
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_START_CONC);
50  printf("-S <value> \t slope of linear C distribution (default %d)\n",DEFAULT_C_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,u32 c_c0,u32 c_slope,int add_c,u32 x_max,u32 y_max,u32 z_max)
67 {
68  /* cryst. c to distribute */
69  int i,j,k;
70  double cryst_c;
71  cryst_c=0;
72  for(i=0;i<x_max*y_max*z_max;i++) if(!(cell_p+i)->status&AMORPH) cryst_c+=(cell_p+i)->conc;
73  for(i=0;i<z_max;i++)
74  {
75   for(j=0;j<x_max*y_max;j++)
76   {
77    if(!(cell_p+j+i*x_max*y_max)->status&AMORPH) 
78  
79  return 23;
80 }
81
82 /* look at cell ... */
83 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)
84 {
85  cell *this_cell;
86  int i,j,k;
87  double pressure;
88  this_cell=cell_p+x+y*x_max+z*x_max*y_max;
89  pressure=p0*URAND_2BYTE_MAX;
90  for(i=-range;i<=range;i++)
91  {
92   for(j=-range;j<=range;j++)
93   {
94    for(k=-range;k<=range;k++)
95    {
96     if(!(i==0 && j==0 && k==0))
97     {
98      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);
99     }
100    }
101   }
102  }
103  if(this_cell->status&AMORPH)
104  {
105   /* wrong probability! just test by now ...*/
106   if(rand_get(URAND_2BYTE_MAX)>pressure) make_cryst(this_cell);
107  } else
108  {
109   if(rand_get(URAND_2BYTE_MAX)<=pressure) make_amorph(this_cell);
110  }
111  return 23;
112 }
113
114 int main(int argc,char **argv) 
115 {
116  u32 x_cell,y_cell,z_cell; /* amount of segments */
117  u32 x,y,z; /* cells */
118  int i,gr; /* for counting */
119  int slope_nel,start_nel; /* nuclear energy loss: slope, constant */
120  int a_p_range; /* p. range of amorphous sic */
121  double a_p_faktor,a_p_p0; /* p0 and faktor of amorphous sic */
122  u32 c_c0,c_slope; /* c start concentration and linear slope of c distribution */
123  int steps; /* # steps */
124  cell *cell_p;
125  struct __display display;
126  u32 display_x,display_y,display_z; /* intercept point of diplayed areas */
127  u32 display_refresh_rate; /* refresh rate for display */
128  int quit=0; /* continue/quit status */
129
130  printfd("debug: sizeof my u32 variable: %d\n",sizeof(u32));
131  printfd("debug: sizeof my cell struct: %d\n",sizeof(cell));
132
133  /* default values */
134  x_cell=DEFAULT_X_SEG;
135  y_cell=DEFAULT_Y_SEG;
136  z_cell=DEFAULT_Z_SEG;
137  slope_nel=DEFAULT_SLOPE_NEL;
138  start_nel=DEFAULT_START_NEL;
139  a_p_range=DEFAULT_A_P_RANGE;
140  a_p_faktor=DEFAULT_A_P_FAKTOR;
141  a_p_p0=DEFAULT_A_P_P0;
142  c_c0=DEFAULT_C_START_CONC;
143  c_slope=DEFAULT_C_SLOPE;
144  steps=DEFAULT_STEPS;
145  display_x=x_cell/2;
146  display_y=y_cell/2;
147  display_z=z_cell/2;
148  display_refresh_rate=DEFAULT_DISPLAY_REF_RATE;
149  
150  /* parse command args */
151  for(i=1;i<argc;i++)
152  {
153   if(argv[i][0]=='-')
154   {
155    switch(argv[i][1])
156    {
157     case 'h':
158      usage();
159      return 23;
160      break;
161     case 'a':
162      slope_nel=atoi(argv[++i]);
163      break;
164     case 'c':
165      start_nel=atoi(argv[++i]);
166      break;
167     case 'x':
168      x_cell=atoi(argv[++i]);
169      break;
170     case 'y':
171      y_cell=atoi(argv[++i]);
172      break;
173     case 'z':
174      z_cell=atoi(argv[++i]);
175      break;
176     case 's':
177      steps=atoi(argv[++i]);
178      break;
179     case 'X':
180      display_x=atoi(argv[++i]);
181      break;
182     case 'Y':
183      display_y=atoi(argv[++i]);
184      break;
185     case 'Z':
186      display_z=atoi(argv[++i]);
187      break;
188     case 'd':
189      display_refresh_rate=atoi(argv[++i]);
190      break;
191     case 'r':
192      a_p_range=atoi(argv[++i]);
193      break;
194     case 'f':
195      a_p_faktor=atof(argv[++i]);
196      break;
197     case 'p':
198      a_p_p0=atof(argv[++i]);
199      break;
200     case 'C':
201      c_c0=atoi(argv[++i]);
202      break;
203     case 'S':
204      c_slope=atoi(argv[++i]);
205      break;
206     default:
207      usage();
208      return -23;
209    }
210   } else usage();
211  }
212
213  /* open random fd */
214  if((random_fd=open("/dev/urandom",O_RDONLY))<0)
215  {
216   puts("cannot open /dev/urandom\n");
217   return -23;
218  }
219
220  /* calculate sum_z_cells one time! */
221  gr=0;
222  for(i=1;i<=z_cell;i++) gr+=i;
223  sum_z_cells=z_cell*start_nel+slope_nel*gr;
224  sum_c_dist=z_cell*c_c0+c_slope*gr;
225  printfd("debug: sum_z_cells -> %u\ndebug: sum_c_dist -> %u\n",sum_z_cells,sum_c_dist);
226
227  /* testing ... */
228
229  /* allocate cells */
230  printf("malloc will free %d bytes now ...\n",x_cell*y_cell*z_cell*sizeof(cell));
231  if((cell_p=(cell *)malloc(x_cell*y_cell*z_cell*sizeof(cell)))==NULL)
232  {
233   puts("failed allocating memory for cells\n");
234   return -23;
235  }
236  memset(cell_p,0,x_cell*y_cell*z_cell*sizeof(cell));
237
238  /* init display */
239  display_init(x_cell,y_cell,z_cell,&display,cell_p,&argc,argv);
240
241  /* main routine */
242  for(i=0;i<steps;i++)
243  {
244   x=rand_get(x_cell);
245   y=rand_get(y_cell);
246   z=rand_get_lgp(slope_nel,start_nel);
247
248   /* todo */
249   distrib_c_conc(cell_p,c_c0,c_slope,i,x_cell,y_cell,z_cell);
250
251   // process_cell(cell_p+x+y*x_cell+z*x_cell*y_cell);
252   process_cell(cell_p,x,y,z,x_cell,y_cell,z_cell,a_p_range,a_p_faktor,a_p_p0);
253
254   /* display stuff */
255   if((i%display_refresh_rate)==0)
256    display_draw(&display,display_x,display_y,display_z);
257  }
258   
259  /* display again and listen for events */
260  display_draw(&display,display_x,display_y,display_z);
261  display_event_init(&display);
262
263  while(!quit)
264  {
265   display_scan_event(&display,&display_x,&display_y,&display_z,&quit);
266   display_draw(&display,display_x,display_y,display_z);
267  }
268
269  display_release(&display);
270
271  return 23;
272 }