implemented saturation value of carbon in amorph cells
[physik/nlsop.git] / nlsop.c
diff --git a/nlsop.c b/nlsop.c
index f2acfb4..07313e4 100644 (file)
--- a/nlsop.c
+++ b/nlsop.c
@@ -1,6 +1,8 @@
 /*
  * nlsop.c 
  *
+ * author: frank zirkelbach (frank.zirkelbach@physik.uni-augsburg.de)
+ *
  * this program tries helping to understand the amorphous depuration
  * and recrystallization of SiCx while ion implantation at temperatures
  * below 400 degree celsius.
  * refs: 
  *  - J. K. N. Lindner. Habilationsschrift, Universitaet Augsburg.
  *  - Maik Haeberlen. Diplomarbeit, Universitaet Augsburg.
+ *
+ * Copyright (C) 2004 Frank Zirkelbach
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
  */
 
 #define _GNU_SOURCE
@@ -34,37 +53,39 @@ int usage(void)
  puts("usage:");
  puts("-h \t\t help");
  puts("-n \t\t no user interaction");
+ puts("-Z \t\t cryst -> amorph c diffusion in z direction");
+ puts("-i \t\t no cryst to cryst diffusion");
  printf("-a <value> \t slope of nuclear energy loss (default %f)\n",A_EL);
  printf("-b <value> \t nuclear energy loss offset (default %f)\n",B_EL);
  printf("-x <value> \t # x cells (default %d)\n",X);
  printf("-y <value> \t # y cells (default %d)\n",Y);
  printf("-z <value> \t # z cells (default %d)\n",Z);
- /*
- printf("-X <value> \t display x (default %d)\n",X/2-1);
- printf("-Y <value> \t display y (default %d)\n",Y/2-1);
- printf("-Z <value> \t display z (default %d)\n",Z/2-1);
- */
  printf("-s <value> \t steps (default %d)\n",STEPS);
  printf("-d <value> \t refresh display (default %d)\n",REFRESH);
  printf("-r <value> \t amorphous influence range (default %d)\n",RANGE);
  printf("-f <value> \t pressure = <value> * 1/distance^2 (default %f)\n",A_AP);
  printf("-p <value> \t pressure offset (default %f)\n",B_AP);
+ printf("-F <value> \t proportionality constant between c conc and ability to get amorphous (default %f)\n",A_CP);
  printf("-A <value> \t slope of linear c distribution (default %f)\n",A_CD);
  printf("-B <value> \t linear c distribution offset (default %f)\n",B_CD);
- /*
- printf("-C <value> \t initial c concentration (default %d)\n",CC);
- */
- printf("-D <value> \t diffusion rate from cryst to amorph cells (default %f)\n",D_R);
+ printf("-D <value> \t diffusion rate from cryst to amorph cells (default %f)\n",DR_AC);
+ printf("-c <value> \t diffusion rate in cryst cells (default %f)\n",DR_CC);
+ printf("-e <value> \t do diffusion every <value> steps (default %d)\n",DIFF_RATE);
+ puts("-g <file> <step> continue simulation from file and step (step > 0)!");
  printf("-W <value> \t write every <value> steps to save file (default %d)\n",RESAVE);
  puts("-C <file> \t convert file to gnuplot format");
  puts("-L <file> \t load from file");
  puts("-S <file> \t save to file");
  puts("-R <file> \t read from random file");
+ puts("-P <file> \t specify implantation profile file");
+ puts("-N <file> \t specify nuclear energy loss profile file");
+ printf("-H <value> \t collisions per ion in simulation window (default %d)\n",CPI);
+ puts("-m <value> \t specify c->a carbon saturation");
  
  return 1;
 }
 
