c-conc and p-val colour table output added
[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. Habil.Schrift, 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 stress induced amorphization influence (default %f)\n",S_D);
65  printf("-p <value> \t ballistic amorphization influence (default %f)\n",B_D);
66  printf("-F <value> \t carbon induced amorphization influence (default %f)\n",C_D);
67  printf("-D <value> \t diffusion rate from cryst to amorph cells (default %f)\n",DR_AC);
68  printf("-e <value> \t do diffusion every <value> steps (default %d)\n",DIFF_RATE);
69  puts("-g <file> <step> continue simulation from file and step (step > 0)!");
70  printf("-W <value> \t write every <value> steps to save file (default %d)\n",RESAVE);
71  puts("-C <file> \t convert file to gnuplot format");
72  puts("-L <file> \t load from file");
73  puts("-S <file> \t save to file");
74  puts("-R <file> \t read from random file");
75  puts("-P <file> \t specify implantation profile file");
76  puts("-N <file> \t specify nuclear energy loss profile file");
77  printf("-H <value> \t collisions per ion in simulation window (default %d)\n",CPI);
78  printf("-m <value> \t number of ion hits sputtering 3nm (default %d)\n",S_RATE);
79  
80  return 1;
81 }
82
83 int sputter(d3_lattice *d3_l)
84 {
85  int i,size;
86  int offh,offl;
87
88  size=d3_l->max_x*d3_l->max_y;
89  offl=0;
90  offh=size;
91
92  for(i=0;i<d3_l->max_z-1;i++)
93  {
94   memcpy(d3_l->status+offl,d3_l->status+offh,size);
95   memcpy(d3_l->extra+offl,d3_l->extra+offh,size*sizeof(int));
96   offl=offh;
97   offh+=size;
98  }
99  memset(d3_l->status+offl,0,size);
100  memset(d3_l->extra+offl,0,size);
101
102  return 1;
103 }
104
105 int process_cell(d3_lattice *d3_l,u32 x,u32 y,u32 z,info *my_info,u32 nel_z)
106 {
107  unsigned char *thiz;
108  int *conc;
109  int i,j;
110  int off;
111  double p,q;
112
113  thiz=d3_l->status+x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y;
114  conc=d3_l->extra+x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y;
115  p=my_info->b*nel_z;
116  for(i=-(my_info->range);i<=my_info->range;i++)
117  {
118   for(j=-(my_info->range);j<=my_info->range;j++)
119   {
120    if(!(i==0 && j==0))
121    {
122     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;
123     if(*(d3_l->status+off)&AMORPH) p+=my_info->s*(*(d3_l->extra+off))*URAND_MAX/(i*i+j*j);
124    } 
125   }
126  }
127  p+=*conc*my_info->c*URAND_MAX;
128  if(!(*thiz&AMORPH))
129  {
130   if(get_rand(URAND_MAX)<=p) MAKE_AMORPH(thiz);
131  } else
132  {
133   /* assume 1-p probability */
134   /* also look for neighbours ! */
135   q=(URAND_MAX-p)>0?URAND_MAX-p:0;
136   j=0;
137   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;
138   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;
139   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;
140   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;
141   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;
142   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;
143
144   p+=((q/6)*j);
145   if(get_rand(URAND_MAX)>p) MAKE_CRYST(thiz);
146  }
147  
148  return 1;
149 }
150
151 int distrib_c(d3_lattice *d3_l,info *my_info,int step,u32 rj_m,u32 *rj_g)
152 {
153  u32 x,y,z;
154  int i,j,k,c;
155  int offset,off;
156  int carry;
157
158  /* put one c ion somewhere in the lattice */
159  x=get_rand(d3_l->max_x);
160  y=get_rand(d3_l->max_y);
161  z=get_rand_reject(d3_l->max_z,rj_m,rj_g);
162  *(d3_l->extra+x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y)+=1;
163  (my_info->cc)++;
164
165  if(step%my_info->diff_rate==0)
166  {
167
168  for(i=0;i<d3_l->max_x;i++)
169  {
170   for(j=0;j<d3_l->max_y;j++)
171   {
172    for(k=0;k<d3_l->max_z;k++)
173    {
174     offset=i+j*d3_l->max_x+k*d3_l->max_x*d3_l->max_y;
175     /* case amorph: amorph <- cryst diffusion */
176     if(*(d3_l->status+offset)&AMORPH)
177     {
178      for(c=-1;c<=1;c++)
179      {
180       if(c!=0)
181       {
182        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;
183        carry=0;
184        if(!(*(d3_l->status+off)&AMORPH)) carry=(int)(my_info->dr_ac*(*(d3_l->extra+off)));
185        if(carry!=0)
186        {
187         *(d3_l->extra+offset)+=carry;
188         *(d3_l->extra+off)-=carry;
189        }
190       }
191      }
192      for(c=-1;c<=1;c++)
193      {
194       if(c!=0)
195       {
196        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;
197        carry=0;
198        if(!(*(d3_l->status+off)&AMORPH)) carry=(int)(my_info->dr_ac*(*(d3_l->extra+off)));
199        if(carry!=0)
200        {
201         *(d3_l->extra+offset)+=carry; 
202         *(d3_l->extra+off)-=carry; 
203        }
204       }
205      }
206      /* z diff */
207      if(k!=0)
208      {
209       off=i+j*d3_l->max_x+(k-1)*d3_l->max_x*d3_l->max_y;
210       carry=0;
211       if(!*(d3_l->status+off)&AMORPH) carry=(int)(my_info->dr_ac*(*(d3_l->extra+off)));
212       if(carry!=0)
213       {
214        *(d3_l->extra+off)-=carry;
215        *(d3_l->extra+offset)+=carry;
216       }
217      }
218      if(k!=d3_l->max_z-1)
219      {
220       off=i+j*d3_l->max_x+(k+1)*d3_l->max_x*d3_l->max_y;
221       carry=0;
222       if(!*(d3_l->status+off)&AMORPH) carry=(int)(my_info->dr_ac*(*(d3_l->extra+off)));
223      if(carry!=0)
224       {
225        *(d3_l->extra+off)-=carry;
226        *(d3_l->extra+offset)+=carry;
227       }
228      }
229     }
230    } /* for z */
231   } /* for y */
232  } /* for x */
233
234  } /* if step modulo diff_rate == 0 */
235
236  return 1;
237 }
238
239 int calc_pressure(d3_lattice *d3_l,int range)
240 {
241  int i,j,off;
242  double count,max=0;
243  int x,y,z;
244
245  for(x=0;x<d3_l->max_x;x++)
246  {
247   for(y=0;y<d3_l->max_y;y++)
248   {
249    for(z=0;z<d3_l->max_z;z++)
250    {
251     count=0;
252     for(i=-range;i<=range;i++)
253     {
254      for(j=-range;j<=range;j++)
255      {
256       if(i!=0 && j!=0)
257       {
258        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;
259        if(*(d3_l->status+off)&AMORPH) count+=((double)*(d3_l->extra+off))/(i*i+j*j);
260       }
261      }
262     }
263     if(count>max) max=count;
264    }
265   }
266  }
267
268  for(x=0;x<d3_l->max_x;x++)
269  {
270   for(y=0;y<d3_l->max_y;y++)
271   {
272    for(z=0;z<d3_l->max_z;z++)
273    {
274     count=0;
275     for(i=-range;i<=range;i++)
276     {
277      for(j=-range;j<=range;j++)
278      {
279       if(i!=0 && j!=0)
280       {
281        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;
282        if(*(d3_l->status+off)&AMORPH) count+=((double)*(d3_l->extra+off))/(i*i+j*j);
283       }
284      }
285     }
286     *((unsigned int *)(d3_l->v_ptr)+x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y)=(count*500/max);
287    }
288   }
289  }
290
291  return 1;
292 }
293
294 int calc_max_extra(d3_lattice *d3_l)
295 {
296  int x,y,z;
297  int off,max=0;
298
299  for(x=0;x<d3_l->max_x;x++)
300  {
301   for(y=0;y<d3_l->max_y;y++)
302   {
303    for(z=0;z<d3_l->max_z;z++)
304    {
305     off=x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y;
306     if(*(d3_l->extra+off)>max) max=*(d3_l->extra+off);
307    }
308   }
309  }
310
311  return max;
312 }
313
314 int write_ac_distr(d3_lattice *d3_l,int ac_distr)
315 {
316  int fd,x,y,z;
317  int count,offset;
318  char file[32];
319  int si_count;
320  
321  if(ac_distr==1) strcpy(file,"carbon_in_av.plot");
322  if(ac_distr==2) strcpy(file,"carbon_in_cv.plot");
323  if(ac_distr==3) strcpy(file,"carbon.plot");
324  if(ac_distr==4) strcpy(file,"amorphous_volumes.plot");
325
326  if((fd=open(file,O_WRONLY|O_CREAT))<0)
327  {
328   puts("cannot open plot file");
329   return -1;
330  }
331
332  for(z=0;z<d3_l->max_z;z++)
333  {
334   count=0;
335   si_count=0;
336   for(x=0;x<d3_l->max_x;x++)
337   {
338    for(y=0;y<d3_l->max_y;y++)
339    {
340     offset=x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y;
341     if(ac_distr==1)
342      if(*(d3_l->status+offset)&AMORPH)
343      {
344       count+=*(d3_l->extra+offset);
345       si_count+=1;
346      }
347     if(ac_distr==2)
348      if(!(*(d3_l->status+offset)&AMORPH))
349      {
350       count+=*(d3_l->extra+offset);
351       si_count+=1;
352      }
353     if(ac_distr==3)
354     {
355      count+=*(d3_l->extra+offset);
356      si_count+=1;
357     }
358     if(ac_distr==4)
359      if(*(d3_l->status+offset)&AMORPH) count+=1;
360    }
361   }
362   si_count*=SI_PER_VOLUME;
363   if(ac_distr==4) dprintf(fd,"%d %d\n",z*CELL_LENGTH,count);
364 #ifdef ATPROZ
365   else
366   {
367    if(si_count)
368     dprintf(fd,"%d %f\n",z*CELL_LENGTH,100.0*count/(si_count+count));
369    else
370     dprintf(fd,"%d 0\n",z*CELL_LENGTH);
371   }
372 #else
373   else dprintf(fd,"%d %d\n",z*CELL_LENGTH,count/(d3_l->max_x*d3_l->max_y));
374 #endif
375  }
376  close(fd);
377  
378  return 1;
379 }
380
381 int write_bmp(d3_lattice *d3_l,int window,u32 x,u32 y,u32 z,int max)
382 {
383  int fd,i,j,size=0,foo=0,k,sum;
384  int width=0,height=0;
385  char bmpfile[MAX_CHARS];
386  char buf[128];
387
388  if(window==1)
389  {
390   sprintf(bmpfile,"x-z_%d.bmp",y);
391   foo=3*d3_l->max_x;
392   size=(foo+(4-foo%4))*d3_l->max_z;
393   height=d3_l->max_z;
394   width=d3_l->max_x;
395  }
396  if(window==2)
397  {
398   sprintf(bmpfile,"y-z_%d.bmp",x);
399   foo=3*d3_l->max_y;
400   size=(foo+(4-foo%4))*d3_l->max_z;
401   height=d3_l->max_z;
402   width=d3_l->max_y;
403  }
404  if(window==3)
405  {
406   sprintf(bmpfile,"x-y_%d.bmp",z);
407   foo=3*d3_l->max_x;
408   size=(foo+(4-foo%4))*d3_l->max_y;
409   width=d3_l->max_x;
410   height=d3_l->max_y;
411  }
412  if(window==4)
413  {
414   sprintf(bmpfile,"x-z_cdistr_%d.bmp",y);
415   foo=3*d3_l->max_x;
416   size=(foo+(4-foo%4))*d3_l->max_z;
417   height=d3_l->max_z;
418   width=d3_l->max_x;
419  }
420  if(window==5)
421  {
422   sprintf(bmpfile,"y-z_cdistr_%d.bmp",x);
423   foo=3*d3_l->max_y;
424   size=(foo+(4-foo%4))*d3_l->max_z;
425   height=d3_l->max_z;
426   width=d3_l->max_y;
427  }
428  if(window==6)
429  {
430   sprintf(bmpfile,"x-y_cdistr_%d.bmp",z);
431   foo=3*d3_l->max_x;
432   size=(foo+(4-foo%4))*d3_l->max_y;
433   width=d3_l->max_x;
434   height=d3_l->max_y;
435  }
436  if(window==7)
437  {
438   sprintf(bmpfile,"x-z_pressure_%d.bmp",y);
439   foo=3*d3_l->max_x;
440   size=(foo+(4-foo%4))*d3_l->max_z;
441   height=d3_l->max_z;
442   width=d3_l->max_x;
443  }
444  if(window==8)
445  {
446   sprintf(bmpfile,"y-z_pressure_%d.bmp",x);
447   foo=3*d3_l->max_y;
448   size=(foo+(4-foo%4))*d3_l->max_z;
449   height=d3_l->max_z;
450   width=d3_l->max_y;
451  }
452  if(window==9)
453  {
454   sprintf(bmpfile,"x-y_pressure_%d.bmp",z);
455   foo=3*d3_l->max_x;
456   size=(foo+(4-foo%4))*d3_l->max_y;
457   height=d3_l->max_y;
458   width=d3_l->max_x;
459  }
460  if(window==10)
461  {
462   sprintf(bmpfile,"x-z_c_cdistr_%d.bmp",y);
463   foo=3*d3_l->max_x;
464   size=(foo+(4-foo%4))*d3_l->max_z;
465   height=d3_l->max_z;
466   width=d3_l->max_x;
467  }
468  if(window==11)
469  {
470   sprintf(bmpfile,"y-z_c_cdistr_%d.bmp",x);
471   foo=3*d3_l->max_y;
472   size=(foo+(4-foo%4))*d3_l->max_z;
473   height=d3_l->max_z;
474   width=d3_l->max_y;
475  }
476  if(window==12)
477  {
478   sprintf(bmpfile,"x-y_c_cdistr_%d.bmp",z);
479   foo=3*d3_l->max_x;
480   size=(foo+(4-foo%4))*d3_l->max_y;
481   height=d3_l->max_y;
482   width=d3_l->max_x;
483  }
484  if((fd=open(bmpfile,O_WRONLY|O_CREAT))<0)
485  {
486   puts("cannot open bmp file");
487   return -1;
488  }
489
490  /* bmpheader */
491  buf[0]='B'; /* std header start */
492  buf[1]='M';
493  buf[2]=(size+0x36)&0xff; /* file size */
494  buf[3]=(size+0x36)>>8;
495  memset(buf+4,0,6);
496  buf[10]=0x36; /* offset to data */
497  memset(buf+11,0,3);
498  buf[14]=0x28; /* length of bmp info header */
499  memset(buf+15,0,3);
500  buf[18]=width&0xff; /* width and height */
501  buf[19]=width>>8;
502  memset(buf+20,0,2);
503  buf[22]=height&0xff;
504  buf[23]=height>>8;
505  memset(buf+24,0,2);
506  buf[26]=1; /* # planes -> 1 */
507  buf[27]=0;
508  buf[28]=24; /* bits per pixel -> 2^24 (true color) */
509  buf[29]=0;
510  memset(buf+30,0,4); /* compression -> none */
511  buf[34]=size&0xff; /* data size */
512  buf[35]=size>>8;
513  memset(buf+36,0,2);
514  buf[38]=0x12; /* res: pixel/meter */
515  buf[39]=0x0b;
516  memset(buf+40,0,2);
517  buf[42]=0x12;
518  buf[43]=0x0b;
519  memset(buf+44,0,2); 
520  memset(buf+46,0,8); /* no colors, no important colors */
521
522  if(write(fd,buf,54)<54)
523  {
524   puts("failed writing bmp header");
525   return -1;
526  }
527  if(window==1)
528  {
529   for(j=0;j<d3_l->max_z;j++)
530   {
531    for(i=0;i<d3_l->max_x;i++)
532    {
533     sum=0;
534     for(k=-2;k<=2;k++)
535      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;
536     sum/=5;
537     memset(buf,(unsigned char)sum,3);
538     if(write(fd,buf,3)<3)
539     {
540      puts("failed writing rgb values to bmp file");
541      return-1;
542     }
543    }
544    if(foo%4)
545    {
546     memset(buf,0,4-foo%4);
547     if(write(fd,buf,4-foo%4)<4-foo%4)
548     {
549      puts("failed writing 4 byte ending");
550      return -1;
551     }
552    } 
553   }
554  }
555  if(window==2)
556  {
557   for(j=0;j<d3_l->max_z;j++)
558   {
559    for(i=0;i<d3_l->max_y;i++)
560    {
561     sum=0;
562     for(k=-2;k<=2;k++)
563      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;
564     sum/=5;
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==3)
584  {
585   for(j=0;j<d3_l->max_y;j++)
586   {
587    for(i=0;i<d3_l->max_x;i++)
588    {
589     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);
590     else memset(buf,0,3);
591     if(write(fd,buf,3)<3)
592     {
593      puts("failed writing rgb values to bmp file");
594      return -1;
595     }
596    }
597    if(foo%4)
598    {
599     memset(buf,0,4-foo%4);
600     if(write(fd,buf,4-foo%4)<4-foo%4)
601     {
602      puts("failed writing 4 byte ending");
603      return -1;
604     }
605    }
606   }
607  }
608  if(window==4)
609  {
610   for(j=0;j<d3_l->max_z;j++)
611   {
612    for(i=0;i<d3_l->max_x;i++)
613    {
614     sum=*(d3_l->extra+i+y*d3_l->max_x+(d3_l->max_z-j-1)*d3_l->max_x*d3_l->max_y);
615     sum=sum*500/max;
616     buf[2]=0;
617     if(sum<=255) {
618       buf[1]=0;
619       buf[0]=sum;
620     } else {
621       buf[1]=(sum-255);
622       buf[0]=0xff;
623     }
624     if(write(fd,buf,3)<3)
625     {
626      puts("failed writing rgb values to bmp file");
627      return-1;
628     }
629    }
630    if(foo%4)
631    {
632     memset(buf,0,4-foo%4);
633     if(write(fd,buf,4-foo%4)<4-foo%4)
634     {
635      puts("failed writing 4 byte ending");
636      return -1;
637     }
638    }
639   }
640  }
641  if(window==5)
642  { 
643   for(j=0;j<d3_l->max_z;j++) 
644   {                     
645    for(i=0;i<d3_l->max_x;i++)
646    {
647     sum=*(d3_l->extra+x+i*d3_l->max_x+(d3_l->max_z-j-1)*d3_l->max_x*d3_l->max_y);
648     sum=sum*500/max;
649     buf[2]=0;
650     if(sum<=255) {
651       buf[1]=0;
652       buf[0]=sum;
653     } else {
654       buf[1]=(sum-255);
655       buf[0]=0xff;
656     }
657     if(write(fd,buf,3)<3)
658     {
659      puts("failed writing rgb values to bmp file");
660      return-1;  
661     }
662    }
663    if(foo%4)
664    {
665     memset(buf,0,4-foo%4);
666     if(write(fd,buf,4-foo%4)<4-foo%4)
667     {
668      puts("failed writing 4 byte ending");
669      return -1;
670     }
671    }
672   }
673  }
674  if(window==6)
675  {   
676   for(j=0;j<d3_l->max_y;j++)
677   {  
678    for(i=0;i<d3_l->max_x;i++)
679    { 
680     sum=*(d3_l->extra+i+(d3_l->max_y-j-1)*d3_l->max_x+z*d3_l->max_x*d3_l->max_y);
681     sum=sum*500/max;
682     buf[2]=0;
683     if(sum<=255) {
684       buf[1]=0;
685       buf[0]=sum;
686     } else {
687       buf[1]=(sum-255);
688       buf[0]=0xff;
689     }
690     if(write(fd,buf,3)<3)
691     {
692      puts("failed writing rgb values to bmp file");
693      return-1;
694     }
695    } 
696    if(foo%4)
697    { 
698     memset(buf,0,4-foo%4);
699     if(write(fd,buf,4-foo%4)<4-foo%4)
700     {
701      puts("failed writing 4 byte ending");
702      return -1;
703     }
704    } 
705   }
706  }
707  if(window==7)
708  {
709   for(j=0;j<d3_l->max_z;j++)
710   {
711    for(i=0;i<d3_l->max_x;i++)
712    {
713     sum=*((unsigned int *)(d3_l->v_ptr)+i+y*d3_l->max_x+(d3_l->max_z-j-1)*d3_l->max_x*d3_l->max_y);
714       buf[0]=0;
715     if(sum<=255) {
716       buf[1]=0;
717       buf[2]=sum;
718     } else {
719       buf[1]=(sum-255);
720       buf[2]=0xff;
721     }
722     if(write(fd,buf,3)<3)
723     {
724      puts("failed writing rgb values to bmp file");
725      return-1;
726     }
727    }
728    if(foo%4)
729    {
730     memset(buf,0,4-foo%4);
731     if(write(fd,buf,4-foo%4)<4-foo%4)
732     {
733      puts("failed writing 4 byte ending");
734      return -1;
735     }
736    }
737   }
738  }
739  if(window==8)
740  { 
741   for(j=0;j<d3_l->max_z;j++) 
742   {                     
743    for(i=0;i<d3_l->max_x;i++)
744    {
745     sum=*((unsigned int *)(d3_l->v_ptr)+x+i*d3_l->max_x+(d3_l->max_z-j-1)*d3_l->max_x*d3_l->max_y);
746       buf[0]=0;
747     if(sum<=255) {
748       buf[1]=0;
749       buf[2]=sum;
750     } else {
751       buf[1]=(sum-255);
752       buf[2]=0xff;
753     }
754     if(write(fd,buf,3)<3)
755     {
756      puts("failed writing rgb values to bmp file");
757      return-1;  
758     }
759    }
760    if(foo%4)
761    {
762     memset(buf,0,4-foo%4);
763     if(write(fd,buf,4-foo%4)<4-foo%4)
764     {
765      puts("failed writing 4 byte ending");
766      return -1;
767     }
768    }
769   }
770  }
771  if(window==9)
772  {   
773   for(j=0;j<d3_l->max_y;j++)
774   {  
775    for(i=0;i<d3_l->max_x;i++)
776    {
777     sum=*((unsigned int *)(d3_l->v_ptr)+i+(d3_l->max_y-j-1)*d3_l->max_x+z*d3_l->max_x*d3_l->max_y);
778       buf[0]=0;
779     if(sum<=255) {
780       buf[1]=0;
781       buf[2]=sum;
782     } else {
783       buf[1]=(sum-255);
784       buf[2]=0xff;
785     }
786     printf("sum = %d => r: %02x g: %02x b: %02x\n",sum,buf[2],buf[1],buf[0]);
787     if(write(fd,buf,3)<3)
788     {
789      puts("failed writing rgb values to bmp file");
790      return-1;
791     }
792    } 
793    if(foo%4)
794    { 
795     memset(buf,0,4-foo%4);
796     if(write(fd,buf,4-foo%4)<4-foo%4)
797     {
798      puts("failed writing 4 byte ending");
799      return -1;
800     }
801    } 
802   }
803  }
804  if(window==10)
805  {
806   for(j=0;j<d3_l->max_z;j++)
807   {
808    for(i=0;i<d3_l->max_x;i++)
809    {
810     if(!(*(d3_l->status+i+y*d3_l->max_x+(d3_l->max_z-j-1)*d3_l->max_x*d3_l->max_y)&RED)) sum=*(d3_l->extra+i+y*d3_l->max_x+(d3_l->max_z-j-1)*d3_l->max_x*d3_l->max_y);
811     else sum=0;
812     sum=sum*255/max;
813     memset(buf,(unsigned char)sum,3);
814     if(write(fd,buf,3)<3)
815     {
816      puts("failed writing rgb values to bmp file");
817      return-1;
818     }
819    }
820    if(foo%4)
821    {
822     memset(buf,0,4-foo%4);
823     if(write(fd,buf,4-foo%4)<4-foo%4)
824     {
825      puts("failed writing 4 byte ending");
826      return -1;
827     }
828    }
829   }
830  }
831  if(window==11)
832  { 
833   for(j=0;j<d3_l->max_z;j++) 
834   {                     
835    for(i=0;i<d3_l->max_x;i++)
836    {
837     if(!(*(d3_l->status+x+i*d3_l->max_x+(d3_l->max_z-j-1)*d3_l->max_x*d3_l->max_y)&RED)) sum=*(d3_l->extra+x+i*d3_l->max_x+(d3_l->max_z-j-1)*d3_l->max_x*d3_l->max_y);
838     else sum=0;
839     sum=sum*255/max;
840     memset(buf,(unsigned char)sum,3);
841     if(write(fd,buf,3)<3)
842     {
843      puts("failed writing rgb values to bmp file");
844      return-1;  
845     }
846    }
847    if(foo%4)
848    {
849     memset(buf,0,4-foo%4);
850     if(write(fd,buf,4-foo%4)<4-foo%4)
851     {
852      puts("failed writing 4 byte ending");
853      return -1;
854     }
855    }
856   }
857  }
858  if(window==12)
859  {   
860   for(j=0;j<d3_l->max_y;j++)
861   {  
862    for(i=0;i<d3_l->max_x;i++)
863    {
864     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)) sum=*(d3_l->extra+i+(d3_l->max_y-j-1)*d3_l->max_x+z*d3_l->max_x*d3_l->max_y);
865     else sum=0;
866     sum=sum*255/max;
867     memset(buf,(unsigned char)sum,3);
868     if(write(fd,buf,3)<3)
869     {
870      puts("failed writing rgb values to bmp file");
871      return-1;
872     }
873    } 
874    if(foo%4)
875    { 
876     memset(buf,0,4-foo%4);
877     if(write(fd,buf,4-foo%4)<4-foo%4)
878     {
879      puts("failed writing 4 byte ending");
880      return -1;
881     }
882    }
883   }
884  }
885  close(fd);
886
887  return 1;
888 }
889
890 int save_to_file(char *sf,d3_lattice *d3_l,info *my_inf)
891 {
892  int sf_fd,c;
893
894  if((sf_fd=open(sf,O_WRONLY|O_CREAT))<0)
895  {
896   puts("cannot open save file");
897   return -1;
898  }
899  if(write(sf_fd,d3_l,sizeof(d3_lattice))<sizeof(d3_lattice))
900  {
901   puts("failed saving d3 lattice struct");
902   return -1;
903  }
904  if(write(sf_fd,my_inf,sizeof(info))<sizeof(info))
905  {
906   puts("failed saving info struct");
907   return-1;
908  }
909  c=d3_l->max_x*d3_l->max_y*d3_l->max_z;
910  if(write(sf_fd,d3_l->status,c*sizeof(unsigned char))<c*sizeof(unsigned char))
911  {
912   puts("failed saving status of d3 lattice sites");
913   return -1;
914  }
915  if(write(sf_fd,d3_l->extra,c*sizeof(int))<c*sizeof(int))
916  {
917   puts("failed saving sites concentration");
918   return -1;
919  }
920  close(sf_fd);
921
922  return 1;
923 }
924
925 int load_from_file(char *lf,d3_lattice *d3_l,info *my_inf)
926 {
927
928  int lf_fd,c,pos,end,data,data_len,strip;
929
930  if((lf_fd=open(lf,O_RDONLY))<0)
931  {
932   puts("cannot open load file");
933   return -1;
934  }
935  if(read(lf_fd,d3_l,sizeof(d3_lattice))<sizeof(d3_lattice))
936  {
937   puts("failed reading d3 lattice struct");
938   return -1;
939  }
940  pos=lseek(lf_fd,0,SEEK_CUR);
941  printf("psition: %d (%d)\n",pos,sizeof(d3_lattice));
942  data=d3_l->max_x*d3_l->max_y*d3_l->max_z;
943  data_len=data*(sizeof(int)+sizeof(unsigned char));
944  printf("there are %d volumes so we need %d of bytes\n",data,data_len);
945  end=lseek(lf_fd,0,SEEK_END);
946  c=end-pos-data_len;
947  printf("end: %d => length: %d => guessed info size: %d bytes\n",end,end-pos,c);
948  strip=sizeof(info)-c;
949  printf("as real programs info size is %d, we strip %d bytes\n",sizeof(info),strip);
950  lseek(lf_fd,pos,SEEK_SET);
951  c=sizeof(info);
952  if(strip>0) c-=strip;
953  if(c<0)
954  {
955   puts("info smaller then strip size");
956   return -1;
957  }
958  if(read(lf_fd,my_inf,c)<c)
959  {
960   puts("failed reading info struct");
961   return-1;
962  }
963  if(strip>0) memset(my_inf+c,0,strip);
964  if(strip<0) lseek(lf_fd,(-1*strip),SEEK_CUR);
965  c=d3_l->max_x*d3_l->max_y*d3_l->max_z;
966  if((d3_l->status=(unsigned char*)malloc(c*sizeof(unsigned char)))==NULL)
967  {
968   puts("cannot allocate status buffer");
969   return -1;
970  }
971  if((d3_l->extra=(int *)malloc(c*sizeof(int)))==NULL)
972  {
973   puts("cannot allocate concentration buffer");
974   return -1;
975  }
976  if(read(lf_fd,d3_l->status,c*sizeof(unsigned char))<c*sizeof(unsigned char))
977  {
978   puts("failed reading status of d3 lattice sites");
979   return -1;
980  }
981  if(read(lf_fd,d3_l->extra,c*sizeof(int))<c*sizeof(int))
982  {
983   puts("failed reading sites concentration");
984   return -1;
985  }
986  close(lf_fd);
987
988  return 1;
989 }
990
991 int convert_file(char *cf,d3_lattice *d3_l)
992 {
993  int x,y,z;
994  int c_fd;
995
996  if((c_fd=open(cf,O_WRONLY|O_CREAT))<0)
997  {
998   puts("cannot open convert file");
999   return -1;
1000  }
1001  dprintf(c_fd,"# created by nlsop (gnuplot format)\n");
1002  for(x=0;x<d3_l->max_x;x++)
1003  {
1004   for(y=0;y<d3_l->max_y;y++)
1005   {
1006    for(z=0;z<d3_l->max_z;z++)
1007    {
1008     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);
1009    }
1010   }
1011  }
1012  close(c_fd);
1013
1014  return 1;
1015 }
1016
1017 int get_c_ratio(double *c_ratio,char *pfile,info *my_info,d3_lattice *d3_l)
1018 {
1019  double all,a,b,d;
1020  int i,k;
1021  int p_fd;
1022  unsigned char buf[32];
1023  char *p;
1024  unsigned char c;
1025
1026  if((p_fd=open(pfile,O_RDONLY))<0)
1027  {
1028   puts("cannot open profile file");
1029   return -1;
1030  }
1031  k=1;
1032  d=0;
1033  all=0;
1034  while(k)
1035  {
1036   for(i=0;i<32;i++)
1037   {
1038    k=read(p_fd,&c,1);
1039    buf[i]=c;
1040    if(c=='\n') break;
1041   }
1042   if(k)
1043   {
1044    p=strtok(buf," ");
1045    a=atof(p)/10; /* nm */
1046    p=strtok(NULL," ");
1047    b=atof(p);
1048    if(a<=d3_l->max_z*CELL_LENGTH) d+=b;
1049    all+=b;
1050   }
1051  }
1052  *c_ratio=d/all;
1053  close(p_fd);
1054
1055  return 1;
1056 }
1057
1058 u32 get_reject_graph(info *my_info,d3_lattice *d3_l,char *file,u32 *graph) {
1059  double a,b;
1060  int i,j,k;
1061  int fd;
1062  char buf[32],*p;
1063  unsigned char *flag;
1064  u32 max;
1065
1066  max=0;
1067  if((fd=open(file,O_RDONLY))<0)
1068  {
1069   puts("cannot open file to calculate rejection graph");
1070   return -1;
1071  }
1072  if((flag=(unsigned char *)malloc(d3_l->max_z))==NULL)
1073  {
1074   puts("cannot malloc flag memory for rejection graph");
1075   return -1;
1076  }
1077  memset(flag,0,d3_l->max_z);
1078  memset(graph,0,d3_l->max_z*sizeof(u32));
1079  /* get fixpoints */
1080  k=1;
1081  while(k)
1082  {
1083   for(i=0;i<32;i++)
1084   {
1085    k=read(fd,&buf[i],1);
1086    if((buf[i]=='\n')||(k==0)) break;
1087   }
1088   if(k)
1089   {
1090    p=strtok(buf," ");
1091    a=atof(p)/10; /* nm */
1092    p=strtok(NULL," ");
1093    b=atof(p);
1094    if(a>d3_l->max_z*CELL_LENGTH) k=0;
1095    else 
1096    {
1097     graph[(int)(a/CELL_LENGTH)]=(int)(URAND_MAX/100*b);
1098     flag[(int)(a/CELL_LENGTH)]=1;
1099    }
1100   }
1101  }
1102  /* do (linear) interpolation here! */
1103  i=0;
1104  a=0;
1105  while(i<d3_l->max_z)
1106  {
1107   /* graph[0] is 0! */
1108   j=i;
1109   i++;
1110   while(flag[i]==0&&i<d3_l->max_z) i++;
1111   for(k=j+1;k<i;k++) graph[k]=(int)((k-j)*((int)graph[i]-(int)graph[j])/(i-j))+graph[j];
1112   if(graph[i]>max) max=graph[i];
1113  }
1114
1115  free(flag);
1116
1117 #ifdef DEBUG_INTERPOL_PROFILE
1118  printf("debug: %s (interpolated profile)\n",file);
1119  for(i=0;i<d3_l->max_z;i++) printf("%d %d\n",i,graph[i]);
1120 #endif
1121
1122  return max;
1123 }
1124
1125 int get_amorphous_layer_info(d3_lattice *d3_l,int *sai,int *sacl,int *eacl) {
1126   int i,j,a,oend,nend,count;
1127   unsigned char sacl_is_set=0;
1128   unsigned char eacl_is_set=0;
1129   unsigned char sai_is_set=0;
1130
1131   a=d3_l->max_x*d3_l->max_y;
1132   nend=a;
1133   oend=0;
1134
1135   for(i=0;i<d3_l->max_z;i++) {
1136     count=0;
1137     for(j=oend;j<nend;j++) if(*(d3_l->status+j)&AMORPH) count++;
1138     oend=nend;
1139     nend+=a;
1140     if((count>=A_START*a)&&(!sai_is_set)) {
1141       *sai=i;
1142       sai_is_set=1;
1143     }
1144     if((count>=AC_START*a)&&(!sacl_is_set)) {
1145       *sacl=i;
1146       sacl_is_set=1;
1147     }
1148     if((count<=A_END*a)&&(sacl_is_set)&&(!eacl_is_set)) {
1149       *eacl=i;
1150       eacl_is_set=1;
1151     }
1152     if((eacl_is_set)&&(count>=A_END*a)) eacl_is_set=0;
1153   }
1154   return 1;
1155 }
1156
1157 int main(int argc,char **argv)
1158 {
1159  u32 x,y,z,x_c,y_c,z_c;
1160  int i,j,quit,escape,switchmode,nowait,bmp,ac_distr;
1161  int refresh,resave;
1162  int c_step;
1163  int sai,sacl,eacl;
1164  char s_file[MAX_CHARS];
1165  char s_file_tmp[MAX_CHARS];
1166  char l_file[MAX_CHARS];
1167  char c_file[MAX_CHARS];
1168  char p_file[MAX_CHARS];
1169  char n_e_file[MAX_CHARS];
1170  char convert;
1171  char r_file[MAX_CHARS];
1172 #ifdef USE_DFB_API
1173  char xyz_txt[MAX_TXT];
1174  char status_txt[MAX_TXT];
1175  char conc_txt[MAX_TXT];
1176  char steps_txt[MAX_TXT];
1177  char cc_txt[MAX_TXT];
1178  char a_txt[MAX_TXT];
1179  char s_txt[MAX_TXT];
1180  char ballistic_txt[MAX_TXT];
1181  char carbon_txt[MAX_TXT];
1182  char stress_txt[MAX_TXT];
1183  char r_txt[MAX_TXT];
1184  char zdiff_txt[MAX_TXT];
1185  char diff_txt[MAX_TXT];
1186  char dr_ac_txt[MAX_TXT];
1187  char dose_txt[MAX_TXT];
1188  char mode_txt[MAX_TXT];
1189  char hpi_txt[MAX_TXT];
1190  char srate_txt[MAX_TXT];
1191  char start_ai_txt[MAX_TXT];
1192  char start_acl_txt[MAX_TXT];
1193  char end_acl_txt[MAX_TXT];
1194  char *arg_v[MAX_ARGV];
1195 #endif
1196  d3_lattice d3_l;
1197  info my_info;
1198  unsigned char mode;
1199  double c_ratio;
1200 #ifdef USE_DFB_API
1201  int max_extra;
1202 #endif
1203  u32 *c_profile;
1204  u32 *n_e_loss;
1205  u32 ne_max,ip_max;
1206  u32 nel_z;
1207  unsigned char do_sputter;
1208
1209  d3_l.max_x=_X;
1210  d3_l.max_y=_Y;
1211  d3_l.max_z=_Z;
1212  my_info.steps=STEPS;
1213  my_info.range=RANGE;
1214  refresh=REFRESH;
1215  resave=RESAVE;
1216  my_info.s=S_D;
1217  my_info.b=B_D;
1218  my_info.c=C_D;
1219  my_info.cc=CC;
1220  my_info.dr_ac=DR_AC;
1221  my_info.diff_rate=DIFF_RATE;
1222  my_info.cpi=CPI;
1223  my_info.s_rate=S_RATE;
1224  nowait=0;
1225  quit=0;
1226  escape=0;
1227  switchmode=0;
1228  c_step=0;
1229  strcpy(s_file,"");
1230  strcpy(l_file,"");
1231  strcpy(c_file,"");
1232  strcpy(p_file,IMP_PROFILE);
1233  strcpy(n_e_file,NEL_PROFILE);
1234  convert=0;
1235  strcpy(r_file,"");
1236  mode=0;
1237  ne_max=0;
1238  ip_max=0;
1239  sai=0;
1240  sacl=0;
1241  eacl=0;
1242  do_sputter=1;
1243
1244 #ifdef MORE_PRINTF
1245  printf("reading argv ...");
1246 #endif
1247
1248  for(i=1;i<argc;i++)
1249  {
1250   if(argv[i][0]=='-')
1251   {
1252    switch(argv[i][1])
1253    {
1254     case 'h':
1255      usage();
1256      return -1;
1257     case 'n':
1258      nowait=1;
1259      break;
1260     case 'x':
1261      d3_l.max_x=atoi(argv[++i]);
1262      break;
1263     case 'y':
1264      d3_l.max_y=atoi(argv[++i]);
1265      break;
1266     case 'z':
1267      d3_l.max_z=atoi(argv[++i]);
1268      break;
1269     case 's':
1270      my_info.steps=atoi(argv[++i]);
1271      break;
1272     case 'd':
1273      refresh=atoi(argv[++i]);
1274      break;
1275     case 'r':
1276      my_info.range=atoi(argv[++i]);
1277      break;
1278     case 'f':
1279      my_info.s=atof(argv[++i]);
1280      break;
1281     case 'p':
1282      my_info.b=atof(argv[++i]);
1283      break;
1284     case 'F':
1285      my_info.c=atof(argv[++i]);
1286      break;
1287     case 'W':
1288      resave=atoi(argv[++i]);
1289      break;
1290     case 'C':
1291      strcpy(l_file,argv[++i]);
1292      if(i<argc-1) if(argv[i+1][0]!='-') strcpy(c_file,argv[++i]);
1293      convert=1;
1294      break;
1295     case 'D':
1296      my_info.dr_ac=atof(argv[++i]);
1297      break;
1298     case 'e':
1299      my_info.diff_rate=atoi(argv[++i]);
1300      break;
1301     case 'L':
1302      strcpy(l_file,argv[++i]);
1303      break;
1304     case 'S':
1305      strcpy(s_file,argv[++i]);
1306      break;
1307     case 'R':
1308      strcpy(r_file,argv[++i]);
1309      break;
1310     case 'P':
1311      strcpy(p_file,argv[++i]);
1312      break;
1313     case 'N':
1314      strcpy(n_e_file,argv[++i]);
1315      break;
1316     case 'g':
1317      strcpy(l_file,argv[++i]);
1318      if(i<argc-1) if(argv[i+1][0]!='-') c_step=atoi(argv[++i]);
1319      break;
1320     case 'H':
1321      my_info.cpi=atoi(argv[++i]);
1322      break;
1323     case 'm':
1324      my_info.s_rate=atoi(argv[++i]);
1325      break;
1326     default:
1327      usage();
1328      return -1;
1329    }
1330   } else usage();
1331  }
1332
1333 #ifdef MORE_PRINTF
1334  printf(" done\n");
1335 #endif
1336
1337  x=d3_l.max_x/2-1;
1338  y=d3_l.max_y/2-1;
1339  z=d3_l.max_z/2-1;
1340
1341 #ifdef NODFB
1342  if(!strcmp(s_file,""))
1343  {
1344   puts("NODFB defined, run with -S option");
1345   return -1;
1346  }
1347 #endif
1348
1349  printf("random api init ...");
1350  if(!strcmp(r_file,"")) rand_init(NULL);
1351  else rand_init(r_file);
1352  printf(" done\n");
1353
1354  if(!strcmp(l_file,""))
1355  {
1356   i=d3_l.max_x*d3_l.max_y*d3_l.max_z;
1357 #ifdef USE_DFB_API
1358   printf("d3 lattice init ...");
1359   d3_lattice_init(&argc,argv,&d3_l);
1360   printf(" done\n");
1361 #endif
1362   if((d3_l.status=(unsigned char *)malloc(i*sizeof(unsigned char)))==NULL)
1363   {
1364    puts("failed allocating status buffer");
1365    return -1;
1366   }
1367   memset(d3_l.status,0,i*sizeof(unsigned char));
1368   if((d3_l.extra=(int *)malloc(i*sizeof(int)))==NULL)
1369   {
1370    puts("failed allocating concentration buffer");
1371    return -1;
1372   }
1373   memset(d3_l.extra,0,i*sizeof(int));
1374  } else
1375  {
1376   printf("loading file ...");
1377   load_from_file(l_file,&d3_l,&my_info);
1378   printf(" done\n");
1379   if(convert) 
1380   {   
1381    if(!strcmp(c_file,"")) sprintf(c_file,"%s_gnuplot",l_file);
1382    printf("converting file %s to %s\n",l_file,c_file);
1383    convert_file(c_file,&d3_l);
1384    puts("done");
1385    return 1;
1386   }
1387 #ifdef USE_DFB_API
1388   printf("d3 lattice init ...");
1389   d3_lattice_init(&argc,argv,&d3_l);
1390   printf(" done\n");
1391 #endif
1392  }
1393
1394 #ifdef USE_DFB_API
1395  printf("event init ...");
1396  d3_event_init(&d3_l);
1397  printf(" done\n");
1398 #endif
1399
1400 #ifdef USE_DFB_API
1401  strcpy(a_txt,"args:");
1402  sprintf(s_txt,"steps: %d",my_info.steps);
1403  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));
1404  sprintf(r_txt,"stress influence range: %d",my_info.range);
1405  sprintf(stress_txt,"stress term: %f",my_info.s);
1406  sprintf(ballistic_txt,"ballistic term: %f",my_info.b);
1407  sprintf(carbon_txt,"carbon term: %f",my_info.c);
1408  sprintf(dr_ac_txt,"a/c diffusion rate: %f",my_info.dr_ac);
1409  sprintf(zdiff_txt,"diffusion in z direction: yes");
1410  sprintf(diff_txt,"diffusion every %d steps",my_info.diff_rate);
1411  strcpy(mode_txt,"view: a/c mode");
1412  sprintf(hpi_txt,"hits per ion: %d",my_info.cpi);
1413  sprintf(srate_txt,"sputter rate (3nm/#c): %d",my_info.s_rate);
1414  get_amorphous_layer_info(&d3_l,&sai,&sacl,&eacl);
1415  sprintf(start_ai_txt,"start of a-inclusions: %d nm (%d)",3*sai,sai);
1416  sprintf(start_acl_txt,"start of a-layer: %d nm (%d)",3*sacl,sacl);
1417  sprintf(end_acl_txt,"end of a-layer: %d nm (%d)",3*eacl,eacl);
1418  arg_v[1]=mode_txt;
1419  arg_v[2]=xyz_txt;
1420  arg_v[3]=status_txt;
1421  arg_v[4]=conc_txt;
1422  arg_v[5]=NULL;
1423  arg_v[6]=steps_txt;
1424  arg_v[7]=cc_txt;
1425  arg_v[8]=NULL;
1426  arg_v[9]=a_txt;
1427  arg_v[10]=s_txt;
1428  arg_v[11]=dose_txt;
1429  arg_v[12]=diff_txt;
1430  arg_v[13]=zdiff_txt;
1431  arg_v[14]=dr_ac_txt;
1432  arg_v[15]=ballistic_txt;
1433  arg_v[16]=carbon_txt;
1434  arg_v[17]=stress_txt;
1435  arg_v[18]=r_txt;
1436  arg_v[19]=hpi_txt;
1437  arg_v[20]=srate_txt;
1438  arg_v[21]=NULL;
1439  arg_v[22]=start_ai_txt;
1440  arg_v[23]=start_acl_txt;
1441  arg_v[24]=end_acl_txt;
1442  arg_v[25]=NULL;
1443 #endif
1444
1445  /* compute graphs for random number rejection method */
1446  printf("random rejection graphs ...");
1447  if((c_profile=(u32 *)malloc(d3_l.max_z*sizeof(unsigned int)))==NULL)
1448  {
1449   puts("failed allocating memory for carbon profile graph");
1450   return -1;
1451  }
1452  if((n_e_loss=(u32 *)malloc(d3_l.max_z*sizeof(unsigned int)))==NULL)
1453  {
1454   puts("failed allocating memory for nuclear energy loss graph");
1455   return -1;
1456  }
1457  ip_max=get_reject_graph(&my_info,&d3_l,p_file,c_profile);
1458  ne_max=get_reject_graph(&my_info,&d3_l,n_e_file,n_e_loss);
1459  printf(" done\n");
1460
1461  if((!strcmp(l_file,""))||(c_step))
1462  {
1463
1464   /* this should be obsolete - z is high enough - we check now! */
1465   if(c_profile[d3_l.max_z-1]!=0)
1466   {
1467    printf("max_z (%d) too small - sputtering not possible\n",d3_l.max_z);
1468    do_sputter=0;
1469   }
1470   /* calculate ratio of c_simwindow / c_total */
1471   if(!do_sputter)
1472   {
1473    get_c_ratio(&c_ratio,p_file,&my_info,&d3_l);
1474    printf("calculated c ratio: %f\n",c_ratio);
1475   }
1476
1477   /* sputtering realy possible ?*/
1478   if(n_e_loss[d3_l.max_z-1]!=0)
1479    printf("warning: max_z (%d) too small - there may be amorphous volumes\n",d3_l.max_z);
1480
1481 #ifdef DEBUG_RAND
1482  i=0;
1483  while(1)
1484  {
1485 #ifdef DEBUG_CP
1486   printf("%d\n",get_rand_reject(d3_l.max_z,ip_max,c_profile));
1487 #endif
1488 #ifdef DEBUG_NEL
1489   printf("%d\n",get_rand_reject(d3_l.max_z,ne_max,n_e_loss));
1490 #endif
1491 #ifdef DEBUG_NORM
1492   printf("%d\n",get_rand(d3_l.max_z));
1493 #endif
1494 #ifdef DEBUG_LINEAR
1495   printf("%d\n",get_rand_lgp(d3_l.max_z,1,0));
1496 #endif
1497  if(i==10000000) return 1;
1498  i++;
1499  }
1500 #endif
1501
1502 #ifdef MORE_PRINTF
1503   printf("starting simulation ... now! :)\n");
1504 #endif
1505
1506   i=(c_step?c_step:0);
1507   while((i<my_info.steps) && (quit==0) && (escape==0))
1508   {
1509 #ifdef MORE_PRINTF
1510    if(i%refresh==0) printf("step: %d\n",i);
1511 #endif
1512    for(j=0;j<my_info.cpi;j++)
1513    {
1514     x_c=get_rand(d3_l.max_x);
1515     y_c=get_rand(d3_l.max_y);
1516     // z_c=get_rand_reject(d3_l.max_z,ne_max,n_e_loss);
1517     z_c=get_rand(d3_l.max_z);
1518     nel_z=URAND_MAX*(1.0*n_e_loss[z_c]/ne_max);
1519     process_cell(&d3_l,x_c,y_c,z_c,&my_info,nel_z);
1520    }
1521    distrib_c(&d3_l,&my_info,i,ip_max,c_profile);
1522 #ifdef USE_DFB_API
1523    if(i%refresh==0)
1524    {
1525     sprintf(xyz_txt,"x: %d  y: %d  z: %d (%d nm)",x+1,y+1,z+1,z*3);
1526     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');
1527     sprintf(conc_txt,"carbon: %d",*(d3_l.extra+x+y*d3_l.max_x+z*d3_l.max_x*d3_l.max_y));
1528     sprintf(steps_txt,"step: %d",i);
1529     sprintf(cc_txt,"total c: %d",my_info.cc);
1530     get_amorphous_layer_info(&d3_l,&sai,&sacl,&eacl);
1531     sprintf(start_ai_txt,"start of a-inclusions: %d nm (%d)",3*sai,sai);
1532     sprintf(start_acl_txt,"start of a-layer: %d nm (%d)",3*sacl,sacl);
1533     sprintf(end_acl_txt,"end of a-layer: %d nm (%d)",3*eacl,eacl);
1534     d3_lattice_draw(&d3_l,x,y,z,25,arg_v,mode,0,NULL,0,NULL,0);
1535    }
1536 #endif
1537    if(i%resave==0 && strcmp(s_file,"") && resave!=0 && i!=0)
1538    {
1539     sprintf(s_file_tmp,"%s_%d_of_%d",s_file,i,my_info.steps);
1540     save_to_file(s_file_tmp,&d3_l,&my_info);
1541 #ifdef NODFB
1542     printf("saved %s\n",s_file_tmp);
1543 #endif
1544    }
1545    i++;
1546    if((do_sputter)&(i%my_info.s_rate==0)) sputter(&d3_l);
1547   }
1548  }
1549
1550  if(strcmp(s_file,""))
1551  {
1552    printf("saved %s\n",s_file);
1553    save_to_file(s_file,&d3_l,&my_info);
1554  }
1555
1556 #ifdef USE_DFB_API
1557  /* allocating buffer for pressure values */
1558  printf("allocating buffer for preassure values ...");
1559  if((d3_l.v_ptr=malloc(d3_l.max_x*d3_l.max_y*d3_l.max_z*sizeof(unsigned int)))==NULL)
1560  {
1561   puts("cannot allocate buffer for pressure values");
1562   return -1;
1563  }
1564  printf(" done\n");
1565  /* calc values */
1566  printf("calculating preassure values ...");
1567  calc_pressure(&d3_l,my_info.range);
1568  printf(" done\n");
1569  printf("finding maximum carbon concentration ...");
1570  max_extra=calc_max_extra(&d3_l);
1571  printf(" done\n");
1572
1573  while((quit==0) && (escape==0) && (nowait==0))
1574  {
1575   /* bahh! */
1576   if(switchmode==0) mode=0;
1577   if(switchmode==1) mode=1;
1578   if(switchmode==2) mode=2;
1579   if(switchmode==3) mode=3;
1580   /* end of bahh! */
1581   sprintf(xyz_txt,"x: %d  y: %d  z: %d (%d nm)",x+1,y+1,z+1,z*3);
1582   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');
1583   sprintf(conc_txt,"carbon: %d",*(d3_l.extra+x+y*d3_l.max_x+z*d3_l.max_x*d3_l.max_y));
1584   sprintf(steps_txt,"step: end");
1585   sprintf(cc_txt,"total c: %d",my_info.cc);
1586   if(switchmode==0) strcpy(mode_txt,"view: a/c mode");
1587   if(switchmode==1) strcpy(mode_txt,"view: c conc mode");
1588   if(switchmode==2) strcpy(mode_txt,"view: a pressure mode");
1589   if(switchmode==3) strcpy(mode_txt,"view: a/c + profiles mode");
1590   d3_lattice_draw(&d3_l,x,y,z,25,arg_v,mode,max_extra,c_profile,ip_max,n_e_loss,ne_max);
1591   bmp=0;
1592   ac_distr=0;
1593   scan_event(&d3_l,&x,&y,&z,&quit,&escape,&switchmode,&bmp,&ac_distr);
1594   if(bmp) write_bmp(&d3_l,bmp,x,y,z,max_extra);
1595   if(ac_distr) write_ac_distr(&d3_l,ac_distr);
1596  }
1597
1598  d3_lattice_release(&d3_l);
1599 #endif
1600
1601  return 1;
1602 }