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