-int process_cell(d3_lattice *d3_l,u32 x,u32 y,u32 z,int r,double a,double b,int *t_c)
+int process_cell(d3_lattice *d3_l,u32 x,u32 y,u32 z,info *my_info)
 {
  unsigned char *thiz;
  int *conc;
@@ -74,19 +95,19 @@ int process_cell(d3_lattice *d3_l,u32 x,u32 y,u32 z,int r,double a,double b,int
 
  thiz=d3_l->status+x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y;
  conc=d3_l->extra+x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y;
- p=b*URAND_MAX;
- for(i=-r;i<=r;i++)
+ p=my_info->b_ap*URAND_MAX;
+ for(i=-(my_info->range);i<=my_info->range;i++)
  {
-  for(j=-r;j<=r;j++)
+  for(j=-(my_info->range);j<=my_info->range;j++)
   {
    if(!(i==0 && j==0))
    {
     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;
-    if(*(d3_l->status+off)&AMORPH) p+=a*(*(d3_l->extra+off))*URAND_MAX/(i*i+j*j);
+    if(*(d3_l->status+off)&AMORPH) p+=my_info->a_ap*(*(d3_l->extra+off))*URAND_MAX/(i*i+j*j);
    } 
   }
  }
// printf("debug: p = %f\n",p);
p+=*conc*my_info->a_cp*URAND_MAX;
  if(!(*thiz&AMORPH))
  {
   if(get_rand(URAND_MAX)<=p) MAKE_AMORPH(thiz);
@@ -96,12 +117,10 @@ int process_cell(d3_lattice *d3_l,u32 x,u32 y,u32 z,int r,double a,double b,int
   if(get_rand(URAND_MAX)>p) MAKE_CRYST(thiz);
  }
  
- *t_c+=1;
-
  return 1;
 }
 
-int distrib_c(d3_lattice *d3_l,double d_r,double a,double b)
+int distrib_c(d3_lattice *d3_l,info *my_info,int step,double c_ratio,u32 rj_m,u32 *rj_g)
 {
  u32 x,y,z;
  int i,j,k,c;
@@ -109,12 +128,21 @@ int distrib_c(d3_lattice *d3_l,double d_r,double a,double b)
  int carry;
 
  /* put one c ion somewhere in the lattice */
- x=get_rand(d3_l->max_x);
- y=get_rand(d3_l->max_y);
- z=get_rand_lgp(d3_l->max_z,a,b);
- *(d3_l->extra+x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y)+=1;
+ if(my_info->cc<c_ratio*step)
+ {
+  x=get_rand(d3_l->max_x);
+  y=get_rand(d3_l->max_y);
+  // printf("nd: %d %d\n",x,y);
+  // z=get_rand_lgp(d3_l->max_z,my_info->a_cd,my_info->b_cd);
+  z=get_rand_reject(d3_l->max_z,rj_m,rj_g);
+  // printf("%d\n",z);
+  *(d3_l->extra+x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y)+=1;
+  (my_info->cc)++;
+ }
+
+ if(step%my_info->diff_rate==0)
+ {
 
- /* diffusion in layer */
  for(i=0;i<d3_l->max_x;i++)
  {
   for(j=0;j<d3_l->max_y;j++)
@@ -122,24 +150,16 @@ int distrib_c(d3_lattice *d3_l,double d_r,double a,double b)
    for(k=0;k<d3_l->max_z;k++)
    {
     offset=i+j*d3_l->max_x+k*d3_l->max_x*d3_l->max_y;
-    /* case amorph */
-    if(*(d3_l->status+offset)&AMORPH)
+    /* case amorph: amorph <- cryst diffusion */
+    if(*(d3_l->status+offset)&AMORPH && *(d3_l->extra+offset)<my_info->c_sat)
     {
-     /* look at neighbours and move c ions */
      for(c=-1;c<=1;c++)
      {
       if(c!=0)
       {
        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;
        carry=0;
-       /* case neighbour not amorph */
-       if(!(*(d3_l->status+off)&AMORPH)) carry=(int)(d_r*(*(d3_l->extra+off)));
-       /* case neighbour amorph */
-       /*
-        * no diffusion between amorphous cells
-        *
-       else carry=(*(d3_l->extra+off)-*(d3_l->extra+offset))/2;
-        */
+       if(!(*(d3_l->status+off)&AMORPH)) carry=(int)(my_info->dr_ac*(*(d3_l->extra+off)));
        if(carry!=0)
        {
         *(d3_l->extra+offset)+=carry;
@@ -153,15 +173,7 @@ int distrib_c(d3_lattice *d3_l,double d_r,double a,double b)
       {
        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;
        carry=0;
-       /* case neighbour not amorph */  
-       if(!(*(d3_l->status+off)&AMORPH)) carry=(int)(d_r*(*(d3_l->extra+off)));                             
-       /* case neighbour amorph */
-       /*
-        *
-        * no diffusion between amorphous cells
-        *
-       else carry=(*(d3_l->extra+off)-*(d3_l->extra+offset))/2;
-        */
+       if(!(*(d3_l->status+off)&AMORPH)) carry=(int)(my_info->dr_ac*(*(d3_l->extra+off)));
        if(carry!=0)
        {
         *(d3_l->extra+offset)+=carry; 
@@ -169,20 +181,45 @@ int distrib_c(d3_lattice *d3_l,double d_r,double a,double b)
        }
       }
      }
+     if(my_info->z_diff)
+     {
+      if(k!=0)
+      {
+       off=i+j*d3_l->max_x+(k-1)*d3_l->max_x*d3_l->max_y;
+       carry=0;
+       if(!*(d3_l->status+off)&AMORPH) carry=(int)(my_info->dr_ac*(*(d3_l->extra+off)));
+       if(carry!=0)
+       {
+        *(d3_l->extra+off)-=carry;
+        *(d3_l->extra+offset)+=carry;
+       }
+      }
+      if(k!=d3_l->max_z-1)
+      {
+       off=i+j*d3_l->max_x+(k+1)*d3_l->max_x*d3_l->max_y;
+       carry=0;
+       if(!*(d3_l->status+off)&AMORPH) carry=(int)(my_info->dr_ac*(*(d3_l->extra+off)));
+       if(carry!=0)
+       {
+        *(d3_l->extra+off)-=carry;
+        *(d3_l->extra+offset)+=carry;
+       }
+      }
+     }  
     } else
-    /* case not amorph */
+    /* case not amorph: cryst <-> cryst diffusion */
+    if(my_info->c_diff) {
+    /* if there is c diff, no diff in z-direction */
     {
-     /* look at neighbours and move c ions */     
      for(c=-1;c<=1;c++) 
      {
       if(c!=0)
       {
        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;
        carry=0;
-       /* case neighbour not amorph */
        if(!(*(d3_l->status+off)&AMORPH))
        {
-        carry=(*(d3_l->extra+off)-*(d3_l->extra+offset))/2;
+        carry=(int)(my_info->dr_cc*(*(d3_l->extra+off)-*(d3_l->extra+offset))/2);
         if(carry!=0)
         {
          *(d3_l->extra+offset)+=carry;
@@ -191,72 +228,325 @@ int distrib_c(d3_lattice *d3_l,double d_r,double a,double b)
        }
       }
      }
+     for(c=-1;c<=1;c++)
+     {
+      if(c!=0)
+      {
+       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;
+       carry=0;
+       if(!(*(d3_l->status+off)&AMORPH))
+       {
+       carry=(int)(my_info->dr_cc*(*(d3_l->extra+off)-*(d3_l->extra+offset))/2);
+       if(carry!=0)
+        {
+         *(d3_l->extra+offset)+=carry;
+        *(d3_l->extra+off)-=carry;
+       }
+       }
+      }
+     }
     }
+    /* end test */
+    }
+    /* */
    } /* for z */
   } /* for y */
  } /* for x */
 
+ } /* if step modulo diff_rate == 0 */
+
  return 1;
 }
 
-int distrib_c_old(d3_lattice *d3_l,int t_c,double a,double b)
+int calc_pressure(d3_lattice *d3_l,int range)
 {
- int i,j,k,total,area;
- double sum;
- int temp,left;
- int *area_h;
- u32 x,y,z;
+ int i,j,off;
+ double count,max=0;
+ int x,y,z;
 
- area=d3_l->max_x*d3_l->max_y;
- area_h=(int *)malloc(d3_l->max_z*sizeof(int));
+ for(x=0;x<d3_l->max_x;x++)
+ {
+  for(y=0;y<d3_l->max_y;y++)
+  {
+   for(z=0;z<d3_l->max_z;z++)
+   {
+    count=0;
+    for(i=-range;i<=range;i++)
+    {
+     for(j=-range;j<=range;j++)
+     {
+      if(i!=0 && j!=0)
+      {
+       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;
+       if(*(d3_l->status+off)&AMORPH) count+=((double)*(d3_l->extra+off))/(i*i+j*j);
+      }
+     }
+    }
+    if(count>max) max=count;
+   }
+  }
+ }
 
- total=0;
- sum=b*d3_l->max_z+a*d3_l->max_z*(d3_l->max_z+1)/2;
- for(i=0;i<d3_l->max_z;i++)
+ for(x=0;x<d3_l->max_x;x++)
  {
-  area_h[i]=0;
-  for(j=0;j<area;j++)
+  for(y=0;y<d3_l->max_y;y++)
   {
-   if(!(*(d3_l->status+(i*area)+j)&AMORPH))
+   for(z=0;z<d3_l->max_z;z++)
    {
-    area_h[i]+=1;
+    count=0;
+    for(i=-range;i<=range;i++)
+    {
+     for(j=-range;j<=range;j++)
+     {
+      if(i!=0 && j!=0)
+      {
+       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;
+       if(*(d3_l->status+off)&AMORPH) count+=((double)*(d3_l->extra+off))/(i*i+j*j);
+      }
+     }
+    }
+    *(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);
    }
   }
-  temp=(int)((i+1)*a+b)*t_c/(sum*area_h[i]);
-  for(j=0;j<area;j++)
+ }
+
+ return 1;
+}
+
+int calc_max_extra(d3_lattice *d3_l)
+{
+ int x,y,z;
+ int off,max=0;
+
+ for(x=0;x<d3_l->max_x;x++)
+ {
+  for(y=0;y<d3_l->max_y;y++)
   {
-   if(!(*(d3_l->status+(i*area)+j)&AMORPH))
+   for(z=0;z<d3_l->max_z;z++)
    {
-    *(d3_l->extra+(i*area)+j)=temp;
-    total+=temp;
+    off=x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y;
+    if(*(d3_l->extra+off)>max) max=*(d3_l->extra+off);
    }
   }
-  left=(int)(((i+1)*a+b)*t_c/sum)%area_h[i];
-  while(left)
+ }
+
+ return max;
+}
+
+int write_ac_distr(d3_lattice *d3_l,int ac_distr)
+{
+ int fd,x,y,z;
+ int count,offset;
+ char file[16];
+
+ if(ac_distr==1) strcpy(file,"a.plot");
+ if(ac_distr==2) strcpy(file,"c.plot");
+ if(ac_distr==3) strcpy(file,"b.plot");
+
+ if((fd=open(file,O_WRONLY|O_CREAT))<0)
+ {
+  puts("cannot open plot file");
+  return -1;
+ }
+
+ for(z=0;z<d3_l->max_z;z++)
+ {
+  count=0;
+  for(x=0;x<d3_l->max_x;x++)
   {
-   x=get_rand(d3_l->max_x);
-   y=get_rand(d3_l->max_y);
-   if(!(*(d3_l->status+(i*area)+x+y*d3_l->max_x)&AMORPH))
+   for(y=0;y<d3_l->max_y;y++)
    {
-    *(d3_l->extra+(i*area)+x+y*d3_l->max_x)+=1;
-    total+=1;
-    left-=1;
+    offset=x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y;
+    if(ac_distr==1)
+     if(*(d3_l->status+offset)&AMORPH) count+=*(d3_l->extra+offset);
+    if(ac_distr==2)
+     if(!(*(d3_l->status+offset)&AMORPH)) count+=*(d3_l->extra+offset);
+    if(ac_distr==3) count+=*(d3_l->extra+offset);
    }
   }
+  dprintf(fd,"%d %d\n",z,count);
  }
- left=t_c-total;
- while(left)
+ close(fd);
+ return 1;
+}
+
+int write_bmp(d3_lattice *d3_l,int window,u32 x,u32 y,u32 z)
+{
+ int fd,i,j,size=0,foo=0,end=0;
+ int width=0,height=0;
+ char bmpfile[MAX_CHARS];
+ char buf[128];
+
+ if(((window==4)||(window==5))&&(d3_l->max_z<FFT_HEIGHT) )
  {
-  x=get_rand(d3_l->max_x);
-  y=get_rand(d3_l->max_y);
-  z=get_rand_lgp(d3_l->max_z,a,b);
-  if(!(*(d3_l->status+x+y*d3_l->max_x+z*area)&AMORPH))
+  puts("error: z < FFT_HEIGHT!");
+  puts("not writing bmp file ...");
+  return -1;
+ }
+
+ if(window%3==1)
+ {
+  sprintf(bmpfile,"x-z_%d.bmp",y);
+  foo=3*d3_l->max_x;
+  if(window==1)
+  {
+   size=(foo+(4-foo%4))*d3_l->max_z;
+   height=d3_l->max_z;
+  }
+  else 
+  {
+   size=(foo+(4-foo%4))*FFT_HEIGHT;
+   end=d3_l->max_z-FFT_HEIGHT;
+   height=FFT_HEIGHT;
+  }
+  width=d3_l->max_x;
+ }
+ if(window%3==2)
+ {
+  sprintf(bmpfile,"y-z_%d.bmp",x);
+  foo=3*d3_l->max_y;
+  if(window==2)
+  {
+   size=(foo+(4-foo%4))*d3_l->max_z;
+   height=d3_l->max_z;
+  }
+  else 
+  {
+   size=(foo+(4-foo%4))*FFT_HEIGHT;
+   end=d3_l->max_z-FFT_HEIGHT;
+   height=FFT_HEIGHT;
+  }
+  width=d3_l->max_y;
+ }
+ if(window==3)
+ {
+  sprintf(bmpfile,"x-y_%d.bmp",z);
+  foo=3*d3_l->max_x;
+  size=(foo+(4-foo%4))*d3_l->max_y;
+  width=d3_l->max_x;
+  height=d3_l->max_y;
+ }
+
+ if((fd=open(bmpfile,O_WRONLY|O_CREAT))<0)
+ {
+  puts("cannot open bmp file");
+  return -1;
+ }
+
+ /* bmpheader */
+ buf[0]='B'; /* std header start */
+ buf[1]='M';
+ buf[2]=(size+0x36)&0xff; /* file size */
+ buf[3]=(size+0x36)>>8;
+ memset(buf+4,0,6);
+ buf[10]=0x36; /* offset to data */
+ memset(buf+11,0,3);
+ buf[14]=0x28; /* length of bmp info header */
+ memset(buf+15,0,3);
+ buf[18]=width&0xff; /* width and height */
+ buf[19]=width>>8;
+ memset(buf+20,0,2);
+ buf[22]=height&0xff;
+ buf[23]=height>>8;
+ memset(buf+24,0,2);
+ buf[26]=1; /* # planes -> 1 */
+ buf[27]=0;
+ buf[28]=24; /* bits per pixel -> 2^24 (true color) */
+ buf[29]=0;
+ memset(buf+30,0,4); /* compression -> none */
+ buf[34]=size&0xff; /* data size */
+ buf[35]=size>>8;
+ memset(buf+36,0,2);
+ buf[38]=0x12; /* res: pixel/meter */
+ buf[39]=0x0b;
+ memset(buf+40,0,2);
+ buf[42]=0x12;
+ buf[43]=0x0b;
+ memset(buf+44,0,2); 
+ memset(buf+46,0,8); /* no colors, no important colors */
+
+ if(write(fd,buf,54)<54)
+ {
+  puts("failed writing bmp header");
+  return -1;
+ }
+ if(window%3==1)
+ {
+  for(j=0;j<d3_l->max_z-end;j++)
   {
-   *(d3_l->extra+x+y*d3_l->max_x+z*area)+=1;
-   left-=1;
+   for(i=0;i<d3_l->max_x;i++)
+   {
+    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);
+    else memset(buf,0,3);
+    if(write(fd,buf,3)<3)
+    {
+     puts("failed writing rgb values to bmp file");
+     return-1;
+    }
+   }
+   if(foo%4)
+   {
+    memset(buf,0,4-foo%4);
+    if(write(fd,buf,4-foo%4)<4-foo%4)
+    {
+     puts("failed writing 4 byte ending");
+     return -1;
+    }
+   } 
+  }
+ }
+ if(window%3==2)
+ {
+  for(j=0;j<d3_l->max_z-end;j++)
+  {
+   for(i=0;i<d3_l->max_y;i++)
+   {
+    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);
+    else memset(buf,0,3);
+    if(write(fd,buf,3)<3)
+    {
+     puts("failed writing rgb values to bmp file");
+     return-1;
+    }
+   }
+   if(foo%4)
+   {
+    memset(buf,0,4-foo%4);
+    if(write(fd,buf,4-foo%4)<4-foo%4)
+    {
+     puts("failed writing 4 byte ending");
+     return -1;
+    }
+   }
+  }
+ }
+ if(window==3)
+ {
+  for(j=0;j<d3_l->max_y;j++)
+  {
+   for(i=0;i<d3_l->max_x;i++)
+   {
+    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);
+    else memset(buf,0,3);
+    if(write(fd,buf,3)<3)
+    {
+     puts("failed writing rgb values to bmp file");
+     return -1;
+    }
+   }
+   if(foo%4)
+   {
+    memset(buf,0,4-foo%4);
+    if(write(fd,buf,4-foo%4)<4-foo%4)
+    {
+     puts("failed writing 4 byte ending");
+     return -1;
+    }
+   }
   }
  }
free(area_h);
close(fd);
 
  return 1;
 }
@@ -367,20 +657,128 @@ int convert_file(char *cf,d3_lattice *d3_l)
  return 1;
 }
 
