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