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("-e <value> \t do diffusion every <value> steps (default %d)\n",DIFF_RATE);
69 puts("-g <file> <step> continue simulation from file and step (step > 0)!");
70 printf("-W <value> \t write every <value> steps to save file (default %d)\n",RESAVE);
71 puts("-C <file> \t convert file to gnuplot format");
72 puts("-L <file> \t load from file");
73 puts("-S <file> \t save to file");
74 puts("-R <file> \t read from random file");
75 puts("-P <file> \t specify implantation profile file");
76 puts("-N <file> \t specify nuclear energy loss profile file");
77 printf("-H <value> \t collisions per ion in simulation window (default %d)\n",CPI);
78 printf("-m <value> \t number of ion hits sputtering 3nm (default %d)\n",S_RATE);
83 int sputter(d3_lattice *d3_l)
88 size=d3_l->max_x*d3_l->max_y;
92 for(i=0;i<d3_l->max_z-1;i++)
94 memcpy(d3_l->status+offl,d3_l->status+offh,size);
95 memcpy(d3_l->extra+offl,d3_l->extra+offh,size*sizeof(int));
99 memset(d3_l->status+offl,0,size);
100 memset(d3_l->extra+offl,0,size);
105 int process_cell(d3_lattice *d3_l,u32 x,u32 y,u32 z,info *my_info,u32 nel_z)
113 thiz=d3_l->status+x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y;
114 conc=d3_l->extra+x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y;
116 for(i=-(my_info->range);i<=my_info->range;i++)
118 for(j=-(my_info->range);j<=my_info->range;j++)
122 off=((x+d3_l->max_x+i)%d3_l->max_x)+((y+d3_l->max_y+j)%d3_l->max_x)*d3_l->max_x+z*d3_l->max_x*d3_l->max_y;
123 if(*(d3_l->status+off)&AMORPH) p+=my_info->s*(*(d3_l->extra+off))*URAND_MAX/(i*i+j*j);
127 p+=*conc*my_info->c*URAND_MAX;
130 if(get_rand(URAND_MAX)<=p) MAKE_AMORPH(thiz);
133 /* assume 1-p probability */
134 /* also look for neighbours ! */
135 q=(URAND_MAX-p)>0?URAND_MAX-p:0;
137 j+=(*(d3_l->status+((x+d3_l->max_x+1)%d3_l->max_x)+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y)&AMORPH)?1:0;
138 j+=(*(d3_l->status+((x+d3_l->max_x-1)%d3_l->max_x)+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y)&AMORPH)?1:0;
139 j+=(*(d3_l->status+x+((y+1+d3_l->max_y)%d3_l->max_y)*d3_l->max_x+z*d3_l->max_x*d3_l->max_y)&AMORPH)?1:0;
140 j+=(*(d3_l->status+x+((y-1+d3_l->max_y)%d3_l->max_y)*d3_l->max_x+z*d3_l->max_x*d3_l->max_y)&AMORPH)?1:0;
141 j+=(*(d3_l->status+x+y*d3_l->max_x+((z+1+d3_l->max_z)%d3_l->max_z)*d3_l->max_x*d3_l->max_y)&AMORPH)?1:0;
142 j+=(*(d3_l->status+x+y*d3_l->max_x+((z-1+d3_l->max_z)%d3_l->max_z)*d3_l->max_x*d3_l->max_y)&AMORPH)?1:0;
145 if(get_rand(URAND_MAX)>p) MAKE_CRYST(thiz);
151 int distrib_c(d3_lattice *d3_l,info *my_info,int step,u32 rj_m,u32 *rj_g)
158 /* put one c ion somewhere in the lattice */
159 x=get_rand(d3_l->max_x);
160 y=get_rand(d3_l->max_y);
161 z=get_rand_reject(d3_l->max_z,rj_m,rj_g);
162 *(d3_l->extra+x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y)+=1;
165 if(step%my_info->diff_rate==0)
168 for(i=0;i<d3_l->max_x;i++)
170 for(j=0;j<d3_l->max_y;j++)
172 for(k=0;k<d3_l->max_z;k++)
174 offset=i+j*d3_l->max_x+k*d3_l->max_x*d3_l->max_y;
175 /* case amorph: amorph <- cryst diffusion */
176 if(*(d3_l->status+offset)&AMORPH)
182 off=((i+d3_l->max_x+c)%d3_l->max_x)+j*d3_l->max_x+k*d3_l->max_x*d3_l->max_y;
184 if(!(*(d3_l->status+off)&AMORPH)) carry=(int)(my_info->dr_ac*(*(d3_l->extra+off)));
187 *(d3_l->extra+offset)+=carry;
188 *(d3_l->extra+off)-=carry;
196 off=i+((j+c+d3_l->max_y)%d3_l->max_y)*d3_l->max_x+k*d3_l->max_x*d3_l->max_y;
198 if(!(*(d3_l->status+off)&AMORPH)) carry=(int)(my_info->dr_ac*(*(d3_l->extra+off)));
201 *(d3_l->extra+offset)+=carry;
202 *(d3_l->extra+off)-=carry;
209 off=i+j*d3_l->max_x+(k-1)*d3_l->max_x*d3_l->max_y;
211 if(!*(d3_l->status+off)&AMORPH) carry=(int)(my_info->dr_ac*(*(d3_l->extra+off)));
214 *(d3_l->extra+off)-=carry;
215 *(d3_l->extra+offset)+=carry;
220 off=i+j*d3_l->max_x+(k+1)*d3_l->max_x*d3_l->max_y;
222 if(!*(d3_l->status+off)&AMORPH) carry=(int)(my_info->dr_ac*(*(d3_l->extra+off)));
225 *(d3_l->extra+off)-=carry;
226 *(d3_l->extra+offset)+=carry;
234 } /* if step modulo diff_rate == 0 */
239 int calc_pressure(d3_lattice *d3_l,int range)
245 for(x=0;x<d3_l->max_x;x++)
247 for(y=0;y<d3_l->max_y;y++)
249 for(z=0;z<d3_l->max_z;z++)
252 for(i=-range;i<=range;i++)
254 for(j=-range;j<=range;j++)
258 off=((x+d3_l->max_x+i)%d3_l->max_x)+((y+d3_l->max_y+j)%d3_l->max_x)*d3_l->max_x+z*d3_l->max_x*d3_l->max_y;
259 if(*(d3_l->status+off)&AMORPH) count+=((double)*(d3_l->extra+off))/(i*i+j*j);
263 if(count>max) max=count;
268 for(x=0;x<d3_l->max_x;x++)
270 for(y=0;y<d3_l->max_y;y++)
272 for(z=0;z<d3_l->max_z;z++)
275 for(i=-range;i<=range;i++)
277 for(j=-range;j<=range;j++)
281 off=((x+d3_l->max_x+i)%d3_l->max_x)+((y+d3_l->max_y+j)%d3_l->max_x)*d3_l->max_x+z*d3_l->max_x*d3_l->max_y;
282 if(*(d3_l->status+off)&AMORPH) count+=((double)*(d3_l->extra+off))/(i*i+j*j);
286 *((unsigned int *)(d3_l->v_ptr)+x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y)=(count*500/max);
294 int calc_max_extra(d3_lattice *d3_l)
299 for(x=0;x<d3_l->max_x;x++)
301 for(y=0;y<d3_l->max_y;y++)
303 for(z=0;z<d3_l->max_z;z++)
305 off=x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y;
306 if(*(d3_l->extra+off)>max) max=*(d3_l->extra+off);
314 int write_ac_distr(d3_lattice *d3_l,int ac_distr)
321 if(ac_distr==1) strcpy(file,"carbon_in_av.plot");
322 if(ac_distr==2) strcpy(file,"carbon_in_cv.plot");
323 if(ac_distr==3) strcpy(file,"carbon.plot");
324 if(ac_distr==4) strcpy(file,"amorphous_volumes.plot");
326 if((fd=open(file,O_WRONLY|O_CREAT))<0)
328 puts("cannot open plot file");
332 for(z=0;z<d3_l->max_z;z++)
336 for(x=0;x<d3_l->max_x;x++)
338 for(y=0;y<d3_l->max_y;y++)
340 offset=x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y;
342 if(*(d3_l->status+offset)&AMORPH)
344 count+=*(d3_l->extra+offset);
348 if(!(*(d3_l->status+offset)&AMORPH))
350 count+=*(d3_l->extra+offset);
355 count+=*(d3_l->extra+offset);
359 if(*(d3_l->status+offset)&AMORPH) count+=1;
362 si_count*=SI_PER_VOLUME;
363 if(ac_distr==4) dprintf(fd,"%d %d\n",z*CELL_LENGTH,count);
368 dprintf(fd,"%d %f\n",z*CELL_LENGTH,100.0*count/(si_count+count));
370 dprintf(fd,"%d 0\n",z*CELL_LENGTH);
373 else dprintf(fd,"%d %d\n",z*CELL_LENGTH,count/(d3_l->max_x*d3_l->max_y));
381 int write_bmp(d3_lattice *d3_l,int window,u32 x,u32 y,u32 z,int max)
383 int fd,i,j,size=0,foo=0,k,sum;
384 int width=0,height=0;
385 char bmpfile[MAX_CHARS];
390 sprintf(bmpfile,"x-z_%d.bmp",y);
392 size=(foo+(4-foo%4))*d3_l->max_z;
398 sprintf(bmpfile,"y-z_%d.bmp",x);
400 size=(foo+(4-foo%4))*d3_l->max_z;
406 sprintf(bmpfile,"x-y_%d.bmp",z);
408 size=(foo+(4-foo%4))*d3_l->max_y;
414 sprintf(bmpfile,"x-z_cdistr_%d.bmp",y);
416 size=(foo+(4-foo%4))*d3_l->max_z;
422 sprintf(bmpfile,"y-z_cdistr_%d.bmp",x);
424 size=(foo+(4-foo%4))*d3_l->max_z;
430 sprintf(bmpfile,"x-y_cdistr_%d.bmp",z);
432 size=(foo+(4-foo%4))*d3_l->max_y;
438 sprintf(bmpfile,"x-z_pressure_%d.bmp",y);
440 size=(foo+(4-foo%4))*d3_l->max_z;
446 sprintf(bmpfile,"y-z_pressure_%d.bmp",x);
448 size=(foo+(4-foo%4))*d3_l->max_z;
454 sprintf(bmpfile,"x-y_pressure_%d.bmp",z);
456 size=(foo+(4-foo%4))*d3_l->max_y;
462 sprintf(bmpfile,"x-z_c_cdistr_%d.bmp",y);
464 size=(foo+(4-foo%4))*d3_l->max_z;
470 sprintf(bmpfile,"y-z_c_cdistr_%d.bmp",x);
472 size=(foo+(4-foo%4))*d3_l->max_z;
478 sprintf(bmpfile,"x-y_c_cdistr_%d.bmp",z);
480 size=(foo+(4-foo%4))*d3_l->max_y;
484 if((fd=open(bmpfile,O_WRONLY|O_CREAT))<0)
486 puts("cannot open bmp file");
491 buf[0]='B'; /* std header start */
493 buf[2]=(size+0x36)&0xff; /* file size */
494 buf[3]=(size+0x36)>>8;
496 buf[10]=0x36; /* offset to data */
498 buf[14]=0x28; /* length of bmp info header */
500 buf[18]=width&0xff; /* width and height */
506 buf[26]=1; /* # planes -> 1 */
508 buf[28]=24; /* bits per pixel -> 2^24 (true color) */
510 memset(buf+30,0,4); /* compression -> none */
511 buf[34]=size&0xff; /* data size */
514 buf[38]=0x12; /* res: pixel/meter */
520 memset(buf+46,0,8); /* no colors, no important colors */
522 if(write(fd,buf,54)<54)
524 puts("failed writing bmp header");
529 for(j=0;j<d3_l->max_z;j++)
531 for(i=0;i<d3_l->max_x;i++)
535 if(*(d3_l->status+i+(((y+k+d3_l->max_y)%d3_l->max_y)*d3_l->max_x)+(d3_l->max_z-j-1)*d3_l->max_x*d3_l->max_y)&RED) sum+=0xff;
537 memset(buf,(unsigned char)sum,3);
538 if(write(fd,buf,3)<3)
540 puts("failed writing rgb values to bmp file");
546 memset(buf,0,4-foo%4);
547 if(write(fd,buf,4-foo%4)<4-foo%4)
549 puts("failed writing 4 byte ending");
557 for(j=0;j<d3_l->max_z;j++)
559 for(i=0;i<d3_l->max_y;i++)
563 if(*(d3_l->status+((x+k+d3_l->max_x)%d3_l->max_x)+i*d3_l->max_x+(d3_l->max_z-j-1)*d3_l->max_x*d3_l->max_y)&RED) sum+=0xff;
565 memset(buf,(unsigned char)sum,3);
566 if(write(fd,buf,3)<3)
568 puts("failed writing rgb values to bmp file");
574 memset(buf,0,4-foo%4);
575 if(write(fd,buf,4-foo%4)<4-foo%4)
577 puts("failed writing 4 byte ending");
585 for(j=0;j<d3_l->max_y;j++)
587 for(i=0;i<d3_l->max_x;i++)
589 if(*(d3_l->status+i+(d3_l->max_y-j-1)*d3_l->max_x+z*d3_l->max_x*d3_l->max_y)&RED) memset(buf,0xff,3);
590 else memset(buf,0,3);
591 if(write(fd,buf,3)<3)
593 puts("failed writing rgb values to bmp file");
599 memset(buf,0,4-foo%4);
600 if(write(fd,buf,4-foo%4)<4-foo%4)
602 puts("failed writing 4 byte ending");
610 for(j=0;j<d3_l->max_z;j++)
612 for(i=0;i<d3_l->max_x;i++)
614 sum=*(d3_l->extra+i+y*d3_l->max_x+(d3_l->max_z-j-1)*d3_l->max_x*d3_l->max_y);
616 memset(buf+1,(unsigned char)sum,2);
618 if(write(fd,buf,3)<3)
620 puts("failed writing rgb values to bmp file");
626 memset(buf,0,4-foo%4);
627 if(write(fd,buf,4-foo%4)<4-foo%4)
629 puts("failed writing 4 byte ending");
637 for(j=0;j<d3_l->max_z;j++)
639 for(i=0;i<d3_l->max_x;i++)
641 sum=*(d3_l->extra+x+i*d3_l->max_x+(d3_l->max_z-j-1)*d3_l->max_x*d3_l->max_y);
643 memset(buf+1,(unsigned char)sum,2);
645 if(write(fd,buf,3)<3)
647 puts("failed writing rgb values to bmp file");
653 memset(buf,0,4-foo%4);
654 if(write(fd,buf,4-foo%4)<4-foo%4)
656 puts("failed writing 4 byte ending");
664 for(j=0;j<d3_l->max_y;j++)
666 for(i=0;i<d3_l->max_x;i++)
668 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 memset(buf+1,(unsigned char)sum,2);
672 if(write(fd,buf,3)<3)
674 puts("failed writing rgb values to bmp file");
680 memset(buf,0,4-foo%4);
681 if(write(fd,buf,4-foo%4)<4-foo%4)
683 puts("failed writing 4 byte ending");
691 for(j=0;j<d3_l->max_z;j++)
693 for(i=0;i<d3_l->max_x;i++)
695 sum=*((unsigned int *)(d3_l->v_ptr)+i+y*d3_l->max_x+(d3_l->max_z-j-1)*d3_l->max_x*d3_l->max_y);
704 if(write(fd,buf,3)<3)
706 puts("failed writing rgb values to bmp file");
712 memset(buf,0,4-foo%4);
713 if(write(fd,buf,4-foo%4)<4-foo%4)
715 puts("failed writing 4 byte ending");
723 for(j=0;j<d3_l->max_z;j++)
725 for(i=0;i<d3_l->max_x;i++)
727 sum=*((unsigned int *)(d3_l->v_ptr)+x+i*d3_l->max_x+(d3_l->max_z-j-1)*d3_l->max_x*d3_l->max_y);
736 if(write(fd,buf,3)<3)
738 puts("failed writing rgb values to bmp file");
744 memset(buf,0,4-foo%4);
745 if(write(fd,buf,4-foo%4)<4-foo%4)
747 puts("failed writing 4 byte ending");
755 for(j=0;j<d3_l->max_y;j++)
757 for(i=0;i<d3_l->max_x;i++)
759 sum=*((unsigned int *)(d3_l->v_ptr)+i+(d3_l->max_y-j-1)*d3_l->max_x+z*d3_l->max_x*d3_l->max_y);
768 printf("sum = %d => r: %02x g: %02x b: %02x\n",sum,buf[2],buf[1],buf[0]);
769 if(write(fd,buf,3)<3)
771 puts("failed writing rgb values to bmp file");
777 memset(buf,0,4-foo%4);
778 if(write(fd,buf,4-foo%4)<4-foo%4)
780 puts("failed writing 4 byte ending");
788 for(j=0;j<d3_l->max_z;j++)
790 for(i=0;i<d3_l->max_x;i++)
792 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);
795 memset(buf,(unsigned char)sum,3);
796 if(write(fd,buf,3)<3)
798 puts("failed writing rgb values to bmp file");
804 memset(buf,0,4-foo%4);
805 if(write(fd,buf,4-foo%4)<4-foo%4)
807 puts("failed writing 4 byte ending");
815 for(j=0;j<d3_l->max_z;j++)
817 for(i=0;i<d3_l->max_x;i++)
819 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);
822 memset(buf,(unsigned char)sum,3);
823 if(write(fd,buf,3)<3)
825 puts("failed writing rgb values to bmp file");
831 memset(buf,0,4-foo%4);
832 if(write(fd,buf,4-foo%4)<4-foo%4)
834 puts("failed writing 4 byte ending");
842 for(j=0;j<d3_l->max_y;j++)
844 for(i=0;i<d3_l->max_x;i++)
846 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);
849 memset(buf,(unsigned char)sum,3);
850 if(write(fd,buf,3)<3)
852 puts("failed writing rgb values to bmp file");
858 memset(buf,0,4-foo%4);
859 if(write(fd,buf,4-foo%4)<4-foo%4)
861 puts("failed writing 4 byte ending");
872 int save_to_file(char *sf,d3_lattice *d3_l,info *my_inf)
876 if((sf_fd=open(sf,O_WRONLY|O_CREAT))<0)
878 puts("cannot open save file");
881 if(write(sf_fd,d3_l,sizeof(d3_lattice))<sizeof(d3_lattice))
883 puts("failed saving d3 lattice struct");
886 if(write(sf_fd,my_inf,sizeof(info))<sizeof(info))
888 puts("failed saving info struct");
891 c=d3_l->max_x*d3_l->max_y*d3_l->max_z;
892 if(write(sf_fd,d3_l->status,c*sizeof(unsigned char))<c*sizeof(unsigned char))
894 puts("failed saving status of d3 lattice sites");
897 if(write(sf_fd,d3_l->extra,c*sizeof(int))<c*sizeof(int))
899 puts("failed saving sites concentration");
907 int load_from_file(char *lf,d3_lattice *d3_l,info *my_inf)
910 int lf_fd,c,pos,end,data,data_len,strip;
912 if((lf_fd=open(lf,O_RDONLY))<0)
914 puts("cannot open load file");
917 if(read(lf_fd,d3_l,sizeof(d3_lattice))<sizeof(d3_lattice))
919 puts("failed reading d3 lattice struct");
922 pos=lseek(lf_fd,0,SEEK_CUR);
923 printf("psition: %d (%d)\n",pos,sizeof(d3_lattice));
924 data=d3_l->max_x*d3_l->max_y*d3_l->max_z;
925 data_len=data*(sizeof(int)+sizeof(unsigned char));
926 printf("there are %d volumes so we need %d of bytes\n",data,data_len);
927 end=lseek(lf_fd,0,SEEK_END);
929 printf("end: %d => length: %d => guessed info size: %d bytes\n",end,end-pos,c);
930 strip=sizeof(info)-c;
931 printf("as real programs info size is %d, we strip %d bytes\n",sizeof(info),strip);
932 lseek(lf_fd,pos,SEEK_SET);
934 if(strip>0) c-=strip;
937 puts("info smaller then strip size");
940 if(read(lf_fd,my_inf,c)<c)
942 puts("failed reading info struct");
945 if(strip>0) memset(my_inf+c,0,strip);
946 if(strip<0) lseek(lf_fd,(-1*strip),SEEK_CUR);
947 c=d3_l->max_x*d3_l->max_y*d3_l->max_z;
948 if((d3_l->status=(unsigned char*)malloc(c*sizeof(unsigned char)))==NULL)
950 puts("cannot allocate status buffer");
953 if((d3_l->extra=(int *)malloc(c*sizeof(int)))==NULL)
955 puts("cannot allocate concentration buffer");
958 if(read(lf_fd,d3_l->status,c*sizeof(unsigned char))<c*sizeof(unsigned char))
960 puts("failed reading status of d3 lattice sites");
963 if(read(lf_fd,d3_l->extra,c*sizeof(int))<c*sizeof(int))
965 puts("failed reading sites concentration");
973 int convert_file(char *cf,d3_lattice *d3_l)
978 if((c_fd=open(cf,O_WRONLY|O_CREAT))<0)
980 puts("cannot open convert file");
983 dprintf(c_fd,"# created by nlsop (gnuplot format)\n");
984 for(x=0;x<d3_l->max_x;x++)
986 for(y=0;y<d3_l->max_y;y++)
988 for(z=0;z<d3_l->max_z;z++)
990 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);
999 int get_c_ratio(double *c_ratio,char *pfile,info *my_info,d3_lattice *d3_l)
1004 unsigned char buf[32];
1008 if((p_fd=open(pfile,O_RDONLY))<0)
1010 puts("cannot open profile file");
1027 a=atof(p)/10; /* nm */
1030 if(a<=d3_l->max_z*CELL_LENGTH) d+=b;
1040 u32 get_reject_graph(info *my_info,d3_lattice *d3_l,char *file,u32 *graph) {
1045 unsigned char *flag;
1049 if((fd=open(file,O_RDONLY))<0)
1051 puts("cannot open file to calculate rejection graph");
1054 if((flag=(unsigned char *)malloc(d3_l->max_z))==NULL)
1056 puts("cannot malloc flag memory for rejection graph");
1059 memset(flag,0,d3_l->max_z);
1060 memset(graph,0,d3_l->max_z*sizeof(u32));
1067 k=read(fd,&buf[i],1);
1068 if((buf[i]=='\n')||(k==0)) break;
1073 a=atof(p)/10; /* nm */
1076 if(a>d3_l->max_z*CELL_LENGTH) k=0;
1079 graph[(int)(a/CELL_LENGTH)]=(int)(URAND_MAX/100*b);
1080 flag[(int)(a/CELL_LENGTH)]=1;
1084 /* do (linear) interpolation here! */
1087 while(i<d3_l->max_z)
1089 /* graph[0] is 0! */
1092 while(flag[i]==0&&i<d3_l->max_z) i++;
1093 for(k=j+1;k<i;k++) graph[k]=(int)((k-j)*((int)graph[i]-(int)graph[j])/(i-j))+graph[j];
1094 if(graph[i]>max) max=graph[i];
1099 #ifdef DEBUG_INTERPOL_PROFILE
1100 printf("debug: %s (interpolated profile)\n",file);
1101 for(i=0;i<d3_l->max_z;i++) printf("%d %d\n",i,graph[i]);
1107 int get_amorphous_layer_info(d3_lattice *d3_l,int *sai,int *sacl,int *eacl) {
1108 int i,j,a,oend,nend,count;
1109 unsigned char sacl_is_set=0;
1110 unsigned char eacl_is_set=0;
1111 unsigned char sai_is_set=0;
1113 a=d3_l->max_x*d3_l->max_y;
1117 for(i=0;i<d3_l->max_z;i++) {
1119 for(j=oend;j<nend;j++) if(*(d3_l->status+j)&AMORPH) count++;
1122 if((count>=A_START*a)&&(!sai_is_set)) {
1126 if((count>=AC_START*a)&&(!sacl_is_set)) {
1130 if((count<=A_END*a)&&(sacl_is_set)&&(!eacl_is_set)) {
1134 if((eacl_is_set)&&(count>=A_END*a)) eacl_is_set=0;
1139 int main(int argc,char **argv)
1141 u32 x,y,z,x_c,y_c,z_c;
1142 int i,j,quit,escape,switchmode,nowait,bmp,ac_distr;
1146 char s_file[MAX_CHARS];
1147 char s_file_tmp[MAX_CHARS];
1148 char l_file[MAX_CHARS];
1149 char c_file[MAX_CHARS];
1150 char p_file[MAX_CHARS];
1151 char n_e_file[MAX_CHARS];
1153 char r_file[MAX_CHARS];
1155 char xyz_txt[MAX_TXT];
1156 char status_txt[MAX_TXT];
1157 char conc_txt[MAX_TXT];
1158 char steps_txt[MAX_TXT];
1159 char cc_txt[MAX_TXT];
1160 char a_txt[MAX_TXT];
1161 char s_txt[MAX_TXT];
1162 char ballistic_txt[MAX_TXT];
1163 char carbon_txt[MAX_TXT];
1164 char stress_txt[MAX_TXT];
1165 char r_txt[MAX_TXT];
1166 char zdiff_txt[MAX_TXT];
1167 char diff_txt[MAX_TXT];
1168 char dr_ac_txt[MAX_TXT];
1169 char dose_txt[MAX_TXT];
1170 char mode_txt[MAX_TXT];
1171 char hpi_txt[MAX_TXT];
1172 char srate_txt[MAX_TXT];
1173 char start_ai_txt[MAX_TXT];
1174 char start_acl_txt[MAX_TXT];
1175 char end_acl_txt[MAX_TXT];
1176 char *arg_v[MAX_ARGV];
1189 unsigned char do_sputter;
1194 my_info.steps=STEPS;
1195 my_info.range=RANGE;
1202 my_info.dr_ac=DR_AC;
1203 my_info.diff_rate=DIFF_RATE;
1205 my_info.s_rate=S_RATE;
1214 strcpy(p_file,IMP_PROFILE);
1215 strcpy(n_e_file,NEL_PROFILE);
1227 printf("reading argv ...");
1243 d3_l.max_x=atoi(argv[++i]);
1246 d3_l.max_y=atoi(argv[++i]);
1249 d3_l.max_z=atoi(argv[++i]);
1252 my_info.steps=atoi(argv[++i]);
1255 refresh=atoi(argv[++i]);
1258 my_info.range=atoi(argv[++i]);
1261 my_info.s=atof(argv[++i]);
1264 my_info.b=atof(argv[++i]);
1267 my_info.c=atof(argv[++i]);
1270 resave=atoi(argv[++i]);
1273 strcpy(l_file,argv[++i]);
1274 if(i<argc-1) if(argv[i+1][0]!='-') strcpy(c_file,argv[++i]);
1278 my_info.dr_ac=atof(argv[++i]);
1281 my_info.diff_rate=atoi(argv[++i]);
1284 strcpy(l_file,argv[++i]);
1287 strcpy(s_file,argv[++i]);
1290 strcpy(r_file,argv[++i]);
1293 strcpy(p_file,argv[++i]);
1296 strcpy(n_e_file,argv[++i]);
1299 strcpy(l_file,argv[++i]);
1300 if(i<argc-1) if(argv[i+1][0]!='-') c_step=atoi(argv[++i]);
1303 my_info.cpi=atoi(argv[++i]);
1306 my_info.s_rate=atoi(argv[++i]);
1324 if(!strcmp(s_file,""))
1326 puts("NODFB defined, run with -S option");
1331 printf("random api init ...");
1332 if(!strcmp(r_file,"")) rand_init(NULL);
1333 else rand_init(r_file);
1336 if(!strcmp(l_file,""))
1338 i=d3_l.max_x*d3_l.max_y*d3_l.max_z;
1340 printf("d3 lattice init ...");
1341 d3_lattice_init(&argc,argv,&d3_l);
1344 if((d3_l.status=(unsigned char *)malloc(i*sizeof(unsigned char)))==NULL)
1346 puts("failed allocating status buffer");
1349 memset(d3_l.status,0,i*sizeof(unsigned char));
1350 if((d3_l.extra=(int *)malloc(i*sizeof(int)))==NULL)
1352 puts("failed allocating concentration buffer");
1355 memset(d3_l.extra,0,i*sizeof(int));
1358 printf("loading file ...");
1359 load_from_file(l_file,&d3_l,&my_info);
1363 if(!strcmp(c_file,"")) sprintf(c_file,"%s_gnuplot",l_file);
1364 printf("converting file %s to %s\n",l_file,c_file);
1365 convert_file(c_file,&d3_l);
1370 printf("d3 lattice init ...");
1371 d3_lattice_init(&argc,argv,&d3_l);
1377 printf("event init ...");
1378 d3_event_init(&d3_l);
1383 strcpy(a_txt,"args:");
1384 sprintf(s_txt,"steps: %d",my_info.steps);
1385 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));
1386 sprintf(r_txt,"stress influence range: %d",my_info.range);
1387 sprintf(stress_txt,"stress term: %f",my_info.s);
1388 sprintf(ballistic_txt,"ballistic term: %f",my_info.b);
1389 sprintf(carbon_txt,"carbon term: %f",my_info.c);
1390 sprintf(dr_ac_txt,"a/c diffusion rate: %f",my_info.dr_ac);
1391 sprintf(zdiff_txt,"diffusion in z direction: yes");
1392 sprintf(diff_txt,"diffusion every %d steps",my_info.diff_rate);
1393 strcpy(mode_txt,"view: a/c mode");
1394 sprintf(hpi_txt,"hits per ion: %d",my_info.cpi);
1395 sprintf(srate_txt,"sputter rate (3nm/#c): %d",my_info.s_rate);
1396 get_amorphous_layer_info(&d3_l,&sai,&sacl,&eacl);
1397 sprintf(start_ai_txt,"start of a-inclusions: %d nm (%d)",3*sai,sai);
1398 sprintf(start_acl_txt,"start of a-layer: %d nm (%d)",3*sacl,sacl);
1399 sprintf(end_acl_txt,"end of a-layer: %d nm (%d)",3*eacl,eacl);
1402 arg_v[3]=status_txt;
1412 arg_v[13]=zdiff_txt;
1413 arg_v[14]=dr_ac_txt;
1414 arg_v[15]=ballistic_txt;
1415 arg_v[16]=carbon_txt;
1416 arg_v[17]=stress_txt;
1419 arg_v[20]=srate_txt;
1421 arg_v[22]=start_ai_txt;
1422 arg_v[23]=start_acl_txt;
1423 arg_v[24]=end_acl_txt;
1427 /* compute graphs for random number rejection method */
1428 printf("random rejection graphs ...");
1429 if((c_profile=(u32 *)malloc(d3_l.max_z*sizeof(unsigned int)))==NULL)
1431 puts("failed allocating memory for carbon profile graph");
1434 if((n_e_loss=(u32 *)malloc(d3_l.max_z*sizeof(unsigned int)))==NULL)
1436 puts("failed allocating memory for nuclear energy loss graph");
1439 ip_max=get_reject_graph(&my_info,&d3_l,p_file,c_profile);
1440 ne_max=get_reject_graph(&my_info,&d3_l,n_e_file,n_e_loss);
1443 if((!strcmp(l_file,""))||(c_step))
1446 /* this should be obsolete - z is high enough - we check now! */
1447 if(c_profile[d3_l.max_z-1]!=0)
1449 printf("max_z (%d) too small - sputtering not possible\n",d3_l.max_z);
1452 /* calculate ratio of c_simwindow / c_total */
1455 get_c_ratio(&c_ratio,p_file,&my_info,&d3_l);
1456 printf("calculated c ratio: %f\n",c_ratio);
1459 /* sputtering realy possible ?*/
1460 if(n_e_loss[d3_l.max_z-1]!=0)
1461 printf("warning: max_z (%d) too small - there may be amorphous volumes\n",d3_l.max_z);
1468 printf("%d\n",get_rand_reject(d3_l.max_z,ip_max,c_profile));
1471 printf("%d\n",get_rand_reject(d3_l.max_z,ne_max,n_e_loss));
1474 printf("%d\n",get_rand(d3_l.max_z));
1477 printf("%d\n",get_rand_lgp(d3_l.max_z,1,0));
1479 if(i==10000000) return 1;
1485 printf("starting simulation ... now! :)\n");
1488 i=(c_step?c_step:0);
1489 while((i<my_info.steps) && (quit==0) && (escape==0))
1492 if(i%refresh==0) printf("step: %d\n",i);
1494 for(j=0;j<my_info.cpi;j++)
1496 x_c=get_rand(d3_l.max_x);
1497 y_c=get_rand(d3_l.max_y);
1498 // z_c=get_rand_reject(d3_l.max_z,ne_max,n_e_loss);
1499 z_c=get_rand(d3_l.max_z);
1500 nel_z=URAND_MAX*(1.0*n_e_loss[z_c]/ne_max);
1501 process_cell(&d3_l,x_c,y_c,z_c,&my_info,nel_z);
1503 distrib_c(&d3_l,&my_info,i,ip_max,c_profile);
1507 sprintf(xyz_txt,"x: %d y: %d z: %d (%d nm)",x+1,y+1,z+1,z*3);
1508 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');
1509 sprintf(conc_txt,"carbon: %d",*(d3_l.extra+x+y*d3_l.max_x+z*d3_l.max_x*d3_l.max_y));
1510 sprintf(steps_txt,"step: %d",i);
1511 sprintf(cc_txt,"total c: %d",my_info.cc);
1512 get_amorphous_layer_info(&d3_l,&sai,&sacl,&eacl);
1513 sprintf(start_ai_txt,"start of a-inclusions: %d nm (%d)",3*sai,sai);
1514 sprintf(start_acl_txt,"start of a-layer: %d nm (%d)",3*sacl,sacl);
1515 sprintf(end_acl_txt,"end of a-layer: %d nm (%d)",3*eacl,eacl);
1516 d3_lattice_draw(&d3_l,x,y,z,25,arg_v,mode,0,NULL,0,NULL,0);
1519 if(i%resave==0 && strcmp(s_file,"") && resave!=0 && i!=0)
1521 sprintf(s_file_tmp,"%s_%d_of_%d",s_file,i,my_info.steps);
1522 save_to_file(s_file_tmp,&d3_l,&my_info);
1524 printf("saved %s\n",s_file_tmp);
1528 if((do_sputter)&(i%my_info.s_rate==0)) sputter(&d3_l);
1532 if(strcmp(s_file,""))
1534 printf("saved %s\n",s_file);
1535 save_to_file(s_file,&d3_l,&my_info);
1539 /* allocating buffer for pressure values */
1540 printf("allocating buffer for preassure values ...");
1541 if((d3_l.v_ptr=malloc(d3_l.max_x*d3_l.max_y*d3_l.max_z*sizeof(unsigned int)))==NULL)
1543 puts("cannot allocate buffer for pressure values");
1548 printf("calculating preassure values ...");
1549 calc_pressure(&d3_l,my_info.range);
1551 printf("finding maximum carbon concentration ...");
1552 max_extra=calc_max_extra(&d3_l);
1555 while((quit==0) && (escape==0) && (nowait==0))
1558 if(switchmode==0) mode=0;
1559 if(switchmode==1) mode=1;
1560 if(switchmode==2) mode=2;
1561 if(switchmode==3) mode=3;
1563 sprintf(xyz_txt,"x: %d y: %d z: %d (%d nm)",x+1,y+1,z+1,z*3);
1564 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');
1565 sprintf(conc_txt,"carbon: %d",*(d3_l.extra+x+y*d3_l.max_x+z*d3_l.max_x*d3_l.max_y));
1566 sprintf(steps_txt,"step: end");
1567 sprintf(cc_txt,"total c: %d",my_info.cc);
1568 if(switchmode==0) strcpy(mode_txt,"view: a/c mode");
1569 if(switchmode==1) strcpy(mode_txt,"view: c conc mode");
1570 if(switchmode==2) strcpy(mode_txt,"view: a pressure mode");
1571 if(switchmode==3) strcpy(mode_txt,"view: a/c + profiles mode");
1572 d3_lattice_draw(&d3_l,x,y,z,25,arg_v,mode,max_extra,c_profile,ip_max,n_e_loss,ne_max);
1575 scan_event(&d3_l,&x,&y,&z,&quit,&escape,&switchmode,&bmp,&ac_distr);
1576 if(bmp) write_bmp(&d3_l,bmp,x,y,z,max_extra);
1577 if(ac_distr) write_ac_distr(&d3_l,ac_distr);
1580 d3_lattice_release(&d3_l);