began propability calc of amorph<->cryst
[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;
30 int random_fd; /* /dev/urandom file descriptor */
31
32 int usage()
33 {
34  puts("usage:");
35  puts("-h: help");
36  puts("-a <value> \t slope of nuclear energy loss (default 1)");
37  puts("-c <value> \t nuclear enery loss at depths 0 (default 0)");
38  puts("-x <value> \t # x cells (default 50)");
39  puts("-y <value> \t # y cells (default 50)");
40  puts("-z <value> \t # z cells (default 100)");
41  puts("-s <value> \t # steps to calculate (default 5000)");
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  puts("-d <value> \t refresh every <value> loops (default 100)");
46  puts("-r <value> \t pressure range from amorphous SiCx (default 2)");
47  puts("-f <value> \t faktor for pressure from amorphous SiCx (default 1)");
48  return -23;
49 }
50
51 int make_amorph(u32 *cell)
52 {
53  *cell|=AMORPH;
54  return 23;
55 }
56
57 int make_cryst(u32 *cell)
58 {
59  *cell&=~AMORPH;
60  return 23;
61 }
62
63 /* look at cell ... */
64 int process_cell(void *cell_p,u32 x,u32 y,u32 z,u32 x_max,u32 y_max,u32 z_max,int range,int faktor)
65 {
66  /* is amorph? */
67  u32 *cell;
68  int i,j,k;
69  float count;
70  cell=(u32 *)(cell_p+x+y*(x_max-1)+z*(x_max-1)*(y_max-1));
71  count=0;
72  for(i=-range;i<=range;i++)
73   for(j=-range;j<=range;j++)
74    for(k=-range;k<=range;k++)
75     if(!(i==0 && j==0 && k==0) && (x>=range && x<x_max-range) \
76         && (y>=range && y<y_max-range) && (z>=range && z<z_max-range))
77      if(*(u32 *)(cell_p+(x+i)+(y+j)*(x_max-1)+(z+k)*(x_max-1)*(y_max-1))&AMORPH)
78       // total+=range*range-
79       count+=0.01*faktor*1.0/(i*i+j*j+k*k);
80  /* count is propability of cell getting amorph */
81  /* for now assume 1 - ~ is propability of cell getting cryst if allready amorphous */    
82  if(*cell&AMORPH)
83  {
84   if((1-count)*URAND_2_MAX<=rand_get(URAND_2_MAX))
85   {  
86    printfd("cell getting cryst. ...(%f)\n",(1-count));
87    make_cryst(cell);
88   }
89  } else
90  {
91   if(count*URAND_2_MAX<=rand_get(URAND_2_MAX))
92   {
93    printfd("cell getting amorph ...(%f)\n",count);
94    make_amorph(cell);
95   }
96  }
97  return 23;
98 }
99
100 int main(int argc,char **argv) 
101 {
102  u32 x_cell,y_cell,z_cell; /* amount of segments */
103  u32 x,y,z; /* cells */
104  int i; /* for counting */
105  int slope_nel,start_nel; /* nuclear energy loss: slope, constant */
106  int a_p_range,a_p_faktor; /* pressure range and faktor from amorphous sic */
107  int steps; /* # steps */
108  void *cell_p;
109  struct __display display;
110  u32 display_x,display_y,display_z; /* intercept point of diplayed areas */
111  u32 display_refresh_rate; /* refresh rate for display */
112
113  /* default values */
114  x_cell=DEFAULT_X_SEG;
115  y_cell=DEFAULT_Y_SEG;
116  z_cell=DEFAULT_Z_SEG;
117  slope_nel=DEFAULT_SLOPE_NEL;
118  start_nel=DEFAULT_START_NEL;
119  a_p_range=DEFAULT_A_P_RANGE;
120  a_p_faktor=DEFAULT_A_P_FAKTOR;
121  steps=DEFAULT_STEPS;
122  display_x=x_cell/2;
123  display_y=y_cell/2;
124  display_z=z_cell/2;
125  display_refresh_rate=DEFAULT_DISPLAY_REF_RATE;
126  
127  /* parse command args */
128  for(i=1;i<argc;i++)
129  {
130   if(argv[i][0]=='-')
131   {
132    switch(argv[i][1])
133    {
134     case 'h':
135      usage();
136      return 23;
137      break;
138     case 'a':
139      slope_nel=atoi(argv[++i]);
140      break;
141     case 'c':
142      start_nel=atoi(argv[++i]);
143      break;
144     case 'x':
145      x_cell=atoi(argv[++i]);
146      break;
147     case 'y':
148      y_cell=atoi(argv[++i]);
149      break;
150     case 'z':
151      z_cell=atoi(argv[++i]);
152      break;
153     case 's':
154      steps=atoi(argv[++i]);
155      break;
156     case 'X':
157      display_x=atoi(argv[++i]);
158      break;
159     case 'Y':
160      display_y=atoi(argv[++i]);
161      break;
162     case 'Z':
163      display_z=atoi(argv[++i]);
164      break;
165     case 'd':
166      display_refresh_rate=atoi(argv[++i]);
167      break;
168     case 'r':
169      a_p_range=atoi(argv[++i]);
170      break;
171     case 'f':
172      a_p_faktor=atoi(argv[++i]);
173      break;
174     default:
175      usage();
176      return -23;
177    }
178   } else usage();
179  }
180
181  /* open random fd */
182  if((random_fd=open("/dev/urandom",O_RDONLY))<0)
183  {
184   puts("cannot open /dev/urandom\n");
185   return -23;
186  }
187
188  /* calculate sum_z_cells one time! */
189  sum_z_cells=0;
190  for(i=1;i<=z_cell;i++) sum_z_cells+=(start_nel+i*slope_nel);
191  printfd("debug: sum z cells -> %d\n",sum_z_cells);
192
193
194  /* testing ... */
195
196  /* allocate cells */
197  printf("malloc will free %d bytes now ...\n",x_cell*y_cell*z_cell*sizeof(u32));
198  if((cell_p=malloc(x_cell*y_cell*z_cell*sizeof(u32)))==NULL)
199  {
200   puts("failed allocating memory for cells\n");
201   return -23;
202  }
203  memset(cell_p,0,x_cell*y_cell*z_cell*sizeof(u32));
204
205  /* init display */
206  display_init(x_cell,y_cell,z_cell,&display,cell_p,&argc,argv);
207
208  /* main routine */
209  for(i=0;i<steps;i++)
210  {
211   x=rand_get(x_cell);
212   y=rand_get(y_cell);
213   z=rand_get_lgp(slope_nel,start_nel);
214
215   /* todo */
216   // distrib_c_conc(cell_p);
217
218   // process_cell((u32 *)(cell_p+x+y*(x_cell-1)+z*(x_cell-1)*(y_cell-1)));
219   process_cell(cell_p,x,y,z,x_cell,y_cell,z_cell,a_p_range,a_p_faktor);
220
221   /* display stuff */
222   if((i%display_refresh_rate)==0) 
223    display_draw(&display,display_x,display_y,display_z);
224  }
225   
226  /* display again and quit when button hit */
227  display_draw(&display,display_x,display_y,display_z);
228  puts("hit button to quit ...");
229  getchar();
230
231  display_release(&display);
232
233  return 23;
234 }