4 * author: frank zirkelbach (frank.zirkelbach@physik.uni-augsburg.de)
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.
13 * - J. K. N. Lindner. Habil.Schrift, Universitaet Augsburg.
14 * - Maik Haeberlen. Diplomarbeit, Universitaet Augsburg.
16 * Copyright (C) 2004 Frank Zirkelbach
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.
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.
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
38 #include <sys/types.h>
48 #define MAKE_AMORPH(N) *(N)|=AMORPH
49 #define MAKE_CRYST(N) *(N)&=~AMORPH
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("-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");
84 int process_cell(d3_lattice *d3_l,u32 x,u32 y,u32 z,info *my_info,u32 nel_z)
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;
95 for(i=-(my_info->range);i<=my_info->range;i++)
97 for(j=-(my_info->range);j<=my_info->range;j++)
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->s*(*(d3_l->extra+off))*URAND_MAX/(i*i+j*j);
106 p+=*conc*my_info->c*URAND_MAX;
109 if(get_rand(URAND_MAX)<=p) MAKE_AMORPH(thiz);
112 /* assume 1-p probability */
113 /* also look for neighbours ! */
114 q=(URAND_MAX-p)>0?URAND_MAX-p: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;
124 if(get_rand(URAND_MAX)>p) MAKE_CRYST(thiz);
130 int distrib_c(d3_lattice *d3_l,info *my_info,int step,double c_ratio,u32 rj_m,u32 *rj_g)
137 /* put one c ion somewhere in the lattice */
138 if(my_info->cc<c_ratio*step)
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;
147 if(step%my_info->diff_rate==0)
150 for(i=0;i<d3_l->max_x;i++)
152 for(j=0;j<d3_l->max_y;j++)
154 for(k=0;k<d3_l->max_z;k++)
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)
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;
166 if(!(*(d3_l->status+off)&AMORPH)) carry=(int)(my_info->dr_ac*(*(d3_l->extra+off)));
169 *(d3_l->extra+offset)+=carry;
170 *(d3_l->extra+off)-=carry;
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;
180 if(!(*(d3_l->status+off)&AMORPH)) carry=(int)(my_info->dr_ac*(*(d3_l->extra+off)));
183 *(d3_l->extra+offset)+=carry;
184 *(d3_l->extra+off)-=carry;
192 off=i+j*d3_l->max_x+(k-1)*d3_l->max_x*d3_l->max_y;
194 if(!*(d3_l->status+off)&AMORPH) carry=(int)(my_info->dr_ac*(*(d3_l->extra+off)));
197 *(d3_l->extra+off)-=carry;
198 *(d3_l->extra+offset)+=carry;
203 off=i+j*d3_l->max_x+(k+1)*d3_l->max_x*d3_l->max_y;
205 if(!*(d3_l->status+off)&AMORPH) carry=(int)(my_info->dr_ac*(*(d3_l->extra+off)));
208 *(d3_l->extra+off)-=carry;
209 *(d3_l->extra+offset)+=carry;
214 /* case not amorph: cryst <-> cryst diffusion */
215 if(my_info->c_diff) {
216 /* if there is c diff, no diff in z-direction */
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;
224 if(!(*(d3_l->status+off)&AMORPH))
226 carry=(int)(my_info->dr_cc*(*(d3_l->extra+off)-*(d3_l->extra+offset))/2);
229 *(d3_l->extra+offset)+=carry;
230 *(d3_l->extra+off)-=carry;
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;
241 if(!(*(d3_l->status+off)&AMORPH))
243 carry=(int)(my_info->dr_cc*(*(d3_l->extra+off)-*(d3_l->extra+offset))/2);
246 *(d3_l->extra+offset)+=carry;
247 *(d3_l->extra+off)-=carry;
260 } /* if step modulo diff_rate == 0 */
265 int calc_pressure(d3_lattice *d3_l,int range)
271 for(x=0;x<d3_l->max_x;x++)
273 for(y=0;y<d3_l->max_y;y++)
275 for(z=0;z<d3_l->max_z;z++)
278 for(i=-range;i<=range;i++)
280 for(j=-range;j<=range;j++)
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);
289 if(count>max) max=count;
294 for(x=0;x<d3_l->max_x;x++)
296 for(y=0;y<d3_l->max_y;y++)
298 for(z=0;z<d3_l->max_z;z++)
301 for(i=-range;i<=range;i++)
303 for(j=-range;j<=range;j++)
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);
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);
320 int calc_max_extra(d3_lattice *d3_l)
325 for(x=0;x<d3_l->max_x;x++)
327 for(y=0;y<d3_l->max_y;y++)
329 for(z=0;z<d3_l->max_z;z++)
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);
340 int write_ac_distr(d3_lattice *d3_l,int ac_distr)
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");
353 if((fd=open(file,O_WRONLY|O_CREAT))<0)
355 puts("cannot open plot file");
359 for(z=0;z<d3_l->max_z;z++)
362 for(x=0;x<d3_l->max_x;x++)
364 for(y=0;y<d3_l->max_y;y++)
366 offset=x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y;
368 if(*(d3_l->status+offset)&AMORPH) count+=*(d3_l->extra+offset);
370 if(!(*(d3_l->status+offset)&AMORPH)) count+=*(d3_l->extra+offset);
371 if(ac_distr==3) count+=*(d3_l->extra+offset);
373 if(*(d3_l->status+offset)&AMORPH) count+=1;
377 if(ac_distr==4) dprintf(fd,"%d %d\n",z*CELL_LENGTH,count);
378 else dprintf(fd,"%d %f\n",z*CELL_LENGTH,100.0*count/si_count);
386 int write_bmp(d3_lattice *d3_l,int window,u32 x,u32 y,u32 z,int max)
388 int fd,i,j,size=0,foo=0,k,sum;
389 int width=0,height=0;
390 char bmpfile[MAX_CHARS];
395 sprintf(bmpfile,"x-z_%d.bmp",y);
397 size=(foo+(4-foo%4))*d3_l->max_z;
403 sprintf(bmpfile,"y-z_%d.bmp",x);
405 size=(foo+(4-foo%4))*d3_l->max_z;
411 sprintf(bmpfile,"x-y_%d.bmp",z);
413 size=(foo+(4-foo%4))*d3_l->max_y;
419 sprintf(bmpfile,"x-z_cdistr_%d.bmp",y);
421 size=(foo+(4-foo%4))*d3_l->max_z;
427 sprintf(bmpfile,"y-z_cdistr_%d.bmp",x);
429 size=(foo+(4-foo%4))*d3_l->max_z;
435 sprintf(bmpfile,"x-y_cdistr_%d.bmp",z);
437 size=(foo+(4-foo%4))*d3_l->max_y;
443 sprintf(bmpfile,"x-z_a_cdistr_%d.bmp",y);
445 size=(foo+(4-foo%4))*d3_l->max_z;
451 sprintf(bmpfile,"y-z_a_cdistr_%d.bmp",x);
453 size=(foo+(4-foo%4))*d3_l->max_z;
459 sprintf(bmpfile,"x-y_a_cdistr_%d.bmp",z);
461 size=(foo+(4-foo%4))*d3_l->max_y;
467 sprintf(bmpfile,"x-z_c_cdistr_%d.bmp",y);
469 size=(foo+(4-foo%4))*d3_l->max_z;
475 sprintf(bmpfile,"y-z_c_cdistr_%d.bmp",x);
477 size=(foo+(4-foo%4))*d3_l->max_z;
483 sprintf(bmpfile,"x-y_c_cdistr_%d.bmp",z);
485 size=(foo+(4-foo%4))*d3_l->max_y;
489 if((fd=open(bmpfile,O_WRONLY|O_CREAT))<0)
491 puts("cannot open bmp file");
496 buf[0]='B'; /* std header start */
498 buf[2]=(size+0x36)&0xff; /* file size */
499 buf[3]=(size+0x36)>>8;
501 buf[10]=0x36; /* offset to data */
503 buf[14]=0x28; /* length of bmp info header */
505 buf[18]=width&0xff; /* width and height */
511 buf[26]=1; /* # planes -> 1 */
513 buf[28]=24; /* bits per pixel -> 2^24 (true color) */
515 memset(buf+30,0,4); /* compression -> none */
516 buf[34]=size&0xff; /* data size */
519 buf[38]=0x12; /* res: pixel/meter */
525 memset(buf+46,0,8); /* no colors, no important colors */
527 if(write(fd,buf,54)<54)
529 puts("failed writing bmp header");
534 for(j=0;j<d3_l->max_z;j++)
536 for(i=0;i<d3_l->max_x;i++)
540 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;
542 memset(buf,(unsigned char)sum,3);
543 if(write(fd,buf,3)<3)
545 puts("failed writing rgb values to bmp file");
551 memset(buf,0,4-foo%4);
552 if(write(fd,buf,4-foo%4)<4-foo%4)
554 puts("failed writing 4 byte ending");
562 for(j=0;j<d3_l->max_z;j++)
564 for(i=0;i<d3_l->max_y;i++)
568 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;
570 memset(buf,(unsigned char)sum,3);
571 if(write(fd,buf,3)<3)
573 puts("failed writing rgb values to bmp file");
579 memset(buf,0,4-foo%4);
580 if(write(fd,buf,4-foo%4)<4-foo%4)
582 puts("failed writing 4 byte ending");
590 for(j=0;j<d3_l->max_y;j++)
592 for(i=0;i<d3_l->max_x;i++)
594 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);
595 else memset(buf,0,3);
596 if(write(fd,buf,3)<3)
598 puts("failed writing rgb values to bmp file");
604 memset(buf,0,4-foo%4);
605 if(write(fd,buf,4-foo%4)<4-foo%4)
607 puts("failed writing 4 byte ending");
615 for(j=0;j<d3_l->max_z;j++)
617 for(i=0;i<d3_l->max_x;i++)
619 sum=*(d3_l->extra+i+y*d3_l->max_x+(d3_l->max_z-j-1)*d3_l->max_x*d3_l->max_y);
621 memset(buf,(unsigned char)sum,3);
622 if(write(fd,buf,3)<3)
624 puts("failed writing rgb values to bmp file");
630 memset(buf,0,4-foo%4);
631 if(write(fd,buf,4-foo%4)<4-foo%4)
633 puts("failed writing 4 byte ending");
641 for(j=0;j<d3_l->max_z;j++)
643 for(i=0;i<d3_l->max_x;i++)
645 sum=*(d3_l->extra+x+i*d3_l->max_x+(d3_l->max_z-j-1)*d3_l->max_x*d3_l->max_y);
647 memset(buf,(unsigned char)sum,3);
648 if(write(fd,buf,3)<3)
650 puts("failed writing rgb values to bmp file");
656 memset(buf,0,4-foo%4);
657 if(write(fd,buf,4-foo%4)<4-foo%4)
659 puts("failed writing 4 byte ending");
667 for(j=0;j<d3_l->max_y;j++)
669 for(i=0;i<d3_l->max_x;i++)
671 sum=*(d3_l->extra+i+(d3_l->max_y-j-1)*d3_l->max_x+z*d3_l->max_x*d3_l->max_y);
673 memset(buf,(unsigned char)sum,3);
674 if(write(fd,buf,3)<3)
676 puts("failed writing rgb values to bmp file");
682 memset(buf,0,4-foo%4);
683 if(write(fd,buf,4-foo%4)<4-foo%4)
685 puts("failed writing 4 byte ending");
693 for(j=0;j<d3_l->max_z;j++)
695 for(i=0;i<d3_l->max_x;i++)
697 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);
700 memset(buf,(unsigned char)sum,3);
701 if(write(fd,buf,3)<3)
703 puts("failed writing rgb values to bmp file");
709 memset(buf,0,4-foo%4);
710 if(write(fd,buf,4-foo%4)<4-foo%4)
712 puts("failed writing 4 byte ending");
720 for(j=0;j<d3_l->max_z;j++)
722 for(i=0;i<d3_l->max_x;i++)
724 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);
727 memset(buf,(unsigned char)sum,3);
728 if(write(fd,buf,3)<3)
730 puts("failed writing rgb values to bmp file");
736 memset(buf,0,4-foo%4);
737 if(write(fd,buf,4-foo%4)<4-foo%4)
739 puts("failed writing 4 byte ending");
747 for(j=0;j<d3_l->max_y;j++)
749 for(i=0;i<d3_l->max_x;i++)
751 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);
754 memset(buf,(unsigned char)sum,3);
755 if(write(fd,buf,3)<3)
757 puts("failed writing rgb values to bmp file");
763 memset(buf,0,4-foo%4);
764 if(write(fd,buf,4-foo%4)<4-foo%4)
766 puts("failed writing 4 byte ending");
774 for(j=0;j<d3_l->max_z;j++)
776 for(i=0;i<d3_l->max_x;i++)
778 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);
781 memset(buf,(unsigned char)sum,3);
782 if(write(fd,buf,3)<3)
784 puts("failed writing rgb values to bmp file");
790 memset(buf,0,4-foo%4);
791 if(write(fd,buf,4-foo%4)<4-foo%4)
793 puts("failed writing 4 byte ending");
801 for(j=0;j<d3_l->max_z;j++)
803 for(i=0;i<d3_l->max_x;i++)
805 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);
808 memset(buf,(unsigned char)sum,3);
809 if(write(fd,buf,3)<3)
811 puts("failed writing rgb values to bmp file");
817 memset(buf,0,4-foo%4);
818 if(write(fd,buf,4-foo%4)<4-foo%4)
820 puts("failed writing 4 byte ending");
828 for(j=0;j<d3_l->max_y;j++)
830 for(i=0;i<d3_l->max_x;i++)
832 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);
835 memset(buf,(unsigned char)sum,3);
836 if(write(fd,buf,3)<3)
838 puts("failed writing rgb values to bmp file");
844 memset(buf,0,4-foo%4);
845 if(write(fd,buf,4-foo%4)<4-foo%4)
847 puts("failed writing 4 byte ending");
858 int save_to_file(char *sf,d3_lattice *d3_l,info *my_inf)
862 if((sf_fd=open(sf,O_WRONLY|O_CREAT))<0)
864 puts("cannot open save file");
867 if(write(sf_fd,d3_l,sizeof(d3_lattice))<sizeof(d3_lattice))
869 puts("failed saving d3 lattice struct");
872 if(write(sf_fd,my_inf,sizeof(info))<sizeof(info))
874 puts("failed saving info struct");
877 c=d3_l->max_x*d3_l->max_y*d3_l->max_z;
878 if(write(sf_fd,d3_l->status,c*sizeof(unsigned char))<c*sizeof(unsigned char))
880 puts("failed saving status of d3 lattice sites");
883 if(write(sf_fd,d3_l->extra,c*sizeof(int))<c*sizeof(int))
885 puts("failed saving sites concentration");
893 int load_from_file(char *lf,d3_lattice *d3_l,info *my_inf)
896 int lf_fd,c,pos,end,data,data_len,strip;
898 if((lf_fd=open(lf,O_RDONLY))<0)
900 puts("cannot open load file");
903 if(read(lf_fd,d3_l,sizeof(d3_lattice))<sizeof(d3_lattice))
905 puts("failed reading d3 lattice struct");
908 pos=lseek(lf_fd,0,SEEK_CUR);
909 printf("psition: %d (%d)\n",pos,sizeof(d3_lattice));
910 data=d3_l->max_x*d3_l->max_y*d3_l->max_z;
911 data_len=data*(sizeof(int)+sizeof(unsigned char));
912 printf("there are %d volumes so we need %d of bytes\n",data,data_len);
913 end=lseek(lf_fd,0,SEEK_END);
915 printf("end: %d => length: %d => guessed info size: %d bytes\n",end,end-pos,c);
916 strip=sizeof(info)-c;
917 printf("as real programs info size is %d, we strip %d bytes\n",sizeof(info),strip);
918 lseek(lf_fd,pos,SEEK_SET);
920 if(strip>0) c-=strip;
923 puts("info smaller then strip size");
926 if(read(lf_fd,my_inf,c)<c)
928 puts("failed reading info struct");
931 if(strip>0) memset(my_inf+c,0,strip);
932 if(strip<0) lseek(lf_fd,(-1*strip),SEEK_CUR);
933 c=d3_l->max_x*d3_l->max_y*d3_l->max_z;
934 if((d3_l->status=(unsigned char*)malloc(c*sizeof(unsigned char)))==NULL)
936 puts("cannot allocate status buffer");
939 if((d3_l->extra=(int *)malloc(c*sizeof(int)))==NULL)
941 puts("cannot allocate concentration buffer");
944 if(read(lf_fd,d3_l->status,c*sizeof(unsigned char))<c*sizeof(unsigned char))
946 puts("failed reading status of d3 lattice sites");
949 if(read(lf_fd,d3_l->extra,c*sizeof(int))<c*sizeof(int))
951 puts("failed reading sites concentration");
959 int convert_file(char *cf,d3_lattice *d3_l)
965 if((c_fd=open(cf,O_WRONLY|O_CREAT))<0)
967 puts("cannot open convert file");
970 dprintf(c_fd,"# created by nlsop (gnuplot format)\n");
971 for(x=0;x<d3_l->max_x;x++)
973 for(y=0;y<d3_l->max_y;y++)
975 for(z=0;z<d3_l->max_z;z++)
977 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);
987 int get_c_ratio(double *c_ratio,char *pfile,info *my_info,d3_lattice *d3_l)
992 unsigned char buf[32];
996 if((p_fd=open(pfile,O_RDONLY))<0)
998 puts("cannot open profile file");
1015 a=atof(p)/10; /* nm */
1018 if(a<=d3_l->max_z*CELL_LENGTH) d+=b;
1028 u32 get_reject_graph(info *my_info,d3_lattice *d3_l,char *file,u32 *graph) {
1033 unsigned char *flag;
1037 if((fd=open(file,O_RDONLY))<0)
1039 puts("cannot open file to calculate rejection graph");
1042 if((flag=(unsigned char *)malloc(d3_l->max_z))==NULL)
1044 puts("cannot malloc flag memory for rejection graph");
1047 memset(flag,0,d3_l->max_z);
1048 memset(graph,0,d3_l->max_z*sizeof(u32));
1055 k=read(fd,&buf[i],1);
1056 if((buf[i]=='\n')||(k==0)) break;
1061 a=atof(p)/10; /* nm */
1064 if(a>d3_l->max_z*CELL_LENGTH) k=0;
1067 graph[(int)(a/CELL_LENGTH)]=(int)(URAND_MAX/100*b);
1068 flag[(int)(a/CELL_LENGTH)]=1;
1072 /* do (linear) interpolation here! */
1075 while(i<d3_l->max_z)
1077 /* graph[0] is 0! */
1080 while(flag[i]==0&&i<d3_l->max_z) i++;
1081 for(k=j+1;k<i;k++) graph[k]=(int)((k-j)*((int)graph[i]-(int)graph[j])/(i-j))+graph[j];
1082 if(graph[i]>max) max=graph[i];
1087 #ifdef DEBUG_INTERPOL_PROFILE
1088 printf("debug: %s (interpolated profile)\n",file);
1089 for(i=0;i<d3_l->max_z;i++) printf("%d %d\n",i,graph[i]);
1095 int main(int argc,char **argv)
1097 u32 x,y,z,x_c,y_c,z_c;
1098 int i,j,quit,escape,switchmode,nowait,bmp,ac_distr;
1101 char s_file[MAX_CHARS];
1102 char s_file_tmp[MAX_CHARS];
1103 char l_file[MAX_CHARS];
1104 char c_file[MAX_CHARS];
1105 char p_file[MAX_CHARS];
1106 char n_e_file[MAX_CHARS];
1108 char r_file[MAX_CHARS];
1110 char xyz_txt[MAX_TXT];
1111 char status_txt[MAX_TXT];
1112 char conc_txt[MAX_TXT];
1113 char steps_txt[MAX_TXT];
1114 char cc_txt[MAX_TXT];
1115 char a_txt[MAX_TXT];
1116 char s_txt[MAX_TXT];
1117 char ballistic_txt[MAX_TXT];
1118 char carbon_txt[MAX_TXT];
1119 char stress_txt[MAX_TXT];
1120 char r_txt[MAX_TXT];
1121 char zdiff_txt[MAX_TXT];
1122 char diff_txt[MAX_TXT];
1123 char dr_ac_txt[MAX_TXT];
1124 char dr_cc_txt[MAX_TXT];
1125 char dose_txt[MAX_TXT];
1126 char mode_txt[MAX_TXT];
1127 char hpi_txt[MAX_TXT];
1128 char csat_txt[MAX_TXT];
1129 char *arg_v[MAX_ARGV];
1146 my_info.steps=STEPS;
1147 my_info.range=RANGE;
1156 my_info.dr_ac=DR_AC;
1157 my_info.dr_cc=DR_CC;
1158 my_info.diff_rate=DIFF_RATE;
1160 my_info.c_sat=C_SAT;
1169 strcpy(p_file,IMP_PROFILE);
1170 strcpy(n_e_file,NEL_PROFILE);
1190 d3_l.max_x=atoi(argv[++i]);
1193 d3_l.max_y=atoi(argv[++i]);
1196 d3_l.max_z=atoi(argv[++i]);
1206 my_info.steps=atoi(argv[++i]);
1209 refresh=atoi(argv[++i]);
1212 my_info.range=atoi(argv[++i]);
1215 my_info.s=atof(argv[++i]);
1218 my_info.b=atof(argv[++i]);
1221 my_info.c=atof(argv[++i]);
1224 resave=atoi(argv[++i]);
1227 strcpy(l_file,argv[++i]);
1228 if(i<argc-1) if(argv[i+1][0]!='-') strcpy(c_file,argv[++i]);
1232 my_info.dr_ac=atof(argv[++i]);
1235 my_info.dr_cc=atof(argv[++i]);
1238 my_info.diff_rate=atoi(argv[++i]);
1241 strcpy(l_file,argv[++i]);
1244 strcpy(s_file,argv[++i]);
1247 strcpy(r_file,argv[++i]);
1250 strcpy(p_file,argv[++i]);
1253 strcpy(n_e_file,argv[++i]);
1256 strcpy(l_file,argv[++i]);
1257 if(i<argc-1) if(argv[i+1][0]!='-') c_step=atoi(argv[++i]);
1260 my_info.cpi=atoi(argv[++i]);
1263 my_info.c_sat=atoi(argv[++i]);
1277 if(!strcmp(s_file,""))
1279 puts("NODFB defined, run with -S option");
1284 if(!strcmp(r_file,"")) rand_init(NULL);
1285 else rand_init(r_file);
1287 if(!strcmp(l_file,""))
1289 i=d3_l.max_x*d3_l.max_y*d3_l.max_z;
1291 d3_lattice_init(&argc,argv,&d3_l);
1293 if((d3_l.status=(unsigned char *)malloc(i*sizeof(unsigned char)))==NULL)
1295 puts("failed allocating status buffer");
1298 memset(d3_l.status,0,i*sizeof(unsigned char));
1299 if((d3_l.extra=(int *)malloc(i*sizeof(int)))==NULL)
1301 puts("failed allocating concentration buffer");
1304 memset(d3_l.extra,0,i*sizeof(int));
1307 load_from_file(l_file,&d3_l,&my_info);
1310 if(!strcmp(c_file,"")) sprintf(c_file,"%s_gnuplot",l_file);
1311 printf("converting file %s to %s\n",l_file,c_file);
1312 convert_file(c_file,&d3_l);
1317 else d3_lattice_init(&argc,argv,&d3_l);
1322 d3_event_init(&d3_l);
1326 strcpy(a_txt,"args:");
1327 sprintf(s_txt,"steps: %d",my_info.steps);
1328 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));
1329 sprintf(r_txt,"pressure range: %d",my_info.range);
1330 sprintf(stress_txt,"stress term: %f",my_info.s);
1331 sprintf(ballistic_txt,"ballistic term: %f",my_info.b);
1332 sprintf(carbon_txt,"carbon term: %f",my_info.c);
1333 sprintf(dr_ac_txt,"a/c diffusion rate: %f",my_info.dr_ac);
1334 if(my_info.c_diff!=0) sprintf(dr_cc_txt,"c/c diffusion rate: %f",my_info.dr_cc);
1335 else sprintf(dr_cc_txt,"c/c diffusion rate: none");
1336 sprintf(zdiff_txt,"diffusion in z direction: %c",my_info.z_diff?'y':'n');
1337 sprintf(diff_txt,"diffusion every %d steps",my_info.diff_rate);
1338 strcpy(mode_txt,"view: a/c mode");
1339 sprintf(hpi_txt,"hits per ion: %d",my_info.cpi);
1340 sprintf(csat_txt,"carbon saturation: %d",my_info.c_sat);
1343 arg_v[3]=status_txt;
1353 arg_v[13]=zdiff_txt;
1355 arg_v[15]=ballistic_txt;
1356 arg_v[16]=carbon_txt;
1357 arg_v[17]=stress_txt;
1358 arg_v[18]=dr_ac_txt;
1359 arg_v[19]=dr_cc_txt;
1368 /* compute graphs for random number rejection method */
1369 if((c_profile=(u32 *)malloc(d3_l.max_z*sizeof(unsigned int)))==NULL)
1371 puts("failed allocating memory for carbon profile graph");
1374 if((n_e_loss=(u32 *)malloc(d3_l.max_z*sizeof(unsigned int)))==NULL)
1376 puts("failed allocating memory for nuclear energy loss graph");
1379 ip_max=get_reject_graph(&my_info,&d3_l,p_file,c_profile);
1380 ne_max=get_reject_graph(&my_info,&d3_l,n_e_file,n_e_loss);
1382 if((!strcmp(l_file,""))||(c_step))
1384 /* calculate ratio of c_simwindow / c_total */
1385 if(get_c_ratio(&c_ratio,p_file,&my_info,&d3_l)!=1)
1387 puts("failed calculating ratio");
1396 printf("%d\n",get_rand_reject(d3_l.max_z,ip_max,c_profile));
1399 printf("%d\n",get_rand_reject(d3_l.max_z,ne_max,n_e_loss));
1402 printf("%d\n",get_rand(d3_l.max_z));
1404 if(i==10000000) return 1;
1409 i=(c_step?c_step:0);
1410 while((i<my_info.steps) && (quit==0) && (escape==0))
1412 for(j=0;j<my_info.cpi;j++)
1414 x_c=get_rand(d3_l.max_x);
1415 y_c=get_rand(d3_l.max_y);
1416 // z_c=get_rand_reject(d3_l.max_z,ne_max,n_e_loss);
1417 z_c=get_rand(d3_l.max_z);
1418 nel_z=URAND_MAX*(1.0*n_e_loss[z_c]/ne_max);
1419 process_cell(&d3_l,x_c,y_c,z_c,&my_info,nel_z);
1421 distrib_c(&d3_l,&my_info,i,c_ratio,ip_max,c_profile);
1425 sprintf(xyz_txt,"x: %d y: %d z: %d",x+1,y+1,z+1);
1426 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');
1427 sprintf(conc_txt,"conc: %d",*(d3_l.extra+x+y*d3_l.max_x+z*d3_l.max_x*d3_l.max_y));
1428 sprintf(steps_txt,"step: %d",i);
1429 sprintf(cc_txt,"total c: %d",my_info.cc);
1430 d3_lattice_draw(&d3_l,x,y,z,25,arg_v,mode,0,NULL,0,NULL,0);
1433 if(i%resave==0 && strcmp(s_file,"") && resave!=0 && i!=0)
1435 sprintf(s_file_tmp,"%s_%d_of_%d",s_file,i,my_info.steps);
1436 save_to_file(s_file_tmp,&d3_l,&my_info);
1438 printf("saved %s\n",s_file_tmp);
1445 if(strcmp(s_file,""))
1447 printf("saved %s\n",s_file);
1448 save_to_file(s_file,&d3_l,&my_info);
1452 /* allocating buffer for pressure values */
1453 if((d3_l.v_ptr=malloc(d3_l.max_x*d3_l.max_y*d3_l.max_z*sizeof(unsigned char)))==NULL)
1455 puts("cannot allocate buffer for pressure values");
1459 calc_pressure(&d3_l,my_info.range);
1460 max_extra=calc_max_extra(&d3_l);
1462 while((quit==0) && (escape==0) && (nowait==0))
1465 if(switchmode==0) mode=0;
1466 if(switchmode==1) mode=1;
1467 if(switchmode==2) mode=2;
1468 if(switchmode==3) mode=3;
1470 sprintf(xyz_txt,"x: %d y: %d z: %d",x+1,y+1,z+1);
1471 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');
1472 sprintf(conc_txt,"conc: %d",*(d3_l.extra+x+y*d3_l.max_x+z*d3_l.max_x*d3_l.max_y));
1473 sprintf(steps_txt,"step: end");
1474 sprintf(cc_txt,"total c: %d",my_info.cc);
1475 if(switchmode==0) strcpy(mode_txt,"view: a/c mode");
1476 if(switchmode==1) strcpy(mode_txt,"view: c conc mode");
1477 if(switchmode==2) strcpy(mode_txt,"view: a pressure mode");
1478 if(switchmode==3) strcpy(mode_txt,"view: a/c + profiles mode");
1479 d3_lattice_draw(&d3_l,x,y,z,25,arg_v,mode,max_extra,c_profile,ip_max,n_e_loss,ne_max);
1482 scan_event(&d3_l,&x,&y,&z,&quit,&escape,&switchmode,&bmp,&ac_distr);
1483 if(bmp) write_bmp(&d3_l,bmp,x,y,z,max_extra);
1484 if(ac_distr) write_ac_distr(&d3_l,ac_distr);
1487 d3_lattice_release(&d3_l);