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