implemented "collisions per ion" variable. new defaults. bugfixed z-diff.
[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_bmp(d3_lattice *d3_l,int window,u32 x,u32 y,u32 z)
332 {
333  int fd,i,j,size=0,foo=0,end=0;
334  int width=0,height=0;
335  char bmpfile[MAX_CHARS];
336  char buf[128];
337
338  if(((window==4)||(window==5))&&(d3_l->max_z<FFT_HEIGHT) )
339  {
340   puts("error: z < FFT_HEIGHT!");
341   puts("not writing bmp file ...");
342   return -1;
343  }
344
345  if(window%3==1)
346  {
347   sprintf(bmpfile,"x-z_%d.bmp",y);
348   foo=3*d3_l->max_x;
349   if(window==1)
350   {
351    size=(foo+(4-foo%4))*d3_l->max_z;
352    height=d3_l->max_z;
353   }
354   else 
355   {
356    size=(foo+(4-foo%4))*FFT_HEIGHT;
357    end=d3_l->max_z-FFT_HEIGHT;
358    height=FFT_HEIGHT;
359   }
360   width=d3_l->max_x;
361  }
362  if(window%3==2)
363  {
364   sprintf(bmpfile,"y-z_%d.bmp",x);
365   foo=3*d3_l->max_y;
366   if(window==2)
367   {
368    size=(foo+(4-foo%4))*d3_l->max_z;
369    height=d3_l->max_z;
370   }
371   else 
372   {
373    size=(foo+(4-foo%4))*FFT_HEIGHT;
374    end=d3_l->max_z-FFT_HEIGHT;
375    height=FFT_HEIGHT;
376   }
377   width=d3_l->max_y;
378  }
379  if(window==3)
380  {
381   sprintf(bmpfile,"x-y_%d.bmp",z);
382   foo=3*d3_l->max_x;
383   size=(foo+(4-foo%4))*d3_l->max_y;
384   width=d3_l->max_x;
385   height=d3_l->max_y;
386  }
387
388  if((fd=open(bmpfile,O_WRONLY|O_CREAT))<0)
389  {
390   puts("cannot open bmp file");
391   return -1;
392  }
393
394  /* bmpheader */
395  buf[0]='B'; /* std header start */
396  buf[1]='M';
397  buf[2]=(size+0x36)&0xff; /* file size */
398  buf[3]=(size+0x36)>>8;
399  memset(buf+4,0,6);
400  buf[10]=0x36; /* offset to data */
401  memset(buf+11,0,3);
402  buf[14]=0x28; /* length of bmp info header */
403  memset(buf+15,0,3);
404  buf[18]=width&0xff; /* width and height */
405  buf[19]=width>>8;
406  memset(buf+20,0,2);
407  buf[22]=height&0xff;
408  buf[23]=height>>8;
409  memset(buf+24,0,2);
410  buf[26]=1; /* # planes -> 1 */
411  buf[27]=0;
412  buf[28]=24; /* bits per pixel -> 2^24 (true color) */
413  buf[29]=0;
414  memset(buf+30,0,4); /* compression -> none */
415  buf[34]=size&0xff; /* data size */
416  buf[35]=size>>8;
417  memset(buf+36,0,2);
418  buf[38]=0x12; /* res: pixel/meter */
419  buf[39]=0x0b;
420  memset(buf+40,0,2);
421  buf[42]=0x12;
422  buf[43]=0x0b;
423  memset(buf+44,0,2); 
424  memset(buf+46,0,8); /* no colors, no important colors */
425
426  if(write(fd,buf,54)<54)
427  {
428   puts("failed writing bmp header");
429   return -1;
430  }
431  if(window%3==1)
432  {
433   for(j=0;j<d3_l->max_z-end;j++)
434   {
435    for(i=0;i<d3_l->max_x;i++)
436    {
437     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);
438     else memset(buf,0,3);
439     if(write(fd,buf,3)<3)
440     {
441      puts("failed writing rgb values to bmp file");
442      return-1;
443     }
444    }
445    if(foo%4)
446    {
447     memset(buf,0,4-foo%4);
448     if(write(fd,buf,4-foo%4)<4-foo%4)
449     {
450      puts("failed writing 4 byte ending");
451      return -1;
452     }
453    } 
454   }
455  }
456  if(window%3==2)
457  {
458   for(j=0;j<d3_l->max_z-end;j++)
459   {
460    for(i=0;i<d3_l->max_y;i++)
461    {
462     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);
463     else memset(buf,0,3);
464     if(write(fd,buf,3)<3)
465     {
466      puts("failed writing rgb values to bmp file");
467      return-1;
468     }
469    }
470    if(foo%4)
471    {
472     memset(buf,0,4-foo%4);
473     if(write(fd,buf,4-foo%4)<4-foo%4)
474     {
475      puts("failed writing 4 byte ending");
476      return -1;
477     }
478    }
479   }
480  }
481  if(window==3)
482  {
483   for(j=0;j<d3_l->max_y;j++)
484   {
485    for(i=0;i<d3_l->max_x;i++)
486    {
487     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);
488     else memset(buf,0,3);
489     if(write(fd,buf,3)<3)
490     {
491      puts("failed writing rgb values to bmp file");
492      return -1;
493     }
494    }
495    if(foo%4)
496    {
497     memset(buf,0,4-foo%4);
498     if(write(fd,buf,4-foo%4)<4-foo%4)
499     {
500      puts("failed writing 4 byte ending");
501      return -1;
502     }
503    }
504   }
505  }
506  close(fd);
507
508  return 1;
509 }
510
511 int save_to_file(char *sf,d3_lattice *d3_l,info *my_inf)
512 {
513  int sf_fd,c;
514
515  if((sf_fd=open(sf,O_WRONLY|O_CREAT))<0)
516  {
517   puts("cannot open save file");
518   return -1;
519  }
520  if(write(sf_fd,d3_l,sizeof(d3_lattice))<sizeof(d3_lattice))
521  {
522   puts("failed saving d3 lattice struct");
523   return -1;
524  }
525  if(write(sf_fd,my_inf,sizeof(info))<sizeof(info))
526  {
527   puts("failed saving info struct");
528   return-1;
529  }
530  c=d3_l->max_x*d3_l->max_y*d3_l->max_z;
531  if(write(sf_fd,d3_l->status,c*sizeof(unsigned char))<c*sizeof(unsigned char))
532  {
533   puts("failed saving status of d3 lattice sites");
534   return -1;
535  }
536  if(write(sf_fd,d3_l->extra,c*sizeof(int))<c*sizeof(int))
537  {
538   puts("failed saving sites concentration");
539   return -1;
540  }
541  close(sf_fd);
542
543  return 1;
544 }
545
546 int load_from_file(char *lf,d3_lattice *d3_l,info *my_inf)
547 {
548  int lf_fd,c;
549
550  if((lf_fd=open(lf,O_RDONLY))<0)
551  {
552   puts("cannot open load file");
553   return -1;
554  }
555  if(read(lf_fd,d3_l,sizeof(d3_lattice))<sizeof(d3_lattice))
556  {
557   puts("failed reading d3 lattice struct");
558   return -1;
559  }
560  if(read(lf_fd,my_inf,sizeof(info))<sizeof(info))
561  {
562   puts("failed reading info struct");
563   return-1;
564  }
565  c=d3_l->max_x*d3_l->max_y*d3_l->max_z;
566  if((d3_l->status=(unsigned char*)malloc(c*sizeof(unsigned char)))==NULL)
567  {
568   puts("cannot allocate status buffer");
569   return -1;
570  }
571  if((d3_l->extra=(int *)malloc(c*sizeof(int)))==NULL)
572  {
573   puts("cannot allocate concentration buffer");
574   return -1;
575  }
576  if(read(lf_fd,d3_l->status,c*sizeof(unsigned char))<c*sizeof(unsigned char))
577  {
578   puts("failed reading status of d3 lattice sites");
579   return -1;
580  }
581  if(read(lf_fd,d3_l->extra,c*sizeof(int))<c*sizeof(int))
582  {
583   puts("failed reading sites concentration");
584   return -1;
585  }
586  close(lf_fd);
587
588  return 1;
589 }
590
591 int convert_file(char *cf,d3_lattice *d3_l)
592 {
593  int x,y,z;
594  int c_fd;
595
596  if((c_fd=open(cf,O_WRONLY|O_CREAT))<0)
597  {
598   puts("cannot open convert file");
599   return -1;
600  }
601  dprintf(c_fd,"# created by nlsop (gnuplot format)\n");
602  for(x=0;x<d3_l->max_x;x++)
603  {
604   for(y=0;y<d3_l->max_y;y++)
605   {
606    for(z=0;z<d3_l->max_z;z++)
607    {
608     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);
609    }
610   }
611  }
612  close(c_fd);
613
614  return 1;
615 }
616
617 int get_c_ratio(double *c_ratio,char *pfile,info *my_info,d3_lattice *d3_l)
618 {
619  double all,a,b,d;
620  int i,k;
621  int p_fd;
622  unsigned char buf[32];
623  char *p;
624  unsigned char c;
625
626  if((p_fd=open(pfile,O_RDONLY))<0)
627  {
628   puts("cannot open profile file");
629   return -1;
630  }
631  k=1;
632  d=0;
633  all=0;
634  while(k)
635  {
636   for(i=0;i<32;i++)
637   {
638    k=read(p_fd,&c,1);
639    buf[i]=c;
640    if(c=='\n') break;
641   }
642   if(k)
643   {
644    p=strtok(buf," ");
645    a=atof(p)/10; /* nm */
646    p=strtok(NULL," ");
647    b=atof(p);
648    if(a>my_info->b_cd*CELL_LENGTH && a<(my_info->b_cd+d3_l->max_z)*CELL_LENGTH) d+=b;
649    all+=b;
650   }
651  }
652  *c_ratio=d/all;
653  close(p_fd);
654
655  return 1;
656 }
657
658 int main(int argc,char **argv)
659 {
660  u32 x,y,z,x_c,y_c,z_c;
661  int i,j,quit,escape,switchmode,nowait,bmp;
662  int refresh,resave;
663  int c_step;
664  char s_file[MAX_CHARS];
665  char s_file_tmp[MAX_CHARS];
666  char l_file[MAX_CHARS];
667  char c_file[MAX_CHARS];
668  char p_file[MAX_CHARS];
669  char convert;
670  char r_file[MAX_CHARS];
671 #ifdef USE_DFB_API
672  char xyz_txt[MAX_TXT];
673  char status_txt[MAX_TXT];
674  char conc_txt[MAX_TXT];
675  char steps_txt[MAX_TXT];
676  char cc_txt[MAX_TXT];
677  char a_txt[MAX_TXT];
678  char s_txt[MAX_TXT];
679  char ap_txt[MAX_TXT];
680  char el_txt[MAX_TXT];
681  char cd_txt[MAX_TXT];
682  char r_txt[MAX_TXT];
683  char cp_txt[MAX_TXT];
684  char zdiff_txt[MAX_TXT];
685  char diff_txt[MAX_TXT];
686  char dr_ac_txt[MAX_TXT];
687  char dr_cc_txt[MAX_TXT];
688  char dose_txt[MAX_TXT];
689  char mode_txt[MAX_TXT];
690  char *arg_v[MAX_ARGV];
691 #endif
692  d3_lattice d3_l;
693  info my_info;
694  unsigned char mode;
695  double c_ratio;
696 #ifdef USE_DFB_API
697  int max_extra;
698 #endif
699
700  d3_l.max_x=X;
701  d3_l.max_y=Y;
702  d3_l.max_z=Z;
703  my_info.steps=STEPS;
704  my_info.range=RANGE;
705  refresh=REFRESH;
706  resave=RESAVE;
707  my_info.z_diff=0;
708  my_info.c_diff=1;
709  my_info.a_el=A_EL;
710  my_info.b_el=B_EL;
711  my_info.a_cd=A_CD;
712  my_info.b_cd=B_CD;
713  my_info.a_ap=A_AP;
714  my_info.b_ap=B_AP;
715  my_info.a_cp=A_CP;
716  my_info.cc=CC;
717  my_info.dr_ac=DR_AC;
718  my_info.dr_cc=DR_CC;
719  my_info.diff_rate=DIFF_RATE;
720  my_info.cpi=CPI;
721  nowait=0;
722  quit=0;
723  escape=0;
724  switchmode=0;
725  c_step=0;
726  strcpy(s_file,"");
727  strcpy(l_file,"");
728  strcpy(c_file,"");
729  strcpy(p_file,IMP_PROFILE);
730  convert=0;
731  strcpy(r_file,"");
732  mode=0;
733
734  for(i=1;i<argc;i++)
735  {
736   if(argv[i][0]=='-')
737   {
738    switch(argv[i][1])
739    {
740     case 'h':
741      usage();
742      return -1;
743     case 'n':
744      nowait=1;
745      break;
746     case 'a':
747      my_info.a_el=atof(argv[++i]);
748      break;
749     case 'b':
750      my_info.b_el=atof(argv[++i]);
751      break;
752     case 'x':
753      d3_l.max_x=atoi(argv[++i]);
754      break;
755     case 'y':
756      d3_l.max_y=atoi(argv[++i]);
757      break;
758     case 'z':
759      d3_l.max_z=atoi(argv[++i]);
760      break;
761     case 'Z':
762      my_info.z_diff=1;
763      break;
764     case 'i':
765      my_info.c_diff=0;
766      my_info.dr_cc=0;
767      break;
768     case 's':
769      my_info.steps=atoi(argv[++i]);
770      break;
771     case 'd':
772      refresh=atoi(argv[++i]);
773      break;
774     case 'r':
775      my_info.range=atoi(argv[++i]);
776      break;
777     case 'f':
778      my_info.a_ap=atof(argv[++i]);
779      break;
780     case 'p':
781      my_info.b_ap=atof(argv[++i]);
782      break;
783     case 'F':
784      my_info.a_cp=atof(argv[++i]);
785      break;
786     case 'A':
787      my_info.a_cd=atof(argv[++i]);
788      break;
789     case 'B':
790      my_info.b_cd=atof(argv[++i]);
791      break;
792     case 'W':
793      resave=atoi(argv[++i]);
794      break;
795     case 'C':
796      strcpy(l_file,argv[++i]);
797      if(i<argc-1) if(argv[i+1][0]!='-') strcpy(c_file,argv[++i]);
798      convert=1;
799      break;
800     case 'D':
801      my_info.dr_ac=atof(argv[++i]);
802      break;
803     case 'c':
804      my_info.dr_cc=atof(argv[++i]);
805      break;
806     case 'e':
807      my_info.diff_rate=atoi(argv[++i]);
808      break;
809     case 'L':
810      strcpy(l_file,argv[++i]);
811      break;
812     case 'S':
813      strcpy(s_file,argv[++i]);
814      break;
815     case 'R':
816      strcpy(r_file,argv[++i]);
817      break;
818     case 'P':
819      strcpy(p_file,argv[++i]);
820      break;
821     case 'g':
822      strcpy(l_file,argv[++i]);
823      if(i<argc-1) if(argv[i+1][0]!='-') c_step=atoi(argv[++i]);
824      break;
825     case 'H':
826      my_info.cpi=atoi(argv[++i]);
827      break;
828     default:
829      usage();
830      return -1;
831    }
832   } else usage();
833  }
834
835  x=d3_l.max_x/2-1;
836  y=d3_l.max_y/2-1;
837  z=d3_l.max_z/2-1;
838
839 #ifdef NODFB
840  if(!strcmp(s_file,""))
841  {
842   puts("NODFB defined, run with -S option");
843   return -1;
844  }
845 #endif
846
847  if(!strcmp(r_file,"")) rand_init(NULL);
848  else rand_init(r_file);
849
850  if(!strcmp(l_file,""))
851  {
852   i=d3_l.max_x*d3_l.max_y*d3_l.max_z;
853 #ifdef USE_DFB_API
854   d3_lattice_init(&argc,argv,&d3_l);
855 #endif
856   if((d3_l.status=(unsigned char *)malloc(i*sizeof(unsigned char)))==NULL)
857   {
858    puts("failed allocating status buffer");
859    return -1;
860   }
861   memset(d3_l.status,0,i*sizeof(unsigned char));
862   if((d3_l.extra=(int *)malloc(i*sizeof(int)))==NULL)
863   {
864    puts("failed allocating concentration buffer");
865    return -1;
866   }
867   memset(d3_l.extra,0,i*sizeof(int));
868  } else
869  {
870   load_from_file(l_file,&d3_l,&my_info);
871   if(convert) 
872   {   
873    if(!strcmp(c_file,"")) sprintf(c_file,"%s_gnuplot",l_file);
874    printf("converting file %s to %s\n",l_file,c_file);
875    convert_file(c_file,&d3_l);
876    puts("done");
877    return 1;
878   } 
879 #ifdef USE_DFB_API
880   else d3_lattice_init(&argc,argv,&d3_l);
881 #endif
882  }
883
884 #ifdef USE_DFB_API
885  d3_event_init(&d3_l);
886 #endif
887
888 #ifdef USE_DFB_API
889  strcpy(a_txt,"args:");
890  sprintf(s_txt,"steps: %d",my_info.steps);
891  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));
892  sprintf(r_txt,"pressure range: %d",my_info.range);
893  sprintf(ap_txt,"a_ap: %.3f  b_ap: %.3f",my_info.a_ap,my_info.b_ap);
894  sprintf(el_txt,"a_el: %.3f  b_el: %.3f",my_info.a_el,my_info.b_el);
895  sprintf(cd_txt,"a_cd: %.3f  b_cd: %.3f",my_info.a_cd,my_info.b_cd);
896  sprintf(cp_txt,"a_cp: %.4f",my_info.a_cp);
897  sprintf(dr_ac_txt,"a/c diffusion rate: %.4f",my_info.dr_ac);
898  if(my_info.c_diff!=0) sprintf(dr_cc_txt,"c/c diffusion rate: %.4f",my_info.dr_cc);
899  else sprintf(dr_cc_txt,"c/c diffusion rate: none");
900  sprintf(zdiff_txt,"diffusion in z direction: %c",my_info.z_diff?'y':'n');
901  sprintf(diff_txt,"diffusion every %d steps",my_info.diff_rate);
902  strcpy(mode_txt,"view: a/c mode");
903  arg_v[1]=xyz_txt;
904  arg_v[2]=NULL;
905  arg_v[3]=status_txt;
906  arg_v[4]=conc_txt;
907  arg_v[5]=NULL;
908  arg_v[6]=mode_txt;
909  arg_v[7]=NULL;
910  arg_v[8]=steps_txt;
911  arg_v[9]=cc_txt;
912  arg_v[10]=NULL;
913  arg_v[11]=diff_txt;
914  arg_v[12]=zdiff_txt;
915  arg_v[13]=NULL;
916  arg_v[14]=a_txt;
917  arg_v[15]=s_txt;
918  arg_v[16]=dose_txt;
919  arg_v[17]=NULL;
920  arg_v[18]=r_txt;
921  arg_v[19]=ap_txt;
922  arg_v[20]=el_txt;
923  arg_v[21]=cd_txt;
924  arg_v[22]=cp_txt;
925  arg_v[23]=NULL;
926  arg_v[24]=dr_ac_txt;
927  arg_v[25]=dr_cc_txt;
928 #endif
929
930  if((!strcmp(l_file,""))||(c_step))
931  {
932   if(get_c_ratio(&c_ratio,p_file,&my_info,&d3_l)!=1)
933   {
934    puts("failed calculating ratio");
935    return -1;
936   }
937   i=(c_step?c_step:0);
938   while((i<my_info.steps) && (quit==0) && (escape==0))
939   {
940    for(j=0;j<my_info.cpi;j++)
941    {
942     x_c=get_rand(d3_l.max_x);
943     y_c=get_rand(d3_l.max_y);
944     z_c=get_rand_lgp(d3_l.max_z,my_info.a_el,my_info.b_el);
945     process_cell(&d3_l,x_c,y_c,z_c,&my_info);
946    }
947    distrib_c(&d3_l,&my_info,i,c_ratio);
948 #ifdef USE_DFB_API
949    if(i%refresh==0)
950    {
951     sprintf(xyz_txt,"x: %d  y: %d  z: %d",x+1,y+1,z+1);
952     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');
953     sprintf(conc_txt,"conc: %d",*(d3_l.extra+x+y*d3_l.max_x+z*d3_l.max_x*d3_l.max_y));
954     sprintf(steps_txt,"step: %d",i);
955     sprintf(cc_txt,"total c: %d",my_info.cc);
956     d3_lattice_draw(&d3_l,x,y,z,25,arg_v,mode,0);
957    }
958 #endif
959    if(i%resave==0 && strcmp(s_file,"") && resave!=0 && i!=0)
960    {
961     sprintf(s_file_tmp,"%s_%d_of_%d",s_file,i,my_info.steps);
962     save_to_file(s_file_tmp,&d3_l,&my_info);
963 #ifdef NODFB
964     printf("saved %s\n",s_file_tmp);
965 #endif
966    }
967    i++;
968   }
969  }
970
971  if(strcmp(s_file,""))
972  {
973    printf("saved %s\n",s_file);
974    save_to_file(s_file,&d3_l,&my_info);
975  }
976
977 #ifdef USE_DFB_API
978  /* allocating buffer for pressure values */
979  if((d3_l.v_ptr=malloc(d3_l.max_x*d3_l.max_y*d3_l.max_z*sizeof(unsigned char)))==NULL)
980  {
981   puts("cannot allocate buffer for pressure values");
982   return -1;
983  }
984  /* calc values */
985  calc_pressure(&d3_l,my_info.range);
986  max_extra=calc_max_extra(&d3_l);
987
988  while((quit==0) && (escape==0) && (nowait==0))
989  {
990   /* bahh! */
991   if(switchmode==0) mode=0;
992   if(switchmode==1) mode=1;
993   if(switchmode==2) mode=2;
994   /* end of bahh! */
995   sprintf(xyz_txt,"x: %d  y: %d  z: %d",x+1,y+1,z+1);
996   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');
997   sprintf(conc_txt,"conc: %d",*(d3_l.extra+x+y*d3_l.max_x+z*d3_l.max_x*d3_l.max_y));
998   sprintf(steps_txt,"step: end");
999   sprintf(cc_txt,"total c: %d",my_info.cc);
1000   if(switchmode==0) strcpy(mode_txt,"view: a/c mode");
1001   if(switchmode==1) strcpy(mode_txt,"view: c conc mode");
1002   if(switchmode==2) strcpy(mode_txt,"view: a pressure mode");
1003   d3_lattice_draw(&d3_l,x,y,z,25,arg_v,mode,max_extra);
1004   bmp=0;
1005   scan_event(&d3_l,&x,&y,&z,&quit,&escape,&switchmode,&bmp);
1006   if(bmp) write_bmp(&d3_l,bmp,x,y,z);
1007
1008  }
1009
1010  d3_lattice_release(&d3_l);
1011 #endif
1012
1013  return 1;
1014 }