added -g (continue from file at certain step feature) good for power interrupts ;)
[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  puts("-g <file> <step> \t continue simulation from file and step (step > 0)!");
59  printf("-W <value> \t write every <value> steps to save file (default %d)\n",RESAVE);
60  puts("-C <file> \t convert file to gnuplot format");
61  puts("-L <file> \t load from file");
62  puts("-S <file> \t save to file");
63  puts("-R <file> \t read from random file");
64  puts("-P <file> \t specify implantatin profile file");
65  
66  return 1;
67 }
68
69 int process_cell(d3_lattice *d3_l,u32 x,u32 y,u32 z,info *my_info)
70 {
71  unsigned char *thiz;
72  int *conc;
73  int i,j;
74  int off;
75  double p;
76
77  thiz=d3_l->status+x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y;
78  conc=d3_l->extra+x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y;
79  p=my_info->b_ap*URAND_MAX;
80  for(i=-(my_info->range);i<=my_info->range;i++)
81  {
82   for(j=-(my_info->range);j<=my_info->range;j++)
83   {
84    if(!(i==0 && j==0))
85    {
86     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;
87     if(*(d3_l->status+off)&AMORPH) p+=my_info->a_ap*(*(d3_l->extra+off))*URAND_MAX/(i*i+j*j);
88    } 
89   }
90  }
91  p+=*conc*my_info->a_cp*URAND_MAX;
92  if(!(*thiz&AMORPH))
93  {
94   if(get_rand(URAND_MAX)<=p)
95   {
96    MAKE_AMORPH(thiz);
97    amorph_count++;
98   }
99  } else
100  {
101   /* assume 1-p probability */
102   if(get_rand(URAND_MAX)>p)
103   {
104    MAKE_CRYST(thiz);
105    cryst_count++;
106   }
107  }
108  
109  return 1;
110 }
111
112 int distrib_c(d3_lattice *d3_l,info *my_info,int step,double c_ratio)
113 {
114  u32 x,y,z;
115  int i,j,k,c;
116  int offset,off;
117  int carry;
118
119  /* put one c ion somewhere in the lattice */
120  if(my_info->cc<c_ratio*step)
121  {
122   x=get_rand(d3_l->max_x);
123   y=get_rand(d3_l->max_y);
124   z=get_rand_lgp(d3_l->max_z,my_info->a_cd,my_info->b_cd);
125   *(d3_l->extra+x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y)+=1;
126   (my_info->cc)++;
127  }
128
129  if(step%my_info->diff_rate==0)
130  {
131
132  for(i=0;i<d3_l->max_x;i++)
133  {
134   for(j=0;j<d3_l->max_y;j++)
135   {
136    for(k=0;k<d3_l->max_z;k++)
137    {
138     offset=i+j*d3_l->max_x+k*d3_l->max_x*d3_l->max_y;
139     /* case amorph - amorph <-> cryst diffusion */
140     if(*(d3_l->status+offset)&AMORPH)
141     {
142      for(c=-1;c<=1;c++)
143      {
144       if(c!=0)
145       {
146        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;
147        carry=0;
148        if(!(*(d3_l->status+off)&AMORPH)) carry=(int)(my_info->dr_ac*(*(d3_l->extra+off)));
149        if(carry!=0)
150        {
151         *(d3_l->extra+offset)+=carry;
152         *(d3_l->extra+off)-=carry;
153        }
154       }
155      }
156      for(c=-1;c<=1;c++)
157      {
158       if(c!=0)
159       {
160        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;
161        carry=0;
162        if(!(*(d3_l->status+off)&AMORPH)) carry=(int)(my_info->dr_ac*(*(d3_l->extra+off)));                             
163        if(carry!=0)
164        {
165         *(d3_l->extra+offset)+=carry; 
166         *(d3_l->extra+off)-=carry; 
167        }
168       }
169      }
170      if(my_info->z_diff)
171      {
172       for(c=-1;c<=1;c++)
173       {
174        if(c!=0)
175        {
176         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;
177         carry=0;
178         if(!*(d3_l->status+off)&AMORPH) carry=(int)(my_info->dr_ac*(*(d3_l->extra+off)));
179         if(carry!=0)
180         {
181          *(d3_l->extra+off)-=carry;
182          *(d3_l->extra+offset)+=carry;
183         }
184        }
185       }
186      }  
187     } else
188     /* case not amorph - cryst <-> cryst diffusion */
189     {
190      for(c=-1;c<=1;c++) 
191      {
192       if(c!=0)
193       {
194        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;
195        carry=0;
196        if(!(*(d3_l->status+off)&AMORPH))
197        {
198         carry=(int)(my_info->dr_cc*(*(d3_l->extra+off)-*(d3_l->extra+offset))/2);
199         if(carry!=0)
200         {
201          *(d3_l->extra+offset)+=carry;
202          *(d3_l->extra+off)-=carry;
203         }
204        }
205       }
206      }
207      for(c=-1;c<=1;c++)
208      {
209       if(c!=0)
210       {
211        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;
212        carry=0;
213        if(!(*(d3_l->status+off)&AMORPH))
214        {
215         carry=(int)(my_info->dr_cc*(*(d3_l->extra+off)-*(d3_l->extra+offset))/2);
216         if(carry!=0)
217         {
218          *(d3_l->extra+offset)+=carry;
219          *(d3_l->extra+off)-=carry;
220         }
221        }
222       }
223      }
224     }
225    } /* for z */
226   } /* for y */
227  } /* for x */
228
229  } /* if step modulo diff_rate == 0 */
230
231  return 1;
232 }
233
234 int calc_pressure(d3_lattice *d3_l,int range)
235 {
236  int i,j,off;
237  double count,max=0;
238  int x,y,z;
239
240  for(x=0;x<d3_l->max_x;x++)
241  {
242   for(y=0;y<d3_l->max_y;y++)
243   {
244    for(z=0;z<d3_l->max_z;z++)
245    {
246     count=0;
247     for(i=-range;i<=range;i++)
248     {
249      for(j=-range;j<=range;j++)
250      {
251       if(i!=0 && j!=0)
252       {
253        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;
254        if(*(d3_l->status+off)&AMORPH) count+=((double)*(d3_l->extra+off))/(i*i+j*j);
255       }
256      }
257     }
258     if(count>max) max=count;
259    }
260   }
261  }
262
263  for(x=0;x<d3_l->max_x;x++)
264  {
265   for(y=0;y<d3_l->max_y;y++)
266   {
267    for(z=0;z<d3_l->max_z;z++)
268    {
269     count=0;
270     for(i=-range;i<=range;i++)
271     {
272      for(j=-range;j<=range;j++)
273      {
274       if(i!=0 && j!=0)
275       {
276        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;
277        if(*(d3_l->status+off)&AMORPH) count+=((double)*(d3_l->extra+off))/(i*i+j*j);
278       }
279      }
280     }
281     *(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);
282    }
283   }
284  }
285
286  return 1;
287 }
288
289 int calc_max_extra(d3_lattice *d3_l)
290 {
291  int x,y,z;
292  int off,max=0;
293
294  for(x=0;x<d3_l->max_x;x++)
295  {
296   for(y=0;y<d3_l->max_y;y++)
297   {
298    for(z=0;z<d3_l->max_z;z++)
299    {
300     off=x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y;
301     if(*(d3_l->extra+off)>max) max=*(d3_l->extra+off);
302    }
303   }
304  }
305
306  return max;
307 }
308
309 int write_bmp(d3_lattice *d3_l,int window,u32 x,u32 y,u32 z)
310 {
311  int fd,i,j,size=0,foo=0;
312  int width=0,height=0;
313  char bmpfile[MAX_CHARS];
314  char buf[128];
315
316  if(window==1)
317  {
318   sprintf(bmpfile,"x-z_%d.bmp",y);
319   foo=3*d3_l->max_x;
320   size=(foo+(4-foo%4))*d3_l->max_z;
321   width=d3_l->max_x;
322   height=d3_l->max_z;
323  }
324  if(window==2)
325  {
326   sprintf(bmpfile,"y-z_%d.bmp",x);
327   foo=3*d3_l->max_y;
328   size=(foo+(4-foo%4))*d3_l->max_z;
329   width=d3_l->max_y;
330   height=d3_l->max_z;
331  }
332  if(window==3)
333  {
334   sprintf(bmpfile,"x-y_%d.bmp",z);
335   foo=3*d3_l->max_x;
336   size=(foo+(4-foo%4))*d3_l->max_y;
337   width=d3_l->max_x;
338   height=d3_l->max_y;
339  }
340
341  if((fd=open(bmpfile,O_WRONLY|O_CREAT))<0)
342  {
343   puts("cannot open bmp file");
344   return -1;
345  }
346
347  /* bmpheader */
348  buf[0]='B'; /* std header start */
349  buf[1]='M';
350  buf[2]=(size+0x36)&0xff; /* file size */
351  buf[3]=(size+0x36)>>8;
352  memset(buf+4,0,6);
353  buf[10]=0x36; /* offset to data */
354  memset(buf+11,0,3);
355  buf[14]=0x28; /* length of bmp info header */
356  memset(buf+15,0,3);
357  buf[18]=width&0xff; /* width and height */
358  buf[19]=width>>8;
359  memset(buf+20,0,2);
360  buf[22]=height&0xff;
361  buf[23]=height>>8;
362  memset(buf+24,0,2);
363  buf[26]=1; /* # planes -> 1 */
364  buf[27]=0;
365  buf[28]=24; /* bits per pixel -> 2^24 (true color) */
366  buf[29]=0;
367  memset(buf+30,0,4); /* compression -> none */
368  buf[34]=size&0xff; /* data size */
369  buf[35]=size>>8;
370  memset(buf+36,0,2);
371  buf[38]=0x12; /* res: pixel/meter */
372  buf[39]=0x0b;
373  memset(buf+40,0,2);
374  buf[42]=0x12;
375  buf[43]=0x0b;
376  memset(buf+44,0,2); 
377  memset(buf+46,0,8); /* no colors, no important colors */
378
379  if(write(fd,buf,54)<54)
380  {
381   puts("failed writing bmp header");
382   return -1;
383  }
384  if(window==1)
385  {
386   for(j=0;j<d3_l->max_z;j++)
387   {
388    for(i=0;i<d3_l->max_x;i++)
389    {
390     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);
391     else memset(buf,0,3);
392     if(write(fd,buf,3)<3)
393     {
394      puts("failed writing rgb values to bmp file");
395      return-1;
396     }
397    }
398    if(foo%4)
399    {
400     memset(buf,0,4-foo%4);
401     if(write(fd,buf,4-foo%4)<4-foo%4)
402     {
403      puts("failed writing 4 byte ending");
404      return -1;
405     }
406    } 
407   }
408  }
409  if(window==2)
410  {
411   for(j=0;j<d3_l->max_z;j++)
412   {
413    for(i=0;i<d3_l->max_y;i++)
414    {
415     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);
416     else memset(buf,0,3);
417     if(write(fd,buf,3)<3)
418     {
419      puts("failed writing rgb values to bmp file");
420      return-1;
421     }
422    }
423    if(foo%4)
424    {
425     memset(buf,0,4-foo%4);
426     if(write(fd,buf,4-foo%4)<4-foo%4)
427     {
428      puts("failed writing 4 byte ending");
429      return -1;
430     }
431    }
432   }
433  }
434  if(window==3)
435  {
436   for(j=0;j<d3_l->max_y;j++)
437   {
438    for(i=0;i<d3_l->max_x;i++)
439    {
440     if(*(d3_l->status+i+(d3_l->max_y-j-1)*d3_l->max_x+z*d3_l->max_x*d3_l->max_y)&RED) memset(buf,0xff,3);
441     else memset(buf,0,3);
442     if(write(fd,buf,3)<3)
443     {
444      puts("failed writing rgb values to bmp file");
445      return -1;
446     }
447    }
448    if(foo%4)
449    {
450     memset(buf,0,4-foo%4);
451     if(write(fd,buf,4-foo%4)<4-foo%4)
452     {
453      puts("failed writing 4 byte ending");
454      return -1;
455     }
456    }
457   }
458  }
459  close(fd);
460
461  return 1;
462 }
463
464 int save_to_file(char *sf,d3_lattice *d3_l,info *my_inf)
465 {
466  int sf_fd,c;
467
468  if((sf_fd=open(sf,O_WRONLY|O_CREAT))<0)
469  {
470   puts("cannot open save file");
471   return -1;
472  }
473  if(write(sf_fd,d3_l,sizeof(d3_lattice))<sizeof(d3_lattice))
474  {
475   puts("failed saving d3 lattice struct");
476   return -1;
477  }
478  if(write(sf_fd,my_inf,sizeof(info))<sizeof(info))
479  {
480   puts("failed saving info struct");
481   return-1;
482  }
483  c=d3_l->max_x*d3_l->max_y*d3_l->max_z;
484  if(write(sf_fd,d3_l->status,c*sizeof(unsigned char))<c*sizeof(unsigned char))
485  {
486   puts("failed saving status of d3 lattice sites");
487   return -1;
488  }
489  if(write(sf_fd,d3_l->extra,c*sizeof(int))<c*sizeof(int))
490  {
491   puts("failed saving sites concentration");
492   return -1;
493  }
494  close(sf_fd);
495
496  return 1;
497 }
498
499 int load_from_file(char *lf,d3_lattice *d3_l,info *my_inf)
500 {
501  int lf_fd,c;
502
503  if((lf_fd=open(lf,O_RDONLY))<0)
504  {
505   puts("cannot open load file");
506   return -1;
507  }
508  if(read(lf_fd,d3_l,sizeof(d3_lattice))<sizeof(d3_lattice))
509  {
510   puts("failed reading d3 lattice struct");
511   return -1;
512  }
513  if(read(lf_fd,my_inf,sizeof(info))<sizeof(info))
514  {
515   puts("failed reading info struct");
516   return-1;
517  }
518  c=d3_l->max_x*d3_l->max_y*d3_l->max_z;
519  if((d3_l->status=(unsigned char*)malloc(c*sizeof(unsigned char)))==NULL)
520  {
521   puts("cannot allocate status buffer");
522   return -1;
523  }
524  if((d3_l->extra=(int *)malloc(c*sizeof(int)))==NULL)
525  {
526   puts("cannot allocate concentration buffer");
527   return -1;
528  }
529  if(read(lf_fd,d3_l->status,c*sizeof(unsigned char))<c*sizeof(unsigned char))
530  {
531   puts("failed reading status of d3 lattice sites");
532   return -1;
533  }
534  if(read(lf_fd,d3_l->extra,c*sizeof(int))<c*sizeof(int))
535  {
536   puts("failed reading sites concentration");
537   return -1;
538  }
539  close(lf_fd);
540
541  return 1;
542 }
543
544 int convert_file(char *cf,d3_lattice *d3_l)
545 {
546  int x,y,z;
547  int c_fd;
548
549  if((c_fd=open(cf,O_WRONLY|O_CREAT))<0)
550  {
551   puts("cannot open convert file");
552   return -1;
553  }
554  dprintf(c_fd,"# created by nlsop (gnuplot format)\n");
555  for(x=0;x<d3_l->max_x;x++)
556  {
557   for(y=0;y<d3_l->max_y;y++)
558   {
559    for(z=0;z<d3_l->max_z;z++)
560    {
561     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);
562    }
563   }
564  }
565  close(c_fd);
566
567  return 1;
568 }
569
570 int get_c_ratio(double *c_ratio,char *pfile,info *my_info,d3_lattice *d3_l)
571 {
572  double all,a,b,d;
573  int i,k;
574  int p_fd;
575  unsigned char buf[32];
576  char *p;
577  unsigned char c;
578
579  if((p_fd=open(pfile,O_RDONLY))<0)
580  {
581   puts("cannot open profile file");
582   return -1;
583  }
584  k=1;
585  d=0;
586  all=0;
587  while(k)
588  {
589   for(i=0;i<32;i++)
590   {
591    k=read(p_fd,&c,1);
592    buf[i]=c;
593    if(c=='\n') break;
594   }
595   if(k)
596   {
597    p=strtok(buf," ");
598    a=atof(p)/10; /* nm */
599    p=strtok(NULL," ");
600    b=atof(p);
601    if(a>my_info->b_cd*CELL_LENGTH && a<(my_info->b_cd+d3_l->max_z)*CELL_LENGTH) d+=b;
602    all+=b;
603   }
604  }
605  *c_ratio=d/all;
606  close(p_fd);
607
608  return 1;
609 }
610
611 int main(int argc,char **argv)
612 {
613  u32 x,y,z,x_c,y_c,z_c;
614  int i,quit,escape,switchmode,nowait,bmp;
615  int refresh,resave;
616  int c_step;
617  char s_file[MAX_CHARS];
618  char s_file_tmp[MAX_CHARS];
619  char l_file[MAX_CHARS];
620  char c_file[MAX_CHARS];
621  char p_file[MAX_CHARS];
622  char convert;
623  char r_file[MAX_CHARS];
624 #ifdef USE_DFB_API
625  char xyz_txt[MAX_TXT];
626  char status_txt[MAX_TXT];
627  char conc_txt[MAX_TXT];
628  char steps_txt[MAX_TXT];
629  char cc_txt[MAX_TXT];
630  char a_txt[MAX_TXT];
631  char s_txt[MAX_TXT];
632  char ap_txt[MAX_TXT];
633  char el_txt[MAX_TXT];
634  char cd_txt[MAX_TXT];
635  char r_txt[MAX_TXT];
636  char cp_txt[MAX_TXT];
637  char zdiff_txt[MAX_TXT];
638  char diff_txt[MAX_TXT];
639  char dr_ac_txt[MAX_TXT];
640  char dr_cc_txt[MAX_TXT];
641  char dose_txt[MAX_TXT];
642  char mode_txt[MAX_TXT];
643  char *arg_v[MAX_ARGV];
644 #endif
645  d3_lattice d3_l;
646  info my_info;
647  unsigned char mode;
648  double c_ratio;
649 #ifdef USE_DFB_API
650  int max_extra;
651 #endif
652
653  d3_l.max_x=X;
654  d3_l.max_y=Y;
655  d3_l.max_z=Z;
656  my_info.steps=STEPS;
657  my_info.range=RANGE;
658  refresh=REFRESH;
659  resave=RESAVE;
660  my_info.z_diff=0;
661  my_info.a_el=A_EL;
662  my_info.b_el=B_EL;
663  my_info.a_cd=A_CD;
664  my_info.b_cd=B_CD;
665  my_info.a_ap=A_AP;
666  my_info.b_ap=B_AP;
667  my_info.a_cp=A_CP;
668  my_info.cc=CC;
669  my_info.dr_ac=DR_AC;
670  my_info.dr_cc=DR_CC;
671  my_info.diff_rate=DIFF_RATE;
672  nowait=0;
673  quit=0;
674  escape=0;
675  switchmode=0;
676  c_step=0;
677  strcpy(s_file,"");
678  strcpy(l_file,"");
679  strcpy(c_file,"");
680  strcpy(p_file,IMP_PROFILE);
681  convert=0;
682  strcpy(r_file,"");
683  mode=0;
684
685  amorph_count=0;
686  cryst_count=0;
687
688  for(i=1;i<argc;i++)
689  {
690   if(argv[i][0]=='-')
691   {
692    switch(argv[i][1])
693    {
694     case 'h':
695      usage();
696      return -1;
697     case 'n':
698      nowait=1;
699      break;
700     case 'a':
701      my_info.a_el=atof(argv[++i]);
702      break;
703     case 'b':
704      my_info.b_el=atof(argv[++i]);
705      break;
706     case 'x':
707      d3_l.max_x=atoi(argv[++i]);
708      break;
709     case 'y':
710      d3_l.max_y=atoi(argv[++i]);
711      break;
712     case 'z':
713      d3_l.max_z=atoi(argv[++i]);
714      break;
715     case 'Z':
716      my_info.z_diff=1;
717      break;
718     case 's':
719      my_info.steps=atoi(argv[++i]);
720      break;
721     case 'd':
722      refresh=atoi(argv[++i]);
723      break;
724     case 'r':
725      my_info.range=atoi(argv[++i]);
726      break;
727     case 'f':
728      my_info.a_ap=atof(argv[++i]);
729      break;
730     case 'p':
731      my_info.b_ap=atof(argv[++i]);
732      break;
733     case 'F':
734      my_info.a_cp=atof(argv[++i]);
735      break;
736     case 'A':
737      my_info.a_cd=atof(argv[++i]);
738      break;
739     case 'B':
740      my_info.b_cd=atof(argv[++i]);
741      break;
742     case 'W':
743      resave=atoi(argv[++i]);
744      break;
745     case 'C':
746      strcpy(l_file,argv[++i]);
747      if(i<argc-1) if(argv[i+1][0]!='-') strcpy(c_file,argv[++i]);
748      convert=1;
749      break;
750     case 'D':
751      my_info.dr_ac=atof(argv[++i]);
752      break;
753     case 'c':
754      my_info.dr_cc=atof(argv[++i]);
755      break;
756     case 'e':
757      my_info.diff_rate=atoi(argv[++i]);
758      break;
759     case 'L':
760      strcpy(l_file,argv[++i]);
761      break;
762     case 'S':
763      strcpy(s_file,argv[++i]);
764      break;
765     case 'R':
766      strcpy(r_file,argv[++i]);
767      break;
768     case 'P':
769      strcpy(p_file,argv[++i]);
770      break;
771     case 'g':
772      strcpy(l_file,argv[++i]);
773      if(i<argc-1) if(argv[i+1][0]!='-') c_step=atoi(argv[++i]);
774      break;
775     default:
776      usage();
777      return -1;
778    }
779   } else usage();
780  }
781
782  x=d3_l.max_x/2-1;
783  y=d3_l.max_y/2-1;
784  z=d3_l.max_z/2-1;
785
786 #ifdef NODFB
787  if(!strcmp(s_file,""))
788  {
789   puts("NODFB defined, run with -S option");
790   return -1;
791  }
792 #endif
793
794  if(!strcmp(r_file,"")) rand_init(NULL);
795  else rand_init(r_file);
796
797  if(!strcmp(l_file,""))
798  {
799   i=d3_l.max_x*d3_l.max_y*d3_l.max_z;
800 #ifdef USE_DFB_API
801   d3_lattice_init(&argc,argv,&d3_l);
802 #endif
803   if((d3_l.status=(unsigned char *)malloc(i*sizeof(unsigned char)))==NULL)
804   {
805    puts("failed allocating status buffer");
806    return -1;
807   }
808   memset(d3_l.status,0,i*sizeof(unsigned char));
809   if((d3_l.extra=(int *)malloc(i*sizeof(int)))==NULL)
810   {
811    puts("failed allocating concentration buffer");
812    return -1;
813   }
814   memset(d3_l.extra,0,i*sizeof(int));
815  } else
816  {
817   load_from_file(l_file,&d3_l,&my_info);
818   if(convert) 
819   {   
820    if(!strcmp(c_file,"")) sprintf(c_file,"%s_gnuplot",l_file);
821    printf("converting file %s to %s\n",l_file,c_file);
822    convert_file(c_file,&d3_l);
823    puts("done");
824    return 1;
825   } 
826 #ifdef USE_DFB_API
827   else d3_lattice_init(&argc,argv,&d3_l);
828 #endif
829  }
830
831 #ifdef USE_DFB_API
832  d3_event_init(&d3_l);
833 #endif
834
835 #ifdef USE_DFB_API
836  strcpy(a_txt,"args:");
837  sprintf(s_txt,"steps: %d",my_info.steps);
838  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));
839  sprintf(r_txt,"pressure range: %d",my_info.range);
840  sprintf(ap_txt,"a_ap: %.3f  b_ap: %.3f",my_info.a_ap,my_info.b_ap);
841  sprintf(el_txt,"a_el: %.3f  b_el: %.3f",my_info.a_el,my_info.b_el);
842  sprintf(cd_txt,"a_cd: %.3f  b_cd: %.3f",my_info.a_cd,my_info.b_cd);
843  sprintf(cp_txt,"a_cp: %.4f",my_info.a_cp);
844  sprintf(dr_ac_txt,"a/c diffusion rate: %.4f",my_info.dr_ac);
845  sprintf(dr_cc_txt,"c/c diffusion rate: %.4f",my_info.dr_cc);
846  sprintf(zdiff_txt,"diffusion in z direction: %c",my_info.z_diff?'y':'n');
847  sprintf(diff_txt,"diffusion every %d steps",my_info.diff_rate);
848  strcpy(mode_txt,"view: a/c mode");
849  arg_v[1]=xyz_txt;
850  arg_v[2]=NULL;
851  arg_v[3]=status_txt;
852  arg_v[4]=conc_txt;
853  arg_v[5]=NULL;
854  arg_v[6]=mode_txt;
855  arg_v[7]=NULL;
856  arg_v[8]=steps_txt;
857  arg_v[9]=cc_txt;
858  arg_v[10]=NULL;
859  arg_v[11]=diff_txt;
860  arg_v[12]=zdiff_txt;
861  arg_v[13]=NULL;
862  arg_v[14]=a_txt;
863  arg_v[15]=s_txt;
864  arg_v[16]=dose_txt;
865  arg_v[17]=NULL;
866  arg_v[18]=r_txt;
867  arg_v[19]=ap_txt;
868  arg_v[20]=el_txt;
869  arg_v[21]=cd_txt;
870  arg_v[22]=cp_txt;
871  arg_v[23]=NULL;
872  arg_v[24]=dr_ac_txt;
873  arg_v[25]=dr_cc_txt;
874 #endif
875
876  if((!strcmp(l_file,""))||(c_step))
877  {
878   if(get_c_ratio(&c_ratio,p_file,&my_info,&d3_l)!=1)
879   {
880    puts("failed calculating ratio");
881    return -1;
882   }
883   i=(c_step?c_step:0);
884   while((i<my_info.steps) && (quit==0) && (escape==0))
885   {
886    x_c=get_rand(d3_l.max_x);
887    y_c=get_rand(d3_l.max_y);
888    z_c=get_rand_lgp(d3_l.max_z,my_info.a_el,my_info.b_el);
889    distrib_c(&d3_l,&my_info,i,c_ratio);
890    process_cell(&d3_l,x_c,y_c,z_c,&my_info);
891 #ifdef USE_DFB_API
892    if(i%refresh==0)
893    {
894     sprintf(xyz_txt,"x: %d  y: %d  z: %d",x+1,y+1,z+1);
895     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');
896     sprintf(conc_txt,"conc: %d",*(d3_l.extra+x+y*d3_l.max_x+z*d3_l.max_x*d3_l.max_y));
897     sprintf(steps_txt,"step: %d  a_count: %d",i,amorph_count);
898     sprintf(cc_txt,"total c: %d  c_count: %d",my_info.cc,cryst_count);
899     d3_lattice_draw(&d3_l,x,y,z,25,arg_v,mode,0);
900    }
901 #endif
902    if(i%resave==0 && strcmp(s_file,"") && resave!=0 && i!=0)
903    {
904     sprintf(s_file_tmp,"%s_%d_of_%d",s_file,i,my_info.steps);
905     save_to_file(s_file_tmp,&d3_l,&my_info);
906 #ifdef NODFB
907     printf("saved %s\n",s_file_tmp);
908 #endif
909    }
910    i++;
911   }
912  }
913
914  if(strcmp(s_file,""))
915  {
916    printf("saved %s\n",s_file);
917    save_to_file(s_file,&d3_l,&my_info);
918  }
919
920 #ifdef USE_DFB_API
921  /* allocating buffer for pressure values */
922  if((d3_l.v_ptr=malloc(d3_l.max_x*d3_l.max_y*d3_l.max_z*sizeof(unsigned char)))==NULL)
923  {
924   puts("cannot allocate buffer for pressure values");
925   return -1;
926  }
927  /* calc values */
928  calc_pressure(&d3_l,my_info.range);
929  max_extra=calc_max_extra(&d3_l);
930
931  while((quit==0) && (escape==0) && (nowait==0))
932  {
933   /* bahh! */
934   if(switchmode==0) mode=0;
935   if(switchmode==1) mode=1;
936   if(switchmode==2) mode=2;
937   /* end of bahh! */
938   sprintf(xyz_txt,"x: %d  y: %d  z: %d",x+1,y+1,z+1);
939   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');
940   sprintf(conc_txt,"conc: %d",*(d3_l.extra+x+y*d3_l.max_x+z*d3_l.max_x*d3_l.max_y));
941   sprintf(steps_txt,"step: end");
942   sprintf(cc_txt,"total c: %d",my_info.cc);
943   if(switchmode==0) strcpy(mode_txt,"view: a/c mode");
944   if(switchmode==1) strcpy(mode_txt,"view: c conc mode");
945   if(switchmode==2) strcpy(mode_txt,"view: a pressure mode");
946   d3_lattice_draw(&d3_l,x,y,z,25,arg_v,mode,max_extra);
947   bmp=0;
948   scan_event(&d3_l,&x,&y,&z,&quit,&escape,&switchmode,&bmp);
949   if(bmp) write_bmp(&d3_l,bmp,x,y,z);
950
951  }
952
953  d3_lattice_release(&d3_l);
954 #endif
955
956  return 1;
957 }