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