some more work getting random reject method to work
[physik/nlsop.git] / nlsop.c
1 /*
2  * nlsop.c 
3  *
4  * author: frank zirkelbach (frank.zirkelbach@physik.uni-augsburg.de)
5  *
6  * this program tries helping to understand the amorphous depuration
7  * and recrystallization of SiCx while ion implantation at temperatures
8  * below 400 degree celsius.
9  * hopefully the program will simulate the stabilization of the
10  * selforganizing lamella structure in the observed behaviour.
11  *
12  * refs: 
13  *  - J. K. N. Lindner. Habilationsschrift, Universitaet Augsburg.
14  *  - Maik Haeberlen. Diplomarbeit, Universitaet Augsburg.
15  *
16  * Copyright (C) 2004 Frank Zirkelbach
17  *
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation; either version 2 of the License, or
21  * (at your option) any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, write to the Free Software
30  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
31  *
32  */
33
34 #define _GNU_SOURCE
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <fcntl.h>
41 #include <unistd.h>
42
43 #include "nlsop.h"
44
45 #include "dfbapi.h"
46 #include "random.h"
47
48 #define MAKE_AMORPH(N) *(N)|=AMORPH
49 #define MAKE_CRYST(N) *(N)&=~AMORPH
50
51 int usage(void)
52 {
53  puts("usage:");
54  puts("-h \t\t help");
55  puts("-n \t\t no user interaction");
56  puts("-Z \t\t cryst -> amorph c diffusion in z direction");
57  puts("-i \t\t no cryst to cryst diffusion");
58  printf("-a <value> \t slope of nuclear energy loss (default %f)\n",A_EL);
59  printf("-b <value> \t nuclear energy loss offset (default %f)\n",B_EL);
60  printf("-x <value> \t # x cells (default %d)\n",X);
61  printf("-y <value> \t # y cells (default %d)\n",Y);
62  printf("-z <value> \t # z cells (default %d)\n",Z);
63  printf("-s <value> \t steps (default %d)\n",STEPS);
64  printf("-d <value> \t refresh display (default %d)\n",REFRESH);
65  printf("-r <value> \t amorphous influence range (default %d)\n",RANGE);
66  printf("-f <value> \t pressure = <value> * 1/distance^2 (default %f)\n",A_AP);
67  printf("-p <value> \t pressure offset (default %f)\n",B_AP);
68  printf("-F <value> \t proportionality constant between c conc and ability to get amorphous (default %f)\n",A_CP);
69  printf("-A <value> \t slope of linear c distribution (default %f)\n",A_CD);
70  printf("-B <value> \t linear c distribution offset (default %f)\n",B_CD);
71  printf("-D <value> \t diffusion rate from cryst to amorph cells (default %f)\n",DR_AC);
72  printf("-c <value> \t diffusion rate in cryst cells (default %f)\n",DR_CC);
73  printf("-e <value> \t do diffusion every <value> steps (default %d)\n",DIFF_RATE);
74  puts("-g <file> <step> continue simulation from file and step (step > 0)!");
75  printf("-W <value> \t write every <value> steps to save file (default %d)\n",RESAVE);
76  puts("-C <file> \t convert file to gnuplot format");
77  puts("-L <file> \t load from file");
78  puts("-S <file> \t save to file");
79  puts("-R <file> \t read from random file");
80  puts("-P <file> \t specify implantation profile file");
81  printf("-H <value> \t collisions per ion in simulation window (default %d)\n",CPI);
82  
83  return 1;
84 }
85
86 int process_cell(d3_lattice *d3_l,u32 x,u32 y,u32 z,info *my_info)
87 {
88  unsigned char *thiz;
89  int *conc;
90  int i,j;
91  int off;
92  double p;
93
94  thiz=d3_l->status+x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y;
95  conc=d3_l->extra+x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y;
96  p=my_info->b_ap*URAND_MAX;
97  for(i=-(my_info->range);i<=my_info->range;i++)
98  {
99   for(j=-(my_info->range);j<=my_info->range;j++)
100   {
101    if(!(i==0 && j==0))
102    {
103     off=((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;
104     if(*(d3_l->status+off)&AMORPH) p+=my_info->a_ap*(*(d3_l->extra+off))*URAND_MAX/(i*i+j*j);
105    } 
106   }
107  }
108  p+=*conc*my_info->a_cp*URAND_MAX;
109  if(!(*thiz&AMORPH))
110  {
111   if(get_rand(URAND_MAX)<=p) MAKE_AMORPH(thiz);
112  } else
113  {
114   /* assume 1-p probability */
115   if(get_rand(URAND_MAX)>p) MAKE_CRYST(thiz);
116  }
117  
118  return 1;
119 }
120
121 int distrib_c(d3_lattice *d3_l,info *my_info,int step,double c_ratio)
122 {
123  u32 x,y,z;
124  int i,j,k,c;
125  int offset,off;
126  int carry;
127
128  /* put one c ion somewhere in the lattice */
129  if(my_info->cc<c_ratio*step)
130  {
131   x=get_rand(d3_l->max_x);
132   y=get_rand(d3_l->max_y);
133   // printf("nd: %d %d\n",x,y);
134   z=get_rand_lgp(d3_l->max_z,my_info->a_cd,my_info->b_cd);
135   // printf("ld: %d\n",z);
136   *(d3_l->extra+x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y)+=1;
137   (my_info->cc)++;
138  }
139
140  if(step%my_info->diff_rate==0)
141  {
142
143  for(i=0;i<d3_l->max_x;i++)
144  {
145   for(j=0;j<d3_l->max_y;j++)
146   {
147    for(k=0;k<d3_l->max_z;k++)
148    {
149     offset=i+j*d3_l->max_x+k*d3_l->max_x*d3_l->max_y;
150     /* case amorph: amorph <- cryst diffusion */
151     if(*(d3_l->status+offset)&AMORPH)
152     {
153      for(c=-1;c<=1;c++)
154      {
155       if(c!=0)
156       {
157        off=((i+d3_l->max_x+c)%d3_l->max_x)+j*d3_l->max_x+k*d3_l->max_x*d3_l->max_y;
158        carry=0;
159        if(!(*(d3_l->status+off)&AMORPH)) carry=(int)(my_info->dr_ac*(*(d3_l->extra+off)));
160        if(carry!=0)
161        {
162         *(d3_l->extra+offset)+=carry;
163         *(d3_l->extra+off)-=carry;
164        }
165       }
166      }
167      for(c=-1;c<=1;c++)
168      {
169       if(c!=0)
170       {
171        off=i+((j+c+d3_l->max_y)%d3_l->max_y)*d3_l->max_x+k*d3_l->max_x*d3_l->max_y;
172        carry=0;
173        if(!(*(d3_l->status+off)&AMORPH)) carry=(int)(my_info->dr_ac*(*(d3_l->extra+off)));
174        if(carry!=0)
175        {
176         *(d3_l->extra+offset)+=carry; 
177         *(d3_l->extra+off)-=carry; 
178        }
179       }
180      }
181      if(my_info->z_diff)
182      {
183       if(k!=0)
184       {
185        off=i+j*d3_l->max_x+(k-1)*d3_l->max_x*d3_l->max_y;
186        carry=0;
187        if(!*(d3_l->status+off)&AMORPH) carry=(int)(my_info->dr_ac*(*(d3_l->extra+off)));
188        if(carry!=0)
189        {
190         *(d3_l->extra+off)-=carry;
191         *(d3_l->extra+offset)+=carry;
192        }
193       }
194       if(k!=d3_l->max_z-1)
195       {
196        off=i+j*d3_l->max_x+(k+1)*d3_l->max_x*d3_l->max_y;
197        carry=0;
198        if(!*(d3_l->status+off)&AMORPH) carry=(int)(my_info->dr_ac*(*(d3_l->extra+off)));
199        if(carry!=0)
200        {
201         *(d3_l->extra+off)-=carry;
202         *(d3_l->extra+offset)+=carry;
203        }
204       }
205      }  
206     } else
207     /* case not amorph: cryst <-> cryst diffusion */
208     if(my_info->c_diff) {
209     /* if there is c diff, no diff in z-direction */
210     {
211      for(c=-1;c<=1;c++) 
212      {
213       if(c!=0)
214       {
215        off=i+((j+c+d3_l->max_y)%d3_l->max_y)*d3_l->max_x+k*d3_l->max_x*d3_l->max_y;
216        carry=0;
217        if(!(*(d3_l->status+off)&AMORPH))
218        {
219         carry=(int)(my_info->dr_cc*(*(d3_l->extra+off)-*(d3_l->extra+offset))/2);
220         if(carry!=0)
221         {
222          *(d3_l->extra+offset)+=carry;
223          *(d3_l->extra+off)-=carry;
224         }
225        }
226       }
227      }
228      for(c=-1;c<=1;c++)
229      {
230       if(c!=0)
231       {
232        off=((i+c+d3_l->max_x)%d3_l->max_x)+j*d3_l->max_x+k*d3_l->max_x*d3_l->max_y;
233        carry=0;
234        if(!(*(d3_l->status+off)&AMORPH))
235        {
236         carry=(int)(my_info->dr_cc*(*(d3_l->extra+off)-*(d3_l->extra+offset))/2);
237         if(carry!=0)
238         {
239          *(d3_l->extra+offset)+=carry;
240          *(d3_l->extra+off)-=carry;
241         }
242        }
243       }
244      }
245     }
246     /* end test */
247     }
248     /* */
249    } /* for z */
250   } /* for y */
251  } /* for x */
252
253  } /* if step modulo diff_rate == 0 */
254
255  return 1;
256 }
257
258 int calc_pressure(d3_lattice *d3_l,int range)
259 {
260  int i,j,off;
261  double count,max=0;
262  int x,y,z;
263
264  for(x=0;x<d3_l->max_x;x++)
265  {
266   for(y=0;y<d3_l->max_y;y++)
267   {
268    for(z=0;z<d3_l->max_z;z++)
269    {
270     count=0;
271     for(i=-range;i<=range;i++)
272     {
273      for(j=-range;j<=range;j++)
274      {
275       if(i!=0 && j!=0)
276       {
277        off=((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;
278        if(*(d3_l->status+off)&AMORPH) count+=((double)*(d3_l->extra+off))/(i*i+j*j);
279       }
280      }
281     }
282     if(count>max) max=count;
283    }
284   }
285  }
286
287  for(x=0;x<d3_l->max_x;x++)
288  {
289   for(y=0;y<d3_l->max_y;y++)
290   {
291    for(z=0;z<d3_l->max_z;z++)
292    {
293     count=0;
294     for(i=-range;i<=range;i++)
295     {
296      for(j=-range;j<=range;j++)
297      {
298       if(i!=0 && j!=0)
299       {
300        off=((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;
301        if(*(d3_l->status+off)&AMORPH) count+=((double)*(d3_l->extra+off))/(i*i+j*j);
302       }
303      }
304     }
305     *(unsigned char *)(d3_l->v_ptr+x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y)=(unsigned char)(count*255/max);
306    }
307   }
308  }
309
310  return 1;
311 }
312
313 int calc_max_extra(d3_lattice *d3_l)
314 {
315  int x,y,z;
316  int off,max=0;
317
318  for(x=0;x<d3_l->max_x;x++)
319  {
320   for(y=0;y<d3_l->max_y;y++)
321   {
322    for(z=0;z<d3_l->max_z;z++)
323    {
324     off=x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y;
325     if(*(d3_l->extra+off)>max) max=*(d3_l->extra+off);
326    }
327   }
328  }
329
330  return max;
331 }
332
333 int write_ac_distr(d3_lattice *d3_l,int ac_distr)
334 {
335  int fd,x,y,z;
336  int count=0,offset;
337  char file[16];
338
339  if(ac_distr==1) strcpy(file,"a.plot");
340  if(ac_distr==2) strcpy(file,"c.plot");
341  if(ac_distr==3) strcpy(file,"b.plot");
342
343  if((fd=open(file,O_WRONLY|O_CREAT))<0)
344  {
345   puts("cannot open plot file");
346   return -1;
347  }
348
349  for(z=0;z<d3_l->max_z;z++)
350  {
351   for(x=0;x<d3_l->max_x;x++)
352   {
353    for(y=0;y<d3_l->max_y;y++)
354    {
355     offset=x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y;
356     if(ac_distr==1)
357      if(*(d3_l->status+offset)&AMORPH) count+=*(d3_l->extra+offset);
358     if(ac_distr==2)
359      if(!(*(d3_l->status+offset)&AMORPH)) count+=*(d3_l->extra+offset);
360     if(ac_distr==3) count+=*(d3_l->extra+offset);
361    }
362   }
363   dprintf(fd,"%d %d\n",z,count);
364  }
365  close(fd);
366  
367  return 1;
368 }
369
370 int write_bmp(d3_lattice *d3_l,int window,u32 x,u32 y,u32 z)
371 {
372  int fd,i,j,size=0,foo=0,end=0;
373  int width=0,height=0;
374  char bmpfile[MAX_CHARS];
375  char buf[128];
376
377  if(((window==4)||(window==5))&&(d3_l->max_z<FFT_HEIGHT) )
378  {
379   puts("error: z < FFT_HEIGHT!");
380   puts("not writing bmp file ...");
381   return -1;
382  }
383
384  if(window%3==1)
385  {
386   sprintf(bmpfile,"x-z_%d.bmp",y);
387   foo=3*d3_l->max_x;
388   if(window==1)
389   {
390    size=(foo+(4-foo%4))*d3_l->max_z;
391    height=d3_l->max_z;
392   }
393   else 
394   {
395    size=(foo+(4-foo%4))*FFT_HEIGHT;
396    end=d3_l->max_z-FFT_HEIGHT;
397    height=FFT_HEIGHT;
398   }
399   width=d3_l->max_x;
400  }
401  if(window%3==2)
402  {
403   sprintf(bmpfile,"y-z_%d.bmp",x);
404   foo=3*d3_l->max_y;
405   if(window==2)
406   {
407    size=(foo+(4-foo%4))*d3_l->max_z;
408    height=d3_l->max_z;
409   }
410   else 
411   {
412    size=(foo+(4-foo%4))*FFT_HEIGHT;
413    end=d3_l->max_z-FFT_HEIGHT;
414    height=FFT_HEIGHT;
415   }
416   width=d3_l->max_y;
417  }
418  if(window==3)
419  {
420   sprintf(bmpfile,"x-y_%d.bmp",z);
421   foo=3*d3_l->max_x;
422   size=(foo+(4-foo%4))*d3_l->max_y;
423   width=d3_l->max_x;
424   height=d3_l->max_y;
425  }
426
427  if((fd=open(bmpfile,O_WRONLY|O_CREAT))<0)
428  {
429   puts("cannot open bmp file");
430   return -1;
431  }
432
433  /* bmpheader */
434  buf[0]='B'; /* std header start */
435  buf[1]='M';
436  buf[2]=(size+0x36)&0xff; /* file size */
437  buf[3]=(size+0x36)>>8;
438  memset(buf+4,0,6);
439  buf[10]=0x36; /* offset to data */
440  memset(buf+11,0,3);
441  buf[14]=0x28; /* length of bmp info header */
442  memset(buf+15,0,3);
443  buf[18]=width&0xff; /* width and height */
444  buf[19]=width>>8;
445  memset(buf+20,0,2);
446  buf[22]=height&0xff;
447  buf[23]=height>>8;
448  memset(buf+24,0,2);
449  buf[26]=1; /* # planes -> 1 */
450  buf[27]=0;
451  buf[28]=24; /* bits per pixel -> 2^24 (true color) */
452  buf[29]=0;
453  memset(buf+30,0,4); /* compression -> none */
454  buf[34]=size&0xff; /* data size */
455  buf[35]=size>>8;
456  memset(buf+36,0,2);
457  buf[38]=0x12; /* res: pixel/meter */
458  buf[39]=0x0b;
459  memset(buf+40,0,2);
460  buf[42]=0x12;
461  buf[43]=0x0b;
462  memset(buf+44,0,2); 
463  memset(buf+46,0,8); /* no colors, no important colors */
464
465  if(write(fd,buf,54)<54)
466  {
467   puts("failed writing bmp header");
468   return -1;
469  }
470  if(window%3==1)
471  {
472   for(j=0;j<d3_l->max_z-end;j++)
473   {
474    for(i=0;i<d3_l->max_x;i++)
475    {
476     if(*(d3_l->status+i+y*d3_l->max_x+(d3_l->max_z-j-1)*d3_l->max_x*d3_l->max_y)&RED) memset(buf,0xff,3);
477     else memset(buf,0,3);
478     if(write(fd,buf,3)<3)
479     {
480      puts("failed writing rgb values to bmp file");
481      return-1;
482     }
483    }
484    if(foo%4)
485    {
486     memset(buf,0,4-foo%4);
487     if(write(fd,buf,4-foo%4)<4-foo%4)
488     {
489      puts("failed writing 4 byte ending");
490      return -1;
491     }
492    } 
493   }
494  }
495  if(window%3==2)
496  {
497   for(j=0;j<d3_l->max_z-end;j++)
498   {
499    for(i=0;i<d3_l->max_y;i++)
500    {
501     if(*(d3_l->status+x+i*d3_l->max_x+(d3_l->max_z-j-1)*d3_l->max_x*d3_l->max_y)&RED) memset(buf,0xff,3);
502     else memset(buf,0,3);
503     if(write(fd,buf,3)<3)
504     {
505      puts("failed writing rgb values to bmp file");
506      return-1;
507     }
508    }
509    if(foo%4)
510    {
511     memset(buf,0,4-foo%4);
512     if(write(fd,buf,4-foo%4)<4-foo%4)
513     {
514      puts("failed writing 4 byte ending");
515      return -1;
516     }
517    }
518   }
519  }
520  if(window==3)
521  {
522   for(j=0;j<d3_l->max_y;j++)
523   {
524    for(i=0;i<d3_l->max_x;i++)
525    {
526     if(*(d3_l->status+i+(d3_l->max_y-j-1)*d3_l->max_x+z*d3_l->max_x*d3_l->max_y)&RED) memset(buf,0xff,3);
527     else memset(buf,0,3);
528     if(write(fd,buf,3)<3)
529     {
530      puts("failed writing rgb values to bmp file");
531      return -1;
532     }
533    }
534    if(foo%4)
535    {
536     memset(buf,0,4-foo%4);
537     if(write(fd,buf,4-foo%4)<4-foo%4)
538     {
539      puts("failed writing 4 byte ending");
540      return -1;
541     }
542    }
543   }
544  }
545  close(fd);
546
547  return 1;
548 }
549
550 int save_to_file(char *sf,d3_lattice *d3_l,info *my_inf)
551 {
552  int sf_fd,c;
553
554  if((sf_fd=open(sf,O_WRONLY|O_CREAT))<0)
555  {
556   puts("cannot open save file");
557   return -1;
558  }
559  if(write(sf_fd,d3_l,sizeof(d3_lattice))<sizeof(d3_lattice))
560  {
561   puts("failed saving d3 lattice struct");
562   return -1;
563  }
564  if(write(sf_fd,my_inf,sizeof(info))<sizeof(info))
565  {
566   puts("failed saving info struct");
567   return-1;
568  }
569  c=d3_l->max_x*d3_l->max_y*d3_l->max_z;
570  if(write(sf_fd,d3_l->status,c*sizeof(unsigned char))<c*sizeof(unsigned char))
571  {
572   puts("failed saving status of d3 lattice sites");
573   return -1;
574  }
575  if(write(sf_fd,d3_l->extra,c*sizeof(int))<c*sizeof(int))
576  {
577   puts("failed saving sites concentration");
578   return -1;
579  }
580  close(sf_fd);
581
582  return 1;
583 }
584
585 int load_from_file(char *lf,d3_lattice *d3_l,info *my_inf)
586 {
587  int lf_fd,c;
588
589  if((lf_fd=open(lf,O_RDONLY))<0)
590  {
591   puts("cannot open load file");
592   return -1;
593  }
594  if(read(lf_fd,d3_l,sizeof(d3_lattice))<sizeof(d3_lattice))
595  {
596   puts("failed reading d3 lattice struct");
597   return -1;
598  }
599  if(read(lf_fd,my_inf,sizeof(info))<sizeof(info))
600  {
601   puts("failed reading info struct");
602   return-1;
603  }
604  c=d3_l->max_x*d3_l->max_y*d3_l->max_z;
605  if((d3_l->status=(unsigned char*)malloc(c*sizeof(unsigned char)))==NULL)
606  {
607   puts("cannot allocate status buffer");
608   return -1;
609  }
610  if((d3_l->extra=(int *)malloc(c*sizeof(int)))==NULL)
611  {
612   puts("cannot allocate concentration buffer");
613   return -1;
614  }
615  if(read(lf_fd,d3_l->status,c*sizeof(unsigned char))<c*sizeof(unsigned char))
616  {
617   puts("failed reading status of d3 lattice sites");
618   return -1;
619  }
620  if(read(lf_fd,d3_l->extra,c*sizeof(int))<c*sizeof(int))
621  {
622   puts("failed reading sites concentration");
623   return -1;
624  }
625  close(lf_fd);
626
627  return 1;
628 }
629
630 int convert_file(char *cf,d3_lattice *d3_l)
631 {
632  int x,y,z;
633  int c_fd;
634
635  if((c_fd=open(cf,O_WRONLY|O_CREAT))<0)
636  {
637   puts("cannot open convert file");
638   return -1;
639  }
640  dprintf(c_fd,"# created by nlsop (gnuplot format)\n");
641  for(x=0;x<d3_l->max_x;x++)
642  {
643   for(y=0;y<d3_l->max_y;y++)
644   {
645    for(z=0;z<d3_l->max_z;z++)
646    {
647     if(*(d3_l->status+x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y)&AMORPH) dprintf(c_fd,"%d %d %d\n",x,y,z);
648    }
649   }
650  }
651  close(c_fd);
652
653  return 1;
654 }
655
656 int get_c_ratio(double *c_ratio,char *pfile,info *my_info,d3_lattice *d3_l)
657 {
658  double all,a,b,d;
659  int i,k;
660  int p_fd;
661  unsigned char buf[32];
662  char *p;
663  unsigned char c;
664
665  if((p_fd=open(pfile,O_RDONLY))<0)
666  {
667   puts("cannot open profile file");
668   return -1;
669  }
670  k=1;
671  d=0;
672  all=0;
673  while(k)
674  {
675   for(i=0;i<32;i++)
676   {
677    k=read(p_fd,&c,1);
678    buf[i]=c;
679    if(c=='\n') break;
680   }
681   if(k)
682   {
683    p=strtok(buf," ");
684    a=atof(p)/10; /* nm */
685    p=strtok(NULL," ");
686    b=atof(p);
687    if(a>my_info->b_cd*CELL_LENGTH && a<(my_info->b_cd+d3_l->max_z)*CELL_LENGTH) d+=b;
688    all+=b;
689   }
690  }
691  *c_ratio=d/all;
692  close(p_fd);
693
694  return 1;
695 }
696
697 u32 get_reject_graph(info *my_info,d3_lattice *d3_l,char *file,u32 *graph) {
698  double a,b;
699  int i,j,k;
700  int fd;
701  char buf[32],*p;
702  unsigned char *flag;
703  u32 max;
704
705  max=0;
706  if((fd=open(file,O_RDONLY))<0)
707  {
708   puts("cannot open file to calculate rejection graph");
709   return -1;
710  }
711  if((flag=(unsigned char *)malloc(d3_l->max_z))==NULL)
712  {
713   puts("cannot malloc flag memory for rejection graph");
714   return -1;
715  }
716  memset(flag,0,d3_l->max_z);
717  memset(graph,0,d3_l->max_z*sizeof(u32));
718  /* get fixpoints */
719  k=1;
720  while(k)
721  {
722   for(i=0;i<32;i++)
723   {
724    k=read(fd,&buf[i],1);
725    if((buf[i]=='\n')||(k==0)) break;
726   }
727   if(k)
728   {
729    p=strtok(buf," ");
730    a=atof(p)/10; /* nm */
731    p=strtok(NULL," ");
732    b=atof(p);
733    if(a>d3_l->max_z*CELL_LENGTH) k=0;
734    else 
735    {
736     graph[(int)(a/CELL_LENGTH)]=(int)(URAND_MAX*b);
737     flag[(int)(a/CELL_LENGTH)]=1;
738    }
739   }
740  }
741  /* do (linear) interpolation here! */
742  i=0;
743  a=0;
744  while(i<d3_l->max_z)
745  {
746   /* graph[0] is 0! */
747   j=i;
748   i++;
749   while(flag[i]==0&&i<d3_l->max_z) i++;
750   printf("debug: i = %d j = %d\n",i,j);
751   for(k=j+1;k<i;k++) graph[k]=(int)((k-j)*((int)graph[i]-(int)graph[j])/(i-j))+graph[j];
752   if(graph[i]>max) max=graph[i];
753  }
754
755  free(flag);
756
757  return max;
758 }
759
760 int main(int argc,char **argv)
761 {
762  u32 x,y,z,x_c,y_c,z_c;
763  int i,j,quit,escape,switchmode,nowait,bmp,ac_distr;
764  int refresh,resave;
765  int c_step;
766  char s_file[MAX_CHARS];
767  char s_file_tmp[MAX_CHARS];
768  char l_file[MAX_CHARS];
769  char c_file[MAX_CHARS];
770  char p_file[MAX_CHARS];
771  char convert;
772  char r_file[MAX_CHARS];
773 #ifdef USE_DFB_API
774  char xyz_txt[MAX_TXT];
775  char status_txt[MAX_TXT];
776  char conc_txt[MAX_TXT];
777  char steps_txt[MAX_TXT];
778  char cc_txt[MAX_TXT];
779  char a_txt[MAX_TXT];
780  char s_txt[MAX_TXT];
781  char ap_txt[MAX_TXT];
782  char el_txt[MAX_TXT];
783  char cd_txt[MAX_TXT];
784  char r_txt[MAX_TXT];
785  char cp_txt[MAX_TXT];
786  char zdiff_txt[MAX_TXT];
787  char diff_txt[MAX_TXT];
788  char dr_ac_txt[MAX_TXT];
789  char dr_cc_txt[MAX_TXT];
790  char dose_txt[MAX_TXT];
791  char mode_txt[MAX_TXT];
792  char *arg_v[MAX_ARGV];
793 #endif
794  d3_lattice d3_l;
795  info my_info;
796  unsigned char mode;
797  double c_ratio;
798 #ifdef USE_DFB_API
799  int max_extra;
800 #endif
801  u32 *c_profile;
802  u32 *n_e_loss;
803
804  d3_l.max_x=X;
805  d3_l.max_y=Y;
806  d3_l.max_z=Z;
807  my_info.steps=STEPS;
808  my_info.range=RANGE;
809  refresh=REFRESH;
810  resave=RESAVE;
811  my_info.z_diff=0;
812  my_info.c_diff=1;
813  my_info.a_el=A_EL;
814  my_info.b_el=B_EL;
815  my_info.a_cd=A_CD;
816  my_info.b_cd=B_CD;
817  my_info.a_ap=A_AP;
818  my_info.b_ap=B_AP;
819  my_info.a_cp=A_CP;
820  my_info.cc=CC;
821  my_info.dr_ac=DR_AC;
822  my_info.dr_cc=DR_CC;
823  my_info.diff_rate=DIFF_RATE;
824  my_info.cpi=CPI;
825  nowait=0;
826  quit=0;
827  escape=0;
828  switchmode=0;
829  c_step=0;
830  strcpy(s_file,"");
831  strcpy(l_file,"");
832  strcpy(c_file,"");
833  strcpy(p_file,IMP_PROFILE);
834  convert=0;
835  strcpy(r_file,"");
836  mode=0;
837  u32 ne_max;
838
839  for(i=1;i<argc;i++)
840  {
841   if(argv[i][0]=='-')
842   {
843    switch(argv[i][1])
844    {
845     case 'h':
846      usage();
847      return -1;
848     case 'n':
849      nowait=1;
850      break;
851     case 'a':
852      my_info.a_el=atof(argv[++i]);
853      break;
854     case 'b':
855      my_info.b_el=atof(argv[++i]);
856      break;
857     case 'x':
858      d3_l.max_x=atoi(argv[++i]);
859      break;
860     case 'y':
861      d3_l.max_y=atoi(argv[++i]);
862      break;
863     case 'z':
864      d3_l.max_z=atoi(argv[++i]);
865      break;
866     case 'Z':
867      my_info.z_diff=1;
868      break;
869     case 'i':
870      my_info.c_diff=0;
871      my_info.dr_cc=0;
872      break;
873     case 's':
874      my_info.steps=atoi(argv[++i]);
875      break;
876     case 'd':
877      refresh=atoi(argv[++i]);
878      break;
879     case 'r':
880      my_info.range=atoi(argv[++i]);
881      break;
882     case 'f':
883      my_info.a_ap=atof(argv[++i]);
884      break;
885     case 'p':
886      my_info.b_ap=atof(argv[++i]);
887      break;
888     case 'F':
889      my_info.a_cp=atof(argv[++i]);
890      break;
891     case 'A':
892      my_info.a_cd=atof(argv[++i]);
893      break;
894     case 'B':
895      my_info.b_cd=atof(argv[++i]);
896      break;
897     case 'W':
898      resave=atoi(argv[++i]);
899      break;
900     case 'C':
901      strcpy(l_file,argv[++i]);
902      if(i<argc-1) if(argv[i+1][0]!='-') strcpy(c_file,argv[++i]);
903      convert=1;
904      break;
905     case 'D':
906      my_info.dr_ac=atof(argv[++i]);
907      break;
908     case 'c':
909      my_info.dr_cc=atof(argv[++i]);
910      break;
911     case 'e':
912      my_info.diff_rate=atoi(argv[++i]);
913      break;
914     case 'L':
915      strcpy(l_file,argv[++i]);
916      break;
917     case 'S':
918      strcpy(s_file,argv[++i]);
919      break;
920     case 'R':
921      strcpy(r_file,argv[++i]);
922      break;
923     case 'P':
924      strcpy(p_file,argv[++i]);
925      break;
926     case 'g':
927      strcpy(l_file,argv[++i]);
928      if(i<argc-1) if(argv[i+1][0]!='-') c_step=atoi(argv[++i]);
929      break;
930     case 'H':
931      my_info.cpi=atoi(argv[++i]);
932      break;
933     default:
934      usage();
935      return -1;
936    }
937   } else usage();
938  }
939
940  x=d3_l.max_x/2-1;
941  y=d3_l.max_y/2-1;
942  z=d3_l.max_z/2-1;
943
944 #ifdef NODFB
945  if(!strcmp(s_file,""))
946  {
947   puts("NODFB defined, run with -S option");
948   return -1;
949  }
950 #endif
951
952  if(!strcmp(r_file,"")) rand_init(NULL);
953  else rand_init(r_file);
954
955  if(!strcmp(l_file,""))
956  {
957   i=d3_l.max_x*d3_l.max_y*d3_l.max_z;
958 #ifdef USE_DFB_API
959   d3_lattice_init(&argc,argv,&d3_l);
960 #endif
961   if((d3_l.status=(unsigned char *)malloc(i*sizeof(unsigned char)))==NULL)
962   {
963    puts("failed allocating status buffer");
964    return -1;
965   }
966   memset(d3_l.status,0,i*sizeof(unsigned char));
967   if((d3_l.extra=(int *)malloc(i*sizeof(int)))==NULL)
968   {
969    puts("failed allocating concentration buffer");
970    return -1;
971   }
972   memset(d3_l.extra,0,i*sizeof(int));
973  } else
974  {
975   load_from_file(l_file,&d3_l,&my_info);
976   if(convert) 
977   {   
978    if(!strcmp(c_file,"")) sprintf(c_file,"%s_gnuplot",l_file);
979    printf("converting file %s to %s\n",l_file,c_file);
980    convert_file(c_file,&d3_l);
981    puts("done");
982    return 1;
983   } 
984 #ifdef USE_DFB_API
985   else d3_lattice_init(&argc,argv,&d3_l);
986 #endif
987  }
988
989 #ifdef USE_DFB_API
990  d3_event_init(&d3_l);
991 #endif
992
993 #ifdef USE_DFB_API
994  strcpy(a_txt,"args:");
995  sprintf(s_txt,"steps: %d",my_info.steps);
996  sprintf(dose_txt,"dose: %.2fe+17 C/cm²",my_info.steps*1.0/(d3_l.max_x*d3_l.max_y*CELL_LENGTH*CELL_LENGTH*1000));
997  sprintf(r_txt,"pressure range: %d",my_info.range);
998  sprintf(ap_txt,"a_ap: %.4f  b_ap: %.3f",my_info.a_ap,my_info.b_ap);
999  sprintf(el_txt,"a_el: %.3f  b_el: %.3f",my_info.a_el,my_info.b_el);
1000  sprintf(cd_txt,"a_cd: %.3f  b_cd: %.3f",my_info.a_cd,my_info.b_cd);
1001  sprintf(cp_txt,"a_cp: %.5f",my_info.a_cp);
1002  sprintf(dr_ac_txt,"a/c diffusion rate: %.4f",my_info.dr_ac);
1003  if(my_info.c_diff!=0) sprintf(dr_cc_txt,"c/c diffusion rate: %.4f",my_info.dr_cc);
1004  else sprintf(dr_cc_txt,"c/c diffusion rate: none");
1005  sprintf(zdiff_txt,"diffusion in z direction: %c",my_info.z_diff?'y':'n');
1006  sprintf(diff_txt,"diffusion every %d steps",my_info.diff_rate);
1007  strcpy(mode_txt,"view: a/c mode");
1008  arg_v[1]=xyz_txt;
1009  arg_v[2]=NULL;
1010  arg_v[3]=status_txt;
1011  arg_v[4]=conc_txt;
1012  arg_v[5]=NULL;
1013  arg_v[6]=mode_txt;
1014  arg_v[7]=NULL;
1015  arg_v[8]=steps_txt;
1016  arg_v[9]=cc_txt;
1017  arg_v[10]=NULL;
1018  arg_v[11]=diff_txt;
1019  arg_v[12]=zdiff_txt;
1020  arg_v[13]=NULL;
1021  arg_v[14]=a_txt;
1022  arg_v[15]=s_txt;
1023  arg_v[16]=dose_txt;
1024  arg_v[17]=NULL;
1025  arg_v[18]=r_txt;
1026  arg_v[19]=ap_txt;
1027  arg_v[20]=el_txt;
1028  arg_v[21]=cd_txt;
1029  arg_v[22]=cp_txt;
1030  arg_v[23]=NULL;
1031  arg_v[24]=dr_ac_txt;
1032  arg_v[25]=dr_cc_txt;
1033 #endif
1034
1035  if((!strcmp(l_file,""))||(c_step))
1036  {
1037   /* calculate ratio of c_simwindow / c_total */
1038   if(get_c_ratio(&c_ratio,p_file,&my_info,&d3_l)!=1)
1039   {
1040    puts("failed calculating ratio");
1041    return -1;
1042   }
1043   /* compute graphs for random number rejection method */
1044   if((c_profile=(u32 *)malloc(d3_l.max_z*sizeof(unsigned int)))==NULL)
1045   {
1046    puts("failed allocating memory for carbon profile graph");
1047    return -1;
1048   }
1049   if((n_e_loss=(u32 *)malloc(d3_l.max_z*sizeof(unsigned int)))==NULL)
1050   {
1051    puts("failed allocating memory for nuclear energy loss graph");
1052    return -1;
1053   }
1054   ne_max=get_reject_graph(&my_info,&d3_l,p_file,c_profile);
1055
1056   /* break here for testing ... */
1057   return 23;
1058
1059   i=(c_step?c_step:0);
1060   while((i<my_info.steps) && (quit==0) && (escape==0))
1061   {
1062    for(j=0;j<my_info.cpi;j++)
1063    {
1064     x_c=get_rand(d3_l.max_x);
1065     y_c=get_rand(d3_l.max_y);
1066     z_c=get_rand_lgp(d3_l.max_z,my_info.a_el,my_info.b_el);
1067     process_cell(&d3_l,x_c,y_c,z_c,&my_info);
1068    }
1069    distrib_c(&d3_l,&my_info,i,c_ratio);
1070 #ifdef USE_DFB_API
1071    if(i%refresh==0)
1072    {
1073     sprintf(xyz_txt,"x: %d  y: %d  z: %d",x+1,y+1,z+1);
1074     sprintf(status_txt,"status: %c",(*(d3_l.status+x+y*d3_l.max_x+z*d3_l.max_x*d3_l.max_y)&AMORPH)?'a':'c');
1075     sprintf(conc_txt,"conc: %d",*(d3_l.extra+x+y*d3_l.max_x+z*d3_l.max_x*d3_l.max_y));
1076     sprintf(steps_txt,"step: %d",i);
1077     sprintf(cc_txt,"total c: %d",my_info.cc);
1078     d3_lattice_draw(&d3_l,x,y,z,25,arg_v,mode,0);
1079    }
1080 #endif
1081    if(i%resave==0 && strcmp(s_file,"") && resave!=0 && i!=0)
1082    {
1083     sprintf(s_file_tmp,"%s_%d_of_%d",s_file,i,my_info.steps);
1084     save_to_file(s_file_tmp,&d3_l,&my_info);
1085 #ifdef NODFB
1086     printf("saved %s\n",s_file_tmp);
1087 #endif
1088    }
1089    i++;
1090   }
1091  }
1092
1093  if(strcmp(s_file,""))
1094  {
1095    printf("saved %s\n",s_file);
1096    save_to_file(s_file,&d3_l,&my_info);
1097  }
1098
1099 #ifdef USE_DFB_API
1100  /* allocating buffer for pressure values */
1101  if((d3_l.v_ptr=malloc(d3_l.max_x*d3_l.max_y*d3_l.max_z*sizeof(unsigned char)))==NULL)
1102  {
1103   puts("cannot allocate buffer for pressure values");
1104   return -1;
1105  }
1106  /* calc values */
1107  calc_pressure(&d3_l,my_info.range);
1108  max_extra=calc_max_extra(&d3_l);
1109
1110  while((quit==0) && (escape==0) && (nowait==0))
1111  {
1112   /* bahh! */
1113   if(switchmode==0) mode=0;
1114   if(switchmode==1) mode=1;
1115   if(switchmode==2) mode=2;
1116   /* end of bahh! */
1117   sprintf(xyz_txt,"x: %d  y: %d  z: %d",x+1,y+1,z+1);
1118   sprintf(status_txt,"status: %c",(*(d3_l.status+x+y*d3_l.max_x+z*d3_l.max_x*d3_l.max_y)&AMORPH)?'a':'c');
1119   sprintf(conc_txt,"conc: %d",*(d3_l.extra+x+y*d3_l.max_x+z*d3_l.max_x*d3_l.max_y));
1120   sprintf(steps_txt,"step: end");
1121   sprintf(cc_txt,"total c: %d",my_info.cc);
1122   if(switchmode==0) strcpy(mode_txt,"view: a/c mode");
1123   if(switchmode==1) strcpy(mode_txt,"view: c conc mode");
1124   if(switchmode==2) strcpy(mode_txt,"view: a pressure mode");
1125   d3_lattice_draw(&d3_l,x,y,z,25,arg_v,mode,max_extra);
1126   bmp=0;
1127   ac_distr=0;
1128   scan_event(&d3_l,&x,&y,&z,&quit,&escape,&switchmode,&bmp,&ac_distr);
1129   if(bmp) write_bmp(&d3_l,bmp,x,y,z);
1130   if(ac_distr) write_ac_distr(&d3_l,ac_distr);
1131  }
1132
1133  d3_lattice_release(&d3_l);
1134 #endif
1135
1136  return 1;
1137 }