added save/load function
[physik/nlsop.git] / nlsop.c
1 /*
2  * nlsop.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 #define _GNU_SOURCE
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <fcntl.h>
21 #include <unistd.h>
22
23 #include "nlsop.h"
24
25 #include "dfbapi.h"
26 #include "random.h"
27
28 #define MAKE_AMORPH(N) *(N)|=AMORPH
29 #define MAKE_CRYST(N) *(N)&=~AMORPH
30
31 int usage(void)
32 {
33  puts("usage:");
34  puts("-h \t\t help");
35  puts("-n \t\t no user interaction");
36  printf("-a <value> \t slope of nuclear energy loss (default %f)\n",A_EL);
37  printf("-b <value> \t nuclear energy loss offset (default %f)\n",B_EL);
38  printf("-x <value> \t # x cells (default %d)\n",X);
39  printf("-y <value> \t # x cells (default %d)\n",Y);
40  printf("-z <value> \t # x cells (default %d)\n",Z);
41  printf("-X <value> \t display x (default %d)\n",X/2-1);
42  printf("-Y <value> \t display y (default %d)\n",Y/2-1);
43  printf("-Z <value> \t display z (default %d)\n",Z/2-1);
44  printf("-s <value> \t steps (default %d)\n",STEPS);
45  printf("-d <value> \t refresh display (default %d)\n",REFRESH);
46  printf("-r <value> \t amorphous influence range (default %d)\n",RANGE);
47  printf("-f <value> \t pressure = <value> * 1/distance^2 (default %f)\n",A_AP);
48  printf("-p <value> \t pressure offset (default %f)\n",B_AP);
49  printf("-A <value> \t slope of linear c distribution (default %f)\n",A_CD);
50  printf("-B <value> \t linear c distribution offset (default %f)\n",B_CD);
51  printf("-C <value> \t initial c concentration (default %d)\n",CC);
52  puts("-L <file> \t load from file");
53  puts("-S <file> \t save to file");
54  puts("-R <file> \t read from random file");
55  
56  return 1;
57 }
58
59 int process_cell(3d_lattice *3d_l,u32 x,u32 y,u32 z,int r,double a,double b,int *t_c)
60 {
61  unsigned char *thiz;
62  int *conc;
63  int i,j;
64  double p;
65
66  thiz=3d_l->status+x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y;
67  conc=3d_l->extra+x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y;
68  p=b*URAND_MAX;
69  for(i=-r;i<=r;i++)
70  {
71   for(j=-r;j<=r;j++)
72   {
73    if(!(i==0 && j==0))
74    {
75     if(*(d3_l->status+((x+d3_l->max_x+i)%d3_l->max_x)+((y+d3_l->max_y+j)%d3_l->max_x)*d3_l->max_x+z*d3_l->max_x*d3_l->max_y)&AMORPH) p+=a*URAND_MAX/(i*i+j*j);
76    } 
77   }
78  }
79  p*=*conc;
80  if(!(*thiz&AMORPH))
81  {
82   if(rand_get(URAND_MAX)<=p)
83   {
84    MAKE_AMORPH(thiz);
85    *t_c=*t_c+1-*conc;
86   } else *t_c+=1;
87  } else
88  {
89   /* assume 1-p probability */
90   if(rand_get(URAND_MAX)>p)
91   {
92    MAKE_CRYST(thiz);
93    *t_c=*t_c+1+*conc;
94   } else *t_c+=1;
95  }
96
97  return 1;
98 }
99
100 int distrib_c(3d_lattice *3d_l,int t_c,double a,double b)
101 {
102  int i,j,k,total,area,sum;
103  int temp,left;
104  int *area_h;
105  u32 x,y,z;
106
107  area=d3_l->max_x*d3_l->max_y;
108  area_h=(int *)malloc(d3_l->max_z*sizeof(int));
109
110  total=0;
111  sum=0;
112  for(i=0;i<d3_l->max_z;i++)
113  {
114   area_h[i]=0;
115   for(j=0;j<area;j++)
116   {
117    if(!(*(d3_l->status+(i*area)+j)&AMORPH))
118    {
119     sum+=(i+1)*a+b;
120     area_h[i]+=1;
121    }
122   }
123  }
124  for(i=0;i<d3_l->max_z;i++)
125  {
126   temp=((i+1)*a+b)*t_c/(sum+area_h[i]);
127   if(temp>1)
128   {
129    for(j=0;j<area;j++)
130    {
131     if(!(*(d3_l->status+(i*area)+j)&AMORPH))
132     {
133      *(d3_l->extra+(i*area)+j)=temp;
134      total+=temp;
135     }
136    }
137   }
138   left=(((i+1)*a+b)*t_c/sum)%area_h[i];
139   while(left)
140   {
141   x=get_rand(d3_l->max_x);
142   y=get_rand(d3_l->max_y);
143   if(!(*(d3_l->status+(i*area)+x+y*d3_l->max_x)&AMORPH))
144   {
145    *(d3_l->extra+(i*area)+x+y*d3_l->max_x)+=1;
146    total+=1;
147    left-=1;
148   }
149  }
150  left=t_c-total;
151  while(left)
152  {
153   x=get_rand(d3_l->max_x);
154   y=get_rand(d3_l->max_y);
155   z=get_rand_lgp(d3_l->max_z,a,b);
156   if(!(*(d3_l->status+x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y)&AMORPH))
157   {
158    *(d3_l->extra+x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y)+=1;
159    left-=1;
160    total+=1;
161   }
162  }
163
164  return 1;
165 }
166
167 int save_to_file(char *sf,3d_lattice *3d_l)
168 {
169  int sf_fd,c;
170
171  if((sf_fd=open(sf,O_WRONLY|O_CREAT))<0)
172  {
173   puts("cannot open save file");
174   return -1;
175  }
176  if(write(sf_fd,3d_l,sizeof(3d_lattice))<sizeof(3d_lattice))
177  {
178   puts("failed saving 3d lattice struct");
179   return -1;
180  }
181  c=d3_l->max_x*d3_l->max_y*d3_l->max_z;
182  if(write(sf_fd,3d_l->status,c*sizeof(unsigned char)<c*sizeof(unsigned char))
183  {
184   puts("failed saving status of 3d lattice sites");
185   return -1;
186  }
187  if(write(sf_fd,3d_l->extra,c*sizeof(int)<c*sizeof(int))
188  {
189   puts("failed saving sites concentration");
190   return -1;
191  }
192  close(sf_fd);
193
194  return 1;
195 }
196
197 int load_from_file(char *lf,3d_lattice *3d_l)
198 {
199  int lf_fd,c;
200
201  if((lf_fd=open(lf,O_RDONLY))<0)
202  {
203   puts("cannot open load file");
204   return -1;
205  }
206  if(read(lf_fd,3d_l,sizeof(3d_lattice))<sizeof(3d_lattice))
207  {
208   puts("failed reading d3 lattice struct");
209   return -1;
210  }
211  c=d3_l->max_x*d3_l->max_y*d3_l->max_z;
212  if((d3_l->status=(unsigned char*)malloc(c*sizeof(unsigned char)))==NULL)
213  {
214   puts("cannot allocate status buffer");
215   return -1;
216  }
217  if((d3_l->extra=(int *)malloc(c*sizeof(int)))==NULL)
218  {
219   puts("cannot allocate concentration buffer");
220   return -1;
221  }
222  if(read(lf_fd,3d_l->status,c*sizeof(unsigned char))<c*sizeof(unsigned char))
223  {
224   puts("failed reading status of 3d lattice sites");
225   return -1;
226  }
227  if(read(lf_fd,3d_l->extra,c*sizeof(int))<c*sizeof(int))
228  {
229   puts("failed reading sites concentration");
230   return -1;
231  }
232  close(lf_fd);
233
234  return 1;
235 }
236
237 int main(int argc,char **argv)
238 {
239  u32 max_x,max_y,max_z,x,y,z,x_c,y_c,z_c;
240  int i,quit,escape,nowait;
241  double a_el,b_el,a_cd,b_cd,a_ap,b_ap;
242  int cc,steps,range,refresh;
243  char s_file[MAX_CHARS];
244  char l_file[MAX_CHARS];
245  char r_file[MAX_CHARS];
246  d3_lattice d3_l;
247
248  max_x=X;
249  max_y=Y;
250  max_z=Z;
251  x=X/2-1;
252  y=Y/2-1;
253  z=Z/2-1;
254  steps=STEPS;
255  range=RANGE;
256  refresh=REFRESH;
257  a_el=A_EL;
258  b_el=B_EL;
259  a_cd=A_CD;
260  b_cd=B_CD;
261  a_ap=A_AP;
262  b_ap=B_AP;
263  cc=CC;
264  nowait=0;
265  strcpy(s_file,"");
266  strcpy(l_file,"");
267  strcpy(r_file,"");
268
269  for(i=1,i<argc,i++)
270  {
271   if(argv[i][0]=='-')
272   {
273    switch(argv[i][1])
274    {
275     case 'h':
276      usage();
277      return -1;
278     case 'n':
279      nowait=1;
280      break;
281     case 'a':
282      a_el=atof(argv[++i]);
283      break;
284     case 'b':
285      b_el=atof(argv[++i]);
286      break;
287     case 'x':
288      max_x=atoi(argv[++i]);
289      break;
290     case 'y':
291      max_y=atoi(argv[++i]);
292      break;
293     case 'z':
294      max_z=atoi(argv[++i]);
295      break;
296     case 'X':
297      x=atoi(argv[++i]);
298      break;
299     case 'Y':
300      y=atoi(argv[++i]);
301      break;
302     case 'Z':
303      z=atoi(argv[++i]);
304      break;
305     case 's':
306      steps=atoi(argv[++i]);
307      break;
308     case 'd':
309      refresh=atoi(argv[++i]);
310      break;
311     case 'r':
312      range=atoi(argv[++i]);
313      break;
314     case 'f':
315      a_ap=atof(argv[++i]);
316      break;
317     case 'p':
318      b_ap=atof(argv[++i]);
319      break;
320     case 'A':
321      a_cd=atof(argv[++i]);
322      break;
323     case 'B':
324      b_cd=atof(argv[++i]);
325      break;
326     case 'C':
327      cc=atoi(argv[++i]);
328      break;
329     case 'L':
330      strcpy(l_file,argv[++i]);
331      break;
332     case 'S':
333      strcpy(s_file,argv[++i]);
334      break;
335     case 'R':
336      strcpy(r_file,argv[++i]);
337      break;
338     default:
339      usage();
340      return -1;
341    }
342   } else usage();
343  }
344
345
346
347
348  return 1;
349 }