implemented TEM like bmp's (1-3) + c concentration bmp's (4-6), added new run
[physik/nlsop.git] / nlsop.c
diff --git a/nlsop.c b/nlsop.c
index 58f19f7..2945c14 100644 (file)
--- a/nlsop.c
+++ b/nlsop.c
@@ -78,7 +78,9 @@ int usage(void)
  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;
 }
@@ -118,7 +120,7 @@ int process_cell(d3_lattice *d3_l,u32 x,u32 y,u32 z,info *my_info)
  return 1;
 }
 
-int distrib_c(d3_lattice *d3_l,info *my_info,int step,double c_ratio)
+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;
@@ -131,8 +133,9 @@ int distrib_c(d3_lattice *d3_l,info *my_info,int step,double c_ratio)
   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);
-  // printf("ld: %d\n",z);
+  // 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)++;
  }
@@ -148,7 +151,7 @@ int distrib_c(d3_lattice *d3_l,info *my_info,int step,double c_ratio)
    {
     offset=i+j*d3_l->max_x+k*d3_l->max_x*d3_l->max_y;
     /* case amorph: amorph <- cryst diffusion */
-    if(*(d3_l->status+offset)&AMORPH)
+    if(*(d3_l->status+offset)&AMORPH && *(d3_l->extra+offset)<my_info->c_sat)
     {
      for(c=-1;c<=1;c++)
      {
@@ -333,7 +336,7 @@ int calc_max_extra(d3_lattice *d3_l)
 int write_ac_distr(d3_lattice *d3_l,int ac_distr)
 {
  int fd,x,y,z;
- int count=0,offset;
+ int count,offset;
  char file[16];
 
  if(ac_distr==1) strcpy(file,"a.plot");
@@ -348,6 +351,7 @@ int write_ac_distr(d3_lattice *d3_l,int ac_distr)
 
  for(z=0;z<d3_l->max_z;z++)
  {
+  count=0;
   for(x=0;x<d3_l->max_x;x++)
   {
    for(y=0;y<d3_l->max_y;y++)
@@ -367,52 +371,27 @@ int write_ac_distr(d3_lattice *d3_l,int ac_distr)
  return 1;
 }
 
-int write_bmp(d3_lattice *d3_l,int window,u32 x,u32 y,u32 z)
+int write_bmp(d3_lattice *d3_l,int window,u32 x,u32 y,u32 z,int max)
 {
- int fd,i,j,size=0,foo=0,end=0;
+ int fd,i,j,size=0,foo=0,k,sum;
  int width=0,height=0;
  char bmpfile[MAX_CHARS];
  char buf[128];
 
- if(((window==4)||(window==5))&&(d3_l->max_z<FFT_HEIGHT) )
- {
-  puts("error: z < FFT_HEIGHT!");
-  puts("not writing bmp file ...");
-  return -1;
- }
-
- if(window%3==1)
+ if(window==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;
-  }
+  size=(foo+(4-foo%4))*d3_l->max_z;
+  height=d3_l->max_z;
   width=d3_l->max_x;
  }
- if(window%3==2)
+ if(window==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;
-  }
+  size=(foo+(4-foo%4))*d3_l->max_z;
+  height=d3_l->max_z;
   width=d3_l->max_y;
  }
  if(window==3)
@@ -423,6 +402,30 @@ int write_bmp(d3_lattice *d3_l,int window,u32 x,u32 y,u32 z)
   width=d3_l->max_x;
   height=d3_l->max_y;
  }
+ if(window==4)
+ {
+  sprintf(bmpfile,"x-z_cdistr_%d.bmp",y);
+  foo=3*d3_l->max_x;
+  size=(foo+(4-foo%4))*d3_l->max_z;
+  height=d3_l->max_z;
+  width=d3_l->max_x;
+ }
+ if(window==5)
+ {
+  sprintf(bmpfile,"y-z_cdistr_%d.bmp",x);
+  foo=3*d3_l->max_y;
+  size=(foo+(4-foo%4))*d3_l->max_z;
+  height=d3_l->max_z;
+  width=d3_l->max_y;
+ }
+ if(window==6)
+ {
+  sprintf(bmpfile,"x-y_cdistr_%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)
  {
@@ -467,14 +470,17 @@ int write_bmp(d3_lattice *d3_l,int window,u32 x,u32 y,u32 z)
   puts("failed writing bmp header");
   return -1;
  }
- if(window%3==1)
+ if(window==1)
  {
-  for(j=0;j<d3_l->max_z-end;j++)
+  for(j=0;j<d3_l->max_z;j++)
   {
    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);
+    sum=0;
+    for(k=-2;k<=2;k++)
+     if(*(d3_l->status+i+(((y+k+d3_l->max_y)%d3_l->max_y)*d3_l->max_x)+(d3_l->max_z-j-1)*d3_l->max_x*d3_l->max_y)&RED) sum+=0xff;
+    sum/=5;
+    memset(buf,(unsigned char)sum,3);
     if(write(fd,buf,3)<3)
     {
      puts("failed writing rgb values to bmp file");
@@ -492,14 +498,17 @@ int write_bmp(d3_lattice *d3_l,int window,u32 x,u32 y,u32 z)
    } 
   }
  }
- if(window%3==2)
+ if(window==2)
  {
-  for(j=0;j<d3_l->max_z-end;j++)
+  for(j=0;j<d3_l->max_z;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);
+    sum=0;
+    for(k=-2;k<=2;k++)
+     if(*(d3_l->status+((x+k+d3_l->max_x)%d3_l->max_x)+i*d3_l->max_x+(d3_l->max_z-j-1)*d3_l->max_x*d3_l->max_y)&RED) sum+=0xff;
+    sum/=5;
+    memset(buf,(unsigned char)sum,3);
     if(write(fd,buf,3)<3)
     {
      puts("failed writing rgb values to bmp file");
@@ -542,6 +551,84 @@ int write_bmp(d3_lattice *d3_l,int window,u32 x,u32 y,u32 z)
    }
   }
  }
+ if(window==4)
+ {
+  for(j=0;j<d3_l->max_z;j++)
+  {
+   for(i=0;i<d3_l->max_x;i++)
+   {
+    sum=*(d3_l->extra+i+y*d3_l->max_x+(d3_l->max_z-j-1)*d3_l->max_x*d3_l->max_y);
+    sum=sum*255/max;
+    memset(buf,(unsigned char)sum,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==5)
+ { 
+  for(j=0;j<d3_l->max_z;j++) 
+  {                     
+   for(i=0;i<d3_l->max_x;i++)
+   {
+    sum=*(d3_l->extra+x+i*d3_l->max_x+(d3_l->max_z-j-1)*d3_l->max_x*d3_l->max_y);
+    sum=sum*255/max;
+    memset(buf,(unsigned char)sum,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==6)
+ {   
+  for(j=0;j<d3_l->max_z;j++)
+  {  
+   for(i=0;i<d3_l->max_x;i++)
+   { 
+    sum=*(d3_l->extra+i+(d3_l->max_y-j-1)*d3_l->max_x+z*d3_l->max_x*d3_l->max_y);
+    sum=sum*255/max;
+    memset(buf,(unsigned char)sum,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;
+    }
+   } 
+  }
+ }
  close(fd);
 
  return 1;
@@ -694,13 +781,15 @@ int get_c_ratio(double *c_ratio,char *pfile,info *my_info,d3_lattice *d3_l)
  return 1;
 }
 
-int get_reject_graph(info *my_info,d3_lattice *d3_l,char *file,u32 *graph) {
+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");
@@ -738,18 +827,23 @@ int get_reject_graph(info *my_info,d3_lattice *d3_l,char *file,u32 *graph) {
  }
  /* do (linear) interpolation here! */
  i=0;
+ a=0;
  while(i<d3_l->max_z)
  {
-  a=0;
-
-  // WEITER HIER !!
-
+  /* 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];
  }
 
- printf("%s graph:\n",file);
- for(i=0;i<d3_l->max_z;i++) printf("%02d | %d\n",i,graph[i]);
+ free(flag);
 
- return 1;
+ // 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)
@@ -763,6 +857,7 @@ int main(int argc,char **argv)
  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];
 #ifdef USE_DFB_API
@@ -795,6 +890,7 @@ int main(int argc,char **argv)
 #endif
  u32 *c_profile;
  u32 *n_e_loss;
+ u32 ne_max,ip_max;
 
  d3_l.max_x=X;
  d3_l.max_y=Y;
@@ -817,6 +913,7 @@ int main(int argc,char **argv)
  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;
@@ -826,9 +923,12 @@ int main(int argc,char **argv)
  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++)
  {
@@ -917,6 +1017,9 @@ int main(int argc,char **argv)
     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]);
@@ -924,6 +1027,9 @@ int main(int argc,char **argv)
     case 'H':
      my_info.cpi=atoi(argv[++i]);
      break;
+    case 'm':
+     my_info.c_sat=atoi(argv[++i]);
+     break;
     default:
      usage();
      return -1;
@@ -989,12 +1095,12 @@ int main(int argc,char **argv)
  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,"a_ap: %.4f  b_ap: %.3f",my_info.a_ap,my_info.b_ap);
- sprintf(el_txt,"a_el: %.3f  b_el: %.3f",my_info.a_el,my_info.b_el);
- sprintf(cd_txt,"a_cd: %.3f  b_cd: %.3f",my_info.a_cd,my_info.b_cd);
- sprintf(cp_txt,"a_cp: %.5f",my_info.a_cp);
- sprintf(dr_ac_txt,"a/c diffusion rate: %.4f",my_info.dr_ac);
- if(my_info.c_diff!=0) sprintf(dr_cc_txt,"c/c diffusion rate: %.4f",my_info.dr_cc);
+ 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);
@@ -1045,14 +1151,20 @@ int main(int argc,char **argv)
    puts("failed allocating memory for nuclear energy loss graph");
    return -1;
   }
-  if(get_reject_graph(&my_info,&d3_l,p_file,c_profile)!=1)
-  {
-   puts("failed to get reject 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);
 
-  /* break here for testing ... */
-  return 23;
+#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))
@@ -1061,10 +1173,11 @@ int main(int argc,char **argv)
    {
     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_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);
+   distrib_c(&d3_l,&my_info,i,c_ratio,ip_max,c_profile);
 #ifdef USE_DFB_API
    if(i%refresh==0)
    {
@@ -1124,7 +1237,7 @@ int main(int argc,char **argv)
   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(bmp) write_bmp(&d3_l,bmp,x,y,z,max_extra);
   if(ac_distr) write_ac_distr(&d3_l,ac_distr);
  }