Diploma Thesis Percolation Simulation C++ Sourcecode Documentation |
00001 // struct_sphere.h 00002 // Andreas Krueger 15.1.2000 00003 // version 2.3 last change: 12.1.2001 00004 // 00005 // #define REAL double // for radius, etc. 00006 // #define NUMBER long // for the number of clusters, etc. 00007 // 00008 // #include <iostream.h> // for cout 00009 // #include "ndim_vector.h" // for the class "myVector" of 2dimensions 00010 // #include "quicksort.h" 00011 00012 class sphere { 00013 public: 00014 myVector c; // pointer to coordinate of center 00015 REAL r; // radius of sphere 00016 NUMBER clno; // is part of the cluster "clno" 00017 NUMBER clsz; // is part of a cluster of the size "clsz"; 00018 public: 00019 sphere(); // default-constructor (dimension=0 !) 00020 sphere(int dimension); // construct sphere in dimension-al space 00021 sphere(const sphere&); // copyconstructor 00022 sphere(myVector&, REAL, NUMBER, NUMBER); // initializer 00023 friend int getdim(const sphere& u); 00024 void unitpointsphere(); // resets to ([0,..,0],0,0,0) 00025 sphere& operator=(const sphere&); 00026 bool operator!=(const sphere& s2); // different spheres? 00027 friend bool overlap (sphere&, sphere&); // do they overlap? 00028 friend bool overlap (sphere&, sphere&, sphere& temp); 00029 friend bool overlap2 (sphere&, sphere&, sphere& temp); 00030 friend ostream& operator<<(ostream&,sphere&); // output operator 00031 REAL distance (sphere&, sphere&); // how far from each other 00032 }; 00033 00034 00035 00036 sphere::sphere(){ 00037 // cout <<"defaultconstruct "<<flush; 00038 } 00039 00040 sphere::sphere(int dimension) { 00041 c.set_dim(dimension); // center-vector; origin-vector! 00042 } 00043 00044 sphere::sphere(const sphere& s2) { 00045 // cout <<"copyconstruct "<<flush; 00046 c.set_dim(getdim(s2.c)); 00047 c=s2.c; 00048 r=s2.r; 00049 clno=s2.clno; 00050 clsz=s2.clsz; 00051 } 00052 00053 sphere::sphere(myVector &coordinate, REAL radius, NUMBER clusno=0, NUMBER clussz=0) { 00054 // cout <<"parameterconstruct "<<flush; 00055 c.set_dim(getdim(coordinate)); 00056 c = coordinate; 00057 r = radius; 00058 clno =clusno; // clusternumber, by default=0 00059 clsz =clussz; // clustersize, by default=0 00060 } 00061 00062 int getdim(const sphere& u) {return getdim(u.c);} 00063 00064 void sphere::unitpointsphere(void) { 00065 c.nullvect(); // resides in the origin 00066 r = 0; // no volume 00067 clno =0; // clusternumber=0 means uncounted 00068 clsz =0; // clustersize 00069 } 00070 00071 sphere& sphere::operator=(const sphere& s2) { 00072 // cout <<"operator= "<<flush; 00073 c.set_dim(getdim(s2.c)); 00074 c=s2.c; 00075 r=s2.r; 00076 clno=s2.clno; 00077 clsz=s2.clsz; 00078 return *this; 00079 } 00080 00081 bool sphere::operator!=(const sphere& s2) { 00082 bool temp=0; 00083 if (c!=s2.c) temp=1; 00084 if (r!=s2.r) temp=1; 00085 if (clno!=s2.clno) temp=1; 00086 if (clsz!=s2.clsz) temp=1; 00087 return temp; 00088 } 00089 00090 REAL distance (sphere& sph1, sphere& sph2) { // reference instead of copy --> faster 00091 return ( length((sph1.c - sph2.c))); 00092 } 00093 00094 bool overlap (sphere& sph1, sphere& sph2) { // reference instead of copy --> faster 00095 return ( length((sph1.c - sph2.c)) < (sph1.r + sph2.r) ); 00096 } 00097 00098 bool overlap (sphere& sph1, sphere& sph2, sphere& temp) { // reference instead of copy --> faster 00099 temp.c = sph1.c; 00100 temp.c -= sph2.c; // using -= instead of - is much faster 00101 return ( length (temp.c) < (sph1.r + sph2.r) ); 00102 } 00103 00104 bool overlap2 (sphere& sph1, sphere& sph2, sphere& temp) { // no SQRT but radii * radii 00105 temp.c = sph1.c; 00106 temp.c -= sph2.c; // using -= instead of - is much faster 00107 return ( squarelength (temp.c) < (sph1.r + sph2.r) * (sph1.r + sph2.r) ); 00108 } 00109 00110 ostream& operator<<(ostream& os, sphere& z) { 00111 return os<<"["<<z.c<<" radius="<<z.r<<" clno="<<z.clno<<" clsize=" << z.clsz<<"]"; 00112 } 00113 00114 00115 // routines for arrays and lists of spheres 00116 00117 00118 REAL find_biggestradius (sphere *array, NUMLIST & sphlist) { 00119 00120 NUMLIST::iterator iter1; 00121 REAL biggest=0; 00122 00123 for (iter1 = sphlist.begin(); iter1 != sphlist.end(); ++iter1){ 00124 if ( array[*iter1].r > biggest) 00125 biggest= array[*iter1].r; 00126 } 00127 return biggest; 00128 } 00129 00130 00131 double myPI=2*acos(0.0); 00132 00133 double unitsphere (int dimension){ // Otto Forster, Analysis 3, p. 52 00134 double halfdim=.5*dimension; 00135 double nominator=pow(myPI, (int)halfdim ); 00136 double denominator=1; 00137 do { 00138 denominator*=halfdim; 00139 halfdim-=1; 00140 }while (halfdim>0); 00141 return (nominator/denominator); 00142 } 00143 00144 00145 // This converts radius -> fillingfactor 00146 REAL give_fillingfactor (REAL radius, NUMBER numberofspheres, COORDFLOAT length, int dimension) { 00147 double density=(REAL)numberofspheres/pow(length, dimension); 00148 return density*unitsphere(dimension)*pow(radius,dimension); 00149 } 00150 00151 // The following function derives the critical fillingfactor from theory 00152 REAL ff_critical (NUMBER N, int dim) { 00153 return critical_fillingfactor(dim,N); 00154 } 00155 REAL ff_critical_infinite_limit (int dim) { 00156 return pow(0.5,dim); // good guess for high dimensions 00157 } 00158 REAL ff_critical_guessed (NUMBER N, int dim) { 00159 REAL temp=ff_critical(N,dim); 00160 if (temp==-1) return ff_critical_infinite_limit(dim); 00161 return temp; 00162 } 00163 00164 REAL ff_critical_guessed (NUMBER N, int dim, REAL dummy) { 00165 return ff_critical_guessed(N, dim) ; 00166 } 00167 00168 // This converts fillingfactor -> radius 00169 REAL give_radius (REAL fillingfactor, NUMBER numberofspheres, COORDFLOAT length, int dimension) { 00170 double density=(REAL)numberofspheres/pow(length, dimension); 00171 return pow(( fillingfactor / density / unitsphere(dimension) ),(1/((double)dimension))); 00172 } 00173 00174 // The following function derives the critical radius from theory 00175 REAL R_critical (NUMBER no_of_spheres, COORDFLOAT length, int dimension) { 00176 REAL ffc=critical_fillingfactor(dimension,no_of_spheres); 00177 if (ffc==-1) return -1; 00178 return give_radius(ffc, no_of_spheres, length, dimension); 00179 } 00180 00181 REAL R_critical_guessed (NUMBER no_of_spheres, COORDFLOAT length, int dimension) { 00182 REAL ffc=critical_fillingfactor(dimension,no_of_spheres); 00183 if (ffc!=-1) return give_radius(ffc, no_of_spheres, length, dimension); 00184 else return give_radius(ff_critical_infinite_limit(dimension), no_of_spheres, length, dimension); 00185 } 00186 00187 00188 00189 00190 REAL percolating_fillingfactor_for_two_spheres_verbose(int dim, 00191 COORDFLOAT length) // length of dim-dimensional volume 00192 { 00193 REAL diagonal=sqrt(dim)*length; // Pythargoras: 00194 // sqrt (L*L + L*L) 00195 // = sqrt(2)*L for 2dim, 00196 // 00197 // sqrt( sqrt(2)*L * sqrt(2)*L + L * L ) 00198 // = sqrt(3)*L for 3dim; 00199 // 00200 // sqrt( sqrt(3)*L * sqrt(3)*L + L * L ) 00201 // = sqrt (4) * L = 2*L for 4dim 00202 00203 REAL radius = 0.5 * diagonal; // two touching spheres in opposite corners 00204 00205 REAL vol_one_sphere=unitsphere(dim)*pow(radius, dim); 00206 00207 REAL vol_space= pow(length, dim); 00208 00209 return ( (2 * vol_one_sphere) / vol_space) ; 00210 } 00211 00212 00213 REAL percolating_fillingfactor_for_two_spheres(int dim) 00214 { 00215 return ( (2 * unitsphere(dim)* pow(0.5*sqrt(dim),dim) ) ) ; 00216 } 00217
Diploma Thesis Sourcecode
Documentation check out the text and the executable binaries |