26a6e6169857dc39b75a1e4b7fe363528b3b64ed
[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=(int)(my_info->dr_cc*(*(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=(int)(my_info->dr_cc*(*(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,max=0;
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     if(count>max) max=count;
258    }
259   }
260  }
261
262  for(x=0;x<d3_l->max_x;x++)
263  {
264   for(y=0;y<d3_l->max_y;y++)
265   {
266    for(z=0;z<d3_l->max_z;z++)
267    {
268     count=0;
269     for(i=-range;i<=range;i++)
270     {
271      for(j=-range;j<=range;j++)
272      {
273       if(i!=0 && j!=0)
274       {
275        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;
276        if(*(d3_l->status+off)&AMORPH) count+=((double)*(d3_l->extra+off))/(i*i+j*j);
277       }
278      }
279     }
280     *(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);
281    }
282   }
283  }
284
285  return 1;
286 }
287
288 int calc_max_extra(d3_lattice *d3_l)
289 {
290  int x,y,z;
291  int off,max=0;
292
293  for(x=0;x<d3_l->max_x;x++)
294  {
295   for(y=0;y<d3_l->max_y;y++)
296   {
297    for(z=0;z<d3_l->max_z;z++)
298    {
299     off=x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y;
300     if(*(d3_l->extra+off)>max) max=*(d3_l->extra+off);
301    }
302   }
303  }
304
305  return max;
306 }
307
308 int save_to_file(char *sf,d3_lattice *d3_l,info *my_inf)
309 {
310  int sf_fd,c;
311
312  if((sf_fd=open(sf,O_WRONLY|O_CREAT))<0)
313  {
314   puts("cannot open save file");
315   return -1;
316  }
317  if(write(sf_fd,d3_l,sizeof(d3_lattice))<sizeof(d3_lattice))
318  {
319   puts("failed saving d3 lattice struct");
320   return -1;
321  }
322  if(write(sf_fd,my_inf,sizeof(info))<sizeof(info))
323  {
324   puts("failed saving info struct");
325   return-1;
326  }
327  c=d3_l->max_x*d3_l->max_y*d3_l->max_z;
328  if(write(sf_fd,d3_l->status,c*sizeof(unsigned char))<c*sizeof(unsigned char))
329  {
330   puts("failed saving status of d3 lattice sites");
331   return -1;
332  }
333  if(write(sf_fd,d3_l->extra,c*sizeof(int))<c*sizeof(int))
334  {
335   puts("failed saving sites concentration");
336   return -1;
337  }
338  close(sf_fd);
339
340  return 1;
341 }
342
343 int load_from_file(char *lf,d3_lattice *d3_l,info *my_inf)
344 {
345  int lf_fd,c;
346
347  if((lf_fd=open(lf,O_RDONLY))<0)
348  {
349   puts("cannot open load file");
350   return -1;
351  }
352  if(read(lf_fd,d3_l,sizeof(d3_lattice))<sizeof(d3_lattice))
353  {
354   puts("failed reading d3 lattice struct");
355   return -1;
356  }
357  if(read(lf_fd,my_inf,sizeof(info))<sizeof(info))
358  {
359   puts("failed reading info struct");
360   return-1;
361  }
362  c=d3_l->max_x*d3_l->max_y*d3_l->max_z;
363  if((d3_l->status=(unsigned char*)malloc(c*sizeof(unsigned char)))==NULL)
364  {
365   puts("cannot allocate status buffer");
366   return -1;
367  }
368  if((d3_l->extra=(int *)malloc(c*sizeof(int)))==NULL)
369  {
370   puts("cannot allocate concentration buffer");
371   return -1;
372  }
373  if(read(lf_fd,d3_l->status,c*sizeof(unsigned char))<c*sizeof(unsigned char))
374  {
375   puts("failed reading status of d3 lattice sites");
376   return -1;
377  }
378  if(read(lf_fd,d3_l->extra,c*sizeof(int))<c*sizeof(int))
379  {
380   puts("failed reading sites concentration");
381   return -1;
382  }
383  close(lf_fd);
384
385  return 1;
386 }
387
388 int convert_file(char *cf,d3_lattice *d3_l)
389 {
390  int x,y,z;
391  int c_fd;
392
393  if((c_fd=open(cf,O_WRONLY|O_CREAT))<0)
394  {
395   puts("cannot open convert file");
396   return -1;
397  }
398  dprintf(c_fd,"# created by nlsop (gnuplot format)\n");
399  for(x=0;x<d3_l->max_x;x++)
400  {
401   for(y=0;y<d3_l->max_y;y++)
402   {
403    for(z=0;z<d3_l->max_z;z++)
404    {
405     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);
406    }
407   }
408  }
409  close(c_fd);
410
411  return 1;
412 }
413
414 int get_c_ratio(double *c_ratio,char *pfile,info *my_info,d3_lattice *d3_l)
415 {
416  double all,a,b,d;
417  int i,k;
418  int p_fd;
419  unsigned char buf[32];
420  char *p;
421  unsigned char c;
422
423  if((p_fd=open(pfile,O_RDONLY))<0)
424  {
425   puts("cannot open profile file");
426   return -1;
427  }
428  k=1;
429  d=0;
430  all=0;
431  while(k)
432  {
433   for(i=0;i<32;i++)
434   {
435    k=read(p_fd,&c,1);
436    buf[i]=c;
437    if(c=='\n') break;
438   }
439   if(k)
440   {
441    p=strtok(buf," ");
442    a=atof(p)/10; /* nm */
443    p=strtok(NULL," ");
444    b=atof(p);
445    if(a>my_info->b_cd*CELL_LENGTH && a<(my_info->b_cd+d3_l->max_z)*CELL_LENGTH) d+=b;
446    all+=b;
447   }
448  }
449  *c_ratio=d/all;
450  close(p_fd);
451
452  return 1;
453 }
454
455 int main(int argc,char **argv)
456 {
457  u32 x,y,z,x_c,y_c,z_c;
458  int i,quit,escape,switchmode,nowait;
459  int refresh,resave;
460  char s_file[MAX_CHARS];
461  char s_file_tmp[MAX_CHARS];
462  char l_file[MAX_CHARS];
463  char c_file[MAX_CHARS];
464  char p_file[MAX_CHARS];
465  char convert;
466  char r_file[MAX_CHARS];
467 #ifdef USE_DFB_API
468  char xyz_txt[MAX_TXT];
469  char status_txt[MAX_TXT];
470  char conc_txt[MAX_TXT];
471  char steps_txt[MAX_TXT];
472  char cc_txt[MAX_TXT];
473  char a_txt[MAX_TXT];
474  char s_txt[MAX_TXT];
475  char ap_txt[MAX_TXT];
476  char el_txt[MAX_TXT];
477  char cd_txt[MAX_TXT];
478  char r_txt[MAX_TXT];
479  char cp_txt[MAX_TXT];
480  char zdiff_txt[MAX_TXT];
481  char diff_txt[MAX_TXT];
482  char dr_ac_txt[MAX_TXT];
483  char dr_cc_txt[MAX_TXT];
484  char dose_txt[MAX_TXT];
485  char mode_txt[MAX_TXT];
486  char *arg_v[MAX_ARGV];
487 #endif
488  d3_lattice d3_l;
489  info my_info;
490  unsigned char mode;
491  double c_ratio;
492 #ifdef USE_DFB_API
493  int max_extra;
494 #endif
495
496  d3_l.max_x=X;
497  d3_l.max_y=Y;
498  d3_l.max_z=Z;
499  my_info.steps=STEPS;
500  my_info.range=RANGE;
501  refresh=REFRESH;
502  resave=RESAVE;
503  my_info.z_diff=0;
504  my_info.a_el=A_EL;
505  my_info.b_el=B_EL;
506  my_info.a_cd=A_CD;
507  my_info.b_cd=B_CD;
508  my_info.a_ap=A_AP;
509  my_info.b_ap=B_AP;
510  my_info.a_cp=A_CP;
511  my_info.cc=CC;
512  my_info.dr_ac=DR_AC;
513  my_info.dr_cc=DR_CC;
514  my_info.diff_rate=DIFF_RATE;
515  nowait=0;
516  quit=0;
517  escape=0;
518  switchmode=0;
519  strcpy(s_file,"");
520  strcpy(l_file,"");
521  strcpy(c_file,"");
522  strcpy(p_file,IMP_PROFILE);
523  convert=0;
524  strcpy(r_file,"");
525  mode=0;
526
527  amorph_count=0;
528  cryst_count=0;
529
530  for(i=1;i<argc;i++)
531  {
532   if(argv[i][0]=='-')
533   {
534    switch(argv[i][1])
535    {
536     case 'h':
537      usage();
538      return -1;
539     case 'n':
540      nowait=1;
541      break;
542     case 'a':
543      my_info.a_el=atof(argv[++i]);
544      break;
545     case 'b':
546      my_info.b_el=atof(argv[++i]);
547      break;
548     case 'x':
549      d3_l.max_x=atoi(argv[++i]);
550      break;
551     case 'y':
552      d3_l.max_y=atoi(argv[++i]);
553      break;
554     case 'z':
555      d3_l.max_z=atoi(argv[++i]);
556      break;
557     case 'Z':
558      my_info.z_diff=1;
559      break;
560     case 's':
561      my_info.steps=atoi(argv[++i]);
562      break;
563     case 'd':
564      refresh=atoi(argv[++i]);
565      break;
566     case 'r':
567      my_info.range=atoi(argv[++i]);
568      break;
569     case 'f':
570      my_info.a_ap=atof(argv[++i]);
571      break;
572     case 'p':
573      my_info.b_ap=atof(argv[++i]);
574      break;
575     case 'F':
576      my_info.a_cp=atof(argv[++i]);
577      break;
578     case 'A':
579      my_info.a_cd=atof(argv[++i]);
580      break;
581     case 'B':
582      my_info.b_cd=atof(argv[++i]);
583      break;
584     case 'W':
585      resave=atoi(argv[++i]);
586      break;
587     case 'C':
588      strcpy(l_file,argv[++i]);
589      if(i<argc-1) if(argv[i+1][0]!='-') strcpy(c_file,argv[++i]);
590      convert=1;
591      break;
592     case 'D':
593      my_info.dr_ac=atof(argv[++i]);
594      break;
595     case 'c':
596      my_info.dr_cc=atof(argv[++i]);
597      break;
598     case 'e':
599      my_info.diff_rate=atoi(argv[++i]);
600      break;
601     case 'L':
602      strcpy(l_file,argv[++i]);
603      break;
604     case 'S':
605      strcpy(s_file,argv[++i]);
606      break;
607     case 'R':
608      strcpy(r_file,argv[++i]);
609      break;
610     case 'P':
611      strcpy(p_file,argv[++i]);
612      break;
613     default:
614      usage();
615      return -1;
616    }
617   } else usage();
618  }
619
620  x=d3_l.max_x/2-1;
621  y=d3_l.max_y/2-1;
622  z=d3_l.max_z/2-1;
623
624 #ifdef NODFB
625  if(!strcmp(s_file,""))
626  {
627   puts("NODFB defined, run with -S option");
628   return -1;
629  }
630 #endif
631
632  if(!strcmp(r_file,"")) rand_init(NULL);
633  else rand_init(r_file);
634
635  if(!strcmp(l_file,""))
636  {
637   i=d3_l.max_x*d3_l.max_y*d3_l.max_z;
638 #ifdef USE_DFB_API
639   d3_lattice_init(&argc,argv,&d3_l);
640 #endif
641   if((d3_l.status=(unsigned char *)malloc(i*sizeof(unsigned char)))==NULL)
642   {
643    puts("failed allocating status buffer");
644    return -1;
645   }
646   memset(d3_l.status,0,i*sizeof(unsigned char));
647   if((d3_l.extra=(int *)malloc(i*sizeof(int)))==NULL)
648   {
649    puts("failed allocating concentration buffer");
650    return -1;
651   }
652   memset(d3_l.extra,0,i*sizeof(int));
653  } else
654  {
655   load_from_file(l_file,&d3_l,&my_info);
656   if(convert) 
657   {   
658    if(!strcmp(c_file,"")) sprintf(c_file,"%s_gnuplot",l_file);
659    printf("converting file %s to %s\n",l_file,c_file);
660    convert_file(c_file,&d3_l);
661    puts("done");
662    return 1;
663   } 
664 #ifdef USE_DFB_API
665     else d3_lattice_init(&argc,argv,&d3_l);
666 #endif
667  }
668
669 #ifdef USE_DFB_API
670  d3_event_init(&d3_l);
671 #endif
672
673 #ifdef USE_DFB_API
674  strcpy(a_txt,"args:");
675  sprintf(s_txt,"steps: %d",my_info.steps);
676  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));
677  sprintf(r_txt,"pressure range: %d",my_info.range);
678  sprintf(ap_txt,"a_ap: %.3f  b_ap: %.3f",my_info.a_ap,my_info.b_ap);
679  sprintf(el_txt,"a_el: %.3f  b_el: %.3f",my_info.a_el,my_info.b_el);
680  sprintf(cd_txt,"a_cd: %.3f  b_cd: %.3f",my_info.a_cd,my_info.b_cd);
681  sprintf(cp_txt,"a_cp: %.4f",my_info.a_cp);
682  sprintf(dr_ac_txt,"a/c diffusion rate: %.4f",my_info.dr_ac);
683  sprintf(dr_cc_txt,"c/c diffusion rate: %.4f",my_info.dr_cc);
684  sprintf(zdiff_txt,"diffusion in z direction: %c",my_info.z_diff?'y':'n');
685  sprintf(diff_txt,"diffusion every %d steps",my_info.diff_rate);
686  strcpy(mode_txt,"view: a/c mode");
687  arg_v[1]=xyz_txt;
688  arg_v[2]=NULL;
689  arg_v[3]=status_txt;
690  arg_v[4]=conc_txt;
691  arg_v[5]=NULL;
692  arg_v[6]=mode_txt;
693  arg_v[7]=NULL;
694  arg_v[8]=steps_txt;
695  arg_v[9]=cc_txt;
696  arg_v[10]=NULL;
697  arg_v[11]=diff_txt;
698  arg_v[12]=zdiff_txt;
699  arg_v[13]=NULL;
700  arg_v[14]=a_txt;
701  arg_v[15]=s_txt;
702  arg_v[16]=dose_txt;
703  arg_v[17]=NULL;
704  arg_v[18]=r_txt;
705  arg_v[19]=ap_txt;
706  arg_v[20]=el_txt;
707  arg_v[21]=cd_txt;
708  arg_v[22]=cp_txt;
709  arg_v[23]=NULL;
710  arg_v[24]=dr_ac_txt;
711  arg_v[25]=dr_cc_txt;
712 #endif
713
714  if(!strcmp(l_file,""))
715  {
716   if(get_c_ratio(&c_ratio,p_file,&my_info,&d3_l)!=1)
717   {
718    puts("failed calculating ratio");
719    return -1;
720   }
721   i=0;
722   while((i<my_info.steps) && (quit==0) && (escape==0))
723   {
724    x_c=get_rand(d3_l.max_x);
725    y_c=get_rand(d3_l.max_y);
726    z_c=get_rand_lgp(d3_l.max_z,my_info.a_el,my_info.b_el);
727    distrib_c(&d3_l,&my_info,i,c_ratio);
728    process_cell(&d3_l,x_c,y_c,z_c,&my_info);
729 #ifdef USE_DFB_API
730    if(i%refresh==0)
731    {
732     sprintf(xyz_txt,"x: %d  y: %d  z: %d",x+1,y+1,z+1);
733     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');
734     sprintf(conc_txt,"conc: %d",*(d3_l.extra+x+y*d3_l.max_x+z*d3_l.max_x*d3_l.max_y));
735     sprintf(steps_txt,"step: %d  a_count: %d",i,amorph_count);
736     sprintf(cc_txt,"total c: %d  c_count: %d",my_info.cc,cryst_count);
737     d3_lattice_draw(&d3_l,x,y,z,25,arg_v,mode,0);
738    }
739 #endif
740    if(i%resave==0 && strcmp(s_file,"") && resave!=0 && i!=0)
741    {
742     sprintf(s_file_tmp,"%s_%d_of_%d",s_file,i,my_info.steps);
743     save_to_file(s_file_tmp,&d3_l,&my_info);
744 #ifdef NODFB
745     printf("saved %s\n",s_file_tmp);
746 #endif
747    }
748    i++;
749   }
750  }
751
752  if(strcmp(s_file,""))
753  {
754    printf("saved %s\n",s_file);
755    save_to_file(s_file,&d3_l,&my_info);
756  }
757
758 #ifdef USE_DFB_API
759  /* allocating buffer for pressure values */
760  if((d3_l.v_ptr=malloc(d3_l.max_x*d3_l.max_y*d3_l.max_z*sizeof(unsigned char)))==NULL)
761  {
762   puts("cannot allocate buffer for pressure values");
763   return -1;
764  }
765  /* calc values */
766  calc_pressure(&d3_l,my_info.range);
767  max_extra=calc_max_extra(&d3_l);
768
769  while((quit==0) && (escape==0) && (nowait==0))
770  {
771   /* bahh! */
772   if(switchmode==0) mode=0;
773   if(switchmode==1) mode=1;
774   if(switchmode==2) mode=2;
775   /* end of bahh! */
776   sprintf(xyz_txt,"x: %d  y: %d  z: %d",x+1,y+1,z+1);
777   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');
778   sprintf(conc_txt,"conc: %d",*(d3_l.extra+x+y*d3_l.max_x+z*d3_l.max_x*d3_l.max_y));
779   sprintf(steps_txt,"step: end");
780   sprintf(cc_txt,"total c: %d",my_info.cc);
781   if(switchmode==0) strcpy(mode_txt,"view: a/c mode");
782   if(switchmode==1) strcpy(mode_txt,"view: c conc mode");
783   if(switchmode==2) strcpy(mode_txt,"view: a pressure mode");
784   d3_lattice_draw(&d3_l,x,y,z,25,arg_v,mode,max_extra);
785   scan_event(&d3_l,&x,&y,&z,&quit,&escape,&switchmode);
786  }
787
788  d3_lattice_release(&d3_l);
789 #endif
790
791  return 1;
792 }