Trisurf Monte Carlo simulator
Samo Penic
2016-07-13 e2db4a0e170272935ccc8532bc245938aadc36fc
commit | author | age
7f6076 1 /* vim: set ts=4 sts=4 sw=4 noet : */
8db569 2 #include"general.h"
M 3 #include"poly.h"
4 #include<stdlib.h>
5 #include"vertex.h"
6 #include"bond.h"
7 #include<math.h>
fedf2b 8 #include"energy.h"
fc6f3e 9 #include"cell.h"
90882f 10 #include"frame.h"
bcf455 11 ts_bool poly_assign_filament_xi(ts_vesicle *vesicle, ts_tape *tape){
M 12     ts_uint i;
13
14     for(i=0;i<vesicle->filament_list->n;i++){
15      vesicle->filament_list->poly[i]->k = tape->xi;
16         }
17     
18     return TS_SUCCESS;
19 }
20
21
48bb92 22 ts_bool poly_assign_spring_const(ts_vesicle *vesicle){
M 23     ts_uint i;
24
25     for(i=0;i<vesicle->poly_list->n;i++){
26      vesicle->poly_list->poly[i]->k = vesicle->spring_constant;
27         }
28     
29     return TS_SUCCESS;
30 }
8db569 31
1d5dff 32 ts_poly    *init_poly(ts_uint n, ts_vertex *grafted_vtx){
M 33     ts_poly    *poly=(ts_poly *)calloc(1,sizeof(ts_poly));
34     poly->vlist = init_vertex_list(n);
35     poly->blist = init_bond_list();
624f81 36     if (grafted_vtx!=NULL){
M 37         poly->grafted_vtx = grafted_vtx;
38         grafted_vtx->grafted_poly = poly;
39     }
1d5dff 40
M 41     ts_uint i;
8db569 42     for(i=0;i<n-1;i++){
1d5dff 43         vtx_add_cneighbour(poly->blist, poly->vlist->vtx[i], poly->vlist->vtx[i+1]);
8db569 44         vtx_add_neighbour(poly->vlist->vtx[i+1], poly->vlist->vtx[i]);
1d5dff 45     }
M 46
48bb92 47     for(i=0;i<poly->blist->n;i++){
M 48     poly->blist->bond[i]->bond_length=sqrt(vtx_distance_sq(poly->blist->bond[i]->vtx1,poly->blist->bond[i]->vtx2));
fedf2b 49     bond_energy(poly->blist->bond[i],poly);
48bb92 50     }
90882f 51     vertex_list_assign_id(poly->vlist,TS_ID_FILAMENT);
1d5dff 52     return poly;
M 53 }
54
55
624f81 56
e2db4a 57 ts_bool poly_initial_distribution(ts_poly_list *poly_list, ts_int i, ts_vesicle *vesicle){
624f81 58     /* Make straight grafted poylmers normal to membrane (polymer brush). Dist. between poly vertices put to 1*/
e2db4a 59         ts_double xnorm,ynorm,znorm,normlength;
e98482 60         ts_int intpoly=vesicle->tape->internal_poly;
fc6f3e 61         ts_int cellidx;
90882f 62         ts_double posX,posY,posZ,prevPosX,prevPosY,prevPosZ, phi,costheta,sintheta;
fc6f3e 63         ts_bool retval;
e2db4a 64         ts_int j,k,l,m;
624f81 65             xnorm=0.0;
M 66             ynorm=0.0;
67             znorm=0.0;
68             for (j=0;j<poly_list->poly[i]->grafted_vtx->tristar_no;j++){
69                 xnorm-=poly_list->poly[i]->grafted_vtx->tristar[j]->xnorm;
70                 ynorm-=poly_list->poly[i]->grafted_vtx->tristar[j]->ynorm;
71                 znorm-=poly_list->poly[i]->grafted_vtx->tristar[j]->znorm;    
72             }
73             normlength=sqrt(xnorm*xnorm+ynorm*ynorm+znorm*znorm);
e98482 74             if(intpoly && i%2){
SP 75                 normlength=-normlength;
76             }
624f81 77             xnorm=xnorm/normlength;
M 78             ynorm=ynorm/normlength;
79             znorm=znorm/normlength;
fc6f3e 80             //prepare starting position for building the polymeres
SP 81             prevPosX=poly_list->poly[i]->grafted_vtx->x;
82             prevPosY=poly_list->poly[i]->grafted_vtx->y;
83             prevPosZ=poly_list->poly[i]->grafted_vtx->z;
624f81 84             for (j=0;j<poly_list->poly[i]->vlist->n;j++){
90882f 85                 //if(j==0){
SP 86                     posX=prevPosX+xnorm*(vesicle->clist->dmin_interspecies);
87                     posY=prevPosY+ynorm*(vesicle->clist->dmin_interspecies);
88                     posZ=prevPosZ+znorm*(vesicle->clist->dmin_interspecies);
89                 //}else{
90                 //    posX=prevPosX+xnorm;
91                 //    posY=prevPosY+ynorm;
92                 //    posZ=prevPosZ+znorm;
93                 //}
fc6f3e 94                 //trying to go towards normal
SP 95                 k=0;
96                 while(1){
97                     poly_list->poly[i]->vlist->vtx[j]->x = posX;
98                     poly_list->poly[i]->vlist->vtx[j]->y = posY;
99                     poly_list->poly[i]->vlist->vtx[j]->z = posZ;
100                     cellidx=vertex_self_avoidance(vesicle, poly_list->poly[i]->vlist->vtx[j]);
90882f 101                     retval=cell_occupation_number_and_internal_proximity(vesicle->clist,cellidx,poly_list->poly[i]->vlist->vtx[j]);
fc6f3e 102                     if(retval==TS_SUCCESS){
e2db4a 103                         retval=cell_add_vertex(vesicle->clist->cell[cellidx],poly_list->poly[i]->vlist->vtx[j]);
fc6f3e 104                         break;
SP 105                     }
106                     else{
90882f 107                     //    printf("%d %d Cannot put the vertex here. Finding another position\n",i,j);
SP 108                         //randomly change the direction.
e2db4a 109                         m=0;
90882f 110                         //we must move first vertex into the vesicle if the normal is in or out of the vesicle if the normal is out
e2db4a 111                         do{
SP 112                             costheta=2.0*drand48()-1.0;
113                             sintheta=sqrt(1-pow(costheta,2));
114                             phi=drand48()*2.0*M_PI;
115                             if(j==0){
116                         //for special cases, when we are on the edge of bipyramid  the distance od dmin_interspecies is not enough
117                                 posX=prevPosX+vesicle->dmax*sintheta*cos(phi);
118                                 posY=prevPosY+vesicle->dmax*sintheta*sin(phi);
119                                 posZ=prevPosZ+vesicle->dmax*costheta;
120
121                             } else {
122                                 posX=prevPosX+vesicle->clist->dmin_interspecies*sintheta*cos(phi);
123                                 posY=prevPosY+vesicle->clist->dmin_interspecies*sintheta*sin(phi);
124                                 posZ=prevPosZ+vesicle->clist->dmin_interspecies*costheta;
125                             }
126                             m++;
127                             if(m>1000) {
128                                 k=9999; //break also ot of the outer loop
129                                 printf("was here\n");
130                                 break;
131                             }
132                         }
90882f 133                         while((xnorm*(poly_list->poly[i]->grafted_vtx->x-posX)+ynorm*(poly_list->poly[i]->grafted_vtx->y-posY)+znorm*(poly_list->poly[i]->grafted_vtx->z-posZ))>0.0 && j==0);
fc6f3e 134                     }
SP 135                     k++;
136                     if(k>1000){
e2db4a 137                         //undo changes to the cell
SP 138                         for(l=0;l<j-1;l++){
139                             cellidx=vertex_self_avoidance(vesicle, poly_list->poly[i]->vlist->vtx[l]);
140                             cell_remove_vertex(vesicle->clist->cell[cellidx],poly_list->poly[i]->vlist->vtx[l]);
fc6f3e 141                         }
e2db4a 142                         return TS_FAIL;
fc6f3e 143                     }
SP 144                 }
145                 prevPosX=posX;
146                 prevPosY=posY;
147                 prevPosZ=posZ;
624f81 148             }
e2db4a 149     printf("did it\n");
SP 150     return TS_SUCCESS;
151
152 }
153
154
155 ts_poly_list *init_poly_list(ts_uint n_poly, ts_uint n_mono, ts_vertex_list *vlist, ts_vesicle *vesicle){
156     ts_poly_list *poly_list=(ts_poly_list *)calloc(1,sizeof(ts_poly_list));
157     poly_list->poly    = (ts_poly **)calloc(n_poly,sizeof(ts_poly *));
158     ts_uint i=0,j=0; //idx;
159     ts_uint gvtxi;
160     ts_bool retval;
161     ts_double dphi,dh;
162     cell_occupation(vesicle); //needed for evading the membrane
163     // Grafting polymers:
164     if (vlist!=NULL){
165         if (n_poly > vlist->n){fatal("Number of polymers larger than numbero f vertices on a vesicle.",310);}
166     
167         while(i<n_poly){
168             gvtxi = rand() % vlist->n;
169             if (vlist->vtx[gvtxi]->grafted_poly == NULL){
170                 poly_list->poly[i] = init_poly(n_mono, vlist->vtx[gvtxi]);
171                 retval=poly_initial_distribution(poly_list, i, vesicle);
172                 if(retval==TS_FAIL){
173                     ts_fprintf(stdout,"Found new potential grafting vertex %d for poly %d\n",gvtxi,i);
174                 }
175                 else {
176                     i++;
177                 }
178             }
8db569 179         }
624f81 180     }
M 181     else
182     {
e2db4a 183         for(i=0;i<n_poly;i++){
SP 184             poly_list->poly[i] = init_poly(n_mono, NULL);
185         }
186     }
187
188     poly_list->n = n_poly;
189
190     if (vlist!=NULL){
191     }
192     else
193     {
624f81 194     /* Make filaments inside the vesicle. Helix with radius... Dist. between poly vertices put to 1*/
fe24d2 195     ts_double a,R,H,tantheta,h,r,phi,A0=1.2;
M 196
197         a = A0*(ts_double)vesicle->nshell;
198         R = A0*((ts_double)vesicle->nshell)/(2.0*sin(M_PI/5.0));
199         H = sqrt(a*a - R*R);
200         tantheta = sqrt(R*R - a*a/4.0)/H;
201         
202         h = -H + sqrt(vesicle->clist->dmin_interspecies)*1.5;
203         r = (H-fabs(h))*tantheta - sqrt(vesicle->clist->dmin_interspecies)*1.5;
204         dphi = 2.0*asin(1.0/2.0/r)*1.001;
402e8f 205         dh = dphi/2.0/M_PI*1.001;
fe24d2 206         phi=0.0;
624f81 207         for(i=0;i<poly_list->n;i++){
M 208             for (j=0;j<poly_list->poly[i]->vlist->n;j++){
fe24d2 209                 h = h + dh;
M 210                 r = (H-fabs(h))*tantheta - sqrt(vesicle->clist->dmin_interspecies)*1.5;
211                 dphi = 2.0*asin(1.0/2.0/r)*1.001;
212                 dh = dphi/2.0/M_PI*1.001;
213                 phi+=dphi;
214                 //ji = j + i*poly_list->poly[i]->vlist->n;
215                 poly_list->poly[i]->vlist->vtx[j]->x = r*cos(phi);
216                 poly_list->poly[i]->vlist->vtx[j]->y = r*sin(phi);
217                 poly_list->poly[i]->vlist->vtx[j]->z = h;// ji*dh - (dh*poly_list->n*poly_list->poly[i]->vlist->n/2.0);
624f81 218             }
8db569 219         }
M 220     }
221
40aa5b 222         //index correction for polymeres. Important, since each vtx has to have unique id
3c1ac1 223 /*    idx=vlist->n;
40aa5b 224     for(i=0;i<n_poly;i++){
SP 225         for(j=0;j<n_mono;j++,idx++){
226
227             poly_list->poly[i]->vlist->vtx[j]->idx=idx;
228
229         }
230     }
3c1ac1 231 */
90882f 232     
1d5dff 233     return poly_list;
M 234 }
235
8db569 236
M 237 ts_bool poly_free(ts_poly *poly){
238
239     if (poly->grafted_vtx!=NULL){
240         poly->grafted_vtx->grafted_poly=NULL;
241     }
242     vtx_list_free(poly->vlist);
243     bond_list_free(poly->blist);
244     free(poly);
245
246     return TS_SUCCESS;
247 }
248
249 ts_bool poly_list_free(ts_poly_list *poly_list){
250     ts_uint i;
251
252     for(i=0;i<poly_list->n;i++){
253         poly_free(poly_list->poly[i]);
254     }
255     free(poly_list->poly);
256     free(poly_list);
257     
258     return TS_SUCCESS;
259 }
a752b5 260
SP 261
262 ts_poly *remove_poly_with_index(ts_poly_list *poly_list, ts_uint idx){
263     ts_uint i;
264     ts_poly *removed_poly=poly_list->poly[idx];
265
266     poly_list->n--; //decrease the total number of polymeres
267     for(i=idx;i<poly_list->n;i++){ //move the rest of the polymeres up.
268         poly_list->poly[i]=poly_list->poly[i+1];
269 //        poly_list->poly[idx]->idx=idx;
270     }
271     
272     return removed_poly;
273 }
274
275
276 ts_bool remove_random_polymeres(ts_poly_list *poly_list, ts_uint number){
277
278     ts_uint i, idx;
279     ts_poly *poly;
280
281     ts_poly **new_poly_array;
282     if(number>poly_list->n) fatal("The number of polymeres to be removed from the list is greater than the number of total polymeres in the list",999);
283     for(i=number;i>0;i--){
284         idx=rand() % poly_list->n;
285         poly=remove_poly_with_index(poly_list, idx);
286         poly_free(poly);
287     }
288     printf("Addr before %ld\n", (long)poly_list->poly);
289     new_poly_array=(ts_poly **)calloc(poly_list->n,sizeof(ts_poly *));
290     for(i=0;i<poly_list->n;i++){
291         new_poly_array[i]=poly_list->poly[i];
292     }
293     free(poly_list->poly);
294     poly_list->poly=new_poly_array;
295     printf("Addr after %ld\n", (long)poly_list->poly);
296     return TS_SUCCESS;
297 }
298