4 * this program tries helping to understand the amorphous depuration
5 * and recrystallization of SiCx while ion implantation at temperatures
6 * below 400 degree celsius.
7 * hopefully the program will simulate the stabilization of the
8 * selforganizing lamella structure in the observed behaviour.
11 * - J. K. N. Lindner. Habilationsschrift, Universitaet Augsburg.
12 * - Maik Haeberlen. Diplomarbeit, Universitaet Augsburg.
19 #include <sys/types.h>
29 #define MAKE_AMORPH(N) *(N)|=AMORPH
30 #define MAKE_CRYST(N) *(N)&=~AMORPH
36 puts("-n \t\t no user interaction");
37 puts("-Z \t\t cryst -> amorph c diffusion in z direction");
38 printf("-a <value> \t slope of nuclear energy loss (default %f)\n",A_EL);
39 printf("-b <value> \t nuclear energy loss offset (default %f)\n",B_EL);
40 printf("-x <value> \t # x cells (default %d)\n",X);
41 printf("-y <value> \t # y cells (default %d)\n",Y);
42 printf("-z <value> \t # z cells (default %d)\n",Z);
44 printf("-X <value> \t display x (default %d)\n",X/2-1);
45 printf("-Y <value> \t display y (default %d)\n",Y/2-1);
46 printf("-Z <value> \t display z (default %d)\n",Z/2-1);
48 printf("-s <value> \t steps (default %d)\n",STEPS);
49 printf("-d <value> \t refresh display (default %d)\n",REFRESH);
50 printf("-r <value> \t amorphous influence range (default %d)\n",RANGE);
51 printf("-f <value> \t pressure = <value> * 1/distance^2 (default %f)\n",A_AP);
52 printf("-p <value> \t pressure offset (default %f)\n",B_AP);
53 printf("-A <value> \t slope of linear c distribution (default %f)\n",A_CD);
54 printf("-B <value> \t linear c distribution offset (default %f)\n",B_CD);
56 printf("-C <value> \t initial c concentration (default %d)\n",CC);
58 printf("-D <value> \t diffusion rate from cryst to amorph cells (default %f)\n",D_R);
59 printf("-W <value> \t write every <value> steps to save file (default %d)\n",RESAVE);
60 puts("-C <file> \t convert file to gnuplot format");
61 puts("-L <file> \t load from file");
62 puts("-S <file> \t save to file");
63 puts("-R <file> \t read from random file");
68 int process_cell(d3_lattice *d3_l,u32 x,u32 y,u32 z,int r,double a,double b,int *t_c)
76 thiz=d3_l->status+x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y;
77 conc=d3_l->extra+x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y;
85 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;
86 if(*(d3_l->status+off)&AMORPH) p+=a*(*(d3_l->extra+off))*URAND_MAX/(i*i+j*j);
90 printf("debug: p = %f\n",p);
93 if(get_rand(URAND_MAX)<=p) MAKE_AMORPH(thiz);
96 /* assume 1-p probability */
97 if(get_rand(URAND_MAX)>p) MAKE_CRYST(thiz);
105 int distrib_c(d3_lattice *d3_l,double d_r,double a,double b,char z_diff)
112 /* put one c ion somewhere in the lattice */
113 x=get_rand(d3_l->max_x);
114 y=get_rand(d3_l->max_y);
115 z=get_rand_lgp(d3_l->max_z,a,b);
116 *(d3_l->extra+x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y)+=1;
118 /* diffusion in layer */
119 for(i=0;i<d3_l->max_x;i++)
121 for(j=0;j<d3_l->max_y;j++)
123 for(k=0;k<d3_l->max_z;k++)
125 offset=i+j*d3_l->max_x+k*d3_l->max_x*d3_l->max_y;
127 if(*(d3_l->status+offset)&AMORPH)
129 /* look at neighbours and move c ions */
134 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;
136 /* case neighbour not amorph */
137 if(!(*(d3_l->status+off)&AMORPH)) carry=(int)(d_r*(*(d3_l->extra+off)));
138 /* case neighbour amorph */
140 * no diffusion between amorphous cells
142 else carry=(*(d3_l->extra+off)-*(d3_l->extra+offset))/2;
146 *(d3_l->extra+offset)+=carry;
147 *(d3_l->extra+off)-=carry;
155 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;
157 /* case neighbour not amorph */
158 if(!(*(d3_l->status+off)&AMORPH)) carry=(int)(d_r*(*(d3_l->extra+off)));
159 /* case neighbour amorph */
162 * no diffusion between amorphous cells
164 else carry=(*(d3_l->extra+off)-*(d3_l->extra+offset))/2;
168 *(d3_l->extra+offset)+=carry;
169 *(d3_l->extra+off)-=carry;
174 /* case not amorph */
176 /* look at neighbours and move c ions */
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 /* case neighbour not amorph */
184 if(!(*(d3_l->status+off)&AMORPH))
186 carry=(*(d3_l->extra+off)-*(d3_l->extra+offset))/2;
189 *(d3_l->extra+offset)+=carry;
190 *(d3_l->extra+off)-=carry;
199 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;
201 /* case neighbour not amorph */
202 if(!(*(d3_l->status+off)&AMORPH))
204 carry=(*(d3_l->extra+off)-*(d3_l->extra+offset))/2;
207 *(d3_l->extra+offset)+=carry;
208 *(d3_l->extra+off)-=carry;
213 /* cryst -> amorph diffusion in z direction */
218 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;
220 if(*(d3_l->status+off)&AMORPH) carry=(int)(d_r*(*(d3_l->extra+offset)));
223 *(d3_l->extra+offset)-=carry;
224 *(d3_l->extra+off)+=carry;
236 int calc_pressure(d3_lattice *d3_l,int range)
242 for(x=0;x<d3_l->max_x;x++)
244 for(y=0;y<d3_l->max_y;y++)
246 for(z=0;z<d3_l->max_z;z++)
249 for(i=-range;i<=range;i++)
251 for(j=-range;j<=range;j++)
255 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;
256 if(*(d3_l->status+off)&AMORPH) count+=((double)*(d3_l->extra+off))/(i*i+j*j);
260 *(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_VPTR);
268 int distrib_c_old(d3_lattice *d3_l,int t_c,double a,double b)
270 int i,j,k,total,area;
276 area=d3_l->max_x*d3_l->max_y;
277 area_h=(int *)malloc(d3_l->max_z*sizeof(int));
280 sum=b*d3_l->max_z+a*d3_l->max_z*(d3_l->max_z+1)/2;
281 for(i=0;i<d3_l->max_z;i++)
286 if(!(*(d3_l->status+(i*area)+j)&AMORPH))
291 temp=(int)((i+1)*a+b)*t_c/(sum*area_h[i]);
294 if(!(*(d3_l->status+(i*area)+j)&AMORPH))
296 *(d3_l->extra+(i*area)+j)=temp;
300 left=(int)(((i+1)*a+b)*t_c/sum)%area_h[i];
303 x=get_rand(d3_l->max_x);
304 y=get_rand(d3_l->max_y);
305 if(!(*(d3_l->status+(i*area)+x+y*d3_l->max_x)&AMORPH))
307 *(d3_l->extra+(i*area)+x+y*d3_l->max_x)+=1;
316 x=get_rand(d3_l->max_x);
317 y=get_rand(d3_l->max_y);
318 z=get_rand_lgp(d3_l->max_z,a,b);
319 if(!(*(d3_l->status+x+y*d3_l->max_x+z*area)&AMORPH))
321 *(d3_l->extra+x+y*d3_l->max_x+z*area)+=1;
330 int save_to_file(char *sf,d3_lattice *d3_l,info *my_inf)
334 if((sf_fd=open(sf,O_WRONLY|O_CREAT))<0)
336 puts("cannot open save file");
339 if(write(sf_fd,d3_l,sizeof(d3_lattice))<sizeof(d3_lattice))
341 puts("failed saving d3 lattice struct");
344 if(write(sf_fd,my_inf,sizeof(info))<sizeof(info))
346 puts("failed saving info struct");
349 c=d3_l->max_x*d3_l->max_y*d3_l->max_z;
350 if(write(sf_fd,d3_l->status,c*sizeof(unsigned char))<c*sizeof(unsigned char))
352 puts("failed saving status of d3 lattice sites");
355 if(write(sf_fd,d3_l->extra,c*sizeof(int))<c*sizeof(int))
357 puts("failed saving sites concentration");
365 int load_from_file(char *lf,d3_lattice *d3_l,info *my_inf)
369 if((lf_fd=open(lf,O_RDONLY))<0)
371 puts("cannot open load file");
374 if(read(lf_fd,d3_l,sizeof(d3_lattice))<sizeof(d3_lattice))
376 puts("failed reading d3 lattice struct");
379 if(read(lf_fd,my_inf,sizeof(info))<sizeof(info))
381 puts("failed reading info struct");
384 c=d3_l->max_x*d3_l->max_y*d3_l->max_z;
385 if((d3_l->status=(unsigned char*)malloc(c*sizeof(unsigned char)))==NULL)
387 puts("cannot allocate status buffer");
390 if((d3_l->extra=(int *)malloc(c*sizeof(int)))==NULL)
392 puts("cannot allocate concentration buffer");
395 if(read(lf_fd,d3_l->status,c*sizeof(unsigned char))<c*sizeof(unsigned char))
397 puts("failed reading status of d3 lattice sites");
400 if(read(lf_fd,d3_l->extra,c*sizeof(int))<c*sizeof(int))
402 puts("failed reading sites concentration");
410 int convert_file(char *cf,d3_lattice *d3_l)
415 if((c_fd=open(cf,O_WRONLY|O_CREAT))<0)
417 puts("cannot open convert file");
420 dprintf(c_fd,"# created by nlsop (gnuplot format)\n");
421 for(x=0;x<d3_l->max_x;x++)
423 for(y=0;y<d3_l->max_y;y++)
425 for(z=0;z<d3_l->max_z;z++)
427 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);
436 int main(int argc,char **argv)
438 u32 x,y,z,x_c,y_c,z_c;
439 int i,quit,escape,switchmode,nowait;
442 char s_file[MAX_CHARS];
443 char s_file_tmp[MAX_CHARS];
444 char l_file[MAX_CHARS];
445 char c_file[MAX_CHARS];
447 char r_file[MAX_CHARS];
452 char status_txt[MAX_TXT];
453 char conc_txt[MAX_TXT];
454 char steps_txt[MAX_TXT];
455 char cc_txt[MAX_TXT];
458 char ap_txt[MAX_TXT];
459 char el_txt[MAX_TXT];
460 char cd_txt[MAX_TXT];
462 char ap2_txt[MAX_TXT];
463 char cd2_txt[MAX_TXT];
464 char el2_txt[MAX_TXT];
465 char dr_txt[MAX_TXT];
466 char mode_txt[MAX_TXT];
467 char *arg_v[MAX_ARGV];
513 my_info.a_el=atof(argv[++i]);
516 my_info.b_el=atof(argv[++i]);
519 d3_l.max_x=atoi(argv[++i]);
522 d3_l.max_y=atoi(argv[++i]);
525 d3_l.max_z=atoi(argv[++i]);
540 my_info.steps=atoi(argv[++i]);
543 refresh=atoi(argv[++i]);
546 my_info.range=atoi(argv[++i]);
549 my_info.a_ap=atof(argv[++i]);
552 my_info.b_ap=atof(argv[++i]);
555 my_info.a_cd=atof(argv[++i]);
558 my_info.b_cd=atof(argv[++i]);
562 my_info.cc=atoi(argv[++i]);
566 resave=atoi(argv[++i]);
569 strcpy(l_file,argv[++i]);
570 if(i<argc-1) if(argv[i+1][0]!='-') strcpy(c_file,argv[++i]);
574 my_info.d_r=atof(argv[++i]);
577 strcpy(l_file,argv[++i]);
580 strcpy(s_file,argv[++i]);
583 strcpy(r_file,argv[++i]);
597 if(!strcmp(s_file,""))
599 puts("NODFB defined, run with -S option");
604 if(!strcmp(r_file,"")) rand_init(NULL);
605 else rand_init(r_file);
607 if(!strcmp(l_file,""))
609 i=d3_l.max_x*d3_l.max_y*d3_l.max_z;
611 d3_lattice_init(&argc,argv,&d3_l);
613 if((d3_l.status=(unsigned char *)malloc(i*sizeof(unsigned char)))==NULL)
615 puts("failed allocating status buffer");
618 memset(d3_l.status,0,i*sizeof(unsigned char));
619 if((d3_l.extra=(int *)malloc(i*sizeof(int)))==NULL)
621 puts("failed allocating concentration buffer");
624 memset(d3_l.extra,0,i*sizeof(int));
627 load_from_file(l_file,&d3_l,&my_info);
630 if(!strcmp(c_file,"")) sprintf(c_file,"%s_gnuplot",l_file);
631 printf("converting file %s to %s\n",l_file,c_file);
632 convert_file(c_file,&d3_l);
637 else d3_lattice_init(&argc,argv,&d3_l);
642 d3_event_init(&d3_l);
646 strcpy(a_txt,"args:");
647 sprintf(s_txt,"steps: %d",my_info.steps);
648 sprintf(r_txt,"pressure range: %d",my_info.range);
649 sprintf(ap_txt,"pressure faktor: %.2f",my_info.a_ap);
650 sprintf(ap2_txt,"pressure offset: %.2f",my_info.b_ap);
651 sprintf(el_txt,"energy loss slope: %.2f",my_info.a_el);
652 sprintf(el2_txt,"energy loss offset: %.2f",my_info.b_el);
653 sprintf(cd_txt,"c distrib slope: %.2f",my_info.a_cd);
654 sprintf(cd2_txt,"c distrib offset: %.2f",my_info.b_cd);
655 sprintf(dr_txt,"diffusion rate: %.2f",my_info.d_r);
656 strcpy(mode_txt,"view: a/c mode");
666 arg_v[10]=steps_txt;;
683 if(!strcmp(l_file,""))
686 while((i<my_info.steps) && (quit==0) && (escape==0))
688 x_c=get_rand(d3_l.max_x);
689 y_c=get_rand(d3_l.max_y);
690 z_c=get_rand_lgp(d3_l.max_z,my_info.a_el,my_info.b_el);
691 distrib_c(&d3_l,my_info.d_r,my_info.a_cd,my_info.b_cd,z_diff);
692 process_cell(&d3_l,x_c,y_c,z_c,my_info.range,my_info.a_ap,my_info.b_ap,&(my_info.cc));
696 sprintf(x_txt,"x: %d",x+1);
697 sprintf(y_txt,"y: %d",y+1);
698 sprintf(z_txt,"z: %d",z+1);
699 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');
700 sprintf(conc_txt,"conc: %d",*(d3_l.extra+x+y*d3_l.max_x+z*d3_l.max_x*d3_l.max_y));
701 sprintf(steps_txt,"step: %d",i);
702 sprintf(cc_txt,"total c: %d",my_info.cc);
703 d3_lattice_draw(&d3_l,x,y,z,24,arg_v,mode);
706 if(i%resave==0 && strcmp(s_file,"") && resave!=0)
708 sprintf(s_file_tmp,"%s_%d_of_%d",s_file,i,my_info.steps);
709 save_to_file(s_file_tmp,&d3_l,&my_info);
711 printf("saved %s\n",s_file_tmp);
718 if(strcmp(s_file,""))
720 printf("saved %s\n",s_file);
721 save_to_file(s_file,&d3_l,&my_info);
725 /* allocating buffer for pressure values */
726 if((d3_l.v_ptr=malloc(d3_l.max_x*d3_l.max_y*d3_l.max_z*sizeof(unsigned char)))==NULL)
728 puts("cannot allocate buffer for pressure values");
732 calc_pressure(&d3_l,my_info.range);
734 while((quit==0) && (escape==0) && (nowait==0))
737 if(switchmode==0) mode=0;
738 if(switchmode==1) mode=1;
739 if(switchmode==2) mode=2;
741 sprintf(x_txt,"x: %d",x+1);
742 sprintf(y_txt,"y: %d",y+1);
743 sprintf(z_txt,"z: %d",z+1);
744 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');
745 sprintf(conc_txt,"conc: %d",*(d3_l.extra+x+y*d3_l.max_x+z*d3_l.max_x*d3_l.max_y));
746 strcpy(steps_txt,"step: end!");
747 sprintf(cc_txt,"total c: %d",my_info.cc);
748 if(switchmode==0) strcpy(mode_txt,"view: a/c mode");
749 if(switchmode==1) strcpy(mode_txt,"view: c conc mode");
750 if(switchmode==2) strcpy(mode_txt,"view: a pressure mode");
751 d3_lattice_draw(&d3_l,x,y,z,24,arg_v,mode);
752 scan_event(&d3_l,&x,&y,&z,&quit,&escape,&switchmode);
755 d3_lattice_release(&d3_l);