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