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