fixed cryst->amorph z direction diffusion bug
[physik/nlsop.git] / nlsop.c
diff --git a/nlsop.c b/nlsop.c
index c4fc825..d61d2ac 100644 (file)
--- a/nlsop.c
+++ b/nlsop.c
@@ -2,9 +2,10 @@
  * nlsop.c 
  *
  * this program tries helping to understand the amorphous depuration
- * and recrystallization of SiCx while ion implanation. hopefully the program 
- * will simulate the stabilization of the selforganizing structure in the
- * observed behaviour.
+ * and recrystallization of SiCx while ion implantation at temperatures
+ * below 400 degree celsius.
+ * hopefully the program will simulate the stabilization of the
+ * selforganizing lamella structure in the observed behaviour.
  *
  * refs: 
  *  - J. K. N. Lindner. Habilationsschrift, Universitaet Augsburg.
@@ -33,11 +34,12 @@ 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");
  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 # x cells (default %d)\n",Y);
- printf("-z <value> \t # x cells (default %d)\n",Z);
+ 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);
@@ -54,6 +56,8 @@ int usage(void)
  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("-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");
@@ -66,6 +70,7 @@ int process_cell(d3_lattice *d3_l,u32 x,u32 y,u32 z,int r,double a,double b,int
  unsigned char *thiz;
  int *conc;
  int i,j;
+ int off;
  double p;
 
  thiz=d3_l->status+x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y;
@@ -77,25 +82,19 @@ int process_cell(d3_lattice *d3_l,u32 x,u32 y,u32 z,int r,double a,double b,int
   {
    if(!(i==0 && j==0))
    {
-    if(*(d3_l->status+((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)&AMORPH) p+=a*URAND_MAX/(i*i+j*j);
+    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);
    } 
   }
  }
+ // printf("debug: p = %f\n",p);
  if(!(*thiz&AMORPH))
  {
-  if(get_rand(URAND_MAX)<=p)
-  {
-   MAKE_AMORPH(thiz);
-   // *t_c=*t_c+1-*conc;
-  } // else *t_c+=1;
+  if(get_rand(URAND_MAX)<=p) MAKE_AMORPH(thiz);
  } else
  {
   /* assume 1-p probability */
-  if(get_rand(URAND_MAX)>p)
-  {
-   MAKE_CRYST(thiz);
-   // *t_c=*t_c+1+*conc;
-  } // else *t_c+=1;
+  if(get_rand(URAND_MAX)>p) MAKE_CRYST(thiz);
  }
  
  *t_c+=1;
@@ -103,7 +102,7 @@ int process_cell(d3_lattice *d3_l,u32 x,u32 y,u32 z,int r,double a,double b,int
  return 1;
 }
 
-int distrib_c(d3_lattice *d3_l,double d_r,double a,double b)
+int distrib_c(d3_lattice *d3_l,double d_r,double a,double b,char z_diff)
 {
  u32 x,y,z;
  int i,j,k,c;
@@ -133,10 +132,15 @@ int distrib_c(d3_lattice *d3_l,double d_r,double a,double b)
       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(carry!=0)
        {
         *(d3_l->extra+offset)+=carry;
@@ -149,10 +153,16 @@ int distrib_c(d3_lattice *d3_l,double d_r,double a,double b)
       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=(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(carry!=0)
        {
         *(d3_l->extra+offset)+=carry; 
@@ -169,6 +179,7 @@ int distrib_c(d3_lattice *d3_l,double d_r,double a,double b)
       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))
        {
@@ -181,6 +192,39 @@ 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;
+       /* case neighbour not amorph */
+       if(!(*(d3_l->status+off)&AMORPH))
+       {
+       carry=(*(d3_l->extra+off)-*(d3_l->extra+offset))/2;
+       if(carry!=0)
+        {
+         *(d3_l->extra+offset)+=carry;
+        *(d3_l->extra+off)-=carry;
+       }
+       }
+      }
+     }
+     /* cryst -> amorph diffusion in z direction */
+     if(z_diff)
+     {
+      for(c=1;c>=-1;c--)
+      {
+       off=i+j*d3_l->max_x+((k+c+d3_l->max_z)%d3_l->max_z)*d3_l->max_x*d3_l->max_y;
+       carry=0;
+       if(*(d3_l->status+off)&AMORPH) carry=(int)(d_r*(*(d3_l->extra+offset)));
+       if(carry!=0)
+       {
+        *(d3_l->extra+offset)-=carry;
+       *(d3_l->extra+off)+=carry;
+       }
+      }
+     } /* if z_diff */
     }
    } /* for z */
   } /* for y */
@@ -331,14 +375,45 @@ int load_from_file(char *lf,d3_lattice *d3_l,info *my_inf)
  return 1;
 }
 
