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>
52 #include "nlsop_general.h"
54 #define MAKE_AMORPH(N) *(N)|=AMORPH
55 #define MAKE_CRYST(N) *(N)&=~AMORPH
59 char p_file[MAX_CHARS];
60 char n_e_file[MAX_CHARS];
61 char r_file[MAX_CHARS];
68 int get_data_and_calc(t_event *event,void *allineed);
69 int nop(t_event *event,void *allineed);
74 printf("%s -i ip -p port -r/P/n random/profile/neloss file\n",prog);
79 * nlsop internal functions
82 int sputter(d3_lattice *d3_l)
87 size=d3_l->max_x*d3_l->max_y;
91 for(i=0;i<d3_l->max_z-1;i++)
93 memcpy(d3_l->status+offl,d3_l->status+offh,size);
94 memcpy(d3_l->extra+offl,d3_l->extra+offh,size*sizeof(int));
98 memset(d3_l->status+offl,0,size);
99 memset(d3_l->extra+offl,0,size);
104 int process_cell(d3_lattice *d3_l,u32 x,u32 y,u32 z,info *my_info,u32 nel_z)
112 thiz=d3_l->status+x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y;
113 conc=d3_l->extra+x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y;
115 for(i=-(my_info->range);i<=my_info->range;i++)
117 for(j=-(my_info->range);j<=my_info->range;j++)
121 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;
122 if(*(d3_l->status+off)&AMORPH) p+=my_info->s*(*(d3_l->extra+off))*URAND_MAX/(i*i+j*j);
126 p+=*conc*my_info->c*URAND_MAX;
129 if(get_rand(URAND_MAX)<=p) MAKE_AMORPH(thiz);
132 /* assume 1-p probability */
133 /* also look for neighbours ! */
134 q=(URAND_MAX-p)>0?URAND_MAX-p:0;
136 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;
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+((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;
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*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;
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;
144 if(get_rand(URAND_MAX)>p) MAKE_CRYST(thiz);
150 int distrib_c(d3_lattice *d3_l,info *my_info,int step,u32 rj_m,u32 *rj_g)
157 /* put one c ion somewhere in the lattice */
158 x=get_rand(d3_l->max_x);
159 y=get_rand(d3_l->max_y);
160 z=get_rand_reject(d3_l->max_z,rj_m,rj_g);
161 *(d3_l->extra+x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y)+=1;
164 if(step%my_info->diff_rate==0)
167 for(i=0;i<d3_l->max_x;i++)
169 for(j=0;j<d3_l->max_y;j++)
171 for(k=0;k<d3_l->max_z;k++)
173 offset=i+j*d3_l->max_x+k*d3_l->max_x*d3_l->max_y;
174 /* case amorph: amorph <- cryst diffusion */
175 if(*(d3_l->status+offset)&AMORPH)
181 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;
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;
195 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;
197 if(!(*(d3_l->status+off)&AMORPH)) carry=(int)(my_info->dr_ac*(*(d3_l->extra+off)));
200 *(d3_l->extra+offset)+=carry;
201 *(d3_l->extra+off)-=carry;
205 /* diff in z direction */
208 off=i+j*d3_l->max_x+(k-1)*d3_l->max_x*d3_l->max_y;
210 if(!*(d3_l->status+off)&AMORPH) carry=(int)(my_info->dr_ac*(*(d3_l->extra+off)));
213 *(d3_l->extra+off)-=carry;
214 *(d3_l->extra+offset)+=carry;
219 off=i+j*d3_l->max_x+(k+1)*d3_l->max_x*d3_l->max_y;
221 if(!*(d3_l->status+off)&AMORPH) carry=(int)(my_info->dr_ac*(*(d3_l->extra+off)));
224 *(d3_l->extra+off)-=carry;
225 *(d3_l->extra+offset)+=carry;
233 } /* if step modulo diff_rate == 0 */
238 u32 get_reject_graph(info *my_info,d3_lattice *d3_l,char *file,u32 *graph) {
247 if((fd=open(file,O_RDONLY))<0)
249 puts("cannot open file to calculate rejection graph");
252 if((flag=(unsigned char *)malloc(d3_l->max_z))==NULL)
254 puts("cannot malloc flag memory for rejection graph");
257 memset(flag,0,d3_l->max_z);
258 memset(graph,0,d3_l->max_z*sizeof(u32));
265 k=read(fd,&buf[i],1);
266 if((buf[i]=='\n')||(k==0)) break;
271 a=atof(p)/10; /* nm */
274 if(a>d3_l->max_z*CELL_LENGTH) k=0;
277 graph[(int)(a/CELL_LENGTH)]=(int)(URAND_MAX/100*b);
278 flag[(int)(a/CELL_LENGTH)]=1;
282 /* do (linear) interpolation here! */
290 while(flag[i]==0&&i<d3_l->max_z) i++;
291 for(k=j+1;k<i;k++) graph[k]=(int)((k-j)*((int)graph[i]-(int)graph[j])/(i-j))+graph[j];
292 if(graph[i]>max) max=graph[i];
297 #ifdef DEBUG_INTERPOL_PROFILE
298 printf("debug: %s (interpolated profile)\n",file);
299 for(i=0;i<d3_l->max_z;i++) printf("%d %d\n",i,graph[i]);
305 void send_data(int signum) {
311 c=gd3_l->max_x*gd3_l->max_y*gd3_l->max_z;
315 network_send(gnet->connection[0].fd,&dc,1);
316 network_receive(gnet->connection[0].fd,&data,sizeof(unsigned char));
317 printf("debug: sent dc\n");
319 network_send(gnet->connection[0].fd,(unsigned char *)gd3_l,
321 network_receive(gnet->connection[0].fd,&data,sizeof(unsigned char));
322 printf("debug: sent d3_lattice\n");
324 network_send(gnet->connection[0].fd,(unsigned char *)gmy_info,sizeof(info));
325 network_receive(gnet->connection[0].fd,&data,sizeof(unsigned char));
326 printf("debug: sent info\n");
328 network_send(gnet->connection[0].fd,gd3_l->status,c*sizeof(unsigned char));
329 network_receive(gnet->connection[0].fd,&data,sizeof(unsigned char));
330 printf("debug: sent ac\n");
332 network_send(gnet->connection[0].fd,(unsigned char *)gd3_l->extra,
334 network_receive(gnet->connection[0].fd,&data,sizeof(unsigned char));
335 printf("debug: sent cc\n");
337 network_send(gnet->connection[0].fd,(unsigned char *)gi,sizeof(int));
338 network_receive(gnet->connection[0].fd,&data,sizeof(unsigned char));
339 printf("debug: sent steps\n");
342 printf("save test file\n");
343 fd=open("./test.save",O_WRONLY|O_CREAT);
344 write(fd,gd3_l,sizeof(d3_lattice));
345 write(fd,gmy_info,sizeof(info));
346 write(fd,gd3_l->status,c*sizeof(unsigned char));
347 write(fd,gd3_l->extra,c*sizeof(int));
358 int main(int argc,char **argv)
365 unsigned char data[256];
371 strcpy(server_ip,"137.250.82.105");
372 strcpy(p_file,IMP_PROFILE);
373 strcpy(n_e_file,NEL_PROFILE);
377 /* parse/check argv */
378 for(i=1;i<argc;i++) {
379 if(argv[i][0]=='-') {
385 strncpy(server_ip,argv[++i],16);
388 strcpy(r_file,argv[++i]);
391 strcpy(p_file,argv[++i]);
394 strcpy(n_e_file,argv[++i]);
397 port=atoi(argv[++i]);
405 if(!strcmp(server_ip,"")) {
411 event_init(&event,1);
412 event_set_timeout(&event,0,0);
414 /* connect to server */
415 network_init(&net,1);
416 network_set_connection_info(&net,0,server_ip,port);
417 if(network_connect(&net,0)==N_E_CONNECT) {
418 printf("unable to connect to server, aborting ...\n");
421 network_select(&net,0);
423 /* tell server: i am a client, i may work for you */
424 data[0]=NLSOP_CLIENT;
425 network_send(net.connection[0].fd,data,1);
428 event_math(net.connection[0].fd,&event,READ,ADD);
429 printf("idle, waiting for jobs ...\n");
430 event_start(&event,NULL,get_data_and_calc,nop);
435 int nop(t_event *event,void *allineed) {
437 printf("\ni did a nop :)\n");
442 int get_data_and_calc(t_event *event,void *allineed) {
467 printf("got a new job ...\n");
469 /* get info (+data) */
470 network_receive(net->connection[0].fd,&data,sizeof(unsigned char));
471 if(data==NLSOP_NJOB || data==NLSOP_CJOB) {
472 network_receive(net->connection[0].fd,(unsigned char *)&d3_l,
474 network_receive(net->connection[0].fd,(unsigned char *)&my_info,
477 j=d3_l.max_x*d3_l.max_y*d3_l.max_z;
478 d3_l.status=(unsigned char *)malloc(j*sizeof(unsigned char));
479 if(d3_l.status==NULL) {
480 printf("status alloc failed\n");
483 d3_l.extra=(int *)malloc(j*sizeof(int));
484 if(d3_l.extra==NULL) {
485 printf("extra malloc failed\n");
488 if(data==NLSOP_CJOB) {
490 network_receive(net->connection[0].fd,d3_l.status,
491 j*sizeof(unsigned char));
492 network_send(net->connection[0].fd,&data,sizeof(unsigned char));
493 network_receive(net->connection[0].fd,(unsigned char *)d3_l.extra,
495 network_send(net->connection[0].fd,&data,sizeof(unsigned char));
496 network_receive(net->connection[0].fd,(unsigned char *)&c_step,
498 network_send(net->connection[0].fd,&data,sizeof(unsigned char));
502 printf("unknown instruction, restarting ...\n");
506 printf("starting simulation with following parameters:\n");
507 printf("b = %f | c = %f | s = %f\n",my_info.b,my_info.c,my_info.s);
508 printf("diff every %d steps | diff rate = %f\n",my_info.diff_rate,
510 printf("current step: %d | total steps: %d\n",c_step,my_info.steps);
513 /* care for signals */
515 signal(SIGTERM,send_data);
518 if(!strcmp(r_file,"")) rand_init(NULL);
519 else rand_init(r_file);
521 /* compute graphs for random number rejection method */
522 if((c_profile=(u32 *)malloc(d3_l.max_z*sizeof(unsigned int)))==NULL) {
523 puts("failed allocating memory for carbon profile graph");
526 if((n_e_loss=(u32 *)malloc(d3_l.max_z*sizeof(unsigned int)))==NULL) {
527 puts("failed allocating memory for nuclear energy loss graph");
530 ip_max=get_reject_graph(&my_info,&d3_l,p_file,c_profile);
531 ne_max=get_reject_graph(&my_info,&d3_l,n_e_file,n_e_loss);
533 /* array nel_z[] keeping nuclear energy loss values scaled to URAND_MAX */
534 nel_z=(u32 *)malloc(d3_l.max_z*sizeof(unsigned int));
536 printf("failed allocating nel_z array mem\n");
539 for(i=0;i<d3_l.max_z;i++) nel_z[i]=URAND_MAX*(1.0*n_e_loss[i]/ne_max);
541 /* this should be obsolete - z is high enough - we check now! */
542 if(c_profile[d3_l.max_z-1]!=0) {
543 printf("max_z (%d) too small - sputtering not possible\n",d3_l.max_z);
547 /* sputtering really possible ?*/
548 if(n_e_loss[d3_l.max_z-1]!=0)
549 printf("warning: max_z (%d) too small, there may be amorphous volumes\n",
553 * start of simulation
557 while(i<my_info.steps) {
558 for(j=0;j<my_info.cpi;j++) {
559 x_c=get_rand(d3_l.max_x);
560 y_c=get_rand(d3_l.max_y);
561 // z_c=get_rand_reject(d3_l.max_z,ne_max,n_e_loss);
562 z_c=get_rand(d3_l.max_z);
563 process_cell(&d3_l,x_c,y_c,z_c,&my_info,nel_z[z_c]);
565 distrib_c(&d3_l,&my_info,i,ip_max,c_profile);
567 if(i%my_info.save_rate==0) {
572 if(i%my_info.s_rate==0) sputter(&d3_l);
580 /* shutdown/free/close everything now ... */