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