Bugfix in search_for_outside_edge routine.
[voro++.git] / branches / exact / examples / no_release / voro_lf.cc
blob5951328ab4dfb0a356569c7c46c8794e3a2c5a08
1 // File import example code
2 //
3 // Author : Chris H. Rycroft (LBL / UC Berkeley)
4 // Email : chr@alum.mit.edu
5 // Date : August 30th 2011
7 #include "voro++.hh"
8 using namespace voro;
10 // Set up constants for the container geometry
11 const double ax=-0.5,bx=25.5;
12 const double ay=-0.5,by=25.5;
13 const double az=-0.5,bz=25.5;
15 int main() {
17 // Manually import the file
18 int i,j,id,max_id=0,n;
19 double x,y,z;
20 vector<int> vid,neigh,f_order;
21 vector<double> vx,vy,vz,vd;
22 FILE *fp=safe_fopen("liq-900K.dat","r"),*fp2,*fp3;
23 while((j=fscanf(fp,"%d %lg %lg %lg",&id,&x,&y,&z))==4) {
24 vid.push_back(id);if(id>max_id) max_id=id;
25 vx.push_back(x);
26 vy.push_back(y);
27 vz.push_back(z);
29 if(j!=EOF) voro_fatal_error("File import error",VOROPP_FILE_ERROR);
30 n=vid.size();
31 fclose(fp);
33 // Compute optimal size for container, and then construct the container
34 double dx=bx-ax,dy=by-ay,dz=bz-az;
35 double l(pow(n/(5.6*dx*dy*dz),1/3.0));
36 int nx=int(dx*l+1),ny=int(dy*l+1),nz=int(dz*l+1);
37 container con(ax,bx,ay,by,az,bz,nx,ny,nz,false,false,false,8);
39 // Print status message
40 printf("Read %d particles, max ID is %d\n"
41 "Container grid is %d by %d by %d\n",n,max_id,nx,ny,nz);
43 // Import the particles, and create ID lookup tables
44 double *xi=new double[max_id+1],*yi=new double[max_id+1],*zi=new double[max_id+1];
45 for(j=0;j<n;j++) {
46 id=vid[j];x=vx[j];y=vy[j];z=vz[j];
47 con.put(id,x,y,z);
48 xi[id]=x;
49 yi[id]=y;
50 zi[id]=z;
53 // Open three output files for statistics and gnuplot cells
54 fp=safe_fopen("liq-900K.out","w");
55 fp2=safe_fopen("liq-900K.gnu","w");
56 fp3=safe_fopen("liq-900K-orig.gnu","w");
58 // Loop over all particles and compute their Voronoi cells
59 voronoicell_neighbor c,c2;
60 c_loop_all cl(con);
61 if(cl.start()) do if(con.compute_cell(c,cl)) {
63 // Get particle position, ID, and neighbor vector
64 cl.pos(x,y,z);
65 id=cl.pid();
66 c.neighbors(neigh);
68 // Get face areas et total surface of faces
69 c.face_areas(vd);c.surface_area();
70 c.draw_gnuplot(x,y,z,fp3);
72 // Initialize second cell
73 c2.init(ax-x,bx-x,ay-y,by-y,az-z,bz-z);
75 // Add condition on surface: >1% total surface. In addition,
76 // skip negative indices, since they correspond to faces
77 // against the container boundaries
78 for(i=0;i<(signed int) vd.size();i++)
79 if(vd[i]>0.01*c.surface_area()&&neigh[i]>=0) {
80 j=neigh[i];
81 c2.nplane(xi[j]-x,yi[j]-y,zi[j]-z,j);
84 // Get information of c2 cell
85 c2.face_areas(vd);c2.face_orders(f_order);
87 // Output information to file
88 i=vd.size();
89 fprintf(fp,"%d %d",id,i);
90 for(j=0;j<i;j++) fprintf(fp," %d",f_order[j]);
91 for(j=0;j<i;j++) fprintf(fp," %.3f",vd[j]);
92 fprintf(fp," %.3f %.3f %.3f\n",x,y,z);
94 c2.draw_gnuplot(x,y,z,fp2);
95 } while (cl.inc());
97 // Close files
98 fclose(fp);
99 fclose(fp2);
101 // Delete dynamically allocated arrays
102 delete [] xi;
103 delete [] yi;
104 delete [] zi;