output whole float (we have enough space now)
[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=0,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   for(x=0;x<d3_l->max_x;x++)
354   {
355    for(y=0;y<d3_l->max_y;y++)
356    {
357     offset=x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y;
358     if(ac_distr==1)
359      if(*(d3_l->status+offset)&AMORPH) count+=*(d3_l->extra+offset);
360     if(ac_distr==2)
361      if(!(*(d3_l->status+offset)&AMORPH)) count+=*(d3_l->extra+offset);
362     if(ac_distr==3) count+=*(d3_l->extra+offset);
363    }
364   }
365   dprintf(fd,"%d %d\n",z,count);
366  }
367  close(fd);
368  
369  return 1;
370 }
371
372 int write_bmp(d3_lattice *d3_l,int window,u32 x,u32 y,u32 z)
373 {
374  int fd,i,j,size=0,foo=0,end=0;
375  int width=0,height=0;
376  char bmpfile[MAX_CHARS];
377  char buf[128];
378
379  if(((window==4)||(window==5))&&(d3_l->max_z<FFT_HEIGHT) )
380  {
381   puts("error: z < FFT_HEIGHT!");
382   puts("not writing bmp file ...");
383   return -1;
384  }
385
386  if(window%3==1)
387  {
388   sprintf(bmpfile,"x-z_%d.bmp",y);
389   foo=3*d3_l->max_x;
390   if(window==1)
391   {
392    size=(foo+(4-foo%4))*d3_l->max_z;
393    height=d3_l->max_z;
394   }
395   else 
396   {
397    size=(foo+(4-foo%4))*FFT_HEIGHT;
398    end=d3_l->max_z-FFT_HEIGHT;
399    height=FFT_HEIGHT;
400   }
401   width=d3_l->max_x;
402  }
403  if(window%3==2)
404  {
405   sprintf(bmpfile,"y-z_%d.bmp",x);
406   foo=3*d3_l->max_y;
407   if(window==2)
408   {
409    size=(foo+(4-foo%4))*d3_l->max_z;
410    height=d3_l->max_z;
411   }
412   else 
413   {
414    size=(foo+(4-foo%4))*FFT_HEIGHT;
415    end=d3_l->max_z-FFT_HEIGHT;
416    height=FFT_HEIGHT;
417   }
418   width=d3_l->max_y;
419  }
420  if(window==3)
421  {
422   sprintf(bmpfile,"x-y_%d.bmp",z);
423   foo=3*d3_l->max_x;
424   size=(foo+(4-foo%4))*d3_l->max_y;
425   width=d3_l->max_x;
426   height=d3_l->max_y;
427  }
428
429  if((fd=open(bmpfile,O_WRONLY|O_CREAT))<0)
430  {
431   puts("cannot open bmp file");
432   return -1;
433  }
434
435  /* bmpheader */
436  buf[0]='B'; /* std header start */
437  buf[1]='M';
438  buf[2]=(size+0x36)&0xff; /* file size */
439  buf[3]=(size+0x36)>>8;
440  memset(buf+4,0,6);
441  buf[10]=0x36; /* offset to data */
442  memset(buf+11,0,3);
443  buf[14]=0x28; /* length of bmp info header */
444  memset(buf+15,0,3);
445  buf[18]=width&0xff; /* width and height */
446  buf[19]=width>>8;
447  memset(buf+20,0,2);
448  buf[22]=height&0xff;
449  buf[23]=height>>8;
450  memset(buf+24,0,2);
451  buf[26]=1; /* # planes -> 1 */
452  buf[27]=0;
453  buf[28]=24; /* bits per pixel -> 2^24 (true color) */
454  buf[29]=0;
455  memset(buf+30,0,4); /* compression -> none */
456  buf[34]=size&0xff; /* data size */
457  buf[35]=size>>8;
458  memset(buf+36,0,2);
459  buf[38]=0x12; /* res: pixel/meter */
460  buf[39]=0x0b;
461  memset(buf+40,0,2);
462  buf[42]=0x12;
463  buf[43]=0x0b;
464  memset(buf+44,0,2); 
465  memset(buf+46,0,8); /* no colors, no important colors */
466
467  if(write(fd,buf,54)<54)
468  {
469   puts("failed writing bmp header");
470   return -1;
471  }
472  if(window%3==1)
473  {
474   for(j=0;j<d3_l->max_z-end;j++)
475   {
476    for(i=0;i<d3_l->max_x;i++)
477    {
478     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);
479     else memset(buf,0,3);
480     if(write(fd,buf,3)<3)
481     {
482      puts("failed writing rgb values to bmp file");
483      return-1;
484     }
485    }
486    if(foo%4)
487    {
488     memset(buf,0,4-foo%4);
489     if(write(fd,buf,4-foo%4)<4-foo%4)
490     {
491      puts("failed writing 4 byte ending");
492      return -1;
493     }
494    } 
495   }
496  }
497  if(window%3==2)
498  {
499   for(j=0;j<d3_l->max_z-end;j++)
500   {
501    for(i=0;i<d3_l->max_y;i++)
502    {
503     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);
504     else memset(buf,0,3);
505     if(write(fd,buf,3)<3)
506     {
507      puts("failed writing rgb values to bmp file");
508      return-1;
509     }
510    }
511    if(foo%4)
512    {
513     memset(buf,0,4-foo%4);
514     if(write(fd,buf,4-foo%4)<4-foo%4)
515     {
516      puts("failed writing 4 byte ending");
517      return -1;
518     }
519    }
520   }
521  }
522  if(window==3)
523  {
524   for(j=0;j<d3_l->max_y;j++)
525   {
526    for(i=0;i<d3_l->max_x;i++)
527    {
528     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);
529     else memset(buf,0,3);
530     if(write(fd,buf,3)<3)
531     {
532      puts("failed writing rgb values to bmp file");
533      return -1;
534     }
535    }
536    if(foo%4)
537    {
538     memset(buf,0,4-foo%4);
539     if(write(fd,buf,4-foo%4)<4-foo%4)
540     {
541      puts("failed writing 4 byte ending");
542      return -1;
543     }
544    }
545   }
546  }
547  close(fd);
548
549  return 1;
550 }
551
552 int save_to_file(char *sf,d3_lattice *d3_l,info *my_inf)
553 {
554  int sf_fd,c;
555
556  if((sf_fd=open(sf,O_WRONLY|O_CREAT))<0)
557  {
558   puts("cannot open save file");
559   return -1;
560  }
561  if(write(sf_fd,d3_l,sizeof(d3_lattice))<sizeof(d3_lattice))
562  {
563   puts("failed saving d3 lattice struct");
564   return -1;
565  }
566  if(write(sf_fd,my_inf,sizeof(info))<sizeof(info))
567  {
568   puts("failed saving info struct");
569   return-1;
570  }
571  c=d3_l->max_x*d3_l->max_y*d3_l->max_z;
572  if(write(sf_fd,d3_l->status,c*sizeof(unsigned char))<c*sizeof(unsigned char))
573  {
574   puts("failed saving status of d3 lattice sites");
575   return -1;
576  }
577  if(write(sf_fd,d3_l->extra,c*sizeof(int))<c*sizeof(int))
578  {
579   puts("failed saving sites concentration");
580   return -1;
581  }
582  close(sf_fd);
583
584  return 1;
585 }
586
587 int load_from_file(char *lf,d3_lattice *d3_l,info *my_inf)
588 {
589  int lf_fd,c;
590
591  if((lf_fd=open(lf,O_RDONLY))<0)
592  {
593   puts("cannot open load file");
594   return -1;
595  }
596  if(read(lf_fd,d3_l,sizeof(d3_lattice))<sizeof(d3_lattice))
597  {
598   puts("failed reading d3 lattice struct");
599   return -1;
600  }
601  if(read(lf_fd,my_inf,sizeof(info))<sizeof(info))
602  {
603   puts("failed reading info struct");
604   return-1;
605  }
606  c=d3_l->max_x*d3_l->max_y*d3_l->max_z;
607  if((d3_l->status=(unsigned char*)malloc(c*sizeof(unsigned char)))==NULL)
608  {
609   puts("cannot allocate status buffer");
610   return -1;
611  }
612  if((d3_l->extra=(int *)malloc(c*sizeof(int)))==NULL)
613  {
614   puts("cannot allocate concentration buffer");
615   return -1;
616  }
617  if(read(lf_fd,d3_l->status,c*sizeof(unsigned char))<c*sizeof(unsigned char))
618  {
619   puts("failed reading status of d3 lattice sites");
620   return -1;
621  }
622  if(read(lf_fd,d3_l->extra,c*sizeof(int))<c*sizeof(int))
623  {
624   puts("failed reading sites concentration");
625   return -1;
626  }
627  close(lf_fd);
628
629  return 1;
630 }
631
632 int convert_file(char *cf,d3_lattice *d3_l)
633 {
634  int x,y,z;
635  int c_fd;
636
637  if((c_fd=open(cf,O_WRONLY|O_CREAT))<0)
638  {
639   puts("cannot open convert file");
640   return -1;
641  }
642  dprintf(c_fd,"# created by nlsop (gnuplot format)\n");
643  for(x=0;x<d3_l->max_x;x++)
644  {
645   for(y=0;y<d3_l->max_y;y++)
646   {
647    for(z=0;z<d3_l->max_z;z++)
648    {
649     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);
650    }
651   }
652  }
653  close(c_fd);
654
655  return 1;
656 }
657
658 int get_c_ratio(double *c_ratio,char *pfile,info *my_info,d3_lattice *d3_l)
659 {
660  double all,a,b,d;
661  int i,k;
662  int p_fd;
663  unsigned char buf[32];
664  char *p;
665  unsigned char c;
666
667  if((p_fd=open(pfile,O_RDONLY))<0)
668  {
669   puts("cannot open profile file");
670   return -1;
671  }
672  k=1;
673  d=0;
674  all=0;
675  while(k)
676  {
677   for(i=0;i<32;i++)
678   {
679    k=read(p_fd,&c,1);
680    buf[i]=c;
681    if(c=='\n') break;
682   }
683   if(k)
684   {
685    p=strtok(buf," ");
686    a=atof(p)/10; /* nm */
687    p=strtok(NULL," ");
688    b=atof(p);
689    if(a>my_info->b_cd*CELL_LENGTH && a<(my_info->b_cd+d3_l->max_z)*CELL_LENGTH) d+=b;
690    all+=b;
691   }
692  }
693  *c_ratio=d/all;
694  close(p_fd);
695
696  return 1;
697 }
698
699 u32 get_reject_graph(info *my_info,d3_lattice *d3_l,char *file,u32 *graph) {
700  double a,b;
701  int i,j,k;
702  int fd;
703  char buf[32],*p;
704  unsigned char *flag;
705  u32 max;
706
707  max=0;
708  if((fd=open(file,O_RDONLY))<0)
709  {
710   puts("cannot open file to calculate rejection graph");
711   return -1;
712  }
713  if((flag=(unsigned char *)malloc(d3_l->max_z))==NULL)
714  {
715   puts("cannot malloc flag memory for rejection graph");
716   return -1;
717  }
718  memset(flag,0,d3_l->max_z);
719  memset(graph,0,d3_l->max_z*sizeof(u32));
720  /* get fixpoints */
721  k=1;
722  while(k)
723  {
724   for(i=0;i<32;i++)
725   {
726    k=read(fd,&buf[i],1);
727    if((buf[i]=='\n')||(k==0)) break;
728   }
729   if(k)
730   {
731    p=strtok(buf," ");
732    a=atof(p)/10; /* nm */
733    p=strtok(NULL," ");
734    b=atof(p);
735    if(a>d3_l->max_z*CELL_LENGTH) k=0;
736    else 
737    {
738     graph[(int)(a/CELL_LENGTH)]=(int)(URAND_MAX*b);
739     flag[(int)(a/CELL_LENGTH)]=1;
740    }
741   }
742  }
743  /* do (linear) interpolation here! */
744  i=0;
745  a=0;
746  while(i<d3_l->max_z)
747  {
748   /* graph[0] is 0! */
749   j=i;
750   i++;
751   while(flag[i]==0&&i<d3_l->max_z) i++;
752   for(k=j+1;k<i;k++) graph[k]=(int)((k-j)*((int)graph[i]-(int)graph[j])/(i-j))+graph[j];
753   if(graph[i]>max) max=graph[i];
754  }
755
756  free(flag);
757
758  // printf("debug: (interpolated c profile)\n");
759  // for(i=0;i<d3_l->max_z;i++) printf("%d %d\n",i,graph[i]);
760
761  return max;
762 }
763
764 int main(int argc,char **argv)
765 {
766  u32 x,y,z,x_c,y_c,z_c;
767  int i,j,quit,escape,switchmode,nowait,bmp,ac_distr;
768  int refresh,resave;
769  int c_step;
770  char s_file[MAX_CHARS];
771  char s_file_tmp[MAX_CHARS];
772  char l_file[MAX_CHARS];
773  char c_file[MAX_CHARS];
774  char p_file[MAX_CHARS];
775  char n_e_file[MAX_CHARS];
776  char convert;
777  char r_file[MAX_CHARS];
778 #ifdef USE_DFB_API
779  char xyz_txt[MAX_TXT];
780  char status_txt[MAX_TXT];
781  char conc_txt[MAX_TXT];
782  char steps_txt[MAX_TXT];
783  char cc_txt[MAX_TXT];
784  char a_txt[MAX_TXT];
785  char s_txt[MAX_TXT];
786  char ap_txt[MAX_TXT];
787  char el_txt[MAX_TXT];
788  char cd_txt[MAX_TXT];
789  char r_txt[MAX_TXT];
790  char cp_txt[MAX_TXT];
791  char zdiff_txt[MAX_TXT];
792  char diff_txt[MAX_TXT];
793  char dr_ac_txt[MAX_TXT];
794  char dr_cc_txt[MAX_TXT];
795  char dose_txt[MAX_TXT];
796  char mode_txt[MAX_TXT];
797  char *arg_v[MAX_ARGV];
798 #endif
799  d3_lattice d3_l;
800  info my_info;
801  unsigned char mode;
802  double c_ratio;
803 #ifdef USE_DFB_API
804  int max_extra;
805 #endif
806  u32 *c_profile;
807  u32 *n_e_loss;
808  u32 ne_max,ip_max;
809
810  d3_l.max_x=X;
811  d3_l.max_y=Y;
812  d3_l.max_z=Z;
813  my_info.steps=STEPS;
814  my_info.range=RANGE;
815  refresh=REFRESH;
816  resave=RESAVE;
817  my_info.z_diff=0;
818  my_info.c_diff=1;
819  my_info.a_el=A_EL;
820  my_info.b_el=B_EL;
821  my_info.a_cd=A_CD;
822  my_info.b_cd=B_CD;
823  my_info.a_ap=A_AP;
824  my_info.b_ap=B_AP;
825  my_info.a_cp=A_CP;
826  my_info.cc=CC;
827  my_info.dr_ac=DR_AC;
828  my_info.dr_cc=DR_CC;
829  my_info.diff_rate=DIFF_RATE;
830  my_info.cpi=CPI;
831  nowait=0;
832  quit=0;
833  escape=0;
834  switchmode=0;
835  c_step=0;
836  strcpy(s_file,"");
837  strcpy(l_file,"");
838  strcpy(c_file,"");
839  strcpy(p_file,IMP_PROFILE);
840  strcpy(n_e_file,NEL_PROFILE);
841  convert=0;
842  strcpy(r_file,"");
843  mode=0;
844  ne_max=0;
845  ip_max=0;
846
847  for(i=1;i<argc;i++)
848  {
849   if(argv[i][0]=='-')
850   {
851    switch(argv[i][1])
852    {
853     case 'h':
854      usage();
855      return -1;
856     case 'n':
857      nowait=1;
858      break;
859     case 'a':
860      my_info.a_el=atof(argv[++i]);
861      break;
862     case 'b':
863      my_info.b_el=atof(argv[++i]);
864      break;
865     case 'x':
866      d3_l.max_x=atoi(argv[++i]);
867      break;
868     case 'y':
869      d3_l.max_y=atoi(argv[++i]);
870      break;
871     case 'z':
872      d3_l.max_z=atoi(argv[++i]);
873      break;
874     case 'Z':
875      my_info.z_diff=1;
876      break;
877     case 'i':
878      my_info.c_diff=0;
879      my_info.dr_cc=0;
880      break;
881     case 's':
882      my_info.steps=atoi(argv[++i]);
883      break;
884     case 'd':
885      refresh=atoi(argv[++i]);
886      break;
887     case 'r':
888      my_info.range=atoi(argv[++i]);
889      break;
890     case 'f':
891      my_info.a_ap=atof(argv[++i]);
892      break;
893     case 'p':
894      my_info.b_ap=atof(argv[++i]);
895      break;
896     case 'F':
897      my_info.a_cp=atof(argv[++i]);
898      break;
899     case 'A':
900      my_info.a_cd=atof(argv[++i]);
901      break;
902     case 'B':
903      my_info.b_cd=atof(argv[++i]);
904      break;
905     case 'W':
906      resave=atoi(argv[++i]);
907      break;
908     case 'C':
909      strcpy(l_file,argv[++i]);
910      if(i<argc-1) if(argv[i+1][0]!='-') strcpy(c_file,argv[++i]);
911      convert=1;
912      break;
913     case 'D':
914      my_info.dr_ac=atof(argv[++i]);
915      break;
916     case 'c':
917      my_info.dr_cc=atof(argv[++i]);
918      break;
919     case 'e':
920      my_info.diff_rate=atoi(argv[++i]);
921      break;
922     case 'L':
923      strcpy(l_file,argv[++i]);
924      break;
925     case 'S':
926      strcpy(s_file,argv[++i]);
927      break;
928     case 'R':
929      strcpy(r_file,argv[++i]);
930      break;
931     case 'P':
932      strcpy(p_file,argv[++i]);
933      break;
934     case 'N':
935      strcpy(n_e_file,argv[++i]);
936      break;
937     case 'g':
938      strcpy(l_file,argv[++i]);
939      if(i<argc-1) if(argv[i+1][0]!='-') c_step=atoi(argv[++i]);
940      break;
941     case 'H':
942      my_info.cpi=atoi(argv[++i]);
943      break;
944     default:
945      usage();
946      return -1;
947    }
948   } else usage();
949  }
950
951  x=d3_l.max_x/2-1;
952  y=d3_l.max_y/2-1;
953  z=d3_l.max_z/2-1;
954
955 #ifdef NODFB
956  if(!strcmp(s_file,""))
957  {
958   puts("NODFB defined, run with -S option");
959   return -1;
960  }
961 #endif
962
963  if(!strcmp(r_file,"")) rand_init(NULL);
964  else rand_init(r_file);
965
966  if(!strcmp(l_file,""))
967  {
968   i=d3_l.max_x*d3_l.max_y*d3_l.max_z;
969 #ifdef USE_DFB_API
970   d3_lattice_init(&argc,argv,&d3_l);
971 #endif
972   if((d3_l.status=(unsigned char *)malloc(i*sizeof(unsigned char)))==NULL)
973   {
974    puts("failed allocating status buffer");
975    return -1;
976   }
977   memset(d3_l.status,0,i*sizeof(unsigned char));
978   if((d3_l.extra=(int *)malloc(i*sizeof(int)))==NULL)
979   {
980    puts("failed allocating concentration buffer");
981    return -1;
982   }
983   memset(d3_l.extra,0,i*sizeof(int));
984  } else
985  {
986   load_from_file(l_file,&d3_l,&my_info);
987   if(convert) 
988   {   
989    if(!strcmp(c_file,"")) sprintf(c_file,"%s_gnuplot",l_file);
990    printf("converting file %s to %s\n",l_file,c_file);
991    convert_file(c_file,&d3_l);
992    puts("done");
993    return 1;
994   } 
995 #ifdef USE_DFB_API
996   else d3_lattice_init(&argc,argv,&d3_l);
997 #endif
998  }
999
1000 #ifdef USE_DFB_API
1001  d3_event_init(&d3_l);
1002 #endif
1003
1004 #ifdef USE_DFB_API
1005  strcpy(a_txt,"args:");
1006  sprintf(s_txt,"steps: %d",my_info.steps);
1007  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));
1008  sprintf(r_txt,"pressure range: %d",my_info.range);
1009  sprintf(ap_txt,"a_ap: %f  b_ap: %f",my_info.a_ap,my_info.b_ap);
1010  sprintf(el_txt,"a_el: %f  b_el: %f",my_info.a_el,my_info.b_el);
1011  sprintf(cd_txt,"a_cd: %f  b_cd: %f",my_info.a_cd,my_info.b_cd);
1012  sprintf(cp_txt,"a_cp: %f",my_info.a_cp);
1013  sprintf(dr_ac_txt,"a/c diffusion rate: %f",my_info.dr_ac);
1014  if(my_info.c_diff!=0) sprintf(dr_cc_txt,"c/c diffusion rate: %f",my_info.dr_cc);
1015  else sprintf(dr_cc_txt,"c/c diffusion rate: none");
1016  sprintf(zdiff_txt,"diffusion in z direction: %c",my_info.z_diff?'y':'n');
1017  sprintf(diff_txt,"diffusion every %d steps",my_info.diff_rate);
1018  strcpy(mode_txt,"view: a/c mode");
1019  arg_v[1]=xyz_txt;
1020  arg_v[2]=NULL;
1021  arg_v[3]=status_txt;
1022  arg_v[4]=conc_txt;
1023  arg_v[5]=NULL;
1024  arg_v[6]=mode_txt;
1025  arg_v[7]=NULL;
1026  arg_v[8]=steps_txt;
1027  arg_v[9]=cc_txt;
1028  arg_v[10]=NULL;
1029  arg_v[11]=diff_txt;
1030  arg_v[12]=zdiff_txt;
1031  arg_v[13]=NULL;
1032  arg_v[14]=a_txt;
1033  arg_v[15]=s_txt;
1034  arg_v[16]=dose_txt;
1035  arg_v[17]=NULL;
1036  arg_v[18]=r_txt;
1037  arg_v[19]=ap_txt;
1038  arg_v[20]=el_txt;
1039  arg_v[21]=cd_txt;
1040  arg_v[22]=cp_txt;
1041  arg_v[23]=NULL;
1042  arg_v[24]=dr_ac_txt;
1043  arg_v[25]=dr_cc_txt;
1044 #endif
1045
1046  if((!strcmp(l_file,""))||(c_step))
1047  {
1048   /* calculate ratio of c_simwindow / c_total */
1049   if(get_c_ratio(&c_ratio,p_file,&my_info,&d3_l)!=1)
1050   {
1051    puts("failed calculating ratio");
1052    return -1;
1053   }
1054   /* compute graphs for random number rejection method */
1055   if((c_profile=(u32 *)malloc(d3_l.max_z*sizeof(unsigned int)))==NULL)
1056   {
1057    puts("failed allocating memory for carbon profile graph");
1058    return -1;
1059   }
1060   if((n_e_loss=(u32 *)malloc(d3_l.max_z*sizeof(unsigned int)))==NULL)
1061   {
1062    puts("failed allocating memory for nuclear energy loss graph");
1063    return -1;
1064   }
1065   ip_max=get_reject_graph(&my_info,&d3_l,p_file,c_profile);
1066   ne_max=get_reject_graph(&my_info,&d3_l,n_e_file,n_e_loss);
1067
1068 #ifdef DEBUG_RAND
1069  while(1)
1070  {
1071 #ifdef DEBUG_CP
1072   printf("%d\n",get_rand_reject(d3_l.max_z,ip_max,c_profile));
1073 #endif
1074 #ifdef DEBUG_NEL
1075   printf("%d\n",get_rand_reject(d3_l.max_z,ne_max,n_e_loss));
1076 #endif
1077  }
1078 #endif
1079
1080   i=(c_step?c_step:0);
1081   while((i<my_info.steps) && (quit==0) && (escape==0))
1082   {
1083    for(j=0;j<my_info.cpi;j++)
1084    {
1085     x_c=get_rand(d3_l.max_x);
1086     y_c=get_rand(d3_l.max_y);
1087     // z_c=get_rand_lgp(d3_l.max_z,my_info.a_el,my_info.b_el);
1088     z_c=get_rand_reject(d3_l.max_z,ne_max,n_e_loss);
1089     process_cell(&d3_l,x_c,y_c,z_c,&my_info);
1090    }
1091    distrib_c(&d3_l,&my_info,i,c_ratio,ip_max,c_profile);
1092 #ifdef USE_DFB_API
1093    if(i%refresh==0)
1094    {
1095     sprintf(xyz_txt,"x: %d  y: %d  z: %d",x+1,y+1,z+1);
1096     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');
1097     sprintf(conc_txt,"conc: %d",*(d3_l.extra+x+y*d3_l.max_x+z*d3_l.max_x*d3_l.max_y));
1098     sprintf(steps_txt,"step: %d",i);
1099     sprintf(cc_txt,"total c: %d",my_info.cc);
1100     d3_lattice_draw(&d3_l,x,y,z,25,arg_v,mode,0);
1101    }
1102 #endif
1103    if(i%resave==0 && strcmp(s_file,"") && resave!=0 && i!=0)
1104    {
1105     sprintf(s_file_tmp,"%s_%d_of_%d",s_file,i,my_info.steps);
1106     save_to_file(s_file_tmp,&d3_l,&my_info);
1107 #ifdef NODFB
1108     printf("saved %s\n",s_file_tmp);
1109 #endif
1110    }
1111    i++;
1112   }
1113  }
1114
1115  if(strcmp(s_file,""))
1116  {
1117    printf("saved %s\n",s_file);
1118    save_to_file(s_file,&d3_l,&my_info);
1119  }
1120
1121 #ifdef USE_DFB_API
1122  /* allocating buffer for pressure values */
1123  if((d3_l.v_ptr=malloc(d3_l.max_x*d3_l.max_y*d3_l.max_z*sizeof(unsigned char)))==NULL)
1124  {
1125   puts("cannot allocate buffer for pressure values");
1126   return -1;
1127  }
1128  /* calc values */
1129  calc_pressure(&d3_l,my_info.range);
1130  max_extra=calc_max_extra(&d3_l);
1131
1132  while((quit==0) && (escape==0) && (nowait==0))
1133  {
1134   /* bahh! */
1135   if(switchmode==0) mode=0;
1136   if(switchmode==1) mode=1;
1137   if(switchmode==2) mode=2;
1138   /* end of bahh! */
1139   sprintf(xyz_txt,"x: %d  y: %d  z: %d",x+1,y+1,z+1);
1140   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');
1141   sprintf(conc_txt,"conc: %d",*(d3_l.extra+x+y*d3_l.max_x+z*d3_l.max_x*d3_l.max_y));
1142   sprintf(steps_txt,"step: end");
1143   sprintf(cc_txt,"total c: %d",my_info.cc);
1144   if(switchmode==0) strcpy(mode_txt,"view: a/c mode");
1145   if(switchmode==1) strcpy(mode_txt,"view: c conc mode");
1146   if(switchmode==2) strcpy(mode_txt,"view: a pressure mode");
1147   d3_lattice_draw(&d3_l,x,y,z,25,arg_v,mode,max_extra);
1148   bmp=0;
1149   ac_distr=0;
1150   scan_event(&d3_l,&x,&y,&z,&quit,&escape,&switchmode,&bmp,&ac_distr);
1151   if(bmp) write_bmp(&d3_l,bmp,x,y,z);
1152   if(ac_distr) write_ac_distr(&d3_l,ac_distr);
1153  }
1154
1155  d3_lattice_release(&d3_l);
1156 #endif
1157
1158  return 1;
1159 }