Trisurf Monte Carlo simulator
Samo Penic
2018-05-15 00251384c9c40368b04cfab4f4082666b5541a80
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
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
82a8ab 138                         for(l=0;l<j;l++){
e2db4a 139                             cellidx=vertex_self_avoidance(vesicle, poly_list->poly[i]->vlist->vtx[l]);
SP 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:
82a8ab 164     int tries=0;
e2db4a 165     if (vlist!=NULL){
SP 166         if (n_poly > vlist->n){fatal("Number of polymers larger than numbero f vertices on a vesicle.",310);}
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);
82a8ab 174                     poly_free(poly_list->poly[i]);
SP 175                     tries++;
e2db4a 176                 }
SP 177                 else {
82a8ab 178                     tries=0;
e2db4a 179                     i++;
SP 180                 }
82a8ab 181                 if(tries>5000){
SP 182                     fatal("Cannot find space for inner polymeres",1001);
183                 }
40aa5b 184             }
8db569 185         }
624f81 186     }
M 187     else
188     {
e2db4a 189         for(i=0;i<n_poly;i++){
SP 190             poly_list->poly[i] = init_poly(n_mono, NULL);
191         }
192     }
193
194     poly_list->n = n_poly;
195
196     if (vlist!=NULL){
624f81 197     }
M 198     else
199     {
200     /* Make filaments inside the vesicle. Helix with radius... Dist. between poly vertices put to 1*/
fe24d2 201     ts_double a,R,H,tantheta,h,r,phi,A0=1.2;
M 202
203         a = A0*(ts_double)vesicle->nshell;
204         R = A0*((ts_double)vesicle->nshell)/(2.0*sin(M_PI/5.0));
205         H = sqrt(a*a - R*R);
206         tantheta = sqrt(R*R - a*a/4.0)/H;
207         
208         h = -H + sqrt(vesicle->clist->dmin_interspecies)*1.5;
209         r = (H-fabs(h))*tantheta - sqrt(vesicle->clist->dmin_interspecies)*1.5;
210         dphi = 2.0*asin(1.0/2.0/r)*1.001;
402e8f 211         dh = dphi/2.0/M_PI*1.001;
fe24d2 212         phi=0.0;
624f81 213         for(i=0;i<poly_list->n;i++){
M 214             for (j=0;j<poly_list->poly[i]->vlist->n;j++){
fe24d2 215                 h = h + dh;
M 216                 r = (H-fabs(h))*tantheta - sqrt(vesicle->clist->dmin_interspecies)*1.5;
217                 dphi = 2.0*asin(1.0/2.0/r)*1.001;
218                 dh = dphi/2.0/M_PI*1.001;
219                 phi+=dphi;
220                 //ji = j + i*poly_list->poly[i]->vlist->n;
221                 poly_list->poly[i]->vlist->vtx[j]->x = r*cos(phi);
222                 poly_list->poly[i]->vlist->vtx[j]->y = r*sin(phi);
223                 poly_list->poly[i]->vlist->vtx[j]->z = h;// ji*dh - (dh*poly_list->n*poly_list->poly[i]->vlist->n/2.0);
624f81 224             }
8db569 225         }
M 226     }
227
40aa5b 228         //index correction for polymeres. Important, since each vtx has to have unique id
3c1ac1 229 /*    idx=vlist->n;
40aa5b 230     for(i=0;i<n_poly;i++){
SP 231         for(j=0;j<n_mono;j++,idx++){
232
233             poly_list->poly[i]->vlist->vtx[j]->idx=idx;
234
235         }
236     }
3c1ac1 237 */
90882f 238     
1d5dff 239     return poly_list;
M 240 }
241
8db569 242
M 243 ts_bool poly_free(ts_poly *poly){
244
3cd5f4 245 //    if (poly->grafted_vtx!=NULL){
SP 246 //        poly->grafted_vtx->grafted_poly=NULL;
247 //    }
8db569 248     vtx_list_free(poly->vlist);
M 249     bond_list_free(poly->blist);
250     free(poly);
251
252     return TS_SUCCESS;
253 }
254
255 ts_bool poly_list_free(ts_poly_list *poly_list){
256     ts_uint i;
3cd5f4 257     //fprintf(stderr,"no. of polys=%d\n", poly_list->n);
8db569 258     for(i=0;i<poly_list->n;i++){
3cd5f4 259     //    fprintf(stderr,"%d poly address in mem=%ld\n",i+1,(long)&(poly_list->poly[i]));
8db569 260         poly_free(poly_list->poly[i]);
M 261     }
262     free(poly_list->poly);
263     free(poly_list);
264     
265     return TS_SUCCESS;
266 }
a752b5 267
SP 268
269 ts_poly *remove_poly_with_index(ts_poly_list *poly_list, ts_uint idx){
270     ts_uint i;
271     ts_poly *removed_poly=poly_list->poly[idx];
272
273     poly_list->n--; //decrease the total number of polymeres
274     for(i=idx;i<poly_list->n;i++){ //move the rest of the polymeres up.
275         poly_list->poly[i]=poly_list->poly[i+1];
276 //        poly_list->poly[idx]->idx=idx;
277     }
278     
279     return removed_poly;
280 }
281
282
283 ts_bool remove_random_polymeres(ts_poly_list *poly_list, ts_uint number){
284
285     ts_uint i, idx;
286     ts_poly *poly;
287
288     ts_poly **new_poly_array;
289     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);
290     for(i=number;i>0;i--){
291         idx=rand() % poly_list->n;
292         poly=remove_poly_with_index(poly_list, idx);
293         poly_free(poly);
294     }
295     printf("Addr before %ld\n", (long)poly_list->poly);
296     new_poly_array=(ts_poly **)calloc(poly_list->n,sizeof(ts_poly *));
297     for(i=0;i<poly_list->n;i++){
298         new_poly_array[i]=poly_list->poly[i];
299     }
300     free(poly_list->poly);
301     poly_list->poly=new_poly_array;
302     printf("Addr after %ld\n", (long)poly_list->poly);
303     return TS_SUCCESS;
304 }
305