added 2 newview modes: c conc & a pressure
[physik/nlsop.git] / nlsop.c
diff --git a/nlsop.c b/nlsop.c
index 7dc862c..e9d72ab 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);
@@ -50,7 +52,12 @@ int usage(void)
  printf("-p <value> \t pressure offset (default %f)\n",B_AP);
  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("-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");
@@ -63,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;
@@ -74,35 +82,190 @@ 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);
    } 
   }
  }
- // p*=*conc;
- // printf("debug: p * conc = %f\n",p);
+ printf("debug: p = %f\n",p);
  if(!(*thiz&AMORPH))
  {
-  if(get_rand(URAND_MAX)<=p)
-  {
-   MAKE_AMORPH(thiz);
-   printf("debug: c->a %d - %d\n",*t_c,*conc);
-   *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)
+  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,char z_diff)
+{
+ u32 x,y,z;
+ int i,j,k,c;
+ int offset,off;
+ 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;
+
+ /* diffusion in layer */
+ for(i=0;i<d3_l->max_x;i++)
+ {
+  for(j=0;j<d3_l->max_y;j++)
+  {
+   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)
+    {
+     /* 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(carry!=0)
+       {
+        *(d3_l->extra+offset)+=carry;
+        *(d3_l->extra+off)-=carry;
+       }
+      }
+     }
+     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=(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; 
+        *(d3_l->extra+off)-=carry; 
+       }
+      }
+     }
+    } else
+    /* case not amorph */
+    {
+     /* 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;
+        if(carry!=0)
+        {
+         *(d3_l->extra+offset)+=carry;
+         *(d3_l->extra+off)-=carry;
+        }
+       }
+      }
+     }
+     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 */
+ } /* for x */
+
+ return 1;
+}
+
+int calc_pressure(d3_lattice *d3_l,int range)
+{
+ int i,j,off;
+ double count;
+ int x,y,z;
+
+ for(x=0;x<d3_l->max_x;x++)
+ {
+  for(y=0;y<d3_l->max_y;y++)
   {
-   MAKE_CRYST(thiz);
-   printf("debug: a->c %d - %d\n",*t_c,*conc);
-   *t_c=*t_c+1+*conc;
-  } else *t_c+=1;
+   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);
+      }
+     }
+    }
+    *(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_VPTR);
+   }
+  }
  }
 
  return 1;
 }
 
-int distrib_c(d3_lattice *d3_l,int t_c,double a,double b)
+int distrib_c_old(d3_lattice *d3_l,int t_c,double a,double b)
 {
  int i,j,k,total,area;
  double sum;
@@ -126,17 +289,14 @@ int distrib_c(d3_lattice *d3_l,int t_c,double a,double b)
    }
   }
   temp=(int)((i+1)*a+b)*t_c/(sum*area_h[i]);
-  // if(temp)
-  // {
-   for(j=0;j<area;j++)
+  for(j=0;j<area;j++)
+  {
+   if(!(*(d3_l->status+(i*area)+j)&AMORPH))
    {
-    if(!(*(d3_l->status+(i*area)+j)&AMORPH))
-    {
-     *(d3_l->extra+(i*area)+j)=temp;
-     total+=temp;
-    }
+    *(d3_l->extra+(i*area)+j)=temp;
+    total+=temp;
    }
-  // }
+  }
   left=(int)(((i+1)*a+b)*t_c/sum)%area_h[i];
   while(left)
   {
@@ -167,7 +327,7 @@ int distrib_c(d3_lattice *d3_l,int t_c,double a,double b)
  return 1;
 }
 
-int save_to_file(char *sf,d3_lattice *d3_l)
+int save_to_file(char *sf,d3_lattice *d3_l,info *my_inf)
 {
  int sf_fd,c;
 
@@ -181,6 +341,11 @@ int save_to_file(char *sf,d3_lattice *d3_l)
   puts("failed saving d3 lattice struct");
   return -1;
  }
+ if(write(sf_fd,my_inf,sizeof(info))<sizeof(info))
+ {
+  puts("failed saving info struct");
+  return-1;
+ }
  c=d3_l->max_x*d3_l->max_y*d3_l->max_z;
  if(write(sf_fd,d3_l->status,c*sizeof(unsigned char))<c*sizeof(unsigned char))
  {
@@ -197,7 +362,7 @@ int save_to_file(char *sf,d3_lattice *d3_l)
  return 1;
 }
 
-int load_from_file(char *lf,d3_lattice *d3_l)
+int load_from_file(char *lf,d3_lattice *d3_l,info *my_inf)
 {
  int lf_fd,c;
 
@@ -211,6 +376,11 @@ int load_from_file(char *lf,d3_lattice *d3_l)
   puts("failed reading d3 lattice struct");
   return -1;
  }
+ if(read(lf_fd,my_inf,sizeof(info))<sizeof(info))
+ {
+  puts("failed reading info struct");
+  return-1;
+ }
  c=d3_l->max_x*d3_l->max_y*d3_l->max_z;
  if((d3_l->status=(unsigned char*)malloc(c*sizeof(unsigned char)))==NULL)
  {
@@ -237,15 +407,45 @@ int load_from_file(char *lf,d3_lattice *d3_l)
  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;
- int i,quit,escape,nowait;
double a_el,b_el,a_cd,b_cd,a_ap,b_ap;
int cc,steps,range,refresh;
+ u32 x,y,z,x_c,y_c,z_c;
+ int i,quit,escape,switchmode,nowait;
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];
@@ -253,28 +453,49 @@ int main(int argc,char **argv)
  char conc_txt[MAX_TXT];
  char steps_txt[MAX_TXT];
  char cc_txt[MAX_TXT];
