+ x=d3_l.max_x/2-1;
+ y=d3_l.max_y/2-1;
+ z=d3_l.max_z/2-1;
+
+#ifdef NODFB
+ if(!strcmp(s_file,""))
+ {
+ puts("NODFB defined, run with -S option");
+ return -1;
+ }
+#endif
+
+ if(!strcmp(r_file,"")) rand_init(NULL);
+ else rand_init(r_file);
+
+ if(!strcmp(l_file,""))
+ {
+ i=d3_l.max_x*d3_l.max_y*d3_l.max_z;
+#ifdef USE_DFB_API
+ d3_lattice_init(&argc,argv,&d3_l);
+#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
+ {
+ load_from_file(l_file,&d3_l,&my_info);
+ 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
+ else d3_lattice_init(&argc,argv,&d3_l);
+#endif
+ }
+
+#ifdef USE_DFB_API
+ d3_event_init(&d3_l);
+#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,"pressure range: %d",my_info.range);
+ sprintf(stress_txt,"stress term: %f",my_info.a_ap);
+ sprintf(ballistic_txt,"ballistic term: %f",my_info.b_ap);
+ sprintf(carbon_txt,"carbon term: %f",my_info.a_cp);
+ sprintf(dr_ac_txt,"a/c diffusion rate: %f",my_info.dr_ac);
+ if(my_info.c_diff!=0) sprintf(dr_cc_txt,"c/c diffusion rate: %f",my_info.dr_cc);
+ else sprintf(dr_cc_txt,"c/c diffusion rate: none");
+ sprintf(zdiff_txt,"diffusion in z direction: %c",my_info.z_diff?'y':'n');
+ 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(csat_txt,"carbon saturation: %d",my_info.c_sat);
+ 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]=r_txt;
+ arg_v[15]=ballistic_txt;
+ arg_v[16]=carbon_txt;
+ arg_v[17]=stress_txt;
+ arg_v[18]=dr_ac_txt;
+ arg_v[19]=dr_cc_txt;
+ arg_v[20]=hpi_txt;
+ arg_v[21]=csat_txt;
+ arg_v[22]=NULL;
+ arg_v[23]=NULL;
+ arg_v[24]=NULL;
+ arg_v[25]=NULL;
+#endif
+
+ /* compute graphs for random number rejection method */
+ 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);
+
+ if((!strcmp(l_file,""))||(c_step))
+ {
+ /* calculate ratio of c_simwindow / c_total */
+ if(get_c_ratio(&c_ratio,p_file,&my_info,&d3_l)!=1)
+ {
+ puts("failed calculating ratio");
+ return -1;
+ }
+
+#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
+ if(i==10000000) return 1;
+ i++;
+ }
+#endif
+
+ i=(c_step?c_step:0);
+ while((i<my_info.steps) && (quit==0) && (escape==0))
+ {
+ 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,c_ratio,ip_max,c_profile);
+#ifdef USE_DFB_API
+ if(i%refresh==0)
+ {
+ sprintf(xyz_txt,"x: %d y: %d z: %d",x+1,y+1,z+1);
+ 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,"conc: %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);
+ 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(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 */
+ 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;
+ }
+ /* calc values */
+ calc_pressure(&d3_l,my_info.range);
+ max_extra=calc_max_extra(&d3_l);
+
+ 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",x+1,y+1,z+1);
+ 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,"conc: %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);
+ }
+
+ d3_lattice_release(&d3_l);
+#endif
+