changed layout of info box
[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  puts("-N <file> \t specify nuclear energy loss profile file");
82  printf("-H <value> \t collisions per ion in simulation window (default %d)\n",CPI);
83  puts("-m <value> \t specify c->a carbon saturation");
84  
85  return 1;
86 }
87
88 int process_cell(d3_lattice *d3_l,u32 x,u32 y,u32 z,info *my_info)
89 {
90  unsigned char *thiz;
91  int *conc;
92  int i,j;
93  int off;
94  double p;
95
96  thiz=d3_l->status+x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y;
97  conc=d3_l->extra+x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y;
98  p=my_info->b_ap*URAND_MAX;
99  for(i=-(my_info->range);i<=my_info->range;i++)
100  {
101   for(j=-(my_info->range);j<=my_info->range;j++)
102   {
103    if(!(i==0 && j==0))
104    {
105     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;
106     if(*(d3_l->status+off)&AMORPH) p+=my_info->a_ap*(*(d3_l->extra+off))*URAND_MAX/(i*i+j*j);
107    } 
108   }
109  }
110  p+=*conc*my_info->a_cp*URAND_MAX;
111  if(!(*thiz&AMORPH))
112  {
113   if(get_rand(URAND_MAX)<=p) MAKE_AMORPH(thiz);
114  } else
115  {
116   /* assume 1-p probability */
117   if(get_rand(URAND_MAX)>p) MAKE_CRYST(thiz);
118  }
119  
120  return 1;
121 }
122
123 int distrib_c(d3_lattice *d3_l,info *my_info,int step,double c_ratio,u32 rj_m,u32 *rj_g)
124 {
125  u32 x,y,z;
126  int i,j,k,c;
127  int offset,off;
128  int carry;
129
130  /* put one c ion somewhere in the lattice */
131  if(my_info->cc<c_ratio*step)
132  {
133   x=get_rand(d3_l->max_x);
134   y=get_rand(d3_l->max_y);
135   z=get_rand_reject(d3_l->max_z,rj_m,rj_g);
136   *(d3_l->extra+x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y)+=1;
137   (my_info->cc)++;
138  }
139
140  if(step%my_info->diff_rate==0)
141  {
142
143  for(i=0;i<d3_l->max_x;i++)
144  {
145   for(j=0;j<d3_l->max_y;j++)
146   {
147    for(k=0;k<d3_l->max_z;k++)
148    {
149     offset=i+j*d3_l->max_x+k*d3_l->max_x*d3_l->max_y;
150     /* case amorph: amorph <- cryst diffusion */
151     if(*(d3_l->status+offset)&AMORPH && *(d3_l->extra+offset)<my_info->c_sat)
152     {
153      for(c=-1;c<=1;c++)
154      {
155       if(c!=0)
156       {
157        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;
158        carry=0;
159        if(!(*(d3_l->status+off)&AMORPH)) carry=(int)(my_info->dr_ac*(*(d3_l->extra+off)));
160        if(carry!=0)
161        {
162         *(d3_l->extra+offset)+=carry;
163         *(d3_l->extra+off)-=carry;
164        }
165       }
166      }
167      for(c=-1;c<=1;c++)
168      {
169       if(c!=0)
170       {
171        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;
172        carry=0;
173        if(!(*(d3_l->status+off)&AMORPH)) carry=(int)(my_info->dr_ac*(*(d3_l->extra+off)));
174        if(carry!=0)
175        {
176         *(d3_l->extra+offset)+=carry; 
177         *(d3_l->extra+off)-=carry; 
178        }
179       }
180      }
181      if(my_info->z_diff)
182      {
183       if(k!=0)
184       {
185        off=i+j*d3_l->max_x+(k-1)*d3_l->max_x*d3_l->max_y;
186        carry=0;
187        if(!*(d3_l->status+off)&AMORPH) carry=(int)(my_info->dr_ac*(*(d3_l->extra+off)));
188        if(carry!=0)
189        {
190         *(d3_l->extra+off)-=carry;
191         *(d3_l->extra+offset)+=carry;
192        }
193       }
194       if(k!=d3_l->max_z-1)
195       {
196        off=i+j*d3_l->max_x+(k+1)*d3_l->max_x*d3_l->max_y;
197        carry=0;
198        if(!*(d3_l->status+off)&AMORPH) carry=(int)(my_info->dr_ac*(*(d3_l->extra+off)));
199        if(carry!=0)
200        {
201         *(d3_l->extra+off)-=carry;
202         *(d3_l->extra+offset)+=carry;
203        }
204       }
205      }  
206     } else
207     /* case not amorph: cryst <-> cryst diffusion */
208     if(my_info->c_diff) {
209     /* if there is c diff, no diff in z-direction */
210     {
211      for(c=-1;c<=1;c++) 
212      {
213       if(c!=0)
214       {
215        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;
216        carry=0;
217        if(!(*(d3_l->status+off)&AMORPH))
218        {
219         carry=(int)(my_info->dr_cc*(*(d3_l->extra+off)-*(d3_l->extra+offset))/2);
220         if(carry!=0)
221         {
222          *(d3_l->extra+offset)+=carry;
223          *(d3_l->extra+off)-=carry;
224         }
225        }
226       }
227      }
228      for(c=-1;c<=1;c++)
229      {
230       if(c!=0)
231       {
232        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;
233        carry=0;
234        if(!(*(d3_l->status+off)&AMORPH))
235        {
236         carry=(int)(my_info->dr_cc*(*(d3_l->extra+off)-*(d3_l->extra+offset))/2);
237         if(carry!=0)
238         {
239          *(d3_l->extra+offset)+=carry;
240          *(d3_l->extra+off)-=carry;
241         }
242        }
243       }
244      }
245     }
246     /* end test */
247     }
248     /* */
249    } /* for z */
250   } /* for y */
251  } /* for x */
252
253  } /* if step modulo diff_rate == 0 */
254
255  return 1;
256 }
257
258 int calc_pressure(d3_lattice *d3_l,int range)
259 {
260  int i,j,off;
261  double count,max=0;
262  int x,y,z;
263
264  for(x=0;x<d3_l->max_x;x++)
265  {
266   for(y=0;y<d3_l->max_y;y++)
267   {
268    for(z=0;z<d3_l->max_z;z++)
269    {
270     count=0;
271     for(i=-range;i<=range;i++)
272     {
273      for(j=-range;j<=range;j++)
274      {
275       if(i!=0 && j!=0)
276       {
277        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;
278        if(*(d3_l->status+off)&AMORPH) count+=((double)*(d3_l->extra+off))/(i*i+j*j);
279       }
280      }
281     }
282     if(count>max) max=count;
283    }
284   }
285  }
286
287  for(x=0;x<d3_l->max_x;x++)
288  {
289   for(y=0;y<d3_l->max_y;y++)
290   {
291    for(z=0;z<d3_l->max_z;z++)
292    {
293     count=0;
294     for(i=-range;i<=range;i++)
295     {
296      for(j=-range;j<=range;j++)
297      {
298       if(i!=0 && j!=0)
299       {
300        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;
301        if(*(d3_l->status+off)&AMORPH) count+=((double)*(d3_l->extra+off))/(i*i+j*j);
302       }
303      }
304     }
305     *(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);
306    }
307   }
308  }
309
310  return 1;
311 }
312
313 int calc_max_extra(d3_lattice *d3_l)
314 {
315  int x,y,z;
316  int off,max=0;
317
318  for(x=0;x<d3_l->max_x;x++)
319  {
320   for(y=0;y<d3_l->max_y;y++)
321   {
322    for(z=0;z<d3_l->max_z;z++)
323    {
324     off=x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y;
325     if(*(d3_l->extra+off)>max) max=*(d3_l->extra+off);
326    }
327   }
328  }
329
330  return max;
331 }
332
333 int write_ac_distr(d3_lattice *d3_l,int ac_distr)
334 {
335  int fd,x,y,z;
336  int count,offset;
337  char file[16];
338
339  if(ac_distr==1) strcpy(file,"a.plot");
340  if(ac_distr==2) strcpy(file,"c.plot");
341  if(ac_distr==3) strcpy(file,"b.plot");
342
343  if((fd=open(file,O_WRONLY|O_CREAT))<0)
344  {
345   puts("cannot open plot file");
346   return -1;
347  }
348
349  for(z=0;z<d3_l->max_z;z++)
350  {
351   count=0;
352   for(x=0;x<d3_l->max_x;x++)
353   {
354    for(y=0;y<d3_l->max_y;y++)
355    {
356     offset=x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y;
357     if(ac_distr==1)
358      if(*(d3_l->status+offset)&AMORPH) count+=*(d3_l->extra+offset);
359     if(ac_distr==2)
360      if(!(*(d3_l->status+offset)&AMORPH)) count+=*(d3_l->extra+offset);
361     if(ac_distr==3) count+=*(d3_l->extra+offset);
362    }
363   }
364   dprintf(fd,"%d %d\n",z,count);
365  }
366  close(fd);
367  
368  return 1;
369 }
370
371 int write_bmp(d3_lattice *d3_l,int window,u32 x,u32 y,u32 z,int max)
372 {
373  int fd,i,j,size=0,foo=0,k,sum;
374  int width=0,height=0;
375  char bmpfile[MAX_CHARS];
376  char buf[128];
377
378  if(window==1)
379  {
380   sprintf(bmpfile,"x-z_%d.bmp",y);
381   foo=3*d3_l->max_x;
382   size=(foo+(4-foo%4))*d3_l->max_z;
383   height=d3_l->max_z;
384   width=d3_l->max_x;
385  }
386  if(window==2)
387  {
388   sprintf(bmpfile,"y-z_%d.bmp",x);
389   foo=3*d3_l->max_y;
390   size=(foo+(4-foo%4))*d3_l->max_z;
391   height=d3_l->max_z;
392   width=d3_l->max_y;
393  }
394  if(window==3)
395  {
396   sprintf(bmpfile,"x-y_%d.bmp",z);
397   foo=3*d3_l->max_x;
398   size=(foo+(4-foo%4))*d3_l->max_y;
399   width=d3_l->max_x;
400   height=d3_l->max_y;
401  }
402  if(window==4)
403  {
404   sprintf(bmpfile,"x-z_cdistr_%d.bmp",y);
405   foo=3*d3_l->max_x;
406   size=(foo+(4-foo%4))*d3_l->max_z;
407   height=d3_l->max_z;
408   width=d3_l->max_x;
409  }
410  if(window==5)
411  {
412   sprintf(bmpfile,"y-z_cdistr_%d.bmp",x);
413   foo=3*d3_l->max_y;
414   size=(foo+(4-foo%4))*d3_l->max_z;
415   height=d3_l->max_z;
416   width=d3_l->max_y;
417  }
418  if(window==6)
419  {
420   sprintf(bmpfile,"x-y_cdistr_%d.bmp",z);
421   foo=3*d3_l->max_x;
422   size=(foo+(4-foo%4))*d3_l->max_y;
423   width=d3_l->max_x;
424   height=d3_l->max_y;
425  }
426
427  if((fd=open(bmpfile,O_WRONLY|O_CREAT))<0)
428  {
429   puts("cannot open bmp file");
430   return -1;
431  }
432
433  /* bmpheader */
434  buf[0]='B'; /* std header start */
435  buf[1]='M';
436  buf[2]=(size+0x36)&0xff; /* file size */
437  buf[3]=(size+0x36)>>8;
438  memset(buf+4,0,6);
439  buf[10]=0x36; /* offset to data */
440  memset(buf+11,0,3);
441  buf[14]=0x28; /* length of bmp info header */
442  memset(buf+15,0,3);
443  buf[18]=width&0xff; /* width and height */
444  buf[19]=width>>8;
445  memset(buf+20,0,2);
446  buf[22]=height&0xff;
447  buf[23]=height>>8;
448  memset(buf+24,0,2);
449  buf[26]=1; /* # planes -> 1 */
450  buf[27]=0;
451  buf[28]=24; /* bits per pixel -> 2^24 (true color) */
452  buf[29]=0;
453  memset(buf+30,0,4); /* compression -> none */
454  buf[34]=size&0xff; /* data size */
455  buf[35]=size>>8;
456  memset(buf+36,0,2);
457  buf[38]=0x12; /* res: pixel/meter */
458  buf[39]=0x0b;
459  memset(buf+40,0,2);
460  buf[42]=0x12;
461  buf[43]=0x0b;
462  memset(buf+44,0,2); 
463  memset(buf+46,0,8); /* no colors, no important colors */
464
465  if(write(fd,buf,54)<54)
466  {
467   puts("failed writing bmp header");
468   return -1;
469  }
470  if(window==1)
471  {
472   for(j=0;j<d3_l->max_z;j++)
473   {
474    for(i=0;i<d3_l->max_x;i++)
475    {
476     sum=0;
477     for(k=-2;k<=2;k++)
478      if(*(d3_l->status+i+(((y+k+d3_l->max_y)%d3_l->max_y)*d3_l->max_x)+(d3_l->max_z-j-1)*d3_l->max_x*d3_l->max_y)&RED) sum+=0xff;
479     sum/=5;
480     memset(buf,(unsigned char)sum,3);
481     if(write(fd,buf,3)<3)
482     {
483      puts("failed writing rgb values to bmp file");
484      return-1;
485     }
486    }
487    if(foo%4)
488    {
489     memset(buf,0,4-foo%4);
490     if(write(fd,buf,4-foo%4)<4-foo%4)
491     {
492      puts("failed writing 4 byte ending");
493      return -1;
494     }
495    } 
496   }
497  }
498  if(window==2)
499  {
500   for(j=0;j<d3_l->max_z;j++)
501   {
502    for(i=0;i<d3_l->max_y;i++)
503    {
504     sum=0;
505     for(k=-2;k<=2;k++)
506      if(*(d3_l->status+((x+k+d3_l->max_x)%d3_l->max_x)+i*d3_l->max_x+(d3_l->max_z-j-1)*d3_l->max_x*d3_l->max_y)&RED) sum+=0xff;
507     sum/=5;
508     memset(buf,(unsigned char)sum,3);
509     if(write(fd,buf,3)<3)
510     {
511      puts("failed writing rgb values to bmp file");
512      return-1;
513     }
514    }
515    if(foo%4)
516    {
517     memset(buf,0,4-foo%4);
518     if(write(fd,buf,4-foo%4)<4-foo%4)
519     {
520      puts("failed writing 4 byte ending");
521      return -1;
522     }
523    }
524   }
525  }
526  if(window==3)
527  {
528   for(j=0;j<d3_l->max_y;j++)
529   {
530    for(i=0;i<d3_l->max_x;i++)
531    {
532     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);
533     else memset(buf,0,3);
534     if(write(fd,buf,3)<3)
535     {
536      puts("failed writing rgb values to bmp file");
537      return -1;
538     }
539    }
540    if(foo%4)
541    {
542     memset(buf,0,4-foo%4);
543     if(write(fd,buf,4-foo%4)<4-foo%4)
544     {
545      puts("failed writing 4 byte ending");
546      return -1;
547     }
548    }
549   }
550  }
551  if(window==4)
552  {
553   for(j=0;j<d3_l->max_z;j++)
554   {
555    for(i=0;i<d3_l->max_x;i++)
556    {
557     sum=*(d3_l->extra+i+y*d3_l->max_x+(d3_l->max_z-j-1)*d3_l->max_x*d3_l->max_y);
558     sum=sum*255/max;
559     memset(buf,(unsigned char)sum,3);
560     if(write(fd,buf,3)<3)
561     {
562      puts("failed writing rgb values to bmp file");
563      return-1;
564     }
565    }
566    if(foo%4)
567    {
568     memset(buf,0,4-foo%4);
569     if(write(fd,buf,4-foo%4)<4-foo%4)
570     {
571      puts("failed writing 4 byte ending");
572      return -1;
573     }
574    }
575   }
576  }
577  if(window==5)
578  { 
579   for(j=0;j<d3_l->max_z;j++) 
580   {                     
581    for(i=0;i<d3_l->max_x;i++)
582    {
583     sum=*(d3_l->extra+x+i*d3_l->max_x+(d3_l->max_z-j-1)*d3_l->max_x*d3_l->max_y);
584     sum=sum*255/max;
585     memset(buf,(unsigned char)sum,3);
586     if(write(fd,buf,3)<3)
587     {
588      puts("failed writing rgb values to bmp file");
589      return-1;  
590     }
591    }
592    if(foo%4)
593    {
594     memset(buf,0,4-foo%4);
595     if(write(fd,buf,4-foo%4)<4-foo%4)
596     {
597      puts("failed writing 4 byte ending");
598      return -1;
599     }
600    }
601   }
602  }
603  if(window==6)
604  {   
605   for(j=0;j<d3_l->max_z;j++)
606   {  
607    for(i=0;i<d3_l->max_x;i++)
608    { 
609     sum=*(d3_l->extra+i+(d3_l->max_y-j-1)*d3_l->max_x+z*d3_l->max_x*d3_l->max_y);
610     sum=sum*255/max;
611     memset(buf,(unsigned char)sum,3);
612     if(write(fd,buf,3)<3)
613     {
614      puts("failed writing rgb values to bmp file");
615      return-1;
616     }
617    } 
618    if(foo%4)
619    { 
620     memset(buf,0,4-foo%4);
621     if(write(fd,buf,4-foo%4)<4-foo%4)
622     {
623      puts("failed writing 4 byte ending");
624      return -1;
625     }
626    } 
627   }
628  }
629  close(fd);
630
631  return 1;
632 }
633
634 int save_to_file(char *sf,d3_lattice *d3_l,info *my_inf)
635 {
636  int sf_fd,c;
637
638  if((sf_fd=open(sf,O_WRONLY|O_CREAT))<0)
639  {
640   puts("cannot open save file");
641   return -1;
642  }
643  if(write(sf_fd,d3_l,sizeof(d3_lattice))<sizeof(d3_lattice))
644  {
645   puts("failed saving d3 lattice struct");
646   return -1;
647  }
648  if(write(sf_fd,my_inf,sizeof(info))<sizeof(info))
649  {
650   puts("failed saving info struct");
651   return-1;
652  }
653  c=d3_l->max_x*d3_l->max_y*d3_l->max_z;
654  if(write(sf_fd,d3_l->status,c*sizeof(unsigned char))<c*sizeof(unsigned char))
655  {
656   puts("failed saving status of d3 lattice sites");
657   return -1;
658  }
659  if(write(sf_fd,d3_l->extra,c*sizeof(int))<c*sizeof(int))
660  {
661   puts("failed saving sites concentration");
662   return -1;
663  }
664  close(sf_fd);
665
666  return 1;
667 }
668
669 int load_from_file(char *lf,d3_lattice *d3_l,info *my_inf)
670 {
671  int lf_fd,c;
672
673  if((lf_fd=open(lf,O_RDONLY))<0)
674  {
675   puts("cannot open load file");
676   return -1;
677  }
678  if(read(lf_fd,d3_l,sizeof(d3_lattice))<sizeof(d3_lattice))
679  {
680   puts("failed reading d3 lattice struct");
681   return -1;
682  }
683  if(read(lf_fd,my_inf,sizeof(info))<sizeof(info))
684  {
685   puts("failed reading info struct");
686   return-1;
687  }
688  c=d3_l->max_x*d3_l->max_y*d3_l->max_z;
689  if((d3_l->status=(unsigned char*)malloc(c*sizeof(unsigned char)))==NULL)
690  {
691   puts("cannot allocate status buffer");
692   return -1;
693  }
694  if((d3_l->extra=(int *)malloc(c*sizeof(int)))==NULL)
695  {
696   puts("cannot allocate concentration buffer");
697   return -1;
698  }
699  if(read(lf_fd,d3_l->status,c*sizeof(unsigned char))<c*sizeof(unsigned char))
700  {
701   puts("failed reading status of d3 lattice sites");
702   return -1;
703  }
704  if(read(lf_fd,d3_l->extra,c*sizeof(int))<c*sizeof(int))
705  {
706   puts("failed reading sites concentration");
707   return -1;
708  }
709  close(lf_fd);
710
711  return 1;
712 }
713
714 int convert_file(char *cf,d3_lattice *d3_l)
715 {
716  int x,y,z;
717  int c_fd;
718
719  if((c_fd=open(cf,O_WRONLY|O_CREAT))<0)
720  {
721   puts("cannot open convert file");
722   return -1;
723  }
724  dprintf(c_fd,"# created by nlsop (gnuplot format)\n");
725  for(x=0;x<d3_l->max_x;x++)
726  {
727   for(y=0;y<d3_l->max_y;y++)
728   {
729    for(z=0;z<d3_l->max_z;z++)
730    {
731     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);
732    }
733   }
734  }
735  close(c_fd);
736
737  return 1;
738 }
739
740 int get_c_ratio(double *c_ratio,char *pfile,info *my_info,d3_lattice *d3_l)
741 {
742  double all,a,b,d;
743  int i,k;
744  int p_fd;
745  unsigned char buf[32];
746  char *p;
747  unsigned char c;
748
749  if((p_fd=open(pfile,O_RDONLY))<0)
750  {
751   puts("cannot open profile file");
752   return -1;
753  }
754  k=1;
755  d=0;
756  all=0;
757  while(k)
758  {
759   for(i=0;i<32;i++)
760   {
761    k=read(p_fd,&c,1);
762    buf[i]=c;
763    if(c=='\n') break;
764   }
765   if(k)
766   {
767    p=strtok(buf," ");
768    a=atof(p)/10; /* nm */
769    p=strtok(NULL," ");
770    b=atof(p);
771    if(a>my_info->b_cd*CELL_LENGTH && a<(my_info->b_cd+d3_l->max_z)*CELL_LENGTH) d+=b;
772    all+=b;
773   }
774  }
775  *c_ratio=d/all;
776  close(p_fd);
777
778  return 1;
779 }
780
781 u32 get_reject_graph(info *my_info,d3_lattice *d3_l,char *file,u32 *graph) {
782  double a,b;
783  int i,j,k;
784  int fd;
785  char buf[32],*p;
786  unsigned char *flag;
787  u32 max;
788
789  max=0;
790  if((fd=open(file,O_RDONLY))<0)
791  {
792   puts("cannot open file to calculate rejection graph");
793   return -1;
794  }
795  if((flag=(unsigned char *)malloc(d3_l->max_z))==NULL)
796  {
797   puts("cannot malloc flag memory for rejection graph");
798   return -1;
799  }
800  memset(flag,0,d3_l->max_z);
801  memset(graph,0,d3_l->max_z*sizeof(u32));
802  /* get fixpoints */
803  k=1;
804  while(k)
805  {
806   for(i=0;i<32;i++)
807   {
808    k=read(fd,&buf[i],1);
809    if((buf[i]=='\n')||(k==0)) break;
810   }
811   if(k)
812   {
813    p=strtok(buf," ");
814    a=atof(p)/10; /* nm */
815    p=strtok(NULL," ");
816    b=atof(p);
817    if(a>d3_l->max_z*CELL_LENGTH) k=0;
818    else 
819    {
820     graph[(int)(a/CELL_LENGTH)]=(int)(URAND_MAX*b);
821     flag[(int)(a/CELL_LENGTH)]=1;
822    }
823   }
824  }
825  /* do (linear) interpolation here! */
826  i=0;
827  a=0;
828  while(i<d3_l->max_z)
829  {
830   /* graph[0] is 0! */
831   j=i;
832   i++;
833   while(flag[i]==0&&i<d3_l->max_z) i++;
834   for(k=j+1;k<i;k++) graph[k]=(int)((k-j)*((int)graph[i]-(int)graph[j])/(i-j))+graph[j];
835   if(graph[i]>max) max=graph[i];
836  }
837
838  free(flag);
839
840 #ifdef DEBUG_INTERPOL_PROFILE
841  printf("debug: (interpolated c profile)\n");
842  for(i=0;i<d3_l->max_z;i++) printf("%d %d\n",i,graph[i]);
843 #endif
844
845  return max;
846 }
847
848 int main(int argc,char **argv)
849 {
850  u32 x,y,z,x_c,y_c,z_c;
851  int i,j,quit,escape,switchmode,nowait,bmp,ac_distr;
852  int refresh,resave;
853  int c_step;
854  char s_file[MAX_CHARS];
855  char s_file_tmp[MAX_CHARS];
856  char l_file[MAX_CHARS];
857  char c_file[MAX_CHARS];
858  char p_file[MAX_CHARS];
859  char n_e_file[MAX_CHARS];
860  char convert;
861  char r_file[MAX_CHARS];
862 #ifdef USE_DFB_API
863  char xyz_txt[MAX_TXT];
864  char status_txt[MAX_TXT];
865  char conc_txt[MAX_TXT];
866  char steps_txt[MAX_TXT];
867  char cc_txt[MAX_TXT];
868  char a_txt[MAX_TXT];
869  char s_txt[MAX_TXT];
870  char ap_txt[MAX_TXT];
871  char el_txt[MAX_TXT];
872  char cd_txt[MAX_TXT];
873  char r_txt[MAX_TXT];
874  char cp_txt[MAX_TXT];
875  char zdiff_txt[MAX_TXT];
876  char diff_txt[MAX_TXT];
877  char dr_ac_txt[MAX_TXT];
878  char dr_cc_txt[MAX_TXT];
879  char dose_txt[MAX_TXT];
880  char mode_txt[MAX_TXT];
881  char hpi_txt[MAX_TXT];
882  char csat_txt[MAX_TXT];
883  char *arg_v[MAX_ARGV];
884 #endif
885  d3_lattice d3_l;
886  info my_info;
887  unsigned char mode;
888  double c_ratio;
889 #ifdef USE_DFB_API
890  int max_extra;
891 #endif
892  u32 *c_profile;
893  u32 *n_e_loss;
894  u32 ne_max,ip_max;
895
896  d3_l.max_x=X;
897  d3_l.max_y=Y;
898  d3_l.max_z=Z;
899  my_info.steps=STEPS;
900  my_info.range=RANGE;
901  refresh=REFRESH;
902  resave=RESAVE;
903  my_info.z_diff=0;
904  my_info.c_diff=1;
905  my_info.a_el=A_EL;
906  my_info.b_el=B_EL;
907  my_info.a_cd=A_CD;
908  my_info.b_cd=B_CD;
909  my_info.a_ap=A_AP;
910  my_info.b_ap=B_AP;
911  my_info.a_cp=A_CP;
912  my_info.cc=CC;
913  my_info.dr_ac=DR_AC;
914  my_info.dr_cc=DR_CC;
915  my_info.diff_rate=DIFF_RATE;
916  my_info.cpi=CPI;
917  my_info.c_sat=C_SAT;
918  nowait=0;
919  quit=0;
920  escape=0;
921  switchmode=0;
922  c_step=0;
923  strcpy(s_file,"");
924  strcpy(l_file,"");
925  strcpy(c_file,"");
926  strcpy(p_file,IMP_PROFILE);
927  strcpy(n_e_file,NEL_PROFILE);
928  convert=0;
929  strcpy(r_file,"");
930  mode=0;
931  ne_max=0;
932  ip_max=0;
933
934  for(i=1;i<argc;i++)
935  {
936   if(argv[i][0]=='-')
937   {
938    switch(argv[i][1])
939    {
940     case 'h':
941      usage();
942      return -1;
943     case 'n':
944      nowait=1;
945      break;
946     case 'a':
947      my_info.a_el=atof(argv[++i]);
948      break;
949     case 'b':
950      my_info.b_el=atof(argv[++i]);
951      break;
952     case 'x':
953      d3_l.max_x=atoi(argv[++i]);
954      break;
955     case 'y':
956      d3_l.max_y=atoi(argv[++i]);
957      break;
958     case 'z':
959      d3_l.max_z=atoi(argv[++i]);
960      break;
961     case 'Z':
962      my_info.z_diff=1;
963      break;
964     case 'i':
965      my_info.c_diff=0;
966      my_info.dr_cc=0;
967      break;
968     case 's':
969      my_info.steps=atoi(argv[++i]);
970      break;
971     case 'd':
972      refresh=atoi(argv[++i]);
973      break;
974     case 'r':
975      my_info.range=atoi(argv[++i]);
976      break;
977     case 'f':
978      my_info.a_ap=atof(argv[++i]);
979      break;
980     case 'p':
981      my_info.b_ap=atof(argv[++i]);
982      break;
983     case 'F':
984      my_info.a_cp=atof(argv[++i]);
985      break;
986     case 'A':
987      my_info.a_cd=atof(argv[++i]);
988      break;
989     case 'B':
990      my_info.b_cd=atof(argv[++i]);
991      break;
992     case 'W':
993      resave=atoi(argv[++i]);
994      break;
995     case 'C':
996      strcpy(l_file,argv[++i]);
997      if(i<argc-1) if(argv[i+1][0]!='-') strcpy(c_file,argv[++i]);
998      convert=1;
999      break;
1000     case 'D':
1001      my_info.dr_ac=atof(argv[++i]);
1002      break;
1003     case 'c':
1004      my_info.dr_cc=atof(argv[++i]);
1005      break;
1006     case 'e':
1007      my_info.diff_rate=atoi(argv[++i]);
1008      break;
1009     case 'L':
1010      strcpy(l_file,argv[++i]);
1011      break;
1012     case 'S':
1013      strcpy(s_file,argv[++i]);
1014      break;
1015     case 'R':
1016      strcpy(r_file,argv[++i]);
1017      break;
1018     case 'P':
1019      strcpy(p_file,argv[++i]);
1020      break;
1021     case 'N':
1022      strcpy(n_e_file,argv[++i]);
1023      break;
1024     case 'g':
1025      strcpy(l_file,argv[++i]);
1026      if(i<argc-1) if(argv[i+1][0]!='-') c_step=atoi(argv[++i]);
1027      break;
1028     case 'H':
1029      my_info.cpi=atoi(argv[++i]);
1030      break;
1031     case 'm':
1032      my_info.c_sat=atoi(argv[++i]);
1033      break;
1034     default:
1035      usage();
1036      return -1;
1037    }
1038   } else usage();
1039  }
1040
1041  x=d3_l.max_x/2-1;
1042  y=d3_l.max_y/2-1;
1043  z=d3_l.max_z/2-1;
1044
1045 #ifdef NODFB
1046  if(!strcmp(s_file,""))
1047  {
1048   puts("NODFB defined, run with -S option");
1049   return -1;
1050  }
1051 #endif
1052
1053  if(!strcmp(r_file,"")) rand_init(NULL);
1054  else rand_init(r_file);
1055
1056  if(!strcmp(l_file,""))
1057  {
1058   i=d3_l.max_x*d3_l.max_y*d3_l.max_z;
1059 #ifdef USE_DFB_API
1060   d3_lattice_init(&argc,argv,&d3_l);
1061 #endif
1062   if((d3_l.status=(unsigned char *)malloc(i*sizeof(unsigned char)))==NULL)
1063   {
1064    puts("failed allocating status buffer");
1065    return -1;
1066   }
1067   memset(d3_l.status,0,i*sizeof(unsigned char));
1068   if((d3_l.extra=(int *)malloc(i*sizeof(int)))==NULL)
1069   {
1070    puts("failed allocating concentration buffer");
1071    return -1;
1072   }
1073   memset(d3_l.extra,0,i*sizeof(int));
1074  } else
1075  {
1076   load_from_file(l_file,&d3_l,&my_info);
1077   if(convert) 
1078   {   
1079    if(!strcmp(c_file,"")) sprintf(c_file,"%s_gnuplot",l_file);
1080    printf("converting file %s to %s\n",l_file,c_file);
1081    convert_file(c_file,&d3_l);
1082    puts("done");
1083    return 1;
1084   } 
1085 #ifdef USE_DFB_API
1086   else d3_lattice_init(&argc,argv,&d3_l);
1087 #endif
1088  }
1089
1090 #ifdef USE_DFB_API
1091  d3_event_init(&d3_l);
1092 #endif
1093
1094 #ifdef USE_DFB_API
1095  strcpy(a_txt,"args:");
1096  sprintf(s_txt,"steps: %d",my_info.steps);
1097  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));
1098  sprintf(r_txt,"pressure range: %d",my_info.range);
1099  sprintf(ap_txt,"a_ap: %f  b_ap: %f",my_info.a_ap,my_info.b_ap);
1100  sprintf(el_txt,"a_el: %f  b_el: %f",my_info.a_el,my_info.b_el);
1101  sprintf(cd_txt,"a_cd: %f  b_cd: %f",my_info.a_cd,my_info.b_cd);
1102  sprintf(cp_txt,"a_cp: %f",my_info.a_cp);
1103  sprintf(dr_ac_txt,"a/c diffusion rate: %f",my_info.dr_ac);
1104  if(my_info.c_diff!=0) sprintf(dr_cc_txt,"c/c diffusion rate: %f",my_info.dr_cc);
1105  else sprintf(dr_cc_txt,"c/c diffusion rate: none");
1106  sprintf(zdiff_txt,"diffusion in z direction: %c",my_info.z_diff?'y':'n');
1107  sprintf(diff_txt,"diffusion every %d steps",my_info.diff_rate);
1108  strcpy(mode_txt,"view: a/c mode");
1109  sprintf(hpi_txt,"hits per ion: %d",my_info.cpi);
1110  sprintf(csat_txt,"carbon saturation: %d",my_info.c_sat);
1111  arg_v[1]=mode_txt;
1112  arg_v[2]=xyz_txt;
1113  arg_v[3]=status_txt;
1114  arg_v[4]=conc_txt;
1115  arg_v[5]=NULL;
1116  arg_v[6]=steps_txt;
1117  arg_v[7]=cc_txt;
1118  arg_v[8]=NULL;
1119  arg_v[9]=a_txt;
1120  arg_v[10]=s_txt;
1121  arg_v[11]=dose_txt;
1122  arg_v[12]=diff_txt;
1123  arg_v[13]=zdiff_txt;
1124  arg_v[14]=r_txt;
1125  arg_v[15]=ap_txt;
1126  arg_v[16]=el_txt;
1127  arg_v[17]=cd_txt;
1128  arg_v[18]=cp_txt;
1129  arg_v[19]=dr_ac_txt;
1130  arg_v[20]=dr_cc_txt;
1131  arg_v[21]=hpi_txt;
1132  arg_v[22]=csat_txt;
1133  arg_v[23]=NULL;
1134  arg_v[24]=NULL;
1135  arg_v[25]=NULL;
1136 #endif
1137
1138  if((!strcmp(l_file,""))||(c_step))
1139  {
1140   /* calculate ratio of c_simwindow / c_total */
1141   if(get_c_ratio(&c_ratio,p_file,&my_info,&d3_l)!=1)
1142   {
1143    puts("failed calculating ratio");
1144    return -1;
1145   }
1146   /* compute graphs for random number rejection method */
1147   if((c_profile=(u32 *)malloc(d3_l.max_z*sizeof(unsigned int)))==NULL)
1148   {
1149    puts("failed allocating memory for carbon profile graph");
1150    return -1;
1151   }
1152   if((n_e_loss=(u32 *)malloc(d3_l.max_z*sizeof(unsigned int)))==NULL)
1153   {
1154    puts("failed allocating memory for nuclear energy loss graph");
1155    return -1;
1156   }
1157   ip_max=get_reject_graph(&my_info,&d3_l,p_file,c_profile);
1158   ne_max=get_reject_graph(&my_info,&d3_l,n_e_file,n_e_loss);
1159
1160 #ifdef DEBUG_RAND
1161  while(1)
1162  {
1163 #ifdef DEBUG_CP
1164   printf("%d\n",get_rand_reject(d3_l.max_z,ip_max,c_profile));
1165 #endif
1166 #ifdef DEBUG_NEL
1167   printf("%d\n",get_rand_reject(d3_l.max_z,ne_max,n_e_loss));
1168 #endif
1169 #ifdef DEBUG_NORM
1170   printf("%d\n",get_rand(d3_l.max_z));
1171 #endif
1172  }
1173 #endif
1174
1175   i=(c_step?c_step:0);
1176   while((i<my_info.steps) && (quit==0) && (escape==0))
1177   {
1178    for(j=0;j<my_info.cpi;j++)
1179    {
1180     x_c=get_rand(d3_l.max_x);
1181     y_c=get_rand(d3_l.max_y);
1182     z_c=get_rand_reject(d3_l.max_z,ne_max,n_e_loss);
1183     process_cell(&d3_l,x_c,y_c,z_c,&my_info);
1184    }
1185    distrib_c(&d3_l,&my_info,i,c_ratio,ip_max,c_profile);
1186 #ifdef USE_DFB_API
1187    if(i%refresh==0)
1188    {
1189     sprintf(xyz_txt,"x: %d  y: %d  z: %d",x+1,y+1,z+1);
1190     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');
1191     sprintf(conc_txt,"conc: %d",*(d3_l.extra+x+y*d3_l.max_x+z*d3_l.max_x*d3_l.max_y));
1192     sprintf(steps_txt,"step: %d",i);
1193     sprintf(cc_txt,"total c: %d",my_info.cc);
1194     d3_lattice_draw(&d3_l,x,y,z,25,arg_v,mode,0);
1195    }
1196 #endif
1197    if(i%resave==0 && strcmp(s_file,"") && resave!=0 && i!=0)
1198    {
1199     sprintf(s_file_tmp,"%s_%d_of_%d",s_file,i,my_info.steps);
1200     save_to_file(s_file_tmp,&d3_l,&my_info);
1201 #ifdef NODFB
1202     printf("saved %s\n",s_file_tmp);
1203 #endif
1204    }
1205    i++;
1206   }
1207  }
1208
1209  if(strcmp(s_file,""))
1210  {
1211    printf("saved %s\n",s_file);
1212    save_to_file(s_file,&d3_l,&my_info);
1213  }
1214
1215 #ifdef USE_DFB_API
1216  /* allocating buffer for pressure values */
1217  if((d3_l.v_ptr=malloc(d3_l.max_x*d3_l.max_y*d3_l.max_z*sizeof(unsigned char)))==NULL)
1218  {
1219   puts("cannot allocate buffer for pressure values");
1220   return -1;
1221  }
1222  /* calc values */
1223  calc_pressure(&d3_l,my_info.range);
1224  max_extra=calc_max_extra(&d3_l);
1225
1226  while((quit==0) && (escape==0) && (nowait==0))
1227  {
1228   /* bahh! */
1229   if(switchmode==0) mode=0;
1230   if(switchmode==1) mode=1;
1231   if(switchmode==2) mode=2;
1232   /* end of bahh! */
1233   sprintf(xyz_txt,"x: %d  y: %d  z: %d",x+1,y+1,z+1);
1234   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');
1235   sprintf(conc_txt,"conc: %d",*(d3_l.extra+x+y*d3_l.max_x+z*d3_l.max_x*d3_l.max_y));
1236   sprintf(steps_txt,"step: end");
1237   sprintf(cc_txt,"total c: %d",my_info.cc);
1238   if(switchmode==0) strcpy(mode_txt,"view: a/c mode");
1239   if(switchmode==1) strcpy(mode_txt,"view: c conc mode");
1240   if(switchmode==2) strcpy(mode_txt,"view: a pressure mode");
1241   d3_lattice_draw(&d3_l,x,y,z,25,arg_v,mode,max_extra);
1242   bmp=0;
1243   ac_distr=0;
1244   scan_event(&d3_l,&x,&y,&z,&quit,&escape,&switchmode,&bmp,&ac_distr);
1245   if(bmp) write_bmp(&d3_l,bmp,x,y,z,max_extra);
1246   if(ac_distr) write_ac_distr(&d3_l,ac_distr);
1247  }
1248
1249  d3_lattice_release(&d3_l);
1250 #endif
1251
1252  return 1;
1253 }