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