+ puts("cannot open save file");
+ return -1;
+ }
+ if(write(sf_fd,d3_l,sizeof(d3_lattice))<sizeof(d3_lattice))
+ {
+ puts("failed saving d3 lattice struct");
+ return -1;
+ }
+ if(write(sf_fd,my_inf,sizeof(info))<sizeof(info))
+ {
+ puts("failed saving info struct");
+ return-1;
+ }
+ c=d3_l->max_x*d3_l->max_y*d3_l->max_z;
+ if(write(sf_fd,d3_l->status,c*sizeof(unsigned char))<c*sizeof(unsigned char))
+ {
+ puts("failed saving status of d3 lattice sites");
+ return -1;
+ }
+ if(write(sf_fd,d3_l->extra,c*sizeof(int))<c*sizeof(int))
+ {
+ puts("failed saving sites concentration");
+ return -1;
+ }
+ close(sf_fd);
+
+ return 1;
+}
+
+int load_from_file(char *lf,d3_lattice *d3_l,info *my_inf)
+{
+ int lf_fd,c;
+
+ if((lf_fd=open(lf,O_RDONLY))<0)
+ {
+ puts("cannot open load file");
+ return -1;
+ }
+ if(read(lf_fd,d3_l,sizeof(d3_lattice))<sizeof(d3_lattice))
+ {
+ puts("failed reading d3 lattice struct");
+ return -1;
+ }
+ if(read(lf_fd,my_inf,sizeof(info))<sizeof(info))
+ {
+ puts("failed reading info struct");
+ return-1;
+ }
+ c=d3_l->max_x*d3_l->max_y*d3_l->max_z;
+ if((d3_l->status=(unsigned char*)malloc(c*sizeof(unsigned char)))==NULL)
+ {
+ puts("cannot allocate status buffer");
+ return -1;
+ }
+ if((d3_l->extra=(int *)malloc(c*sizeof(int)))==NULL)
+ {
+ puts("cannot allocate concentration buffer");
+ return -1;
+ }
+ if(read(lf_fd,d3_l->status,c*sizeof(unsigned char))<c*sizeof(unsigned char))
+ {
+ puts("failed reading status of d3 lattice sites");
+ return -1;
+ }
+ if(read(lf_fd,d3_l->extra,c*sizeof(int))<c*sizeof(int))
+ {
+ puts("failed reading sites concentration");
+ return -1;
+ }
+ close(lf_fd);
+
+ return 1;
+}
+
+int convert_file(char *cf,d3_lattice *d3_l)
+{
+ int x,y,z;
+ int c_fd;
+
+ if((c_fd=open(cf,O_WRONLY|O_CREAT))<0)
+ {
+ puts("cannot open convert file");
+ return -1;
+ }
+ dprintf(c_fd,"# created by nlsop (gnuplot format)\n");
+ for(x=0;x<d3_l->max_x;x++)
+ {
+ for(y=0;y<d3_l->max_y;y++)
+ {
+ for(z=0;z<d3_l->max_z;z++)
+ {
+ 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);
+ }
+ }
+ }
+ close(c_fd);
+
+ return 1;
+}
+
+int main(int argc,char **argv)
+{
+ u32 x,y,z,x_c,y_c,z_c;
+ int i,quit,escape,switchmode,nowait;
+ int refresh,resave;
+ char s_file[MAX_CHARS];
+ char s_file_tmp[MAX_CHARS];
+ char l_file[MAX_CHARS];
+ char c_file[MAX_CHARS];
+ char p_file[MAX_CHARS];
+ char convert;
+ char r_file[MAX_CHARS];
+#ifdef USE_DFB_API
+ char xyz_txt[MAX_TXT];
+ char status_txt[MAX_TXT];
+ char conc_txt[MAX_TXT];
+ char steps_txt[MAX_TXT];
+ char cc_txt[MAX_TXT];
+ char a_txt[MAX_TXT];
+ char s_txt[MAX_TXT];
+ char ap_txt[MAX_TXT];
+ char el_txt[MAX_TXT];
+ char cd_txt[MAX_TXT];
+ char r_txt[MAX_TXT];
+ char cp_txt[MAX_TXT];
+ char zdiff_txt[MAX_TXT];
+ char diff_txt[MAX_TXT];
+ char dr_ac_txt[MAX_TXT];
+ char dr_cc_txt[MAX_TXT];
+ char mode_txt[MAX_TXT];
+ char *arg_v[MAX_ARGV];
+#endif
+ d3_lattice d3_l;
+ info my_info;
+ unsigned char mode;
+
+ d3_l.max_x=X;
+ d3_l.max_y=Y;
+ d3_l.max_z=Z;
+ my_info.steps=STEPS;
+ my_info.range=RANGE;
+ refresh=REFRESH;
+ resave=RESAVE;
+ my_info.z_diff=0;
+ my_info.a_el=A_EL;
+ my_info.b_el=B_EL;
+ my_info.a_cd=A_CD;
+ my_info.b_cd=B_CD;
+ my_info.a_ap=A_AP;
+ my_info.b_ap=B_AP;
+ my_info.a_cp=A_CP;
+ my_info.cc=CC;
+ my_info.dr_ac=DR_AC;
+ my_info.dr_cc=DR_CC;
+ my_info.diff_rate=DIFF_RATE;
+ nowait=0;
+ quit=0;
+ escape=0;
+ switchmode=0;
+ strcpy(s_file,"");
+ strcpy(l_file,"");
+ strcpy(c_file,"");
+ strcpy(p_file,IMP_PROFILE);
+ convert=0;
+ strcpy(r_file,"");
+ mode=0;
+
+ for(i=1;i<argc;i++)
+ {
+ if(argv[i][0]=='-')
+ {
+ switch(argv[i][1])
+ {
+ case 'h':
+ usage();
+ return -1;
+ case 'n':
+ nowait=1;
+ break;
+ case 'a':
+ my_info.a_el=atof(argv[++i]);
+ break;
+ case 'b':
+ my_info.b_el=atof(argv[++i]);
+ break;
+ case 'x':
+ d3_l.max_x=atoi(argv[++i]);
+ break;
+ case 'y':
+ d3_l.max_y=atoi(argv[++i]);
+ break;
+ case 'z':
+ d3_l.max_z=atoi(argv[++i]);
+ break;
+ case 'Z':
+ my_info.z_diff=1;
+ break;
+ case 's':
+ my_info.steps=atoi(argv[++i]);
+ break;
+ case 'd':
+ refresh=atoi(argv[++i]);
+ break;
+ case 'r':
+ my_info.range=atoi(argv[++i]);
+ break;
+ case 'f':
+ my_info.a_ap=atof(argv[++i]);
+ break;
+ case 'p':
+ my_info.b_ap=atof(argv[++i]);
+ break;
+ case 'F':
+ my_info.a_cp=atof(argv[++i]);
+ break;
+ case 'A':
+ my_info.a_cd=atof(argv[++i]);
+ break;
+ case 'B':
+ my_info.b_cd=atof(argv[++i]);
+ break;
+ case 'W':
+ resave=atoi(argv[++i]);
+ break;
+ case 'C':
+ strcpy(l_file,argv[++i]);
+ if(i<argc-1) if(argv[i+1][0]!='-') strcpy(c_file,argv[++i]);
+ convert=1;
+ break;
+ case 'D':
+ my_info.dr_ac=atof(argv[++i]);
+ break;
+ case 'c':
+ my_info.dr_cc=atof(argv[++i]);
+ break;
+ case 'e':
+ my_info.diff_rate=atoi(argv[++i]);
+ break;
+ case 'L':
+ strcpy(l_file,argv[++i]);
+ break;
+ case 'S':
+ strcpy(s_file,argv[++i]);
+ break;
+ case 'R':
+ strcpy(r_file,argv[++i]);
+ break;
+ case 'P':
+ strcpy(p_file,argv[++i]);
+ break;
+ default:
+ usage();
+ return -1;
+ }
+ } else usage();
+ }
+
+ 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