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