+ char a_txt[MAX_TXT];
+ char s_txt[MAX_TXT];
+ char ap_txt[MAX_TXT];
+ 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 mode_txt[MAX_TXT];
  char *arg_v[MAX_ARGV];
+#endif
  d3_lattice d3_l;
+ info my_info;
+ unsigned char mode;
 
- max_x=X;
- max_y=Y;
- max_z=Z;
- steps=STEPS;
- range=RANGE;
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;
- a_el=A_EL;
- b_el=B_EL;
- a_cd=A_CD;
- b_cd=B_CD;
- a_ap=A_AP;
- b_ap=B_AP;
- cc=CC;
+ resave=RESAVE;
+ z_diff=0;
+ 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.cc=CC;
+ my_info.d_r=D_R;
  nowait=0;
  quit=0;
  escape=0;
+ switchmode=0;
  strcpy(s_file,"");
  strcpy(l_file,"");
+ strcpy(c_file,"");
+ convert=0;
  strcpy(r_file,"");
+ mode=0;
 
  for(i=1;i<argc;i++)
  {
@@ -289,19 +510,19 @@ int main(int argc,char **argv)
      nowait=1;
      break;
     case 'a':
-     a_el=atof(argv[++i]);
+     my_info.a_el=atof(argv[++i]);
      break;
     case 'b':
-     b_el=atof(argv[++i]);
+     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':
@@ -310,33 +531,47 @@ 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':
-     steps=atoi(argv[++i]);
+     my_info.steps=atoi(argv[++i]);
      break;
     case 'd':
      refresh=atoi(argv[++i]);
      break;
     case 'r':
-     range=atoi(argv[++i]);
+     my_info.range=atoi(argv[++i]);
      break;
     case 'f':
-     a_ap=atof(argv[++i]);
+     my_info.a_ap=atof(argv[++i]);
      break;
     case 'p':
-     b_ap=atof(argv[++i]);
+     my_info.b_ap=atof(argv[++i]);
      break;
     case 'A':
-     a_cd=atof(argv[++i]);
+     my_info.a_cd=atof(argv[++i]);
      break;
     case 'B':
-     b_cd=atof(argv[++i]);
+     my_info.b_cd=atof(argv[++i]);
      break;
+    /*
     case 'C':
-     cc=atoi(argv[++i]);
+     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;
     case 'L':
      strcpy(l_file,argv[++i]);
@@ -354,17 +589,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");
@@ -379,22 +624,73 @@ int main(int argc,char **argv)
   memset(d3_l.extra,0,i*sizeof(int));
  } else
  {
-  load_from_file(l_file,&d3_l);
-  d3_lattice_init(&argc,argv,&d3_l,d3_l.max_x,d3_l.max_y,d3_l.max_z);
+  load_from_file(l_file,&d3_l,&my_info);
+  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);
+ 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);
+ strcpy(mode_txt,"view: a/c mode");
+ 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;
+ arg_v[7]=NULL;
+ arg_v[8]=mode_txt;
+ arg_v[9]=NULL;
+ arg_v[10]=steps_txt;;
+ arg_v[11]=cc_txt;
+ arg_v[12]=NULL;
+ 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[20]=el_txt;
+ arg_v[21]=el2_txt;
+ arg_v[22]=cd_txt;
+ arg_v[23]=cd2_txt;
+ arg_v[24]=dr_txt;
+#endif
 
  if(!strcmp(l_file,""))
  {
   i=0;
-  while((i<steps) && (quit==0) && (escape==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,a_el,b_el);
-   distrib_c(&d3_l,cc,a_cd,b_cd);
-   process_cell(&d3_l,x_c,y_c,z_c,range,a_ap,b_ap,&cc);
+   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,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);
@@ -403,48 +699,61 @@ int main(int argc,char **argv)
     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",cc);
-    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;
-    arg_v[7]=NULL;
-    arg_v[8]=NULL;
-    arg_v[9]=steps_txt;
-    arg_v[10]=cc_txt;
-    d3_lattice_draw(&d3_l,x,y,z,10,arg_v);
-    // scan_event(&d3_l,&x,&y,&z,&quit,&escape);
+    sprintf(cc_txt,"total c: %d",my_info.cc);
+    d3_lattice_draw(&d3_l,x,y,z,24,arg_v,mode);
+   }
+#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);
+ 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);
 
  while((quit==0) && (escape==0) && (nowait==0))
  {
-  sprintf(x_txt,"x: %d",x);
-  sprintf(y_txt,"y: %d",y);
-  sprintf(z_txt,"z: %d",z);
+  /* bahh! */
+  if(switchmode==0) mode=0;
+  if(switchmode==1) mode=1;
+  if(switchmode==2) mode=2;
+  /* end of bahh! */
+  sprintf(x_txt,"x: %d",x+1);
+  sprintf(y_txt,"y: %d",y+1);
+  sprintf(z_txt,"z: %d",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(cc_txt,"total c: %d",cc);
-  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;
-  arg_v[7]=NULL;
-  arg_v[8]=NULL;
-  arg_v[9]=steps_txt;
-  arg_v[10]=cc_txt;
-  d3_lattice_draw(&d3_l,x,y,z,10,arg_v);
-  scan_event(&d3_l,&x,&y,&z,&quit,&escape);
+  sprintf(cc_txt,"total c: %d",my_info.cc);
+  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,24,arg_v,mode);
+  scan_event(&d3_l,&x,&y,&z,&quit,&escape,&switchmode);
  }
 
+ d3_lattice_release(&d3_l);
+#endif
+
  return 1;
 }