Minkowski test code.
[voro++.git] / trunk / src / common.cc
blob7647ffd2967ca16a6f69772daa245e7287cab05e
1 // Voro++, a 3D cell-based Voronoi library
2 //
3 // Author : Chris H. Rycroft (LBL / UC Berkeley)
4 // Email : chr@alum.mit.edu
5 // Date : August 30th 2011
7 /** \file common.cc
8 * \brief Implementations of the small helper functions. */
10 #include "common.hh"
12 namespace voro {
14 void check_duplicate(int n,double x,double y,double z,int id,double *qp) {
15 double dx=*qp-x,dy=qp[1]-y,dz=qp[2]-z;
16 if(dx*dx+dy*dy+dz*dz<1e-10) {
17 printf("Duplicate: %d (%g,%g,%g) matches %d (%g,%g,%g)\n",n,x,y,z,id,*qp,qp[1],qp[2]);
18 exit(1);
22 /** \brief Function for printing fatal error messages and exiting.
24 * Function for printing fatal error messages and exiting.
25 * \param[in] p a pointer to the message to print.
26 * \param[in] status the status code to return with. */
27 void voro_fatal_error(const char *p,int status) {
28 fprintf(stderr,"voro++: %s\n",p);
29 exit(status);
32 /** \brief Prints a vector of positions.
34 * Prints a vector of positions as bracketed triplets.
35 * \param[in] v the vector to print.
36 * \param[in] fp the file stream to print to. */
37 void voro_print_positions(std::vector<double> &v,FILE *fp) {
38 if(v.size()>0) {
39 fprintf(fp,"(%g,%g,%g)",v[0],v[1],v[2]);
40 for(int k=3;(unsigned int) k<v.size();k+=3) {
41 fprintf(fp," (%g,%g,%g)",v[k],v[k+1],v[k+2]);
46 /** \brief Opens a file and checks the operation was successful.
48 * Opens a file, and checks the return value to ensure that the operation
49 * was successful.
50 * \param[in] filename the file to open.
51 * \param[in] mode the cstdio fopen mode to use.
52 * \return The file handle. */
53 FILE* safe_fopen(const char *filename,const char *mode) {
54 FILE *fp=fopen(filename,mode);
55 if(fp==NULL) {
56 fprintf(stderr,"voro++: Unable to open file '%s'\n",filename);
57 exit(VOROPP_FILE_ERROR);
59 return fp;
62 /** \brief Prints a vector of integers.
64 * Prints a vector of integers.
65 * \param[in] v the vector to print.
66 * \param[in] fp the file stream to print to. */
67 void voro_print_vector(std::vector<int> &v,FILE *fp) {
68 int k=0,s=v.size();
69 while(k+4<s) {
70 fprintf(fp,"%d %d %d %d ",v[k],v[k+1],v[k+2],v[k+3]);
71 k+=4;
73 if(k+3<=s) {
74 if(k+4==s) fprintf(fp,"%d %d %d %d",v[k],v[k+1],v[k+2],v[k+3]);
75 else fprintf(fp,"%d %d %d",v[k],v[k+1],v[k+2]);
76 } else {
77 if(k+2==s) fprintf(fp,"%d %d",v[k],v[k+1]);
78 else fprintf(fp,"%d",v[k]);
82 /** \brief Prints a vector of doubles.
84 * Prints a vector of doubles.
85 * \param[in] v the vector to print.
86 * \param[in] fp the file stream to print to. */
87 void voro_print_vector(std::vector<double> &v,FILE *fp) {
88 int k=0,s=v.size();
89 while(k+4<s) {
90 fprintf(fp,"%g %g %g %g ",v[k],v[k+1],v[k+2],v[k+3]);
91 k+=4;
93 if(k+3<=s) {
94 if(k+4==s) fprintf(fp,"%g %g %g %g",v[k],v[k+1],v[k+2],v[k+3]);
95 else fprintf(fp,"%g %g %g",v[k],v[k+1],v[k+2]);
96 } else {
97 if(k+2==s) fprintf(fp,"%g %g",v[k],v[k+1]);
98 else fprintf(fp,"%g",v[k]);
102 /** \brief Prints a vector a face vertex information.
104 * Prints a vector of face vertex information. A value is read, which
105 * corresponds to the number of vertices in the next face. The routine reads
106 * this number of values and prints them as a bracked list. This is repeated
107 * until the end of the vector is reached.
108 * \param[in] v the vector to interpret and print.
109 * \param[in] fp the file stream to print to. */
110 void voro_print_face_vertices(std::vector<int> &v,FILE *fp) {
111 int j,k=0,l;
112 if(v.size()>0) {
113 l=v[k++];
114 if(l<=1) {
115 if(l==1) fprintf(fp,"(%d)",v[k++]);
116 else fputs("()",fp);
117 } else {
118 j=k+l;
119 fprintf(fp,"(%d",v[k++]);
120 while(k<j) fprintf(fp,",%d",v[k++]);
121 fputs(")",fp);
123 while((unsigned int) k<v.size()) {
124 l=v[k++];
125 if(l<=1) {
126 if(l==1) fprintf(fp," (%d)",v[k++]);
127 else fputs(" ()",fp);
128 } else {
129 j=k+l;
130 fprintf(fp," (%d",v[k++]);
131 while(k<j) fprintf(fp,",%d",v[k++]);
132 fputs(")",fp);