4 * author: frank zirkelbach (frank@xeen.net)
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. Habilationsschrift, 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
51 /* test globals - get removed or included in my_info struct later */
60 puts("-n \t\t no user interaction");
61 puts("-Z \t\t cryst -> amorph c diffusion in z direction");
62 puts("-i \t\t no cryst to cryst diffusion");
63 printf("-a <value> \t slope of nuclear energy loss (default %f)\n",A_EL);
64 printf("-b <value> \t nuclear energy loss offset (default %f)\n",B_EL);
65 printf("-x <value> \t # x cells (default %d)\n",X);
66 printf("-y <value> \t # y cells (default %d)\n",Y);
67 printf("-z <value> \t # z cells (default %d)\n",Z);
68 printf("-s <value> \t steps (default %d)\n",STEPS);
69 printf("-d <value> \t refresh display (default %d)\n",REFRESH);
70 printf("-r <value> \t amorphous influence range (default %d)\n",RANGE);
71 printf("-f <value> \t pressure = <value> * 1/distance^2 (default %f)\n",A_AP);
72 printf("-p <value> \t pressure offset (default %f)\n",B_AP);
73 printf("-F <value> \t proportionality constant between c conc and ability to get amorphous (default %f)\n",A_CP);
74 printf("-A <value> \t slope of linear c distribution (default %f)\n",A_CD);
75 printf("-B <value> \t linear c distribution offset (default %f)\n",B_CD);
76 printf("-D <value> \t diffusion rate from cryst to amorph cells (default %f)\n",DR_AC);
77 printf("-c <value> \t diffusion rate in cryst cells (default %f)\n",DR_CC);
78 printf("-e <value> \t do diffusion every <value> steps (default %d)\n",DIFF_RATE);
79 puts("-g <file> <step> continue simulation from file and step (step > 0)!");
80 printf("-W <value> \t write every <value> steps to save file (default %d)\n",RESAVE);
81 puts("-C <file> \t convert file to gnuplot format");
82 puts("-L <file> \t load from file");
83 puts("-S <file> \t save to file");
84 puts("-R <file> \t read from random file");
85 puts("-P <file> \t specify implantation profile file");
90 int process_cell(d3_lattice *d3_l,u32 x,u32 y,u32 z,info *my_info)
98 thiz=d3_l->status+x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y;
99 conc=d3_l->extra+x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y;
100 p=my_info->b_ap*URAND_MAX;
101 for(i=-(my_info->range);i<=my_info->range;i++)
103 for(j=-(my_info->range);j<=my_info->range;j++)
107 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;
108 if(*(d3_l->status+off)&AMORPH) p+=my_info->a_ap*(*(d3_l->extra+off))*URAND_MAX/(i*i+j*j);
112 p+=*conc*my_info->a_cp*URAND_MAX;
115 if(get_rand(URAND_MAX)<=p)
122 /* assume 1-p probability */
123 if(get_rand(URAND_MAX)>p)
133 int distrib_c(d3_lattice *d3_l,info *my_info,int step,double c_ratio)
140 /* put one c ion somewhere in the lattice */
141 if(my_info->cc<c_ratio*step)
143 x=get_rand(d3_l->max_x);
144 y=get_rand(d3_l->max_y);
145 z=get_rand_lgp(d3_l->max_z,my_info->a_cd,my_info->b_cd);
146 *(d3_l->extra+x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y)+=1;
150 if(step%my_info->diff_rate==0)
153 for(i=0;i<d3_l->max_x;i++)
155 for(j=0;j<d3_l->max_y;j++)
157 for(k=0;k<d3_l->max_z;k++)
159 offset=i+j*d3_l->max_x+k*d3_l->max_x*d3_l->max_y;
160 /* case amorph: amorph <- cryst diffusion */
161 if(*(d3_l->status+offset)&AMORPH)
167 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;
169 if(!(*(d3_l->status+off)&AMORPH)) carry=(int)(my_info->dr_ac*(*(d3_l->extra+off)));
172 *(d3_l->extra+offset)+=carry;
173 *(d3_l->extra+off)-=carry;
181 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;
183 if(!(*(d3_l->status+off)&AMORPH)) carry=(int)(my_info->dr_ac*(*(d3_l->extra+off)));
186 *(d3_l->extra+offset)+=carry;
187 *(d3_l->extra+off)-=carry;
197 off=i+j*d3_l->max_x+((k+c+d3_l->max_z)%d3_l->max_z)*d3_l->max_x*d3_l->max_y;
199 if(!*(d3_l->status+off)&AMORPH) carry=(int)(my_info->dr_ac*(*(d3_l->extra+off)));
202 *(d3_l->extra+off)-=carry;
203 *(d3_l->extra+offset)+=carry;
209 /* case not amorph: cryst <-> cryst diffusion */
219 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;
221 if(!(*(d3_l->status+off)&AMORPH))
223 carry=(int)(my_info->dr_cc*(*(d3_l->extra+off)-*(d3_l->extra+offset))/2);
226 *(d3_l->extra+offset)+=carry;
227 *(d3_l->extra+off)-=carry;
236 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;
238 if(!(*(d3_l->status+off)&AMORPH))
240 carry=(int)(my_info->dr_cc*(*(d3_l->extra+off)-*(d3_l->extra+offset))/2);
243 *(d3_l->extra+offset)+=carry;
244 *(d3_l->extra+off)-=carry;
257 } /* if step modulo diff_rate == 0 */
262 int calc_pressure(d3_lattice *d3_l,int range)
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 if(count>max) max=count;
291 for(x=0;x<d3_l->max_x;x++)
293 for(y=0;y<d3_l->max_y;y++)
295 for(z=0;z<d3_l->max_z;z++)
298 for(i=-range;i<=range;i++)
300 for(j=-range;j<=range;j++)
304 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;
305 if(*(d3_l->status+off)&AMORPH) count+=((double)*(d3_l->extra+off))/(i*i+j*j);
309 *(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);
317 int calc_max_extra(d3_lattice *d3_l)
322 for(x=0;x<d3_l->max_x;x++)
324 for(y=0;y<d3_l->max_y;y++)
326 for(z=0;z<d3_l->max_z;z++)
328 off=x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y;
329 if(*(d3_l->extra+off)>max) max=*(d3_l->extra+off);
337 int write_bmp(d3_lattice *d3_l,int window,u32 x,u32 y,u32 z)
339 int fd,i,j,size=0,foo=0;
340 int width=0,height=0;
341 char bmpfile[MAX_CHARS];
346 sprintf(bmpfile,"x-z_%d.bmp",y);
348 size=(foo+(4-foo%4))*d3_l->max_z;
354 sprintf(bmpfile,"y-z_%d.bmp",x);
356 size=(foo+(4-foo%4))*d3_l->max_z;
362 sprintf(bmpfile,"x-y_%d.bmp",z);
364 size=(foo+(4-foo%4))*d3_l->max_y;
369 if((fd=open(bmpfile,O_WRONLY|O_CREAT))<0)
371 puts("cannot open bmp file");
376 buf[0]='B'; /* std header start */
378 buf[2]=(size+0x36)&0xff; /* file size */
379 buf[3]=(size+0x36)>>8;
381 buf[10]=0x36; /* offset to data */
383 buf[14]=0x28; /* length of bmp info header */
385 buf[18]=width&0xff; /* width and height */
391 buf[26]=1; /* # planes -> 1 */
393 buf[28]=24; /* bits per pixel -> 2^24 (true color) */
395 memset(buf+30,0,4); /* compression -> none */
396 buf[34]=size&0xff; /* data size */
399 buf[38]=0x12; /* res: pixel/meter */
405 memset(buf+46,0,8); /* no colors, no important colors */
407 if(write(fd,buf,54)<54)
409 puts("failed writing bmp header");
414 for(j=0;j<d3_l->max_z;j++)
416 for(i=0;i<d3_l->max_x;i++)
418 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) memset(buf,0xff,3);
419 else memset(buf,0,3);
420 if(write(fd,buf,3)<3)
422 puts("failed writing rgb values to bmp file");
428 memset(buf,0,4-foo%4);
429 if(write(fd,buf,4-foo%4)<4-foo%4)
431 puts("failed writing 4 byte ending");
439 for(j=0;j<d3_l->max_z;j++)
441 for(i=0;i<d3_l->max_y;i++)
443 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) memset(buf,0xff,3);
444 else memset(buf,0,3);
445 if(write(fd,buf,3)<3)
447 puts("failed writing rgb values to bmp file");
453 memset(buf,0,4-foo%4);
454 if(write(fd,buf,4-foo%4)<4-foo%4)
456 puts("failed writing 4 byte ending");
464 for(j=0;j<d3_l->max_y;j++)
466 for(i=0;i<d3_l->max_x;i++)
468 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);
469 else memset(buf,0,3);
470 if(write(fd,buf,3)<3)
472 puts("failed writing rgb values to bmp file");
478 memset(buf,0,4-foo%4);
479 if(write(fd,buf,4-foo%4)<4-foo%4)
481 puts("failed writing 4 byte ending");
492 int save_to_file(char *sf,d3_lattice *d3_l,info *my_inf)
496 if((sf_fd=open(sf,O_WRONLY|O_CREAT))<0)
498 puts("cannot open save file");
501 if(write(sf_fd,d3_l,sizeof(d3_lattice))<sizeof(d3_lattice))
503 puts("failed saving d3 lattice struct");
506 if(write(sf_fd,my_inf,sizeof(info))<sizeof(info))
508 puts("failed saving info struct");
511 c=d3_l->max_x*d3_l->max_y*d3_l->max_z;
512 if(write(sf_fd,d3_l->status,c*sizeof(unsigned char))<c*sizeof(unsigned char))
514 puts("failed saving status of d3 lattice sites");
517 if(write(sf_fd,d3_l->extra,c*sizeof(int))<c*sizeof(int))
519 puts("failed saving sites concentration");
527 int load_from_file(char *lf,d3_lattice *d3_l,info *my_inf)
531 if((lf_fd=open(lf,O_RDONLY))<0)
533 puts("cannot open load file");
536 if(read(lf_fd,d3_l,sizeof(d3_lattice))<sizeof(d3_lattice))
538 puts("failed reading d3 lattice struct");
541 if(read(lf_fd,my_inf,sizeof(info))<sizeof(info))
543 puts("failed reading info struct");
546 c=d3_l->max_x*d3_l->max_y*d3_l->max_z;
547 if((d3_l->status=(unsigned char*)malloc(c*sizeof(unsigned char)))==NULL)
549 puts("cannot allocate status buffer");
552 if((d3_l->extra=(int *)malloc(c*sizeof(int)))==NULL)
554 puts("cannot allocate concentration buffer");
557 if(read(lf_fd,d3_l->status,c*sizeof(unsigned char))<c*sizeof(unsigned char))
559 puts("failed reading status of d3 lattice sites");
562 if(read(lf_fd,d3_l->extra,c*sizeof(int))<c*sizeof(int))
564 puts("failed reading sites concentration");
572 int convert_file(char *cf,d3_lattice *d3_l)
577 if((c_fd=open(cf,O_WRONLY|O_CREAT))<0)
579 puts("cannot open convert file");
582 dprintf(c_fd,"# created by nlsop (gnuplot format)\n");
583 for(x=0;x<d3_l->max_x;x++)
585 for(y=0;y<d3_l->max_y;y++)
587 for(z=0;z<d3_l->max_z;z++)
589 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);
598 int get_c_ratio(double *c_ratio,char *pfile,info *my_info,d3_lattice *d3_l)
603 unsigned char buf[32];
607 if((p_fd=open(pfile,O_RDONLY))<0)
609 puts("cannot open profile file");
626 a=atof(p)/10; /* nm */
629 if(a>my_info->b_cd*CELL_LENGTH && a<(my_info->b_cd+d3_l->max_z)*CELL_LENGTH) d+=b;
639 int main(int argc,char **argv)
641 u32 x,y,z,x_c,y_c,z_c;
642 int i,quit,escape,switchmode,nowait,bmp;
645 char s_file[MAX_CHARS];
646 char s_file_tmp[MAX_CHARS];
647 char l_file[MAX_CHARS];
648 char c_file[MAX_CHARS];
649 char p_file[MAX_CHARS];
651 char r_file[MAX_CHARS];
653 char xyz_txt[MAX_TXT];
654 char status_txt[MAX_TXT];
655 char conc_txt[MAX_TXT];
656 char steps_txt[MAX_TXT];
657 char cc_txt[MAX_TXT];
660 char ap_txt[MAX_TXT];
661 char el_txt[MAX_TXT];
662 char cd_txt[MAX_TXT];
664 char cp_txt[MAX_TXT];
665 char zdiff_txt[MAX_TXT];
666 char diff_txt[MAX_TXT];
667 char dr_ac_txt[MAX_TXT];
668 char dr_cc_txt[MAX_TXT];
669 char dose_txt[MAX_TXT];
670 char mode_txt[MAX_TXT];
671 char *arg_v[MAX_ARGV];
700 my_info.diff_rate=DIFF_RATE;
709 strcpy(p_file,IMP_PROFILE);
730 my_info.a_el=atof(argv[++i]);
733 my_info.b_el=atof(argv[++i]);
736 d3_l.max_x=atoi(argv[++i]);
739 d3_l.max_y=atoi(argv[++i]);
742 d3_l.max_z=atoi(argv[++i]);
752 my_info.steps=atoi(argv[++i]);
755 refresh=atoi(argv[++i]);
758 my_info.range=atoi(argv[++i]);
761 my_info.a_ap=atof(argv[++i]);
764 my_info.b_ap=atof(argv[++i]);
767 my_info.a_cp=atof(argv[++i]);
770 my_info.a_cd=atof(argv[++i]);
773 my_info.b_cd=atof(argv[++i]);
776 resave=atoi(argv[++i]);
779 strcpy(l_file,argv[++i]);
780 if(i<argc-1) if(argv[i+1][0]!='-') strcpy(c_file,argv[++i]);
784 my_info.dr_ac=atof(argv[++i]);
787 my_info.dr_cc=atof(argv[++i]);
790 my_info.diff_rate=atoi(argv[++i]);
793 strcpy(l_file,argv[++i]);
796 strcpy(s_file,argv[++i]);
799 strcpy(r_file,argv[++i]);
802 strcpy(p_file,argv[++i]);
805 strcpy(l_file,argv[++i]);
806 if(i<argc-1) if(argv[i+1][0]!='-') c_step=atoi(argv[++i]);
820 if(!strcmp(s_file,""))
822 puts("NODFB defined, run with -S option");
827 if(!strcmp(r_file,"")) rand_init(NULL);
828 else rand_init(r_file);
830 if(!strcmp(l_file,""))
832 i=d3_l.max_x*d3_l.max_y*d3_l.max_z;
834 d3_lattice_init(&argc,argv,&d3_l);
836 if((d3_l.status=(unsigned char *)malloc(i*sizeof(unsigned char)))==NULL)
838 puts("failed allocating status buffer");
841 memset(d3_l.status,0,i*sizeof(unsigned char));
842 if((d3_l.extra=(int *)malloc(i*sizeof(int)))==NULL)
844 puts("failed allocating concentration buffer");
847 memset(d3_l.extra,0,i*sizeof(int));
850 load_from_file(l_file,&d3_l,&my_info);
853 if(!strcmp(c_file,"")) sprintf(c_file,"%s_gnuplot",l_file);
854 printf("converting file %s to %s\n",l_file,c_file);
855 convert_file(c_file,&d3_l);
860 else d3_lattice_init(&argc,argv,&d3_l);
865 d3_event_init(&d3_l);
869 strcpy(a_txt,"args:");
870 sprintf(s_txt,"steps: %d",my_info.steps);
871 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));
872 sprintf(r_txt,"pressure range: %d",my_info.range);
873 sprintf(ap_txt,"a_ap: %.3f b_ap: %.3f",my_info.a_ap,my_info.b_ap);
874 sprintf(el_txt,"a_el: %.3f b_el: %.3f",my_info.a_el,my_info.b_el);
875 sprintf(cd_txt,"a_cd: %.3f b_cd: %.3f",my_info.a_cd,my_info.b_cd);
876 sprintf(cp_txt,"a_cp: %.4f",my_info.a_cp);
877 sprintf(dr_ac_txt,"a/c diffusion rate: %.4f",my_info.dr_ac);
878 if(my_info.dr_cc!=0) sprintf(dr_cc_txt,"c/c diffusion rate: %.4f",my_info.dr_cc);
879 else sprintf(dr_cc_txt,"c/c diffusion rate: none");
880 sprintf(zdiff_txt,"diffusion in z direction: %c",my_info.z_diff?'y':'n');
881 sprintf(diff_txt,"diffusion every %d steps",my_info.diff_rate);
882 strcpy(mode_txt,"view: a/c mode");
910 if((!strcmp(l_file,""))||(c_step))
912 if(get_c_ratio(&c_ratio,p_file,&my_info,&d3_l)!=1)
914 puts("failed calculating ratio");
918 while((i<my_info.steps) && (quit==0) && (escape==0))
920 x_c=get_rand(d3_l.max_x);
921 y_c=get_rand(d3_l.max_y);
922 z_c=get_rand_lgp(d3_l.max_z,my_info.a_el,my_info.b_el);
923 distrib_c(&d3_l,&my_info,i,c_ratio);
924 process_cell(&d3_l,x_c,y_c,z_c,&my_info);
928 sprintf(xyz_txt,"x: %d y: %d z: %d",x+1,y+1,z+1);
929 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');
930 sprintf(conc_txt,"conc: %d",*(d3_l.extra+x+y*d3_l.max_x+z*d3_l.max_x*d3_l.max_y));
931 sprintf(steps_txt,"step: %d a_count: %d",i,amorph_count);
932 sprintf(cc_txt,"total c: %d c_count: %d",my_info.cc,cryst_count);
933 d3_lattice_draw(&d3_l,x,y,z,25,arg_v,mode,0);
936 if(i%resave==0 && strcmp(s_file,"") && resave!=0 && i!=0)
938 sprintf(s_file_tmp,"%s_%d_of_%d",s_file,i,my_info.steps);
939 save_to_file(s_file_tmp,&d3_l,&my_info);
941 printf("saved %s\n",s_file_tmp);
948 if(strcmp(s_file,""))
950 printf("saved %s\n",s_file);
951 save_to_file(s_file,&d3_l,&my_info);
955 /* allocating buffer for pressure values */
956 if((d3_l.v_ptr=malloc(d3_l.max_x*d3_l.max_y*d3_l.max_z*sizeof(unsigned char)))==NULL)
958 puts("cannot allocate buffer for pressure values");
962 calc_pressure(&d3_l,my_info.range);
963 max_extra=calc_max_extra(&d3_l);
965 while((quit==0) && (escape==0) && (nowait==0))
968 if(switchmode==0) mode=0;
969 if(switchmode==1) mode=1;
970 if(switchmode==2) mode=2;
972 sprintf(xyz_txt,"x: %d y: %d z: %d",x+1,y+1,z+1);
973 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');
974 sprintf(conc_txt,"conc: %d",*(d3_l.extra+x+y*d3_l.max_x+z*d3_l.max_x*d3_l.max_y));
975 sprintf(steps_txt,"step: end");
976 sprintf(cc_txt,"total c: %d",my_info.cc);
977 if(switchmode==0) strcpy(mode_txt,"view: a/c mode");
978 if(switchmode==1) strcpy(mode_txt,"view: c conc mode");
979 if(switchmode==2) strcpy(mode_txt,"view: a pressure mode");
980 d3_lattice_draw(&d3_l,x,y,z,25,arg_v,mode,max_extra);
982 scan_event(&d3_l,&x,&y,&z,&quit,&escape,&switchmode,&bmp);
983 if(bmp) write_bmp(&d3_l,bmp,x,y,z);
987 d3_lattice_release(&d3_l);