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