-
[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  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  return -23;
50 }
51
52 int make_amorph(u32 *cell)
53 {
54  *cell|=AMORPH;
55  return 23;
56 }
57
58 int make_cryst(u32 *cell)
59 {
60  *cell&=~AMORPH;
61  return 23;
62 }
63
64 /* look at cell ... */
65 int process_cell(void *cell_p,u32 x,u32 y,u32 z,u32 x_max,u32 y_max,u32 z_max,int range,double faktor,double p0)
66 {
67  u32 *cell;
68  int i,j,k;
69  double pressure;
70  cell=(u32 *)(cell_p+x+y*(x_max-1)+z*(x_max-1)*(y_max-1));
71  pressure=p0*URAND_2BYTE_MAX;
72  for(i=-range;i<=range;i++)
73  {
74   for(j=-range;j<=range;j++)
75   {
76    for(k=-range;k<=range;k++)
77    {
78     if(!(i==0 && j==0 && k==0))
79     {
80      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) pressure+=faktor*URAND_2BYTE_MAX/(i*i+j*j+k*k);
81     }
82    }
83   }
84  }
85  if(*cell&AMORPH)
86  {
87   /* wrong probability! just test by now ...*/
88   if(rand_get(URAND_2BYTE_MAX)>pressure) make_cryst(cell);
89  } else
90  {
91   if(rand_get(URAND_2BYTE_MAX)<=pressure) make_amorph(cell);
92  }
93  return 23;
94 }
95
96 int main(int argc,char **argv) 
97 {
98  u32 x_cell,y_cell,z_cell; /* amount of segments */
99  u32 x,y,z; /* cells */
100  int i; /* for counting */
101  int slope_nel,start_nel; /* nuclear energy loss: slope, constant */
102  int a_p_range; /* p. range of amorphous sic */
103  double a_p_faktor,a_p_p0; /* p0 and faktor of amorphous sic */
104  int steps; /* # steps */
105  void *cell_p;
106  struct __display display;
107  u32 display_x,display_y,display_z; /* intercept point of diplayed areas */
108  u32 display_refresh_rate; /* refresh rate for display */
109  int quit=0; /* continue/quit status */
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=DEFAULT_A_P_P0;
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=atof(argv[++i]);
172      break;
173     case 'p':
174      a_p_p0=atof(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 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
195  /* testing ... */
196
197  /* allocate cells */
198  printf("malloc will free %d bytes now ...\n",x_cell*y_cell*z_cell*sizeof(u32));
199  if((cell_p=malloc(x_cell*y_cell*z_cell*sizeof(u32)))==NULL)
200  {
201   puts("failed allocating memory for cells\n");
202   return -23;
203  }
204  memset(cell_p,0,x_cell*y_cell*z_cell*sizeof(u32));
205
206  /* init display */
207  display_init(x_cell,y_cell,z_cell,&display,cell_p,&argc,argv);
208
209  /* main routine */
210  for(i=0;i<steps;i++)
211  {
212   x=rand_get(x_cell);
213   y=rand_get(y_cell);
214   z=rand_get_lgp(slope_nel,start_nel);
215
216   /* todo */
217   // distrib_c_conc(cell_p);
218
219   // process_cell((u32 *)(cell_p+x+y*(x_cell-1)+z*(x_cell-1)*(y_cell-1)));
220   process_cell(cell_p,x,y,z,x_cell,y_cell,z_cell,a_p_range,a_p_faktor,a_p_p0);
221  
222   if(*(u32 *)(cell_p+x+y*(x_cell-1)+z*(x_cell-1)*(y_cell-1)) && *(u32 *)(cell_p+x+y*(x_cell-1)+z*(x_cell-1)*(y_cell-1))!=1)
223   { 
224    printfd("debug: x: %u y: %u z: %u -> %x\n",x,y,z,*(u32 *)(cell_p+x+y*(x_cell-1)+z*(x_cell-1)*(y_cell-1)));
225   }
226
227   /* display stuff */
228   if((i%display_refresh_rate)==0)
229    display_draw(&display,display_x,display_y,display_z);
230  }
231   
232  /* display again and listen for events */
233  display_draw(&display,display_x,display_y,display_z);
234  display_event_init(&display);
235
236  while(!quit)
237  {
238   display_scan_event(&display,&display_x,&display_y,&display_z,&quit);
239   display_draw(&display,display_x,display_y,display_z);
240   printfd("idle?\n");
241  }
242
243  display_release(&display);
244
245  return 23;
246 }