+int get_c_ratio(double *c_ratio,char *pfile,info *my_info,d3_lattice *d3_l)
+{
+ double all,a,b,d;
+ int i,k;
+ int p_fd;
+ unsigned char buf[32];
+ char *p;
+ unsigned char c;
+
+ if((p_fd=open(pfile,O_RDONLY))<0)
+ {
+  puts("cannot open profile file");
+  return -1;
+ }
+ k=1;
+ d=0;
+ all=0;
+ while(k)
+ {
+  for(i=0;i<32;i++)
+  {
+   k=read(p_fd,&c,1);
+   buf[i]=c;
+   if(c=='\n') break;
+  }
+  if(k)
+  {
+   p=strtok(buf," ");
+   a=atof(p)/10; /* nm */
+   p=strtok(NULL," ");
+   b=atof(p);
+   if(a>my_info->b_cd*CELL_LENGTH && a<(my_info->b_cd+d3_l->max_z)*CELL_LENGTH) d+=b;
+   all+=b;
+  }
+ }
+ *c_ratio=d/all;
+ close(p_fd);
+
+ return 1;
+}
+
+u32 get_reject_graph(info *my_info,d3_lattice *d3_l,char *file,u32 *graph) {
+ double a,b;
+ int i,j,k;
+ int fd;
+ char buf[32],*p;
+ unsigned char *flag;
+ u32 max;
+
+ max=0;
+ if((fd=open(file,O_RDONLY))<0)
+ {
+  puts("cannot open file to calculate rejection graph");
+  return -1;
+ }
+ if((flag=(unsigned char *)malloc(d3_l->max_z))==NULL)
+ {
+  puts("cannot malloc flag memory for rejection graph");
+  return -1;
+ }
+ memset(flag,0,d3_l->max_z);
+ memset(graph,0,d3_l->max_z*sizeof(u32));
+ /* get fixpoints */
+ k=1;
+ while(k)
+ {
+  for(i=0;i<32;i++)
+  {
+   k=read(fd,&buf[i],1);
+   if((buf[i]=='\n')||(k==0)) break;
+  }
+  if(k)
+  {
+   p=strtok(buf," ");
+   a=atof(p)/10; /* nm */
+   p=strtok(NULL," ");
+   b=atof(p);
+   if(a>d3_l->max_z*CELL_LENGTH) k=0;
+   else 
+   {
+    graph[(int)(a/CELL_LENGTH)]=(int)(URAND_MAX*b);
+    flag[(int)(a/CELL_LENGTH)]=1;
+   }
+  }
+ }
+ /* do (linear) interpolation here! */
+ i=0;
+ a=0;
+ while(i<d3_l->max_z)
+ {
+  /* graph[0] is 0! */
+  j=i;
+  i++;
+  while(flag[i]==0&&i<d3_l->max_z) i++;
+  for(k=j+1;k<i;k++) graph[k]=(int)((k-j)*((int)graph[i]-(int)graph[j])/(i-j))+graph[j];
+  if(graph[i]>max) max=graph[i];
+ }
+
+ free(flag);
+
+ // printf("debug: (interpolated c profile)\n");
+ // for(i=0;i<d3_l->max_z;i++) printf("%d %d\n",i,graph[i]);
+
+ return max;
+}
+
 int main(int argc,char **argv)
 {
  u32 x,y,z,x_c,y_c,z_c;
- int i,quit,escape,nowait;
+ int i,j,quit,escape,switchmode,nowait,bmp,ac_distr;
  int refresh,resave;
+ int c_step;
  char s_file[MAX_CHARS];
  char s_file_tmp[MAX_CHARS];
  char l_file[MAX_CHARS];
  char c_file[MAX_CHARS];
+ char p_file[MAX_CHARS];
+ char n_e_file[MAX_CHARS];
  char convert;
  char r_file[MAX_CHARS];
- char x_txt[MAX_TXT];
- char y_txt[MAX_TXT];
- char z_txt[MAX_TXT];
+#ifdef USE_DFB_API
+ char xyz_txt[MAX_TXT];
  char status_txt[MAX_TXT];
  char conc_txt[MAX_TXT];
  char steps_txt[MAX_TXT];
@@ -391,13 +789,25 @@ int main(int argc,char **argv)
  char el_txt[MAX_TXT];
  char cd_txt[MAX_TXT];
  char r_txt[MAX_TXT];
- char ap2_txt[MAX_TXT];
- char cd2_txt[MAX_TXT];
- char el2_txt[MAX_TXT];
- char dr_txt[MAX_TXT];
+ char cp_txt[MAX_TXT];
+ char zdiff_txt[MAX_TXT];
+ char diff_txt[MAX_TXT];
+ char dr_ac_txt[MAX_TXT];
+ char dr_cc_txt[MAX_TXT];
+ char dose_txt[MAX_TXT];
+ char mode_txt[MAX_TXT];
  char *arg_v[MAX_ARGV];
+#endif
  d3_lattice d3_l;
  info my_info;
+ unsigned char mode;
+ double c_ratio;
+#ifdef USE_DFB_API
+ int max_extra;
+#endif
+ u32 *c_profile;
+ u32 *n_e_loss;
+ u32 ne_max,ip_max;
 
  d3_l.max_x=X;
  d3_l.max_y=Y;
@@ -406,22 +816,36 @@ int main(int argc,char **argv)
  my_info.range=RANGE;
  refresh=REFRESH;
  resave=RESAVE;
+ my_info.z_diff=0;
+ my_info.c_diff=1;
  my_info.a_el=A_EL;
  my_info.b_el=B_EL;
  my_info.a_cd=A_CD;
  my_info.b_cd=B_CD;
  my_info.a_ap=A_AP;
  my_info.b_ap=B_AP;
+ my_info.a_cp=A_CP;
  my_info.cc=CC;
- my_info.d_r=D_R;
+ my_info.dr_ac=DR_AC;
+ my_info.dr_cc=DR_CC;
+ my_info.diff_rate=DIFF_RATE;
+ my_info.cpi=CPI;
+ my_info.c_sat=C_SAT;
  nowait=0;
  quit=0;
  escape=0;
+ switchmode=0;
+ c_step=0;
  strcpy(s_file,"");
  strcpy(l_file,"");
  strcpy(c_file,"");
+ strcpy(p_file,IMP_PROFILE);
+ strcpy(n_e_file,NEL_PROFILE);
  convert=0;
  strcpy(r_file,"");
+ mode=0;
+ ne_max=0;
+ ip_max=0;
 
  for(i=1;i<argc;i++)
  {
@@ -450,17 +874,13 @@ int main(int argc,char **argv)
     case 'z':
      d3_l.max_z=atoi(argv[++i]);
      break;
-    /*
-    case 'X':
-     x=atoi(argv[++i]);
-     break;
-    case 'Y':
-     y=atoi(argv[++i]);
-     break;
     case 'Z':
-     z=atoi(argv[++i]);
+     my_info.z_diff=1;
+     break;
+    case 'i':
+     my_info.c_diff=0;
+     my_info.dr_cc=0;
      break;
-    */
     case 's':
      my_info.steps=atoi(argv[++i]);
      break;
@@ -476,17 +896,15 @@ int main(int argc,char **argv)
     case 'p':
      my_info.b_ap=atof(argv[++i]);
      break;
+    case 'F':
+     my_info.a_cp=atof(argv[++i]);
+     break;
     case 'A':
      my_info.a_cd=atof(argv[++i]);
      break;
     case 'B':
      my_info.b_cd=atof(argv[++i]);
      break;
-    /*
-    case 'C':
-     my_info.cc=atoi(argv[++i]);
-     break;
-    */
     case 'W':
      resave=atoi(argv[++i]);
      break;
@@ -496,7 +914,13 @@ int main(int argc,char **argv)
      convert=1;
      break;
     case 'D':
-     my_info.d_r=atof(argv[++i]);
+     my_info.dr_ac=atof(argv[++i]);
+     break;
+    case 'c':
+     my_info.dr_cc=atof(argv[++i]);
+     break;
+    case 'e':
+     my_info.diff_rate=atoi(argv[++i]);
      break;
     case 'L':
      strcpy(l_file,argv[++i]);
@@ -507,6 +931,22 @@ int main(int argc,char **argv)
     case 'R':
      strcpy(r_file,argv[++i]);
      break;
+    case 'P':
+     strcpy(p_file,argv[++i]);
+     break;
+    case 'N':
+     strcpy(n_e_file,argv[++i]);
+     break;
+    case 'g':
+     strcpy(l_file,argv[++i]);
+     if(i<argc-1) if(argv[i+1][0]!='-') c_step=atoi(argv[++i]);
+     break;
+    case 'H':
+     my_info.cpi=atoi(argv[++i]);
+     break;
+    case 'm':
+     my_info.c_sat=atoi(argv[++i]);
+     break;
     default:
      usage();
      return -1;
@@ -519,11 +959,12 @@ int main(int argc,char **argv)
  z=d3_l.max_z/2-1;
 
 #ifdef NODFB
- if(!strcmp(s_file,"")
+ if(!strcmp(s_file,""))
  {
   puts("NODFB defined, run with -S option");
   return -1;
  }
+#endif
 
  if(!strcmp(r_file,"")) rand_init(NULL);
  else rand_init(r_file);
@@ -558,100 +999,165 @@ int main(int argc,char **argv)
    return 1;
   } 
 #ifdef USE_DFB_API
-    else d3_lattice_init(&argc,argv,&d3_l);
+  else d3_lattice_init(&argc,argv,&d3_l);
 #endif
  }
 
-#ifedef USE_DFB_API
+#ifdef USE_DFB_API
  d3_event_init(&d3_l);
 #endif
 
+#ifdef USE_DFB_API
  strcpy(a_txt,"args:");
  sprintf(s_txt,"steps: %d",my_info.steps);
+ 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));
  sprintf(r_txt,"pressure range: %d",my_info.range);
