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