+ printf("random api init ...");
+ if(!strcmp(r_file,"")) rand_init(NULL);
+ else rand_init(r_file);
+ printf(" done\n");
+
+ if(!strcmp(l_file,""))
+ {
+ i=d3_l.max_x*d3_l.max_y*d3_l.max_z;
+#ifdef USE_DFB_API
+ printf("d3 lattice init ...");
+ d3_lattice_init(&argc,argv,&d3_l);
+ printf(" done\n");
+#endif
+ if((d3_l.status=(unsigned char *)malloc(i*sizeof(unsigned char)))==NULL)
+ {
+ puts("failed allocating status buffer");
+ return -1;
+ }
+ memset(d3_l.status,0,i*sizeof(unsigned char));
+ if((d3_l.extra=(int *)malloc(i*sizeof(int)))==NULL)
+ {
+ puts("failed allocating concentration buffer");
+ return -1;
+ }
+ memset(d3_l.extra,0,i*sizeof(int));
+ } else
+ {
+ printf("loading file ...");
+ load_from_file(l_file,&d3_l,&my_info);
+ printf(" done\n");
+ if(convert)
+ {
+ if(!strcmp(c_file,"")) sprintf(c_file,"%s_gnuplot",l_file);
+ printf("converting file %s to %s\n",l_file,c_file);
+ convert_file(c_file,&d3_l);
+ puts("done");
+ return 1;
+ }
+#ifdef USE_DFB_API
+ printf("d3 lattice init ...");
+ d3_lattice_init(&argc,argv,&d3_l);
+ printf(" done\n");
+#endif
+ }
+
+#ifdef USE_DFB_API
+ printf("event init ...");
+ d3_event_init(&d3_l);
+ printf(" done\n");
+#endif
+
+#ifdef USE_DFB_API
+ strcpy(a_txt,"args:");
+ sprintf(s_txt,"steps: %d",my_info.steps);
+ 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));
+ sprintf(r_txt,"stress influence range: %d",my_info.range);
+ sprintf(stress_txt,"stress term: %f",my_info.s);
+ sprintf(ballistic_txt,"ballistic term: %f",my_info.b);
+ sprintf(carbon_txt,"carbon term: %f",my_info.c);
+ sprintf(dr_ac_txt,"a/c diffusion rate: %f",my_info.dr_ac);
+ sprintf(zdiff_txt,"diffusion in z direction: yes");
+ sprintf(diff_txt,"diffusion every %d steps",my_info.diff_rate);
+ strcpy(mode_txt,"view: a/c mode");
+ sprintf(hpi_txt,"hits per ion: %d",my_info.cpi);
+ sprintf(srate_txt,"sputter rate (3nm/#c): %d",my_info.s_rate);
+ get_amorphous_layer_info(&d3_l,&sai,&sacl,&eacl);
+ sprintf(start_ai_txt,"start of a-inclusions: %d nm (%d)",3*sai,sai);
+ sprintf(start_acl_txt,"start of a-layer: %d nm (%d)",3*sacl,sacl);
+ sprintf(end_acl_txt,"end of a-layer: %d nm (%d)",3*eacl,eacl);
+ arg_v[1]=mode_txt;
+ arg_v[2]=xyz_txt;
+ arg_v[3]=status_txt;
+ arg_v[4]=conc_txt;
+ arg_v[5]=NULL;
+ arg_v[6]=steps_txt;
+ arg_v[7]=cc_txt;
+ arg_v[8]=NULL;
+ arg_v[9]=a_txt;
+ arg_v[10]=s_txt;
+ arg_v[11]=dose_txt;
+ arg_v[12]=diff_txt;
+ arg_v[13]=zdiff_txt;
+ arg_v[14]=dr_ac_txt;
+ arg_v[15]=ballistic_txt;
+ arg_v[16]=carbon_txt;
+ arg_v[17]=stress_txt;
+ arg_v[18]=r_txt;
+ arg_v[19]=hpi_txt;
+ arg_v[20]=srate_txt;
+ arg_v[21]=NULL;
+ arg_v[22]=start_ai_txt;
+ arg_v[23]=start_acl_txt;
+ arg_v[24]=end_acl_txt;
+ arg_v[25]=NULL;
+#endif
+
+ /* compute graphs for random number rejection method */
+ printf("random rejection graphs ...");
+ if((c_profile=(u32 *)malloc(d3_l.max_z*sizeof(unsigned int)))==NULL)
+ {
+ puts("failed allocating memory for carbon profile graph");
+ return -1;
+ }
+ if((n_e_loss=(u32 *)malloc(d3_l.max_z*sizeof(unsigned int)))==NULL)
+ {
+ puts("failed allocating memory for nuclear energy loss graph");
+ return -1;
+ }
+ ip_max=get_reject_graph(&my_info,&d3_l,p_file,c_profile);
+ ne_max=get_reject_graph(&my_info,&d3_l,n_e_file,n_e_loss);
+ printf(" done\n");
+
+ if((!strcmp(l_file,""))||(c_step))
+ {
+
+ /* this should be obsolete - z is high enough - we check now! */
+ if(c_profile[d3_l.max_z-1]!=0)
+ {
+ printf("max_z (%d) too small - sputtering not possible\n",d3_l.max_z);
+ do_sputter=0;
+ }
+ /* calculate ratio of c_simwindow / c_total */
+ if(!do_sputter)
+ {
+ get_c_ratio(&c_ratio,p_file,&my_info,&d3_l);
+ printf("calculated c ratio: %f\n",c_ratio);
+ }
+
+ /* sputtering realy possible ?*/
+ if(n_e_loss[d3_l.max_z-1]!=0)
+ printf("warning: max_z (%d) too small - there may be amorphous volumes\n",d3_l.max_z);
+
+#ifdef DEBUG_RAND
+ i=0;
+ while(1)
+ {
+#ifdef DEBUG_CP
+ printf("%d\n",get_rand_reject(d3_l.max_z,ip_max,c_profile));
+#endif
+#ifdef DEBUG_NEL
+ printf("%d\n",get_rand_reject(d3_l.max_z,ne_max,n_e_loss));
+#endif
+#ifdef DEBUG_NORM
+ printf("%d\n",get_rand(d3_l.max_z));
+#endif
+#ifdef DEBUG_LINEAR
+ printf("%d\n",get_rand_lgp(d3_l.max_z,1,0));
+#endif
+ if(i==10000000) return 1;
+ i++;
+ }
+#endif
+
+#ifdef MORE_PRINTF
+ printf("starting simulation ... now! :)\n");
+#endif
+
+ i=(c_step?c_step:0);
+ while((i<my_info.steps) && (quit==0) && (escape==0))
+ {
+#ifdef MORE_PRINTF
+ if(i%refresh==0) printf("step: %d\n",i);
+#endif
+ for(j=0;j<my_info.cpi;j++)
+ {
+ x_c=get_rand(d3_l.max_x);
+ y_c=get_rand(d3_l.max_y);
+ // z_c=get_rand_reject(d3_l.max_z,ne_max,n_e_loss);
+ z_c=get_rand(d3_l.max_z);
+ nel_z=URAND_MAX*(1.0*n_e_loss[z_c]/ne_max);
+ process_cell(&d3_l,x_c,y_c,z_c,&my_info,nel_z);
+ }
+ distrib_c(&d3_l,&my_info,i,ip_max,c_profile);
+#ifdef USE_DFB_API
+ if(i%refresh==0)
+ {
+ sprintf(xyz_txt,"x: %d y: %d z: %d (%d nm)",x+1,y+1,z+1,z*3);
+ 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');
+ sprintf(conc_txt,"carbon: %d",*(d3_l.extra+x+y*d3_l.max_x+z*d3_l.max_x*d3_l.max_y));
+ sprintf(steps_txt,"step: %d",i);
+ sprintf(cc_txt,"total c: %d",my_info.cc);
+ get_amorphous_layer_info(&d3_l,&sai,&sacl,&eacl);
+ sprintf(start_ai_txt,"start of a-inclusions: %d nm (%d)",3*sai,sai);
+ sprintf(start_acl_txt,"start of a-layer: %d nm (%d)",3*sacl,sacl);
+ sprintf(end_acl_txt,"end of a-layer: %d nm (%d)",3*eacl,eacl);
+ d3_lattice_draw(&d3_l,x,y,z,25,arg_v,mode,0,NULL,0,NULL,0);
+ }
+#endif
+ if(i%resave==0 && strcmp(s_file,"") && resave!=0 && i!=0)
+ {
+ sprintf(s_file_tmp,"%s_%d_of_%d",s_file,i,my_info.steps);
+ save_to_file(s_file_tmp,&d3_l,&my_info);
+#ifdef NODFB
+ printf("saved %s\n",s_file_tmp);
+#endif
+ }
+ i++;
+ if((do_sputter)&(i%my_info.s_rate==0)) sputter(&d3_l);
+ }
+ }
+
+ if(strcmp(s_file,""))
+ {
+ printf("saved %s\n",s_file);
+ save_to_file(s_file,&d3_l,&my_info);
+ }
+
+#ifdef USE_DFB_API
+ /* allocating buffer for pressure values */
+ printf("allocating buffer for preassure values ...");
+ if((d3_l.v_ptr=malloc(d3_l.max_x*d3_l.max_y*d3_l.max_z*sizeof(unsigned char)))==NULL)
+ {
+ puts("cannot allocate buffer for pressure values");
+ return -1;
+ }
+ printf(" done\n");
+ /* calc values */
+ printf("calculating preassure values ...");
+ calc_pressure(&d3_l,my_info.range);
+ printf(" done\n");
+ printf("finding maximum carbon concentration ...");
+ max_extra=calc_max_extra(&d3_l);
+ printf(" done\n");
+
+ while((quit==0) && (escape==0) && (nowait==0))
+ {
+ /* bahh! */
+ if(switchmode==0) mode=0;
+ if(switchmode==1) mode=1;
+ if(switchmode==2) mode=2;
+ if(switchmode==3) mode=3;
+ /* end of bahh! */
+ sprintf(xyz_txt,"x: %d y: %d z: %d (%d nm)",x+1,y+1,z+1,z*3);
+ 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');
+ sprintf(conc_txt,"carbon: %d",*(d3_l.extra+x+y*d3_l.max_x+z*d3_l.max_x*d3_l.max_y));
+ sprintf(steps_txt,"step: end");
+ sprintf(cc_txt,"total c: %d",my_info.cc);
+ if(switchmode==0) strcpy(mode_txt,"view: a/c mode");
+ if(switchmode==1) strcpy(mode_txt,"view: c conc mode");
+ if(switchmode==2) strcpy(mode_txt,"view: a pressure mode");
+ if(switchmode==3) strcpy(mode_txt,"view: a/c + profiles mode");
+ d3_lattice_draw(&d3_l,x,y,z,25,arg_v,mode,max_extra,c_profile,ip_max,n_e_loss,ne_max);
+ bmp=0;
+ ac_distr=0;
+ scan_event(&d3_l,&x,&y,&z,&quit,&escape,&switchmode,&bmp,&ac_distr);
+ if(bmp) write_bmp(&d3_l,bmp,x,y,z,max_extra);
+ if(ac_distr) write_ac_distr(&d3_l,ac_distr);
+ }