- sprintf(ap_txt,"pressure faktor: %.2f",my_info.a_ap);
- sprintf(ap2_txt,"pressure offset: %.2f",my_info.b_ap);
- sprintf(el_txt,"energy loss slope: %.2f",my_info.a_el);
- sprintf(el2_txt,"energy loss offset: %.2f",my_info.b_el);
- sprintf(cd_txt,"c distrib slope: %.2f",my_info.a_cd);
- sprintf(cd2_txt,"c distrib offset: %.2f",my_info.b_cd);
- sprintf(dr_txt,"diffusion rate: %.2f",my_info.d_r);
- arg_v[1]=x_txt;
- arg_v[2]=y_txt;
- arg_v[3]=z_txt;
- arg_v[4]=NULL;
- arg_v[5]=status_txt;
- arg_v[6]=conc_txt;
+ sprintf(ap_txt,"a_ap: %f  b_ap: %f",my_info.a_ap,my_info.b_ap);
+ sprintf(el_txt,"a_el: %f  b_el: %f",my_info.a_el,my_info.b_el);
+ sprintf(cd_txt,"a_cd: %f  b_cd: %f",my_info.a_cd,my_info.b_cd);
+ sprintf(cp_txt,"a_cp: %f",my_info.a_cp);
+ sprintf(dr_ac_txt,"a/c diffusion rate: %f",my_info.dr_ac);
+ if(my_info.c_diff!=0) sprintf(dr_cc_txt,"c/c diffusion rate: %f",my_info.dr_cc);
+ else sprintf(dr_cc_txt,"c/c diffusion rate: none");
+ sprintf(zdiff_txt,"diffusion in z direction: %c",my_info.z_diff?'y':'n');
+ sprintf(diff_txt,"diffusion every %d steps",my_info.diff_rate);
+ strcpy(mode_txt,"view: a/c mode");
+ arg_v[1]=xyz_txt;
+ arg_v[2]=NULL;
+ arg_v[3]=status_txt;
+ arg_v[4]=conc_txt;
+ arg_v[5]=NULL;
+ arg_v[6]=mode_txt;
  arg_v[7]=NULL;
