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>
47 #include "../api/network.h"
48 #include "../api/event.h"
49 #include "../api/list.h"
51 #define MAKE_AMORPH(N) *(N)|=AMORPH
52 #define MAKE_CRYST(N) *(N)&=~AMORPH
57 printf("%s <server ip>\n",prog);
62 * nlsop internal functions
65 int sputter(d3_lattice *d3_l)
70 size=d3_l->max_x*d3_l->max_y;
74 for(i=0;i<d3_l->max_z-1;i++)
76 memcpy(d3_l->status+offl,d3_l->status+offh,size);
77 memcpy(d3_l->extra+offl,d3_l->extra+offh,size*sizeof(int));
81 memset(d3_l->status+offl,0,size);
82 memset(d3_l->extra+offl,0,size);
87 int process_cell(d3_lattice *d3_l,u32 x,u32 y,u32 z,info *my_info,u32 nel_z)
95 thiz=d3_l->status+x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y;
96 conc=d3_l->extra+x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y;
98 for(i=-(my_info->range);i<=my_info->range;i++)
100 for(j=-(my_info->range);j<=my_info->range;j++)
104 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;
105 if(*(d3_l->status+off)&AMORPH) p+=my_info->s*(*(d3_l->extra+off))*URAND_MAX/(i*i+j*j);
109 p+=*conc*my_info->c*URAND_MAX;
112 if(get_rand(URAND_MAX)<=p) MAKE_AMORPH(thiz);
115 /* assume 1-p probability */
116 /* also look for neighbours ! */
117 q=(URAND_MAX-p)>0?URAND_MAX-p:0;
119 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;
120 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;
121 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;
122 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;
123 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 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;
127 if(get_rand(URAND_MAX)>p) MAKE_CRYST(thiz);
133 int distrib_c(d3_lattice *d3_l,info *my_info,int step,u32 rj_m,u32 *rj_g)
140 /* put one c ion somewhere in the lattice */
141 x=get_rand(d3_l->max_x);
142 y=get_rand(d3_l->max_y);
143 z=get_rand_reject(d3_l->max_z,rj_m,rj_g);
144 *(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)
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;
218 } /* if step modulo diff_rate == 0 */
223 /* save to file --> send to server :) --> T O D O <-- */
229 int save_to_file(char *sf,d3_lattice *d3_l,info *my_inf)
233 if((sf_fd=open(sf,O_WRONLY|O_CREAT))<0)
235 puts("cannot open save file");
238 if(write(sf_fd,d3_l,sizeof(d3_lattice))<sizeof(d3_lattice))
240 puts("failed saving d3 lattice struct");
243 if(write(sf_fd,my_inf,sizeof(info))<sizeof(info))
245 puts("failed saving info struct");
248 c=d3_l->max_x*d3_l->max_y*d3_l->max_z;
249 if(write(sf_fd,d3_l->status,c*sizeof(unsigned char))<c*sizeof(unsigned char))
251 puts("failed saving status of d3 lattice sites");
254 if(write(sf_fd,d3_l->extra,c*sizeof(int))<c*sizeof(int))
256 puts("failed saving sites concentration");
264 /* load from file --> receive from server :) --> T O D O <-- */
265 int receive_data( ) {
270 int load_from_file(char *lf,d3_lattice *d3_l,info *my_inf)
273 int lf_fd,c,pos,end,data,data_len,strip;
275 if((lf_fd=open(lf,O_RDONLY))<0)
277 puts("cannot open load file");
280 if(read(lf_fd,d3_l,sizeof(d3_lattice))<sizeof(d3_lattice))
282 puts("failed reading d3 lattice struct");
285 pos=lseek(lf_fd,0,SEEK_CUR);
286 printf("psition: %d (%d)\n",pos,sizeof(d3_lattice));
287 data=d3_l->max_x*d3_l->max_y*d3_l->max_z;
288 data_len=data*(sizeof(int)+sizeof(unsigned char));
289 printf("there are %d volumes so we need %d of bytes\n",data,data_len);
290 end=lseek(lf_fd,0,SEEK_END);
292 printf("end: %d => length: %d => guessed info size: %d bytes\n",end,end-pos,c);
293 strip=sizeof(info)-c;
294 printf("as real programs info size is %d, we strip %d bytes\n",sizeof(info),strip);
295 lseek(lf_fd,pos,SEEK_SET);
297 if(strip>0) c-=strip;
300 puts("info smaller then strip size");
303 if(read(lf_fd,my_inf,c)<c)
305 puts("failed reading info struct");
308 if(strip>0) memset(my_inf+c,0,strip);
309 if(strip<0) lseek(lf_fd,(-1*strip),SEEK_CUR);
310 c=d3_l->max_x*d3_l->max_y*d3_l->max_z;
311 if((d3_l->status=(unsigned char*)malloc(c*sizeof(unsigned char)))==NULL)
313 puts("cannot allocate status buffer");
316 if((d3_l->extra=(int *)malloc(c*sizeof(int)))==NULL)
318 puts("cannot allocate concentration buffer");
321 if(read(lf_fd,d3_l->status,c*sizeof(unsigned char))<c*sizeof(unsigned char))
323 puts("failed reading status of d3 lattice sites");
326 if(read(lf_fd,d3_l->extra,c*sizeof(int))<c*sizeof(int))
328 puts("failed reading sites concentration");
336 u32 get_reject_graph(info *my_info,d3_lattice *d3_l,char *file,u32 *graph) {
345 if((fd=open(file,O_RDONLY))<0)
347 puts("cannot open file to calculate rejection graph");
350 if((flag=(unsigned char *)malloc(d3_l->max_z))==NULL)
352 puts("cannot malloc flag memory for rejection graph");
355 memset(flag,0,d3_l->max_z);
356 memset(graph,0,d3_l->max_z*sizeof(u32));
363 k=read(fd,&buf[i],1);
364 if((buf[i]=='\n')||(k==0)) break;
369 a=atof(p)/10; /* nm */
372 if(a>d3_l->max_z*CELL_LENGTH) k=0;
375 graph[(int)(a/CELL_LENGTH)]=(int)(URAND_MAX/100*b);
376 flag[(int)(a/CELL_LENGTH)]=1;
380 /* do (linear) interpolation here! */
388 while(flag[i]==0&&i<d3_l->max_z) i++;
389 for(k=j+1;k<i;k++) graph[k]=(int)((k-j)*((int)graph[i]-(int)graph[j])/(i-j))+graph[j];
390 if(graph[i]>max) max=graph[i];
395 #ifdef DEBUG_INTERPOL_PROFILE
396 printf("debug: %s (interpolated profile)\n",file);
397 for(i=0;i<d3_l->max_z;i++) printf("%d %d\n",i,graph[i]);
409 int main(int argc,char **argv)
412 u32 x,y,z,x_c,y_c,z_c;
418 char p_file[MAX_CHARS];
419 char n_e_file[MAX_CHARS];
420 char r_file[MAX_CHARS];
427 unsigned char err_dc,dc;
428 #define DC_QUIT (1<<0)
430 #define DC_END (1<<2)
436 strcpy(server_ip,"");
437 strcpy(p_file,IMP_PROFILE);
438 strcpy(n_e_file,NEL_PROFILE);
446 /* parse/check argv */
447 for(i=1;i<argc;i++) {
448 if(argv[i][0]=='-') {
454 strncpy(server_ip,argv[++i],16);
457 strcpy(r_file,argv[++i]);
460 strcpy(p_file,argv[++i]);
463 strcpy(n_e_file,argv[++i]);
466 port=atoi(argv[++i]);
474 if(!strcmp(server_ip,"")) {
479 /* connect to server and notify about free resource */
480 network_init(&net,1);
481 network_set_connection_info(&net,0,server_ip,port);
482 network_connect(&net,0);
486 /* get info (+data) */
488 /* care for signals */
490 signal(SIGTERM,send_data(&d3_l,&my_info,i,&err_dc));
493 if(!strcmp(r_file,"")) rand_init(NULL);
494 else rand_init(r_file);
496 /* compute graphs for random number rejection method */
497 if((c_profile=(u32 *)malloc(d3_l.max_z*sizeof(unsigned int)))==NULL) {
498 puts("failed allocating memory for carbon profile graph");
501 if((n_e_loss=(u32 *)malloc(d3_l.max_z*sizeof(unsigned int)))==NULL) {
502 puts("failed allocating memory for nuclear energy loss graph");
505 ip_max=get_reject_graph(&my_info,&d3_l,p_file,c_profile);
506 ne_max=get_reject_graph(&my_info,&d3_l,n_e_file,n_e_loss);
508 /* array nel_z[] keeping nuclear energy loss values scaled to URAND_MAX */
509 nel_z=(u32 *)malloc(d3_l.max_z*sizeof(unsigned int));
511 printf("failed allocating nel_z array mem\n");
514 for(i=0;i<d3_l-max_z;i++) nel_z[i]=URAND_MAX*(1.0*n_e_loss[i]/ne_max);
517 /* this should be obsolete - z is high enough - we check now! */
518 if(c_profile[d3_l.max_z-1]!=0) {
519 printf("max_z (%d) too small - sputtering not possible\n",d3_l.max_z);
523 /* sputtering really possible ?*/
524 if(n_e_loss[d3_l.max_z-1]!=0)
525 printf("warning: max_z (%d) too small, there may be amorphous volumes\n",
529 * start of simulation
533 while(i<my_info.steps) {
534 for(j=0;j<my_info.cpi;j++) {
535 x_c=get_rand(d3_l.max_x);
536 y_c=get_rand(d3_l.max_y);
537 // z_c=get_rand_reject(d3_l.max_z,ne_max,n_e_loss);
538 z_c=get_rand(d3_l.max_z);
539 process_cell(&d3_l,x_c,y_c,z_c,&my_info,nel_z[z_c]);
541 distrib_c(&d3_l,&my_info,i,ip_max,c_profile);
542 if(i%resave==0 && i!=0) {
544 send_data(&d3_l,&my_info,i,&dc);
547 if(i%my_info.s_rate==0) sputter(&d3_l);
552 send_data(&d3_l,&my_info,i,&dc);
554 /* shutdown/free/close everything now ... */