712702bd2043b1ce3fdb8a6d3f89b758355f17cb
[physik/nlsop.git] / nlsop.c
1 /*
2  * nlsop.c 
3  *
4  * this program tries helping to understand the amorphous depuration
5  * and recrystallization of SiCx while ion implantation at temperatures
6  * below 400 degree celsius.
7  * hopefully the program will simulate the stabilization of the
8  * selforganizing lamella structure in the observed behaviour.
9  *
10  * refs: 
11  *  - J. K. N. Lindner. Habilationsschrift, Universitaet Augsburg.
12  *  - Maik Haeberlen. Diplomarbeit, Universitaet Augsburg.
13  */
14
15 #define _GNU_SOURCE
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <fcntl.h>
22 #include <unistd.h>
23
24 #include "nlsop.h"
25
26 #include "dfbapi.h"
27 #include "random.h"
28
29 #define MAKE_AMORPH(N) *(N)|=AMORPH
30 #define MAKE_CRYST(N) *(N)&=~AMORPH
31
32 /* test globals - get removed or included in my_info struct later */
33 int amorph_count;
34 int cryst_count;
35
36 int usage(void)
37 {
38  puts("usage:");
39  puts("-h \t\t help");
40  puts("-n \t\t no user interaction");
41  puts("-Z \t\t cryst -> amorph c diffusion in z direction");
42  printf("-a <value> \t slope of nuclear energy loss (default %f)\n",A_EL);
43  printf("-b <value> \t nuclear energy loss offset (default %f)\n",B_EL);
44  printf("-x <value> \t # x cells (default %d)\n",X);
45  printf("-y <value> \t # y cells (default %d)\n",Y);
46  printf("-z <value> \t # z cells (default %d)\n",Z);
47  printf("-s <value> \t steps (default %d)\n",STEPS);
48  printf("-d <value> \t refresh display (default %d)\n",REFRESH);
49  printf("-r <value> \t amorphous influence range (default %d)\n",RANGE);
50  printf("-f <value> \t pressure = <value> * 1/distance^2 (default %f)\n",A_AP);
51  printf("-p <value> \t pressure offset (default %f)\n",B_AP);
52  printf("-F <value> \t proportionality constant between c conc and ability to get amorphous (default %f)\n",A_CP);
53  printf("-A <value> \t slope of linear c distribution (default %f)\n",A_CD);
54  printf("-B <value> \t linear c distribution offset (default %f)\n",B_CD);
55  printf("-D <value> \t diffusion rate from cryst to amorph cells (default %f)\n",DR_AC);
56  printf("-c <value> \t diffusion rate in cryst cells (default %f)\n",DR_CC);
57  printf("-e <value> \t do diffusion every <value> steps (default %d)\n",DIFF_RATE);
58  printf("-W <value> \t write every <value> steps to save file (default %d)\n",RESAVE);
59  puts("-C <file> \t convert file to gnuplot format");
60  puts("-L <file> \t load from file");
61  puts("-S <file> \t save to file");
62  puts("-R <file> \t read from random file");
63  puts("-P <file> \t specify implantatin profile file");
64  
65  return 1;
66 }
67
68 int process_cell(d3_lattice *d3_l,u32 x,u32 y,u32 z,info *my_info)
69 {
70  unsigned char *thiz;
71  int *conc;
72  int i,j;
73  int off;
74  double p;
75
76  thiz=d3_l->status+x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y;
77  conc=d3_l->extra+x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y;
78  p=my_info->b_ap*URAND_MAX;
79  for(i=-(my_info->range);i<=my_info->range;i++)
80  {
81   for(j=-(my_info->range);j<=my_info->range;j++)
82   {
83    if(!(i==0 && j==0))
84    {
85     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;
86     if(*(d3_l->status+off)&AMORPH) p+=my_info->a_ap*(*(d3_l->extra+off))*URAND_MAX/(i*i+j*j);
87    } 
88   }
89  }
90  p+=*conc*my_info->a_cp*URAND_MAX;
91  if(!(*thiz&AMORPH))
92  {
93   if(get_rand(URAND_MAX)<=p)
94   {
95    MAKE_AMORPH(thiz);
96    amorph_count++;
97   }
98  } else
99  {
100   /* assume 1-p probability */
101   if(get_rand(URAND_MAX)>p)
102   {
103    MAKE_CRYST(thiz);
104    cryst_count++;
105   }
106  }
107  
108  return 1;
109 }
110
111 int distrib_c(d3_lattice *d3_l,info *my_info,int step,double c_ratio)
112 {
113  u32 x,y,z;
114  int i,j,k,c;
115  int offset,off;
116  int carry;
117
118  /* put one c ion somewhere in the lattice */
119  if(my_info->cc<c_ratio*step)
120  {
121   x=get_rand(d3_l->max_x);
122   y=get_rand(d3_l->max_y);
123   z=get_rand_lgp(d3_l->max_z,my_info->a_cd,my_info->b_cd);
124   *(d3_l->extra+x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y)+=1;
125   (my_info->cc)++;
126  }
127
128  if(step%my_info->diff_rate==0)
129  {
130
131  for(i=0;i<d3_l->max_x;i++)
132  {
133   for(j=0;j<d3_l->max_y;j++)
134   {
135    for(k=0;k<d3_l->max_z;k++)
136    {
137     offset=i+j*d3_l->max_x+k*d3_l->max_x*d3_l->max_y;
138     /* case amorph - amorph <-> cryst diffusion */
139     if(*(d3_l->status+offset)&AMORPH)
140     {
141      for(c=-1;c<=1;c++)
142      {
143       if(c!=0)
144       {
145        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;
146        carry=0;
147        if(!(*(d3_l->status+off)&AMORPH)) carry=(int)(my_info->dr_ac*(*(d3_l->extra+off)));
148        if(carry!=0)
149        {
150         *(d3_l->extra+offset)+=carry;
151         *(d3_l->extra+off)-=carry;
152        }
153       }
154      }
155      for(c=-1;c<=1;c++)
156      {
157       if(c!=0)
158       {
159        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;
160        carry=0;
161        if(!(*(d3_l->status+off)&AMORPH)) carry=(int)(my_info->dr_ac*(*(d3_l->extra+off)));                             
162        if(carry!=0)
163        {
164         *(d3_l->extra+offset)+=carry; 
165         *(d3_l->extra+off)-=carry; 
166        }
167       }
168      }
169      if(my_info->z_diff)
170      {
171       for(c=-1;c<=1;c++)
172       {
173        if(c!=0)
174        {
175         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;
176         carry=0;
177         if(!*(d3_l->status+off)&AMORPH) carry=(int)(my_info->dr_ac*(*(d3_l->extra+off)));
178         if(carry!=0)
179         {
180          *(d3_l->extra+off)-=carry;
181          *(d3_l->extra+offset)+=carry;
182         }
183        }
184       }
185      }  
186     } else
187     /* case not amorph - cryst <-> cryst diffusion */
188     {
189      for(c=-1;c<=1;c++) 
190      {
191       if(c!=0)
192       {
193        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;
194        carry=0;
195        if(!(*(d3_l->status+off)&AMORPH))
196        {
197         carry=(*(d3_l->extra+off)-*(d3_l->extra+offset))/2;
198         if(carry!=0)
199         {
200          *(d3_l->extra+offset)+=carry;
201          *(d3_l->extra+off)-=carry;
202         }
203        }
204       }
205      }
206      for(c=-1;c<=1;c++)
207      {
208       if(c!=0)
209       {
210        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;
211        carry=0;
212        if(!(*(d3_l->status+off)&AMORPH))
213        {
214         carry=(*(d3_l->extra+off)-*(d3_l->extra+offset))/2;
215         if(carry!=0)
216         {
217          *(d3_l->extra+offset)+=carry;
218          *(d3_l->extra+off)-=carry;
219         }
220        }
221       }
222      }
223     }
224    } /* for z */
225   } /* for y */
226  } /* for x */
227
228  } /* if step modulo diff_rate == 0 */
229
230  return 1;
231 }
232
233 int calc_pressure(d3_lattice *d3_l,int range)
234 {
235  int i,j,off;
236  double count;
237  int x,y,z;
238
239  for(x=0;x<d3_l->max_x;x++)
240  {
241   for(y=0;y<d3_l->max_y;y++)
242   {
243    for(z=0;z<d3_l->max_z;z++)
244    {
245     count=0;
246     for(i=-range;i<=range;i++)
247     {
248      for(j=-range;j<=range;j++)
249      {
250       if(i!=0 && j!=0)
251       {
252        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;
253        if(*(d3_l->status+off)&AMORPH) count+=((double)*(d3_l->extra+off))/(i*i+j*j);
254       }
255      }
256     }
257     *(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);
258    }
259   }
260  }
261
262  return 1;
263 }
264
265 int save_to_file(char *sf,d3_lattice *d3_l,info *my_inf)
266 {
267  int sf_fd,c;
268
269  if((sf_fd=open(sf,O_WRONLY|O_CREAT))<0)
270  {
271   puts("cannot open save file");
272   return -1;
273  }
274  if(write(sf_fd,d3_l,sizeof(d3_lattice))<sizeof(d3_lattice))
275  {
276   puts("failed saving d3 lattice struct");
277   return -1;
278  }
279  if(write(sf_fd,my_inf,sizeof(info))<sizeof(info))
280  {
281   puts("failed saving info struct");
282   return-1;
283  }
284  c=d3_l->max_x*d3_l->max_y*d3_l->max_z;
285  if(write(sf_fd,d3_l->status,c*sizeof(unsigned char))<c*sizeof(unsigned char))
286  {
287   puts("failed saving status of d3 lattice sites");
288   return -1;
289  }
290  if(write(sf_fd,d3_l->extra,c*sizeof(int))<c*sizeof(int))
291  {
292   puts("failed saving sites concentration");
293   return -1;
294  }
295  close(sf_fd);
296
297  return 1;
298 }
299
300 int load_from_file(char *lf,d3_lattice *d3_l,info *my_inf)
301 {
302  int lf_fd,c;
303
304  if((lf_fd=open(lf,O_RDONLY))<0)
305  {
306   puts("cannot open load file");
307   return -1;
308  }
309  if(read(lf_fd,d3_l,sizeof(d3_lattice))<sizeof(d3_lattice))
310  {
311   puts("failed reading d3 lattice struct");
312   return -1;
313  }
314  if(read(lf_fd,my_inf,sizeof(info))<sizeof(info))
315  {
316   puts("failed reading info struct");
317   return-1;
318  }
319  c=d3_l->max_x*d3_l->max_y*d3_l->max_z;
320  if((d3_l->status=(unsigned char*)malloc(c*sizeof(unsigned char)))==NULL)
321  {
322   puts("cannot allocate status buffer");
323   return -1;
324  }
325  if((d3_l->extra=(int *)malloc(c*sizeof(int)))==NULL)
326  {
327   puts("cannot allocate concentration buffer");
328   return -1;
329  }
330  if(read(lf_fd,d3_l->status,c*sizeof(unsigned char))<c*sizeof(unsigned char))
331  {
332   puts("failed reading status of d3 lattice sites");
333   return -1;
334  }
335  if(read(lf_fd,d3_l->extra,c*sizeof(int))<c*sizeof(int))
336  {
337   puts("failed reading sites concentration");
338   return -1;
339  }
340  close(lf_fd);
341
342  return 1;
343 }
344
345 int convert_file(char *cf,d3_lattice *d3_l)
346 {
347  int x,y,z;
348  int c_fd;
349
350  if((c_fd=open(cf,O_WRONLY|O_CREAT))<0)
351  {
352   puts("cannot open convert file");
353   return -1;
354  }
355  dprintf(c_fd,"# created by nlsop (gnuplot format)\n");
356  for(x=0;x<d3_l->max_x;x++)
357  {
358   for(y=0;y<d3_l->max_y;y++)
359   {
360    for(z=0;z<d3_l->max_z;z++)
361    {
362     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);
363    }
364   }
365  }
366  close(c_fd);
367
368  return 1;
369 }
370
371 int get_c_ratio(double *c_ratio,char *pfile,info *my_info,d3_lattice *d3_l)
372 {
373  double all,a,b,d;
374  int i,k;
375  int p_fd;
376  unsigned char buf[32];
377  char *p;
378  unsigned char c;
379
380  if((p_fd=open(pfile,O_RDONLY))<0)
381  {
382   puts("cannot open profile file");
383   return -1;
384  }
385  k=1;
386  d=0;
387  all=0;
388  while(k)
389  {
390   for(i=0;i<32;i++)
391   {
392    k=read(p_fd,&c,1);
393    buf[i]=c;
394    if(c=='\n') break;
395   }
396   if(k)
397   {
398    p=strtok(buf," ");
399    a=atof(p)/10; /* nm */
400    p=strtok(NULL," ");
401    b=atof(p);
402    if(a>my_info->b_cd*CELL_LENGTH && a<(my_info->b_cd+d3_l->max_z)*CELL_LENGTH) d+=b;
403    all+=b;
404   }
405  }
406  *c_ratio=d/all;
407  close(p_fd);
408
409  return 1;
410 }
411
412 int main(int argc,char **argv)
413 {
414  u32 x,y,z,x_c,y_c,z_c;
415  int i,quit,escape,switchmode,nowait;
416  int refresh,resave;
417  char s_file[MAX_CHARS];
418  char s_file_tmp[MAX_CHARS];
419  char l_file[MAX_CHARS];
420  char c_file[MAX_CHARS];
421  char p_file[MAX_CHARS];
422  char convert;
423  char r_file[MAX_CHARS];
424 #ifdef USE_DFB_API
425  char xyz_txt[MAX_TXT];
426  char status_txt[MAX_TXT];
427  char conc_txt[MAX_TXT];
428  char steps_txt[MAX_TXT];
429  char cc_txt[MAX_TXT];
430  char a_txt[MAX_TXT];
431  char s_txt[MAX_TXT];
432  char ap_txt[MAX_TXT];
433  char el_txt[MAX_TXT];
434  char cd_txt[MAX_TXT];
435  char r_txt[MAX_TXT];
436  char cp_txt[MAX_TXT];
437  char zdiff_txt[MAX_TXT];
438  char diff_txt[MAX_TXT];
439  char dr_ac_txt[MAX_TXT];
440  char dr_cc_txt[MAX_TXT];
441  char dose_txt[MAX_TXT];
442  char mode_txt[MAX_TXT];
443  char *arg_v[MAX_ARGV];
444 #endif
445  d3_lattice d3_l;
446  info my_info;
447  unsigned char mode;
448  double c_ratio;
449
450  d3_l.max_x=X;
451  d3_l.max_y=Y;
452  d3_l.max_z=Z;
453  my_info.steps=STEPS;
454  my_info.range=RANGE;
455  refresh=REFRESH;
456  resave=RESAVE;
457  my_info.z_diff=0;
458  my_info.a_el=A_EL;
459  my_info.b_el=B_EL;
460  my_info.a_cd=A_CD;
461  my_info.b_cd=B_CD;
462  my_info.a_ap=A_AP;
463  my_info.b_ap=B_AP;
464  my_info.a_cp=A_CP;
465  my_info.cc=CC;
466  my_info.dr_ac=DR_AC;
467  my_info.dr_cc=DR_CC;
468  my_info.diff_rate=DIFF_RATE;
469  nowait=0;
470  quit=0;
471  escape=0;
472  switchmode=0;
473  strcpy(s_file,"");
474  strcpy(l_file,"");
475  strcpy(c_file,"");
476  strcpy(p_file,IMP_PROFILE);
477  convert=0;
478  strcpy(r_file,"");
479  mode=0;
480
481  amorph_count=0;
482  cryst_count=0;
483
484  for(i=1;i<argc;i++)
485  {
486   if(argv[i][0]=='-')
487   {
488    switch(argv[i][1])
489    {
490     case 'h':
491      usage();
492      return -1;
493     case 'n':
494      nowait=1;
495      break;
496     case 'a':
497      my_info.a_el=atof(argv[++i]);
498      break;
499     case 'b':
500      my_info.b_el=atof(argv[++i]);
501      break;
502     case 'x':
503      d3_l.max_x=atoi(argv[++i]);
504      break;
505     case 'y':
506      d3_l.max_y=atoi(argv[++i]);
507      break;
508     case 'z':
509      d3_l.max_z=atoi(argv[++i]);
510      break;
511     case 'Z':
512      my_info.z_diff=1;
513      break;
514     case 's':
515      my_info.steps=atoi(argv[++i]);
516      break;
517     case 'd':
518      refresh=atoi(argv[++i]);
519      break;
520     case 'r':
521      my_info.range=atoi(argv[++i]);
522      break;
523     case 'f':
524      my_info.a_ap=atof(argv[++i]);
525      break;
526     case 'p':
527      my_info.b_ap=atof(argv[++i]);
528      break;
529     case 'F':
530      my_info.a_cp=atof(argv[++i]);
531      break;
532     case 'A':
533      my_info.a_cd=atof(argv[++i]);
534      break;
535     case 'B':
536      my_info.b_cd=atof(argv[++i]);
537      break;
538     case 'W':
539      resave=atoi(argv[++i]);
540      break;
541     case 'C':
542      strcpy(l_file,argv[++i]);
543      if(i<argc-1) if(argv[i+1][0]!='-') strcpy(c_file,argv[++i]);
544      convert=1;
545      break;
546     case 'D':
547      my_info.dr_ac=atof(argv[++i]);
548      break;
549     case 'c':
550      my_info.dr_cc=atof(argv[++i]);
551      break;
552     case 'e':
553      my_info.diff_rate=atoi(argv[++i]);
554      break;
555     case 'L':
556      strcpy(l_file,argv[++i]);
557      break;
558     case 'S':
559      strcpy(s_file,argv[++i]);
560      break;
561     case 'R':
562      strcpy(r_file,argv[++i]);
563      break;
564     case 'P':
565      strcpy(p_file,argv[++i]);
566      break;
567     default:
568      usage();
569      return -1;
570    }
571   } else usage();
572  }
573
574  x=d3_l.max_x/2-1;
575  y=d3_l.max_y/2-1;
576  z=d3_l.max_z/2-1;
577
578 #ifdef NODFB
579  if(!strcmp(s_file,""))
580  {
581   puts("NODFB defined, run with -S option");
582   return -1;
583  }
584 #endif
585
586  if(!strcmp(r_file,"")) rand_init(NULL);
587  else rand_init(r_file);
588
589  if(!strcmp(l_file,""))
590  {
591   i=d3_l.max_x*d3_l.max_y*d3_l.max_z;
592 #ifdef USE_DFB_API
593   d3_lattice_init(&argc,argv,&d3_l);
594 #endif
595   if((d3_l.status=(unsigned char *)malloc(i*sizeof(unsigned char)))==NULL)
596   {
597    puts("failed allocating status buffer");
598    return -1;
599   }
600   memset(d3_l.status,0,i*sizeof(unsigned char));
601   if((d3_l.extra=(int *)malloc(i*sizeof(int)))==NULL)
602   {
603    puts("failed allocating concentration buffer");
604    return -1;
605   }
606   memset(d3_l.extra,0,i*sizeof(int));
607  } else
608  {
609   load_from_file(l_file,&d3_l,&my_info);
610   if(convert) 
611   {   
612    if(!strcmp(c_file,"")) sprintf(c_file,"%s_gnuplot",l_file);
613    printf("converting file %s to %s\n",l_file,c_file);
614    convert_file(c_file,&d3_l);
615    puts("done");
616    return 1;
617   } 
618 #ifdef USE_DFB_API
619     else d3_lattice_init(&argc,argv,&d3_l);
620 #endif
621  }
622
623 #ifdef USE_DFB_API
624  d3_event_init(&d3_l);
625 #endif
626
627 #ifdef USE_DFB_API
628  strcpy(a_txt,"args:");
629  sprintf(s_txt,"steps: %d",my_info.steps);
630  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));
631  sprintf(r_txt,"pressure range: %d",my_info.range);
632  sprintf(ap_txt,"a_ap: %.2f  b_ap: %.3f",my_info.a_ap,my_info.b_ap);
633  sprintf(el_txt,"a_el: %.2f  b_el: %.3f",my_info.a_el,my_info.b_el);
634  sprintf(cd_txt,"a_cd: %.2f  b_cd: %.3f",my_info.a_cd,my_info.b_cd);
635  sprintf(cp_txt,"a_cp: %.4f",my_info.a_cp);
636  sprintf(dr_ac_txt,"a/c diffusion rate: %.4f",my_info.dr_ac);
637  sprintf(dr_cc_txt,"c/c diffusion rate: %.4f",my_info.dr_cc);
638  sprintf(zdiff_txt,"diffusion in z direction: %c",my_info.z_diff?'y':'n');
639  sprintf(diff_txt,"diffusion every %d steps",my_info.diff_rate);
640  strcpy(mode_txt,"view: a/c mode");
641  arg_v[1]=xyz_txt;
642  arg_v[2]=NULL;
643  arg_v[3]=status_txt;
644  arg_v[4]=conc_txt;
645  arg_v[5]=NULL;
646  arg_v[6]=mode_txt;
647  arg_v[7]=NULL;
648  arg_v[8]=steps_txt;
649  arg_v[9]=cc_txt;
650  arg_v[10]=NULL;
651  arg_v[11]=diff_txt;
652  arg_v[12]=zdiff_txt;
653  arg_v[13]=NULL;
654  arg_v[14]=a_txt;
655  arg_v[15]=s_txt;
656  arg_v[16]=dose_txt;
657  arg_v[17]=NULL;
658  arg_v[18]=r_txt;
659  arg_v[19]=ap_txt;
660  arg_v[20]=el_txt;
661  arg_v[21]=cd_txt;
662  arg_v[22]=cp_txt;
663  arg_v[23]=NULL;
664  arg_v[24]=dr_ac_txt;
665  arg_v[25]=dr_cc_txt;
666 #endif
667
668  if(!strcmp(l_file,""))
669  {
670   if(get_c_ratio(&c_ratio,p_file,&my_info,&d3_l)!=1)
671   {
672    puts("failed calculating ratio");
673    return -1;
674   }
675   i=0;
676   while((i<my_info.steps) && (quit==0) && (escape==0))
677   {
678    x_c=get_rand(d3_l.max_x);
679    y_c=get_rand(d3_l.max_y);
680    z_c=get_rand_lgp(d3_l.max_z,my_info.a_el,my_info.b_el);
681    distrib_c(&d3_l,&my_info,i,c_ratio);
682    process_cell(&d3_l,x_c,y_c,z_c,&my_info);
683 #ifdef USE_DFB_API
684    if(i%refresh==0)
685    {
686     sprintf(xyz_txt,"x: %d  y: %d  z: %d",x+1,y+1,z+1);
687     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');
688     sprintf(conc_txt,"conc: %d",*(d3_l.extra+x+y*d3_l.max_x+z*d3_l.max_x*d3_l.max_y));
689     sprintf(steps_txt,"step: %d  a_count: %d",i,amorph_count);
690     sprintf(cc_txt,"total c: %d  c_count: %d",my_info.cc,cryst_count);
691     d3_lattice_draw(&d3_l,x,y,z,25,arg_v,mode);
692    }
693 #endif
694    if(i%resave==0 && strcmp(s_file,"") && resave!=0 && i!=0)
695    {
696     sprintf(s_file_tmp,"%s_%d_of_%d",s_file,i,my_info.steps);
697     save_to_file(s_file_tmp,&d3_l,&my_info);
698 #ifdef NODFB
699     printf("saved %s\n",s_file_tmp);
700 #endif
701    }
702    i++;
703   }
704  }
705
706  if(strcmp(s_file,""))
707  {
708    printf("saved %s\n",s_file);
709    save_to_file(s_file,&d3_l,&my_info);
710  }
711
712 #ifdef USE_DFB_API
713  /* allocating buffer for pressure values */
714  if((d3_l.v_ptr=malloc(d3_l.max_x*d3_l.max_y*d3_l.max_z*sizeof(unsigned char)))==NULL)
715  {
716   puts("cannot allocate buffer for pressure values");
717   return -1;
718  }
719  /* calc values */
720  calc_pressure(&d3_l,my_info.range);
721
722  while((quit==0) && (escape==0) && (nowait==0))
723  {
724   /* bahh! */
725   if(switchmode==0) mode=0;
726   if(switchmode==1) mode=1;
727   if(switchmode==2) mode=2;
728   /* end of bahh! */
729   sprintf(xyz_txt,"x: %d  y: %d  z: %d",x+1,y+1,z+1);
730   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');
731   sprintf(conc_txt,"conc: %d",*(d3_l.extra+x+y*d3_l.max_x+z*d3_l.max_x*d3_l.max_y));
732   sprintf(steps_txt,"step: end!  a_count: %d",amorph_count);
733   sprintf(cc_txt,"total c: %d  c_count: %d",my_info.cc,cryst_count);
734   if(switchmode==0) strcpy(mode_txt,"view: a/c mode");
735   if(switchmode==1) strcpy(mode_txt,"view: c conc mode");
736   if(switchmode==2) strcpy(mode_txt,"view: a pressure mode");
737   d3_lattice_draw(&d3_l,x,y,z,25,arg_v,mode);
738   scan_event(&d3_l,&x,&y,&z,&quit,&escape,&switchmode);
739  }
740
741  d3_lattice_release(&d3_l);
742 #endif
743
744  return 1;
745 }