+int convert_file(char *cf,d3_lattice *d3_l)
+{
+ int x,y,z;
+ int c_fd;
+
+ if((c_fd=open(cf,O_WRONLY|O_CREAT))<0)
+ {
+  puts("cannot open convert file");
+  return -1;
+ }
+ dprintf(c_fd,"# created by nlsop (gnuplot format)\n");
+ 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++)
+   {
+    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);
+   }
+  }
+ }
+ close(c_fd);
+
+ return 1;
+}
+
 int main(int argc,char **argv)
 {
- u32 max_x,max_y,max_z,x,y,z,x_c,y_c,z_c;
+ u32 x,y,z,x_c,y_c,z_c;
  int i,quit,escape,nowait;
- int refresh;
+ int refresh,resave;
+ char z_diff;
  char s_file[MAX_CHARS];
+ char s_file_tmp[MAX_CHARS];
  char l_file[MAX_CHARS];
+ char c_file[MAX_CHARS];
+ char convert;
  char r_file[MAX_CHARS];
+#ifdef USE_DFB_API
  char x_txt[MAX_TXT];
  char y_txt[MAX_TXT];
  char z_txt[MAX_TXT];
@@ -357,15 +432,18 @@ int main(int argc,char **argv)
  char el2_txt[MAX_TXT];
  char dr_txt[MAX_TXT];
  char *arg_v[MAX_ARGV];
+#endif
  d3_lattice d3_l;
  info my_info;
 
- max_x=X;
- max_y=Y;
- max_z=Z;
d3_l.max_x=X;
d3_l.max_y=Y;
d3_l.max_z=Z;
  my_info.steps=STEPS;
  my_info.range=RANGE;
  refresh=REFRESH;
+ resave=RESAVE;
+ z_diff=0;
  my_info.a_el=A_EL;
  my_info.b_el=B_EL;
  my_info.a_cd=A_CD;
@@ -379,6 +457,8 @@ int main(int argc,char **argv)
  escape=0;
  strcpy(s_file,"");
  strcpy(l_file,"");
+ strcpy(c_file,"");
+ convert=0;
  strcpy(r_file,"");
 
  for(i=1;i<argc;i++)
@@ -400,13 +480,13 @@ int main(int argc,char **argv)
      my_info.b_el=atof(argv[++i]);
      break;
     case 'x':
-     max_x=atoi(argv[++i]);
+     d3_l.max_x=atoi(argv[++i]);
      break;
     case 'y':
-     max_y=atoi(argv[++i]);
+     d3_l.max_y=atoi(argv[++i]);
      break;
     case 'z':
-     max_z=atoi(argv[++i]);
+     d3_l.max_z=atoi(argv[++i]);
      break;
     /*
     case 'X':
@@ -415,10 +495,11 @@ int main(int argc,char **argv)
     case 'Y':
      y=atoi(argv[++i]);
      break;
+    */
     case 'Z':
-     z=atoi(argv[++i]);
+     z_diff=1;
      break;
-    */
+    /* */
     case 's':
      my_info.steps=atoi(argv[++i]);
      break;
@@ -445,6 +526,14 @@ int main(int argc,char **argv)
      my_info.cc=atoi(argv[++i]);
      break;
     */
+    case 'W':
+     resave=atoi(argv[++i]);
+     break;
+    case 'C':
+     strcpy(l_file,argv[++i]);
+     if(i<argc-1) if(argv[i+1][0]!='-') strcpy(c_file,argv[++i]);
+     convert=1;
+     break;
     case 'D':
      my_info.d_r=atof(argv[++i]);
      break;
@@ -464,19 +553,27 @@ int main(int argc,char **argv)
   } else usage();
  }
 
- x=max_x/2-1;
- y=max_y/2-1;
- z=max_z/2-1;
-
+ x=d3_l.max_x/2-1;
+ y=d3_l.max_y/2-1;
+ z=d3_l.max_z/2-1;
 
+#ifdef NODFB
+ 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);
 
  if(!strcmp(l_file,""))
  {
-  i=max_x*max_y*max_z;
-  d3_lattice_init(&argc,argv,&d3_l,max_x,max_y,max_z);
+  i=d3_l.max_x*d3_l.max_y*d3_l.max_z;
+#ifdef USE_DFB_API
+  d3_lattice_init(&argc,argv,&d3_l);
+#endif
   if((d3_l.status=(unsigned char *)malloc(i*sizeof(unsigned char)))==NULL)
   {
    puts("failed allocating status buffer");
@@ -492,11 +589,24 @@ int main(int argc,char **argv)
  } else
  {
   load_from_file(l_file,&d3_l,&my_info);
-  d3_lattice_init(&argc,argv,&d3_l,d3_l.max_x,d3_l.max_y,d3_l.max_z);
+  if(convert) 
+  {   
+   if(!strcmp(c_file,"")) sprintf(c_file,"%s_gnuplot",l_file);
+   printf("converting file %s to %s\n",l_file,c_file);
+   convert_file(c_file,&d3_l);
+   puts("done");
+   return 1;
+  } 
+#ifdef USE_DFB_API
+    else d3_lattice_init(&argc,argv,&d3_l);
+#endif
  }
 
+#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(r_txt,"pressure range: %d",my_info.range);
@@ -531,6 +641,7 @@ int main(int argc,char **argv)
  arg_v[22]=cd_txt;
  arg_v[23]=cd2_txt;
  arg_v[24]=dr_txt;
+#endif
 
  if(!strcmp(l_file,""))
  {
@@ -540,8 +651,9 @@ 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);
-   distrib_c(&d3_l,my_info.d_r,my_info.a_cd,my_info.b_cd);
+   distrib_c(&d3_l,my_info.d_r,my_info.a_cd,my_info.b_cd,z_diff);
    process_cell(&d3_l,x_c,y_c,z_c,my_info.range,my_info.a_ap,my_info.b_ap,&(my_info.cc));
+#ifdef USE_DFB_API
    if(i%refresh==0)
    {
     sprintf(x_txt,"x: %d",x+1);
@@ -552,14 +664,27 @@ int main(int argc,char **argv)
     sprintf(steps_txt,"step: %d",i);
     sprintf(cc_txt,"total c: %d",my_info.cc);
     d3_lattice_draw(&d3_l,x,y,z,24,arg_v);
-    // scan_event(&d3_l,&x,&y,&z,&quit,&escape);
+   }
+#endif
+   if(i%resave==0 && strcmp(s_file,"") && resave!=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
  while((quit==0) && (escape==0) && (nowait==0))
  {
   sprintf(x_txt,"x: %d",x+1);
@@ -574,6 +699,7 @@ int main(int argc,char **argv)
  }
 
  d3_lattice_release(&d3_l);
+#endif
 
  return 1;
 }