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