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