- arg_v[8]=NULL;
- arg_v[9]=NULL;
- arg_v[10]=steps_txt;;
- arg_v[11]=cc_txt;
- arg_v[12]=NULL;
+ arg_v[8]=steps_txt;
+ arg_v[9]=cc_txt;
+ arg_v[10]=NULL;
+ arg_v[11]=diff_txt;
+ arg_v[12]=zdiff_txt;
  arg_v[13]=NULL;
  arg_v[14]=a_txt;
- arg_v[15]=NULL;
- arg_v[16]=s_txt;
- arg_v[17]=r_txt;
- arg_v[18]=ap_txt;
- arg_v[19]=ap2_txt;
+ arg_v[15]=s_txt;
+ arg_v[16]=dose_txt;
+ arg_v[17]=NULL;
+ arg_v[18]=r_txt;
+ arg_v[19]=ap_txt;
  arg_v[20]=el_txt;
- arg_v[21]=el2_txt;
- arg_v[22]=cd_txt;
- arg_v[23]=cd2_txt;
- arg_v[24]=dr_txt;
+ arg_v[21]=cd_txt;
+ arg_v[22]=cp_txt;
+ arg_v[23]=NULL;
+ arg_v[24]=dr_ac_txt;
+ arg_v[25]=dr_cc_txt;
+#endif
 
