Trisurf Monte Carlo simulator
Samo Penic
2010-12-04 a6b1b556b6c8c8da22c5f322b57ccc1528578e10
commit | author | age
7958e9 1 #include<stdlib.h>
SP 2 #include<math.h>
3 #include<stdio.h>
4 #include "general.h"
5 #include "vertex.h"
6 #include "bond.h"
7 #include "vesicle.h"
8 #include "vertex.h"
9 #include "triangle.h"
10 #include "initial_distribution.h"
11
12 ts_vesicle *initial_distribution_dipyramid(ts_uint nshell, ts_uint ncmax1, ts_uint ncmax2, ts_uint ncmax3, ts_double stepsize){
13     ts_fprintf(stderr,"Starting initial_distribution on vesicle with %u shells!...\n",nshell);
14     ts_bool retval;
15     ts_uint no_vertices=5*nshell*nshell+2;
16     
17     ts_vesicle *vesicle=init_vesicle(no_vertices,ncmax1,ncmax2,ncmax3,stepsize);
18     vesicle->nshell=nshell;
19     retval = vtx_set_global_values(vesicle);
20     retval = pentagonal_dipyramid_vertex_distribution(vesicle->vlist);
21     retval = init_vertex_neighbours(vesicle->vlist);
22     retval = init_sort_neighbours(vesicle->vlist);
23     retval = init_vesicle_bonds(vesicle);
24     retval = init_triangles(vesicle);
25     retval = init_triangle_neighbours(vesicle);
26     retval = init_common_vertex_triangle_neighbours(vesicle);
27  ts_fprintf(stderr,"initial_distribution finished!\n");
28     return vesicle;
29
30
31
32 ts_bool pentagonal_dipyramid_vertex_distribution(ts_vertex_list *vlist){
33     /* Some often used relations */
34     const ts_double s1= sin(2.0*M_PI/5.0);
35     const ts_double s2= sin(4.0*M_PI/5.0);
36     const ts_double c1= cos(2.0*M_PI/5.0);
37     const ts_double c2= cos(4.0*M_PI/5.0);
38
39     /* Calculates projection lenght of an edge bond to pentagram plane */
40     const ts_double xl0=A0/(2.0*sin(M_PI/5.0));
41 #ifdef TS_DOUBLE_DOUBLE
42     const ts_double z0=sqrt(pow(A0,2)-pow(xl0,2));
43 #endif
44 #ifdef TS_DOUBLE_FLOAT
45     const ts_double z0=sqrtf(powf(A0,2)-powf(xl0,2));
46 #endif
47 #ifdef TS_DOUBLE_LONGDOUBLE
48     const ts_double z0=sqrtl(powl(A0,2)-powl(xl0,2));
49 #endif
50 //    const z0=sqrt(A0*A0 -xl0*xl0); /* I could use pow function but if pow is used make a check on the float type. If float then powf, if long double use powl */
51
52 /*placeholder for the pointer to vertex datastructure list... DIRTY: actual pointer points towards invalid address, one position before actual beginning of the list... This is to solve the difference between 1 based indexing in original program in fortran and 0 based indexing in C. All algorithms remain unchanged because of this!*/
53     ts_vertex **vtx=vlist->vtx -1 ; 
54
55
56     ts_uint nshell=(ts_uint)( sqrt((ts_double)(vlist->n-2)/5));
57 //    printf("nshell=%u\n",nshell);
58     ts_uint i,n0; // some for loop prereq
59     ts_int j,k;
60     ts_double dx,dy; // end loop prereq
61
62     /* topmost vertex */
63     vtx[1]->data->x=0.0;
64     vtx[1]->data->y=0.0;
65     vtx[1]->data->z=z0*(ts_double)nshell;
66     
67     /* starting from to in circular order on pentagrams */    
68     for(i=1;i<=nshell;i++){
69         n0=2+5*i*(i-1)/2; //-1 would be for the reason that C index starts from 0 
70         vtx[n0]->data->x=0.0;
71         vtx[n0]->data->y=(ts_double)i*xl0;
72         vtx[n0+i]->data->x=vtx[n0]->data->y*s1;
73         vtx[n0+i]->data->y=vtx[n0]->data->y*c1;
74         vtx[n0+2*i]->data->x=vtx[n0]->data->y*s2;
75         vtx[n0+2*i]->data->y=vtx[n0]->data->y*c2;
76         vtx[n0+3*i]->data->x=-vtx[n0+2*i]->data->x;
77         vtx[n0+3*i]->data->y=vtx[n0+2*i]->data->y;
78         vtx[n0+4*i]->data->x=-vtx[n0+i]->data->x;
79         vtx[n0+4*i]->data->y=vtx[n0+i]->data->y;
80     }
81
82     /* vertexes on the faces of the dipyramid */
83     for(i=1;i<=nshell;i++){
84         n0=2+5*i*(i-1)/2; // -1 would be because of C!
85         for(j=1;j<=i-1;j++){
86             dx=(vtx[n0]->data->x-vtx[n0+4*i]->data->x)/(ts_double)i;
87             dy=(vtx[n0]->data->y-vtx[n0+4*i]->data->y)/(ts_double)i;
88             vtx[n0+4*i+j]->data->x=(ts_double)j*dx+vtx[n0+4*i]->data->x;
89             vtx[n0+4*i+j]->data->y=(ts_double)j*dy+vtx[n0+4*i]->data->y;
90         }
91         for(k=0;k<=3;k++){ // I would be worried about zero starting of for
92             dx=(vtx[n0+(k+1)*i]->data->x - vtx[n0+k*i]->data->x)/(ts_double) i;
93             dy=(vtx[n0+(k+1)*i]->data->y - vtx[n0+k*i]->data->y)/(ts_double) i;
94             for(j=1; j<=i-1;j++){
95                 vtx[n0+k*i+j]->data->x= (ts_double)j*dx+vtx[n0+k*i]->data->x;
96                 vtx[n0+k*i+j]->data->y= (ts_double)j*dy+vtx[n0+k*i]->data->y;
97             } 
98         } 
99     }
100
101     for(i=1;i<=nshell;i++){
102         n0= 2+ 5*i*(i-1)/2;
103         for(j=0;j<=5*i-1;j++){
104         vtx[n0+j]->data->z= z0*(ts_double)(nshell-i);   // I would be worried about zero starting of for
105         }
106     }
107
108 /* for botom part of dipyramide we calculate the positions of vertices */
109     for(i=2+5*nshell*(nshell+1)/2;i<=vlist->n;i++){
110         vtx[i]->data->x=vtx[vlist->n - i +1]->data->x;
111         vtx[i]->data->y=vtx[vlist->n - i +1]->data->y;
112         vtx[i]->data->z=-vtx[vlist->n - i +1]->data->z;
113     }
114
115     for(i=1;i<=vlist->n;i++){
116         for(j=1;j<=vlist->n;j++){
117             if(i!=j && vtx_distance_sq(vtx[i],vtx[j])<0.001){
118                 printf("Vertices %u and %u are the same!\n",i,j);
119             }
120         }
121     }
122     return TS_SUCCESS;
123 }
124
125
126
127 ts_bool init_vertex_neighbours(ts_vertex_list *vlist){
128     ts_vertex **vtx=vlist->vtx -1; // take a look at dipyramid function for comment.
129     const ts_double eps=0.001; //TODO: find out if you can use EPS from math.h
130     ts_uint i,j;
131     ts_double dist2; // Square of distance of neighbours
132     /*this is not required if we zero all data in vertex structure at initialization */
133     /*if we force zeroing at initialization this for loop can safely be deleted */
134     //for(i=1;i<=vlist->n;i++){
135     //    vtx[i].neigh_no=0;
136     //}
137     for(i=1;i<=vlist->n;i++){
138         for(j=1;j<=vlist->n;j++){
139             dist2=vtx_distance_sq(vtx[i],vtx[j]);
140             if( (dist2>eps) && (dist2<(A0*A0+eps))){ 
141     //if it is close enough, but not too much close (solves problem of comparing when i==j)
142                 vtx_add_neighbour(vtx[i],vtx[j]);
143             }
144         }
145     //        printf ("vertex %u ima %u sosedov!\n",i,vtx[i]->data->neigh_no);
146     }
147
148     return TS_SUCCESS;
149 }
150
151 // TODO: with new datastructure can be rewritten.
152 ts_bool init_sort_neighbours(ts_vertex_list *vlist){
153     ts_vertex **vtx=vlist->vtx -1; // take a look at dipyramid function for comment.
154     ts_uint i,l,j,jj,jjj,k=0;   
155     ts_double eps=0.001; // Take a look if EPS from math.h can be used
156
157 /*lets initialize memory for temporary vertex_list. Should we write a function instead */
158     ts_vertex_list *tvlist=init_vertex_list(vlist->n);
159     ts_vertex **tvtx=tvlist->vtx -1;  /* again to compensate for 0-indexing */
160
161     ts_double dist2; // Square of distance of neighbours
162     ts_double direct; // Something, dont know what, but could be normal of some kind
163     for(i=1;i<=vlist->n;i++){
164         k++; // WHY i IS NOT GOOD??
165            vtx_add_neighbour(tvtx[k], tvtx[vtx[i]->data->neigh[0]->idx+1]); //always add 1st
166            jjj=1;
167            jj=1;
168            for(l=2;l<=vtx[i]->data->neigh_no;l++){
169                for(j=2;j<=vtx[i]->data->neigh_no;j++){
170                    dist2=vtx_distance_sq(vtx[i]->data->neigh[j-1],vtx[i]->data->neigh[jj-1]);
171                    direct=vtx_direct(vtx[i],vtx[i]->data->neigh[j-1],vtx[i]->data->neigh[jj-1]);
172                    if( (fabs(dist2-A0*A0)<=eps) && (direct>0.0) && (j!=jjj) ){
173                        vtx_add_neighbour(tvtx[k],tvtx[vtx[i]->data->neigh[j-1]->idx+1]);
174                        jjj=jj;
175                        jj=j;
176                        break;
177                    }
178                }
179            }    
180     }
181
182     for(i=1;i<=vlist->n;i++){
183         for(j=1;j<=vtx[i]->data->neigh_no;j++){
184             if(vtx[i]->data->neigh_no!=tvtx[i]->data->neigh_no){ //doesn't work with nshell=1!
185 //                fprintf(stderr,"data1=%u data2=%u\n",vtx[i]->data->neigh_no,tvtx[i]->data->neigh_no);
186                 fatal("Number of neighbours not the same in init_sort_neighbours.",4);
187             }
188             //we must correct the pointers in original to point to their
189             //neighbours according to indexes. Must be sure not to do it any
190             //other way! Also, we need to repair the collection of bonds...
191             vtx[i]->data->neigh[j-1]=vtx[tvtx[i]->data->neigh[j-1]->idx+1];
192         }
193     }
194
195    // Must free memory for temporary vertex array to avoid memory leak! HERE! NOW!
196 //    free_vertex(tvlist.vertex,tvlist.n);
197     vtx_list_free(tvlist);
198     return TS_SUCCESS;
199 }
200
201
202 ts_bool init_vesicle_bonds(ts_vesicle *vesicle){
203     ts_vertex_list *vlist=vesicle->vlist;
204     ts_bond_list *blist=vesicle->blist;
205     ts_vertex **vtx=vesicle->vlist->vtx - 1; // Because of 0 indexing
206 /* lets make correct clockwise ordering of in nearest neighbour list */
207     ts_uint i,j,k;
208     for(i=1;i<=vlist->n;i++){
209         for(j=i+1;j<=vlist->n;j++){
210             for(k=0;k<vtx[i]->data->neigh_no;k++){ // has changed 0 to < instead of 1 and <=
211                 if(vtx[i]->data->neigh[k]==vtx[j]){  //if addresses matches it is the same
212                     bond_add(blist,vtx[i],vtx[j]);
213                     break;
214                 }
215             }
216         }
217     } 
218 /* Let's make a check if the number of bonds is correct */
219     if((blist->n)!=3*(vlist->n-2)){
220         ts_fprintf(stderr,"Number of bonds is %u should be %u!\n", blist->n, 3*(vlist->n-2));
221         fatal("Number of bonds is not 3*(no_vertex-2).",4);
222     }
223     return TS_SUCCESS;
224 }
225
226
227
228 ts_bool init_triangles(ts_vesicle *vesicle){
229     ts_uint i,j,jj,k;
230     ts_vertex **vtx=vesicle->vlist->vtx -1; // difference between 0 indexing and 1 indexing
231     ts_triangle_list *tlist=vesicle->tlist;
232     ts_double dist, direct;
233     ts_double eps=0.001; // can we use EPS from math.h?
234     k=0;
235     for(i=1;i<=vesicle->vlist->n;i++){
236         for(j=1;j<=vtx[i]->data->neigh_no;j++){
237             for(jj=1;jj<=vtx[i]->data->neigh_no;jj++){
238         //        ts_fprintf(stderr,"%u: (%u,%u) neigh_no=%u ",i,j,jj,vtx[i].neigh_no);
239         //      ts_fprintf(stderr,"%e, %e",vtx[i].neigh[j-1]->x,vtx[i].neigh[jj-1]->x);
240                 dist=vtx_distance_sq(vtx[i]->data->neigh[j-1],vtx[i]->data->neigh[jj-1]);
241                 direct=vtx_direct(vtx[i],vtx[i]->data->neigh[j-1],vtx[i]->data->neigh[jj-1]);                
242                 if(fabs(dist-A0*A0)<=eps && direct < 0.0 && vtx[i]->data->neigh[j-1]->idx+1 > i && vtx[i]->data->neigh[jj-1]->idx+1 >i){
243                     triangle_add(tlist,vtx[i],vtx[i]->data->neigh[j-1],vtx[i]->data->neigh[jj-1]);
244                 }    
245             }    
246         }
247     }
248 /* We check if all triangles have 3 vertices and if the number of triangles
249  * matches the theoretical value.
250  */
251     for(i=0;i<tlist->n;i++){
252         k=0;
253         for(j=0;j<3;j++){
254             if(tlist->tria[i]->data->vertex[j]!=NULL)
255             k++;
256         }
257             if(k!=3){
258                 fatal("Some triangles has less than 3 vertices..",4);
259             }   
260     } 
261     if(tlist->n!=2*(vesicle->vlist->n -2)){
262         ts_fprintf(stderr,"The number of triangles is %u but should be %u!\n",tlist->n,2*(vesicle->vlist->n -2));
263         fatal("The number of triangles doesn't match 2*(no_vertex -2).",4);
264     }
265     return TS_SUCCESS;
266 }
267
268
269
270 ts_bool init_triangle_neighbours(ts_vesicle *vesicle){
271     ts_uint i,j,nobo;
272     ts_vertex *i1,*i2,*i3,*j1,*j2,*j3;
273 //    ts_vertex **vtx=vesicle->vlist->vtx -1; // difference between 0 indexing and 1 indexing
274     ts_triangle_list *tlist=vesicle->tlist;
275     ts_triangle **tria=tlist->tria -1;
276     nobo=0;
277     for(i=1;i<=tlist->n;i++){
278         i1=tria[i]->data->vertex[0]; 
279         i2=tria[i]->data->vertex[1]; 
280         i3=tria[i]->data->vertex[2]; 
281         for(j=1;j<=tlist->n;j++){
282             if(j==i) continue;
283             j1=tria[j]->data->vertex[0]; 
284             j2=tria[j]->data->vertex[1]; 
285             j3=tria[j]->data->vertex[2]; 
286             if((i1==j1 && i3==j2) || (i1==j2 && i3==j3) || (i1==j3 && i3==j1)){
287                     triangle_add_neighbour(tria[i],tria[j]);
288                     nobo++;
289             }
290         }
291     }
292     for(i=1;i<=tlist->n;i++){
293         i1=tria[i]->data->vertex[0]; 
294         i2=tria[i]->data->vertex[1]; 
295         i3=tria[i]->data->vertex[2]; 
296         for(j=1;j<=tlist->n;j++){
297             if(j==i) continue;
298             j1=tria[j]->data->vertex[0]; 
299             j2=tria[j]->data->vertex[1]; 
300             j3=tria[j]->data->vertex[2]; 
301             if((i1==j1 && i2==j3) || (i1==j3 && i2==j2) || (i1==j2 && i2==j1)){
302                 triangle_add_neighbour(tria[i],tria[j]);
303                 nobo++;
304             }
305         }
306     }
307     for(i=1;i<=tlist->n;i++){
308         i1=tria[i]->data->vertex[0]; 
309         i2=tria[i]->data->vertex[1]; 
310         i3=tria[i]->data->vertex[2]; 
311         for(j=1;j<=tlist->n;j++){
312             if(j==i) continue;
313             j1=tria[j]->data->vertex[0]; 
314             j2=tria[j]->data->vertex[1]; 
315             j3=tria[j]->data->vertex[2]; 
316             if((i2==j1 && i3==j3) || (i2==j3 && i3==j2) || (i2==j2 && i3==j1)){
317                 triangle_add_neighbour(tria[i],tria[j]);
318                 nobo++;
319             }
320         }
321     }
322     if(nobo != vesicle->blist->n*2) {
323             ts_fprintf(stderr,"Number of triangles= %u, number of bonds= %u\n",nobo/2, vesicle->blist->n);
324             fatal("Number of triangle neighbour pairs differs from double the number of bonds!",4);
325     }
326     return TS_SUCCESS;
327 }
328
329
330 ts_bool init_common_vertex_triangle_neighbours(ts_vesicle *vesicle){
331     ts_uint i,j,jp,k;
332     ts_vertex *k1,*k2,*k3,*k4,*k5;
333     ts_vertex **vtx=vesicle->vlist->vtx -1; // difference between 0 indexing and 1 indexing
334     ts_triangle_list *tlist=vesicle->tlist;
335     ts_triangle **tria=tlist->tria -1;
336
337     for(i=1;i<=vesicle->vlist->n;i++){
338         for(j=1;j<=vtx[i]->data->neigh_no;j++){
339             k1=vtx[i]->data->neigh[j-1];
340             jp=j+1;
341             if(j == vtx[i]->data->neigh_no) jp=1;
342             k2=vtx[i]->data->neigh[jp-1];
343             for(k=1;k<=tlist->n;k++){        // VERY NON-OPTIMAL!!! too many loops (vlist.n * vtx.neigh * tlist.n )!
344                 k3=tria[k]->data->vertex[0];
345                 k4=tria[k]->data->vertex[1];
346                 k5=tria[k]->data->vertex[2];
347 //                ts_fprintf(stderr,"%u %u: k=(%u %u %u)\n",k1,k2,k3,k4,k5);
348                 if((vtx[i]==k3 && k1==k4 && k2==k5) ||
349                 (vtx[i]==k4 && k1==k5 && k2==k3) ||
350                 (vtx[i]==k5 && k1==k3 && k2==k4)){
351           //          ts_fprintf(stderr, "Added to tristar! ");
352                     vertex_add_tristar(vtx[i],tria[k]);
353                 }
354             }
355         }
356 /*        ts_fprintf(stderr,"TRISTAR for %u (%u):",i-1,vtx[i].tristar_no);
357         for(j=0;j<vtx[i].tristar_no;j++){
358             ts_fprintf(stderr," %u,",vtx[i].tristar[j]->idx);
359         }
360         ts_fprintf(stderr,"\n"); */
361     }
362     return TS_SUCCESS;
363 }
364
365
366 ts_bool init_normal_vectors(ts_triangle_list *tlist){
367     /* Normals point INSIDE vesicle */
368     ts_uint k;
369     ts_triangle **tria=tlist->tria -1; //for 0 indexing
370     for(k=1;k<=tlist->n;k++){
371         triangle_normal_vector(tria[k]);    
372     }
373     return TS_SUCCESS;
374 }