+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 get_c_ratio(double *c_ratio,char *pfile,info *my_info,d3_lattice *d3_l)
+{
+ double all,a,b,d;
+ int i,k;
+ int p_fd;
+ unsigned char buf[32];
+ char *p;
+ unsigned char c;
+
+ if((p_fd=open(pfile,O_RDONLY))<0)
+ {
+ puts("cannot open profile file");
+ return -1;
+ }
+ k=1;
+ d=0;
+ all=0;
+ while(k)
+ {
+ for(i=0;i<32;i++)
+ {
+ k=read(p_fd,&c,1);
+ buf[i]=c;
+ if(c=='\n') break;
+ }
+ if(k)
+ {
+ p=strtok(buf," ");
+ a=atof(p)/10; /* nm */
+ p=strtok(NULL," ");
+ b=atof(p);
+ if(a<=d3_l->max_z*CELL_LENGTH) d+=b;
+ all+=b;
+ }
+ }
+ *c_ratio=d/all;
+ close(p_fd);
+
+ return 1;
+}
+
+u32 get_reject_graph(info *my_info,d3_lattice *d3_l,char *file,u32 *graph) {
+ double a,b;
+ int i,j,k;
+ int fd;
+ char buf[32],*p;
+ unsigned char *flag;
+ u32 max;
+
+ max=0;
+ if((fd=open(file,O_RDONLY))<0)
+ {
+ puts("cannot open file to calculate rejection graph");
+ return -1;
+ }
+ if((flag=(unsigned char *)malloc(d3_l->max_z))==NULL)
+ {
+ puts("cannot malloc flag memory for rejection graph");
+ return -1;
+ }
+ memset(flag,0,d3_l->max_z);
+ memset(graph,0,d3_l->max_z*sizeof(u32));
+ /* get fixpoints */
+ k=1;
+ while(k)
+ {
+ for(i=0;i<32;i++)
+ {
+ k=read(fd,&buf[i],1);
+ if((buf[i]=='\n')||(k==0)) break;
+ }
+ if(k)
+ {
+ p=strtok(buf," ");
+ a=atof(p)/10; /* nm */
+ p=strtok(NULL," ");
+ b=atof(p);
+ if(a>d3_l->max_z*CELL_LENGTH) k=0;
+ else
+ {
+ graph[(int)(a/CELL_LENGTH)]=(int)(URAND_MAX/100*b);
+ flag[(int)(a/CELL_LENGTH)]=1;
+ }
+ }
+ }
+ /* do (linear) interpolation here! */
+ i=0;
+ a=0;
+ while(i<d3_l->max_z)
+ {
+ /* graph[0] is 0! */
+ j=i;
+ i++;
+ while(flag[i]==0&&i<d3_l->max_z) i++;
+ for(k=j+1;k<i;k++) graph[k]=(int)((k-j)*((int)graph[i]-(int)graph[j])/(i-j))+graph[j];
+ if(graph[i]>max) max=graph[i];
+ }
+
+ free(flag);
+
+#ifdef DEBUG_INTERPOL_PROFILE
+ printf("debug: %s (interpolated profile)\n",file);
+ for(i=0;i<d3_l->max_z;i++) printf("%d %d\n",i,graph[i]);
+#endif
+
+ return max;
+}
+
+int get_amorphous_layer_info(d3_lattice *d3_l,int *sai,int *sacl,int *eacl) {
+ int i,j,a,oend,nend,count;
+ unsigned char sacl_is_set=0;
+ unsigned char eacl_is_set=0;
+ unsigned char sai_is_set=0;
+
+ a=d3_l->max_x*d3_l->max_y;
+ nend=a;
+ oend=0;
+
+ for(i=0;i<d3_l->max_z;i++) {
+ count=0;
+ for(j=oend;j<nend;j++) if(*(d3_l->status+j)&AMORPH) count++;
+ oend=nend;
+ nend+=a;
+ if((count>=A_START*a)&&(!sai_is_set)) {
+ *sai=i;
+ sai_is_set=1;
+ }
+ if((count>=AC_START*a)&&(!sacl_is_set)) {
+ *sacl=i;
+ sacl_is_set=1;
+ }
+ if((count<=A_END*a)&&(sacl_is_set)&&(!eacl_is_set)) {
+ *eacl=i;
+ eacl_is_set=1;
+ }
+ if((eacl_is_set)&&(count>=A_END*a)) eacl_is_set=0;
+ }
+ return 1;
+}
+