- if(!strcmp(l_file,""))
+ if((!strcmp(l_file,""))||(c_step))
  {
-  i=0;
+  /* calculate ratio of c_simwindow / c_total */
+  if(get_c_ratio(&c_ratio,p_file,&my_info,&d3_l)!=1)
+  {
+   puts("failed calculating ratio");
+   return -1;
+  }
+  /* compute graphs for random number rejection method */
+  if((c_profile=(u32 *)malloc(d3_l.max_z*sizeof(unsigned int)))==NULL)
+  {
+   puts("failed allocating memory for carbon profile graph");
+   return -1;
+  }
+  if((n_e_loss=(u32 *)malloc(d3_l.max_z*sizeof(unsigned int)))==NULL)
+  {
+   puts("failed allocating memory for nuclear energy loss graph");
+   return -1;
+  }
+  ip_max=get_reject_graph(&my_info,&d3_l,p_file,c_profile);
+  ne_max=get_reject_graph(&my_info,&d3_l,n_e_file,n_e_loss);
+
+#ifdef DEBUG_RAND
+ while(1)
+ {
+#ifdef DEBUG_CP
+  printf("%d\n",get_rand_reject(d3_l.max_z,ip_max,c_profile));
+#endif
+#ifdef DEBUG_NEL
+  printf("%d\n",get_rand_reject(d3_l.max_z,ne_max,n_e_loss));
+#endif
+ }
+#endif
+
+  i=(c_step?c_step:0);
   while((i<my_info.steps) && (quit==0) && (escape==0))
   {
-   x_c=get_rand(d3_l.max_x);
-   y_c=get_rand(d3_l.max_y);
-   z_c=get_rand_lgp(d3_l.max_z,my_info.a_el,my_info.b_el);
-   distrib_c(&d3_l,my_info.d_r,my_info.a_cd,my_info.b_cd);
-   process_cell(&d3_l,x_c,y_c,z_c,my_info.range,my_info.a_ap,my_info.b_ap,&(my_info.cc));
+   for(j=0;j<my_info.cpi;j++)
+   {
+    x_c=get_rand(d3_l.max_x);
+    y_c=get_rand(d3_l.max_y);
+    // z_c=get_rand_lgp(d3_l.max_z,my_info.a_el,my_info.b_el);
+    z_c=get_rand_reject(d3_l.max_z,ne_max,n_e_loss);
+    process_cell(&d3_l,x_c,y_c,z_c,&my_info);
+   }
+   distrib_c(&d3_l,&my_info,i,c_ratio,ip_max,c_profile);
+#ifdef USE_DFB_API
    if(i%refresh==0)
    {
-    sprintf(x_txt,"x: %d",x+1);
-    sprintf(y_txt,"y: %d",y+1);
-    sprintf(z_txt,"z: %d",z+1);
+    sprintf(xyz_txt,"x: %d  y: %d  z: %d",x+1,y+1,z+1);
     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');
     sprintf(conc_txt,"conc: %d",*(d3_l.extra+x+y*d3_l.max_x+z*d3_l.max_x*d3_l.max_y));
     sprintf(steps_txt,"step: %d",i);
     sprintf(cc_txt,"total c: %d",my_info.cc);
-#ifdef USE_DFB_API
-    d3_lattice_draw(&d3_l,x,y,z,24,arg_v);
-#endif
-    // scan_event(&d3_l,&x,&y,&z,&quit,&escape);
+    d3_lattice_draw(&d3_l,x,y,z,25,arg_v,mode,0);
    }
-   if(i%resave==0 && strcmp(s_file,"") && resave!=0)
+#endif
+   if(i%resave==0 && strcmp(s_file,"") && resave!=0 && i!=0)
    {
     sprintf(s_file_tmp,"%s_%d_of_%d",s_file,i,my_info.steps);
     save_to_file(s_file_tmp,&d3_l,&my_info);
+#ifdef NODFB
+    printf("saved %s\n",s_file_tmp);
+#endif
    }
    i++;
   }
  }
 
