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