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 printf("-m <value> \t number of ion hits sputtering 3nm (default %d)\n",S_RATE);
84 int sputter(d3_lattice *d3_l)
89 size=d3_l->max_x*d3_l->max_y;
93 for(i=0;i<d3_l->max_z-1;i++)
95 memcpy(d3_l->status+offl,d3_l->status+offh,size);
96 memcpy(d3_l->extra+offl,d3_l->extra+offh,size*sizeof(int));
100 memset(d3_l->status+offl,0,size);
101 memset(d3_l->extra+offl,0,size);
106 int process_cell(d3_lattice *d3_l,u32 x,u32 y,u32 z,info *my_info,u32 nel_z)
114 thiz=d3_l->status+x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y;
115 conc=d3_l->extra+x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y;
117 for(i=-(my_info->range);i<=my_info->range;i++)
119 for(j=-(my_info->range);j<=my_info->range;j++)
123 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;
124 if(*(d3_l->status+off)&AMORPH) p+=my_info->s*(*(d3_l->extra+off))*URAND_MAX/(i*i+j*j);
128 p+=*conc*my_info->c*URAND_MAX;
131 if(get_rand(URAND_MAX)<=p) MAKE_AMORPH(thiz);
134 /* assume 1-p probability */
135 /* also look for neighbours ! */
136 q=(URAND_MAX-p)>0?URAND_MAX-p: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+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;
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-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;
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;
143 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;
146 if(get_rand(URAND_MAX)>p) MAKE_CRYST(thiz);
152 int distrib_c(d3_lattice *d3_l,info *my_info,int step,u32 rj_m,u32 *rj_g)
159 /* put one c ion somewhere in the lattice */
160 x=get_rand(d3_l->max_x);
161 y=get_rand(d3_l->max_y);
162 z=get_rand_reject(d3_l->max_z,rj_m,rj_g);
163 *(d3_l->extra+x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y)+=1;
166 if(step%my_info->diff_rate==0)
169 for(i=0;i<d3_l->max_x;i++)
171 for(j=0;j<d3_l->max_y;j++)
173 for(k=0;k<d3_l->max_z;k++)
175 offset=i+j*d3_l->max_x+k*d3_l->max_x*d3_l->max_y;
176 /* case amorph: amorph <- cryst diffusion */
177 if(*(d3_l->status+offset)&AMORPH)
183 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;
185 if(!(*(d3_l->status+off)&AMORPH)) carry=(int)(my_info->dr_ac*(*(d3_l->extra+off)));
188 *(d3_l->extra+offset)+=carry;
189 *(d3_l->extra+off)-=carry;
197 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;
199 if(!(*(d3_l->status+off)&AMORPH)) carry=(int)(my_info->dr_ac*(*(d3_l->extra+off)));
202 *(d3_l->extra+offset)+=carry;
203 *(d3_l->extra+off)-=carry;
211 off=i+j*d3_l->max_x+(k-1)*d3_l->max_x*d3_l->max_y;
213 if(!*(d3_l->status+off)&AMORPH) carry=(int)(my_info->dr_ac*(*(d3_l->extra+off)));
216 *(d3_l->extra+off)-=carry;
217 *(d3_l->extra+offset)+=carry;
222 off=i+j*d3_l->max_x+(k+1)*d3_l->max_x*d3_l->max_y;
224 if(!*(d3_l->status+off)&AMORPH) carry=(int)(my_info->dr_ac*(*(d3_l->extra+off)));
227 *(d3_l->extra+off)-=carry;
228 *(d3_l->extra+offset)+=carry;
233 /* case not amorph: cryst <-> cryst diffusion */
234 if(my_info->c_diff) {
235 /* if there is c diff, no diff in z-direction */
241 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;
243 if(!(*(d3_l->status+off)&AMORPH))
245 carry=(int)(my_info->dr_cc*(*(d3_l->extra+off)-*(d3_l->extra+offset))/2);
248 *(d3_l->extra+offset)+=carry;
249 *(d3_l->extra+off)-=carry;
258 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;
260 if(!(*(d3_l->status+off)&AMORPH))
262 carry=(int)(my_info->dr_cc*(*(d3_l->extra+off)-*(d3_l->extra+offset))/2);
265 *(d3_l->extra+offset)+=carry;
266 *(d3_l->extra+off)-=carry;
279 } /* if step modulo diff_rate == 0 */
284 int calc_pressure(d3_lattice *d3_l,int range)
290 for(x=0;x<d3_l->max_x;x++)
292 for(y=0;y<d3_l->max_y;y++)
294 for(z=0;z<d3_l->max_z;z++)
297 for(i=-range;i<=range;i++)
299 for(j=-range;j<=range;j++)
303 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;
304 if(*(d3_l->status+off)&AMORPH) count+=((double)*(d3_l->extra+off))/(i*i+j*j);
308 if(count>max) max=count;
313 for(x=0;x<d3_l->max_x;x++)
315 for(y=0;y<d3_l->max_y;y++)
317 for(z=0;z<d3_l->max_z;z++)
320 for(i=-range;i<=range;i++)
322 for(j=-range;j<=range;j++)
326 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;
327 if(*(d3_l->status+off)&AMORPH) count+=((double)*(d3_l->extra+off))/(i*i+j*j);
331 *(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);
339 int calc_max_extra(d3_lattice *d3_l)
344 for(x=0;x<d3_l->max_x;x++)
346 for(y=0;y<d3_l->max_y;y++)
348 for(z=0;z<d3_l->max_z;z++)
350 off=x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y;
351 if(*(d3_l->extra+off)>max) max=*(d3_l->extra+off);
359 int write_ac_distr(d3_lattice *d3_l,int ac_distr)
366 si_count=d3_l->max_x*d3_l->max_y*SI_PER_VOLUME;
367 if(ac_distr==1) strcpy(file,"carbon_in_av.plot");
368 if(ac_distr==2) strcpy(file,"carbon_in_cv.plot");
369 if(ac_distr==3) strcpy(file,"carbon.plot");
370 if(ac_distr==4) strcpy(file,"amorphous_volumes.plot");
372 if((fd=open(file,O_WRONLY|O_CREAT))<0)
374 puts("cannot open plot file");
378 for(z=0;z<d3_l->max_z;z++)
381 for(x=0;x<d3_l->max_x;x++)
383 for(y=0;y<d3_l->max_y;y++)
385 offset=x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y;
387 if(*(d3_l->status+offset)&AMORPH) count+=*(d3_l->extra+offset);
389 if(!(*(d3_l->status+offset)&AMORPH)) count+=*(d3_l->extra+offset);
390 if(ac_distr==3) count+=*(d3_l->extra+offset);
392 if(*(d3_l->status+offset)&AMORPH) count+=1;
396 if(ac_distr==4) dprintf(fd,"%d %d\n",z*CELL_LENGTH,count);
397 else dprintf(fd,"%d %f\n",z*CELL_LENGTH,100.0*count/si_count);
405 int write_bmp(d3_lattice *d3_l,int window,u32 x,u32 y,u32 z,int max)
407 int fd,i,j,size=0,foo=0,k,sum;
408 int width=0,height=0;
409 char bmpfile[MAX_CHARS];
414 sprintf(bmpfile,"x-z_%d.bmp",y);
416 size=(foo+(4-foo%4))*d3_l->max_z;
422 sprintf(bmpfile,"y-z_%d.bmp",x);
424 size=(foo+(4-foo%4))*d3_l->max_z;
430 sprintf(bmpfile,"x-y_%d.bmp",z);
432 size=(foo+(4-foo%4))*d3_l->max_y;
438 sprintf(bmpfile,"x-z_cdistr_%d.bmp",y);
440 size=(foo+(4-foo%4))*d3_l->max_z;
446 sprintf(bmpfile,"y-z_cdistr_%d.bmp",x);
448 size=(foo+(4-foo%4))*d3_l->max_z;
454 sprintf(bmpfile,"x-y_cdistr_%d.bmp",z);
456 size=(foo+(4-foo%4))*d3_l->max_y;
462 sprintf(bmpfile,"x-z_a_cdistr_%d.bmp",y);
464 size=(foo+(4-foo%4))*d3_l->max_z;
470 sprintf(bmpfile,"y-z_a_cdistr_%d.bmp",x);
472 size=(foo+(4-foo%4))*d3_l->max_z;
478 sprintf(bmpfile,"x-y_a_cdistr_%d.bmp",z);
480 size=(foo+(4-foo%4))*d3_l->max_y;
486 sprintf(bmpfile,"x-z_c_cdistr_%d.bmp",y);
488 size=(foo+(4-foo%4))*d3_l->max_z;
494 sprintf(bmpfile,"y-z_c_cdistr_%d.bmp",x);
496 size=(foo+(4-foo%4))*d3_l->max_z;
502 sprintf(bmpfile,"x-y_c_cdistr_%d.bmp",z);
504 size=(foo+(4-foo%4))*d3_l->max_y;
508 if((fd=open(bmpfile,O_WRONLY|O_CREAT))<0)
510 puts("cannot open bmp file");
515 buf[0]='B'; /* std header start */
517 buf[2]=(size+0x36)&0xff; /* file size */
518 buf[3]=(size+0x36)>>8;
520 buf[10]=0x36; /* offset to data */
522 buf[14]=0x28; /* length of bmp info header */
524 buf[18]=width&0xff; /* width and height */
530 buf[26]=1; /* # planes -> 1 */
532 buf[28]=24; /* bits per pixel -> 2^24 (true color) */
534 memset(buf+30,0,4); /* compression -> none */
535 buf[34]=size&0xff; /* data size */
538 buf[38]=0x12; /* res: pixel/meter */
544 memset(buf+46,0,8); /* no colors, no important colors */
546 if(write(fd,buf,54)<54)
548 puts("failed writing bmp header");
553 for(j=0;j<d3_l->max_z;j++)
555 for(i=0;i<d3_l->max_x;i++)
559 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;
561 memset(buf,(unsigned char)sum,3);
562 if(write(fd,buf,3)<3)
564 puts("failed writing rgb values to bmp file");
570 memset(buf,0,4-foo%4);
571 if(write(fd,buf,4-foo%4)<4-foo%4)
573 puts("failed writing 4 byte ending");
581 for(j=0;j<d3_l->max_z;j++)
583 for(i=0;i<d3_l->max_y;i++)
587 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;
589 memset(buf,(unsigned char)sum,3);
590 if(write(fd,buf,3)<3)
592 puts("failed writing rgb values to bmp file");
598 memset(buf,0,4-foo%4);
599 if(write(fd,buf,4-foo%4)<4-foo%4)
601 puts("failed writing 4 byte ending");
609 for(j=0;j<d3_l->max_y;j++)
611 for(i=0;i<d3_l->max_x;i++)
613 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);
614 else memset(buf,0,3);
615 if(write(fd,buf,3)<3)
617 puts("failed writing rgb values to bmp file");
623 memset(buf,0,4-foo%4);
624 if(write(fd,buf,4-foo%4)<4-foo%4)
626 puts("failed writing 4 byte ending");
634 for(j=0;j<d3_l->max_z;j++)
636 for(i=0;i<d3_l->max_x;i++)
638 sum=*(d3_l->extra+i+y*d3_l->max_x+(d3_l->max_z-j-1)*d3_l->max_x*d3_l->max_y);
640 memset(buf,(unsigned char)sum,3);
641 if(write(fd,buf,3)<3)
643 puts("failed writing rgb values to bmp file");
649 memset(buf,0,4-foo%4);
650 if(write(fd,buf,4-foo%4)<4-foo%4)
652 puts("failed writing 4 byte ending");
660 for(j=0;j<d3_l->max_z;j++)
662 for(i=0;i<d3_l->max_x;i++)
664 sum=*(d3_l->extra+x+i*d3_l->max_x+(d3_l->max_z-j-1)*d3_l->max_x*d3_l->max_y);
666 memset(buf,(unsigned char)sum,3);
667 if(write(fd,buf,3)<3)
669 puts("failed writing rgb values to bmp file");
675 memset(buf,0,4-foo%4);
676 if(write(fd,buf,4-foo%4)<4-foo%4)
678 puts("failed writing 4 byte ending");
686 for(j=0;j<d3_l->max_y;j++)
688 for(i=0;i<d3_l->max_x;i++)
690 sum=*(d3_l->extra+i+(d3_l->max_y-j-1)*d3_l->max_x+z*d3_l->max_x*d3_l->max_y);
692 memset(buf,(unsigned char)sum,3);
693 if(write(fd,buf,3)<3)
695 puts("failed writing rgb values to bmp file");
701 memset(buf,0,4-foo%4);
702 if(write(fd,buf,4-foo%4)<4-foo%4)
704 puts("failed writing 4 byte ending");
712 for(j=0;j<d3_l->max_z;j++)
714 for(i=0;i<d3_l->max_x;i++)
716 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);
719 memset(buf,(unsigned char)sum,3);
720 if(write(fd,buf,3)<3)
722 puts("failed writing rgb values to bmp file");
728 memset(buf,0,4-foo%4);
729 if(write(fd,buf,4-foo%4)<4-foo%4)
731 puts("failed writing 4 byte ending");
739 for(j=0;j<d3_l->max_z;j++)
741 for(i=0;i<d3_l->max_x;i++)
743 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);
746 memset(buf,(unsigned char)sum,3);
747 if(write(fd,buf,3)<3)
749 puts("failed writing rgb values to bmp file");
755 memset(buf,0,4-foo%4);
756 if(write(fd,buf,4-foo%4)<4-foo%4)
758 puts("failed writing 4 byte ending");
766 for(j=0;j<d3_l->max_y;j++)
768 for(i=0;i<d3_l->max_x;i++)
770 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);
773 memset(buf,(unsigned char)sum,3);
774 if(write(fd,buf,3)<3)
776 puts("failed writing rgb values to bmp file");
782 memset(buf,0,4-foo%4);
783 if(write(fd,buf,4-foo%4)<4-foo%4)
785 puts("failed writing 4 byte ending");
793 for(j=0;j<d3_l->max_z;j++)
795 for(i=0;i<d3_l->max_x;i++)
797 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);
800 memset(buf,(unsigned char)sum,3);
801 if(write(fd,buf,3)<3)
803 puts("failed writing rgb values to bmp file");
809 memset(buf,0,4-foo%4);
810 if(write(fd,buf,4-foo%4)<4-foo%4)
812 puts("failed writing 4 byte ending");
820 for(j=0;j<d3_l->max_z;j++)
822 for(i=0;i<d3_l->max_x;i++)
824 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);
827 memset(buf,(unsigned char)sum,3);
828 if(write(fd,buf,3)<3)
830 puts("failed writing rgb values to bmp file");
836 memset(buf,0,4-foo%4);
837 if(write(fd,buf,4-foo%4)<4-foo%4)
839 puts("failed writing 4 byte ending");
847 for(j=0;j<d3_l->max_y;j++)
849 for(i=0;i<d3_l->max_x;i++)
851 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);
854 memset(buf,(unsigned char)sum,3);
855 if(write(fd,buf,3)<3)
857 puts("failed writing rgb values to bmp file");
863 memset(buf,0,4-foo%4);
864 if(write(fd,buf,4-foo%4)<4-foo%4)
866 puts("failed writing 4 byte ending");
877 int save_to_file(char *sf,d3_lattice *d3_l,info *my_inf)
881 if((sf_fd=open(sf,O_WRONLY|O_CREAT))<0)
883 puts("cannot open save file");
886 if(write(sf_fd,d3_l,sizeof(d3_lattice))<sizeof(d3_lattice))
888 puts("failed saving d3 lattice struct");
891 if(write(sf_fd,my_inf,sizeof(info))<sizeof(info))
893 puts("failed saving info struct");
896 c=d3_l->max_x*d3_l->max_y*d3_l->max_z;
897 if(write(sf_fd,d3_l->status,c*sizeof(unsigned char))<c*sizeof(unsigned char))
899 puts("failed saving status of d3 lattice sites");
902 if(write(sf_fd,d3_l->extra,c*sizeof(int))<c*sizeof(int))
904 puts("failed saving sites concentration");
912 int load_from_file(char *lf,d3_lattice *d3_l,info *my_inf)
915 int lf_fd,c,pos,end,data,data_len,strip;
917 if((lf_fd=open(lf,O_RDONLY))<0)
919 puts("cannot open load file");
922 if(read(lf_fd,d3_l,sizeof(d3_lattice))<sizeof(d3_lattice))
924 puts("failed reading d3 lattice struct");
927 pos=lseek(lf_fd,0,SEEK_CUR);
928 printf("psition: %d (%d)\n",pos,sizeof(d3_lattice));
929 data=d3_l->max_x*d3_l->max_y*d3_l->max_z;
930 data_len=data*(sizeof(int)+sizeof(unsigned char));
931 printf("there are %d volumes so we need %d of bytes\n",data,data_len);
932 end=lseek(lf_fd,0,SEEK_END);
934 printf("end: %d => length: %d => guessed info size: %d bytes\n",end,end-pos,c);
935 strip=sizeof(info)-c;
936 printf("as real programs info size is %d, we strip %d bytes\n",sizeof(info),strip);
937 lseek(lf_fd,pos,SEEK_SET);
939 if(strip>0) c-=strip;
942 puts("info smaller then strip size");
945 if(read(lf_fd,my_inf,c)<c)
947 puts("failed reading info struct");
950 if(strip>0) memset(my_inf+c,0,strip);
951 if(strip<0) lseek(lf_fd,(-1*strip),SEEK_CUR);
952 c=d3_l->max_x*d3_l->max_y*d3_l->max_z;
953 if((d3_l->status=(unsigned char*)malloc(c*sizeof(unsigned char)))==NULL)
955 puts("cannot allocate status buffer");
958 if((d3_l->extra=(int *)malloc(c*sizeof(int)))==NULL)
960 puts("cannot allocate concentration buffer");
963 if(read(lf_fd,d3_l->status,c*sizeof(unsigned char))<c*sizeof(unsigned char))
965 puts("failed reading status of d3 lattice sites");
968 if(read(lf_fd,d3_l->extra,c*sizeof(int))<c*sizeof(int))
970 puts("failed reading sites concentration");
978 int convert_file(char *cf,d3_lattice *d3_l)
984 if((c_fd=open(cf,O_WRONLY|O_CREAT))<0)
986 puts("cannot open convert file");
989 dprintf(c_fd,"# created by nlsop (gnuplot format)\n");
990 for(x=0;x<d3_l->max_x;x++)
992 for(y=0;y<d3_l->max_y;y++)
994 for(z=0;z<d3_l->max_z;z++)
996 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);
1006 int get_c_ratio(double *c_ratio,char *pfile,info *my_info,d3_lattice *d3_l)
1011 unsigned char buf[32];
1015 if((p_fd=open(pfile,O_RDONLY))<0)
1017 puts("cannot open profile file");
1034 a=atof(p)/10; /* nm */
1037 if(a<=d3_l->max_z*CELL_LENGTH) d+=b;
1047 u32 get_reject_graph(info *my_info,d3_lattice *d3_l,char *file,u32 *graph) {
1052 unsigned char *flag;
1056 if((fd=open(file,O_RDONLY))<0)
1058 puts("cannot open file to calculate rejection graph");
1061 if((flag=(unsigned char *)malloc(d3_l->max_z))==NULL)
1063 puts("cannot malloc flag memory for rejection graph");
1066 memset(flag,0,d3_l->max_z);
1067 memset(graph,0,d3_l->max_z*sizeof(u32));
1074 k=read(fd,&buf[i],1);
1075 if((buf[i]=='\n')||(k==0)) break;
1080 a=atof(p)/10; /* nm */
1083 if(a>d3_l->max_z*CELL_LENGTH) k=0;
1086 graph[(int)(a/CELL_LENGTH)]=(int)(URAND_MAX/100*b);
1087 flag[(int)(a/CELL_LENGTH)]=1;
1091 /* do (linear) interpolation here! */
1094 while(i<d3_l->max_z)
1096 /* graph[0] is 0! */
1099 while(flag[i]==0&&i<d3_l->max_z) i++;
1100 for(k=j+1;k<i;k++) graph[k]=(int)((k-j)*((int)graph[i]-(int)graph[j])/(i-j))+graph[j];
1101 if(graph[i]>max) max=graph[i];
1106 #ifdef DEBUG_INTERPOL_PROFILE
1107 printf("debug: %s (interpolated profile)\n",file);
1108 for(i=0;i<d3_l->max_z;i++) printf("%d %d\n",i,graph[i]);
1114 int main(int argc,char **argv)
1116 u32 x,y,z,x_c,y_c,z_c;
1117 int i,j,quit,escape,switchmode,nowait,bmp,ac_distr;
1120 char s_file[MAX_CHARS];
1121 char s_file_tmp[MAX_CHARS];
1122 char l_file[MAX_CHARS];
1123 char c_file[MAX_CHARS];
1124 char p_file[MAX_CHARS];
1125 char n_e_file[MAX_CHARS];
1127 char r_file[MAX_CHARS];
1129 char xyz_txt[MAX_TXT];
1130 char status_txt[MAX_TXT];
1131 char conc_txt[MAX_TXT];
1132 char steps_txt[MAX_TXT];
1133 char cc_txt[MAX_TXT];
1134 char a_txt[MAX_TXT];
1135 char s_txt[MAX_TXT];
1136 char ballistic_txt[MAX_TXT];
1137 char carbon_txt[MAX_TXT];
1138 char stress_txt[MAX_TXT];
1139 char r_txt[MAX_TXT];
1140 char zdiff_txt[MAX_TXT];
1141 char diff_txt[MAX_TXT];
1142 char dr_ac_txt[MAX_TXT];
1143 char dr_cc_txt[MAX_TXT];
1144 char dose_txt[MAX_TXT];
1145 char mode_txt[MAX_TXT];
1146 char hpi_txt[MAX_TXT];
1147 char csat_txt[MAX_TXT];
1148 char *arg_v[MAX_ARGV];
1165 my_info.steps=STEPS;
1166 my_info.range=RANGE;
1175 my_info.dr_ac=DR_AC;
1176 my_info.dr_cc=DR_CC;
1177 my_info.diff_rate=DIFF_RATE;
1179 my_info.s_rate=S_RATE;
1188 strcpy(p_file,IMP_PROFILE);
1189 strcpy(n_e_file,NEL_PROFILE);
1197 printf("reading argv ...");
1213 d3_l.max_x=atoi(argv[++i]);
1216 d3_l.max_y=atoi(argv[++i]);
1219 d3_l.max_z=atoi(argv[++i]);
1229 my_info.steps=atoi(argv[++i]);
1232 refresh=atoi(argv[++i]);
1235 my_info.range=atoi(argv[++i]);
1238 my_info.s=atof(argv[++i]);
1241 my_info.b=atof(argv[++i]);
1244 my_info.c=atof(argv[++i]);
1247 resave=atoi(argv[++i]);
1250 strcpy(l_file,argv[++i]);
1251 if(i<argc-1) if(argv[i+1][0]!='-') strcpy(c_file,argv[++i]);
1255 my_info.dr_ac=atof(argv[++i]);
1258 my_info.dr_cc=atof(argv[++i]);
1261 my_info.diff_rate=atoi(argv[++i]);
1264 strcpy(l_file,argv[++i]);
1267 strcpy(s_file,argv[++i]);
1270 strcpy(r_file,argv[++i]);
1273 strcpy(p_file,argv[++i]);
1276 strcpy(n_e_file,argv[++i]);
1279 strcpy(l_file,argv[++i]);
1280 if(i<argc-1) if(argv[i+1][0]!='-') c_step=atoi(argv[++i]);
1283 my_info.cpi=atoi(argv[++i]);
1286 my_info.s_rate=atoi(argv[++i]);
1304 if(!strcmp(s_file,""))
1306 puts("NODFB defined, run with -S option");
1312 printf("rand init ...");
1315 if(!strcmp(r_file,"")) rand_init(NULL);
1316 else rand_init(r_file);
1320 printf("allocating data ...");
1323 if(!strcmp(l_file,""))
1325 i=d3_l.max_x*d3_l.max_y*d3_l.max_z;
1327 d3_lattice_init(&argc,argv,&d3_l);
1329 if((d3_l.status=(unsigned char *)malloc(i*sizeof(unsigned char)))==NULL)
1331 puts("failed allocating status buffer");
1334 memset(d3_l.status,0,i*sizeof(unsigned char));
1335 if((d3_l.extra=(int *)malloc(i*sizeof(int)))==NULL)
1337 puts("failed allocating concentration buffer");
1340 memset(d3_l.extra,0,i*sizeof(int));
1343 load_from_file(l_file,&d3_l,&my_info);
1346 if(!strcmp(c_file,"")) sprintf(c_file,"%s_gnuplot",l_file);
1347 printf("converting file %s to %s\n",l_file,c_file);
1348 convert_file(c_file,&d3_l);
1353 else d3_lattice_init(&argc,argv,&d3_l);
1362 d3_event_init(&d3_l);
1366 strcpy(a_txt,"args:");
1367 sprintf(s_txt,"steps: %d",my_info.steps);
1368 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));
1369 sprintf(r_txt,"pressure range: %d",my_info.range);
1370 sprintf(stress_txt,"stress term: %f",my_info.s);
1371 sprintf(ballistic_txt,"ballistic term: %f",my_info.b);
1372 sprintf(carbon_txt,"carbon term: %f",my_info.c);
1373 sprintf(dr_ac_txt,"a/c diffusion rate: %f",my_info.dr_ac);
1374 if(my_info.c_diff!=0) sprintf(dr_cc_txt,"c/c diffusion rate: %f",my_info.dr_cc);
1375 else sprintf(dr_cc_txt,"c/c diffusion rate: none");
1376 sprintf(zdiff_txt,"diffusion in z direction: %c",my_info.z_diff?'y':'n');
1377 sprintf(diff_txt,"diffusion every %d steps",my_info.diff_rate);
1378 strcpy(mode_txt,"view: a/c mode");
1379 sprintf(hpi_txt,"hits per ion: %d",my_info.cpi);
1380 sprintf(csat_txt,"sputter rate (3nm/#c): %d",my_info.s_rate);
1383 arg_v[3]=status_txt;
1393 arg_v[13]=zdiff_txt;
1395 arg_v[15]=ballistic_txt;
1396 arg_v[16]=carbon_txt;
1397 arg_v[17]=stress_txt;
1398 arg_v[18]=dr_ac_txt;
1399 arg_v[19]=dr_cc_txt;
1409 printf("random rejection graphs ...");
1412 /* compute graphs for random number rejection method */
1413 if((c_profile=(u32 *)malloc(d3_l.max_z*sizeof(unsigned int)))==NULL)
1415 puts("failed allocating memory for carbon profile graph");
1418 if((n_e_loss=(u32 *)malloc(d3_l.max_z*sizeof(unsigned int)))==NULL)
1420 puts("failed allocating memory for nuclear energy loss graph");
1423 ip_max=get_reject_graph(&my_info,&d3_l,p_file,c_profile);
1424 ne_max=get_reject_graph(&my_info,&d3_l,n_e_file,n_e_loss);
1431 if((!strcmp(l_file,""))||(c_step))
1434 /* this should be obsolete - z is high enough - we check now! */
1435 if(c_profile[d3_l.max_z-1]!=0)
1437 printf("max_z (%d) too small - sputtering not possible\n",d3_l.max_z);
1440 /* calculate ratio of c_simwindow / c_total */
1441 //if(get_c_ratio(&c_ratio,p_file,&my_info,&d3_l)!=1)
1443 // puts("failed calculating ratio");
1447 /* sputtering realy possible ?*/
1448 if(n_e_loss[d3_l.max_z-1]!=0)
1449 printf("warning: max_z (%d) too small - there may be amorphous volumes\n",d3_l.max_z);
1456 printf("%d\n",get_rand_reject(d3_l.max_z,ip_max,c_profile));
1459 printf("%d\n",get_rand_reject(d3_l.max_z,ne_max,n_e_loss));
1462 printf("%d\n",get_rand(d3_l.max_z));
1464 if(i==10000000) return 1;
1470 printf("starting simulation ... now! :)\n");
1473 i=(c_step?c_step:0);
1474 while((i<my_info.steps) && (quit==0) && (escape==0))
1477 if(i%refresh==0) printf("step: %d\n",i);
1479 for(j=0;j<my_info.cpi;j++)
1481 x_c=get_rand(d3_l.max_x);
1482 y_c=get_rand(d3_l.max_y);
1483 // z_c=get_rand_reject(d3_l.max_z,ne_max,n_e_loss);
1484 z_c=get_rand(d3_l.max_z);
1485 nel_z=URAND_MAX*(1.0*n_e_loss[z_c]/ne_max);
1486 process_cell(&d3_l,x_c,y_c,z_c,&my_info,nel_z);
1488 distrib_c(&d3_l,&my_info,i,ip_max,c_profile);
1492 sprintf(xyz_txt,"x: %d y: %d z: %d",x+1,y+1,z+1);
1493 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');
1494 sprintf(conc_txt,"conc: %d",*(d3_l.extra+x+y*d3_l.max_x+z*d3_l.max_x*d3_l.max_y));
1495 sprintf(steps_txt,"step: %d",i);
1496 sprintf(cc_txt,"total c: %d",my_info.cc);
1497 d3_lattice_draw(&d3_l,x,y,z,25,arg_v,mode,0,NULL,0,NULL,0);
1500 if(i%resave==0 && strcmp(s_file,"") && resave!=0 && i!=0)
1502 sprintf(s_file_tmp,"%s_%d_of_%d",s_file,i,my_info.steps);
1503 save_to_file(s_file_tmp,&d3_l,&my_info);
1505 printf("saved %s\n",s_file_tmp);
1509 if(i%my_info.s_rate==0) sputter(&d3_l);
1513 if(strcmp(s_file,""))
1515 printf("saved %s\n",s_file);
1516 save_to_file(s_file,&d3_l,&my_info);
1520 /* allocating buffer for pressure values */
1521 if((d3_l.v_ptr=malloc(d3_l.max_x*d3_l.max_y*d3_l.max_z*sizeof(unsigned char)))==NULL)
1523 puts("cannot allocate buffer for pressure values");
1527 calc_pressure(&d3_l,my_info.range);
1528 max_extra=calc_max_extra(&d3_l);
1530 while((quit==0) && (escape==0) && (nowait==0))
1533 if(switchmode==0) mode=0;
1534 if(switchmode==1) mode=1;
1535 if(switchmode==2) mode=2;
1536 if(switchmode==3) mode=3;
1538 sprintf(xyz_txt,"x: %d y: %d z: %d",x+1,y+1,z+1);
1539 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');
1540 sprintf(conc_txt,"conc: %d",*(d3_l.extra+x+y*d3_l.max_x+z*d3_l.max_x*d3_l.max_y));
1541 sprintf(steps_txt,"step: end");
1542 sprintf(cc_txt,"total c: %d",my_info.cc);
1543 if(switchmode==0) strcpy(mode_txt,"view: a/c mode");
1544 if(switchmode==1) strcpy(mode_txt,"view: c conc mode");
1545 if(switchmode==2) strcpy(mode_txt,"view: a pressure mode");
1546 if(switchmode==3) strcpy(mode_txt,"view: a/c + profiles mode");
1547 d3_lattice_draw(&d3_l,x,y,z,25,arg_v,mode,max_extra,c_profile,ip_max,n_e_loss,ne_max);
1550 scan_event(&d3_l,&x,&y,&z,&quit,&escape,&switchmode,&bmp,&ac_distr);
1551 if(bmp) write_bmp(&d3_l,bmp,x,y,z,max_extra);
1552 if(ac_distr) write_ac_distr(&d3_l,ac_distr);
1555 d3_lattice_release(&d3_l);