- if(strcmp(s_file,"")) save_to_file(s_file,&d3_l,&my_info);
+ if(strcmp(s_file,""))
+ {
+   printf("saved %s\n",s_file);
+   save_to_file(s_file,&d3_l,&my_info);
+ }
+
+#ifdef USE_DFB_API
+ /* allocating buffer for pressure values */
+ if((d3_l.v_ptr=malloc(d3_l.max_x*d3_l.max_y*d3_l.max_z*sizeof(unsigned char)))==NULL)
+ {
+  puts("cannot allocate buffer for pressure values");
+  return -1;
+ }
+ /* calc values */
+ calc_pressure(&d3_l,my_info.range);
+ max_extra=calc_max_extra(&d3_l);
 
  while((quit==0) && (escape==0) && (nowait==0))
  {
-  sprintf(x_txt,"x: %d",x+1);
-  sprintf(y_txt,"y: %d",y+1);
-  sprintf(z_txt,"z: %d",z+1);
+  /* bahh! */
+  if(switchmode==0) mode=0;
+  if(switchmode==1) mode=1;
+  if(switchmode==2) mode=2;
+  /* end of bahh! */
+  sprintf(xyz_txt,"x: %d  y: %d  z: %d",x+1,y+1,z+1);
   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');
   sprintf(conc_txt,"conc: %d",*(d3_l.extra+x+y*d3_l.max_x+z*d3_l.max_x*d3_l.max_y));
-  strcpy(steps_txt,"step: end!");
+  sprintf(steps_txt,"step: end");
   sprintf(cc_txt,"total c: %d",my_info.cc);
-#ifdef USE_DFB_API
-  d3_lattice_draw(&d3_l,x,y,z,24,arg_v);
-  scan_event(&d3_l,&x,&y,&z,&quit,&escape);
-#endif
+  if(switchmode==0) strcpy(mode_txt,"view: a/c mode");
+  if(switchmode==1) strcpy(mode_txt,"view: c conc mode");
+  if(switchmode==2) strcpy(mode_txt,"view: a pressure mode");
+  d3_lattice_draw(&d3_l,x,y,z,25,arg_v,mode,max_extra);
+  bmp=0;
+  ac_distr=0;
+  scan_event(&d3_l,&x,&y,&z,&quit,&escape,&switchmode,&bmp,&ac_distr);
+  if(bmp) write_bmp(&d3_l,bmp,x,y,z);
+  if(ac_distr) write_ac_distr(&d3_l,ac_distr);
  }
 
-#ifdef USE_DFB_API
  d3_lattice_release(&d3_l);
 #endif