Trisurf Monte Carlo simulator
Samo Penic
2016-07-13 90882f51387b945bc0f660d8b12200b2f48e80fa
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 ts_poly_list *init_poly_list(ts_uint n_poly, ts_uint n_mono, ts_vertex_list *vlist, ts_vesicle *vesicle){
1d5dff 57     ts_poly_list *poly_list=(ts_poly_list *)calloc(1,sizeof(ts_poly_list));
8db569 58     poly_list->poly    = (ts_poly **)calloc(n_poly,sizeof(ts_poly *));
fc6f3e 59     ts_uint i=0,j=0,k; //idx;
8db569 60     ts_uint gvtxi;
M 61     ts_double xnorm,ynorm,znorm,normlength;
624f81 62     ts_double dphi,dh;
90882f 63     cell_occupation(vesicle); //needed for evading the membrane
624f81 64     // Grafting polymers:
M 65     if (vlist!=NULL){
a752b5 66         if (n_poly > vlist->n){fatal("Number of polymers larger than numbero f vertices on a vesicle.",310);}
1d5dff 67     
624f81 68         while(i<n_poly){
M 69             gvtxi = rand() % vlist->n;
70             if (vlist->vtx[gvtxi]->grafted_poly == NULL){
71             poly_list->poly[i] = init_poly(n_mono, vlist->vtx[gvtxi]);
72             i++;
73             }
1d5dff 74         }
M 75     }
624f81 76     else
M 77     {
78         for(i=0;i<n_poly;i++){
79             poly_list->poly[i] = init_poly(n_mono, NULL);
80         }
81     }
82
1d5dff 83     poly_list->n = n_poly;
M 84
624f81 85     if (vlist!=NULL){
M 86     /* Make straight grafted poylmers normal to membrane (polymer brush). Dist. between poly vertices put to 1*/
e98482 87         ts_int intpoly=vesicle->tape->internal_poly;
fc6f3e 88         ts_int cellidx;
90882f 89         ts_double posX,posY,posZ,prevPosX,prevPosY,prevPosZ, phi,costheta,sintheta;
fc6f3e 90         ts_bool retval;
SP 91         ts_int l;
624f81 92         for (i=0;i<poly_list->n;i++){
M 93     
94             xnorm=0.0;
95             ynorm=0.0;
96             znorm=0.0;
97             for (j=0;j<poly_list->poly[i]->grafted_vtx->tristar_no;j++){
98                 xnorm-=poly_list->poly[i]->grafted_vtx->tristar[j]->xnorm;
99                 ynorm-=poly_list->poly[i]->grafted_vtx->tristar[j]->ynorm;
100                 znorm-=poly_list->poly[i]->grafted_vtx->tristar[j]->znorm;    
101             }
102             normlength=sqrt(xnorm*xnorm+ynorm*ynorm+znorm*znorm);
e98482 103             if(intpoly && i%2){
SP 104                 normlength=-normlength;
105             }
624f81 106             xnorm=xnorm/normlength;
M 107             ynorm=ynorm/normlength;
108             znorm=znorm/normlength;
8db569 109
fc6f3e 110             //prepare starting position for building the polymeres
SP 111             prevPosX=poly_list->poly[i]->grafted_vtx->x;
112             prevPosY=poly_list->poly[i]->grafted_vtx->y;
113             prevPosZ=poly_list->poly[i]->grafted_vtx->z;
624f81 114             for (j=0;j<poly_list->poly[i]->vlist->n;j++){
90882f 115                 //if(j==0){
SP 116                     posX=prevPosX+xnorm*(vesicle->clist->dmin_interspecies);
117                     posY=prevPosY+ynorm*(vesicle->clist->dmin_interspecies);
118                     posZ=prevPosZ+znorm*(vesicle->clist->dmin_interspecies);
119                 //}else{
120                 //    posX=prevPosX+xnorm;
121                 //    posY=prevPosY+ynorm;
122                 //    posZ=prevPosZ+znorm;
123                 //}
fc6f3e 124                 //trying to go towards normal
SP 125                 k=0;
126                 l=0;
127                 while(1){
128                     poly_list->poly[i]->vlist->vtx[j]->x = posX;
129                     poly_list->poly[i]->vlist->vtx[j]->y = posY;
130                     poly_list->poly[i]->vlist->vtx[j]->z = posZ;
131                     cellidx=vertex_self_avoidance(vesicle, poly_list->poly[i]->vlist->vtx[j]);
90882f 132                     retval=cell_occupation_number_and_internal_proximity(vesicle->clist,cellidx,poly_list->poly[i]->vlist->vtx[j]);
SP 133
fc6f3e 134                     if(retval==TS_SUCCESS){
90882f 135                         //retval=cell_add_vertex(vesicle->clist->cell[cellidx],poly_list->poly[i]->vlist->vtx[j]);
fc6f3e 136                         break;
SP 137                     }
138                     else{
90882f 139                     //    printf("%d %d Cannot put the vertex here. Finding another position\n",i,j);
SP 140                         //randomly change the direction.
141                         do{
142                         costheta=2.0*drand48()-1.0;
143                         sintheta=sqrt(1-pow(costheta,2));
144                         phi=drand48()*2.0*M_PI;
145                         if(j==0){
146                         //for special cases, when we are on the edge of bipytamid  the distance od dmin_interspecies is not enough
147                         posX=prevPosX+vesicle->dmax*sintheta*cos(phi);
148                         posY=prevPosY+vesicle->dmax*sintheta*sin(phi);
149                         posZ=prevPosZ+vesicle->dmax*costheta;
150
151
152                         } else {
153                         posX=prevPosX+vesicle->clist->dmin_interspecies*sintheta*cos(phi);
154                         posY=prevPosY+vesicle->clist->dmin_interspecies*sintheta*sin(phi);
155                         posZ=prevPosZ+vesicle->clist->dmin_interspecies*costheta;
156                         }
157                     //    if(j>0) break;
158                     //    if((xnorm*sintheta*cos(phi)+ynorm*sintheta*sin(phi)+znorm*costheta)<0.0 && j==0) break;
159                         }
160                     //    while(j==0);
161                         //we must move first vertex into the vesicle if the normal is in or out of the vesicle if the normal is out
162                         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 163                     }
SP 164                     k++;
165                     if(k>1000){
166                         //lets choose another grafting vertex;
167                         while(1){
168                             gvtxi = rand() % vesicle->vlist->n;
169                             if (vesicle->vlist->vtx[gvtxi]->grafted_poly == NULL){
170                                 ts_fprintf(stdout,"Found new potential grafting vertex %d for poly %d\n",gvtxi,i);
171                                 poly_list->poly[i]->grafted_vtx->grafted_poly=NULL;
172                                 poly_list->poly[i]->grafted_vtx = vesicle->vlist->vtx[gvtxi];
173                                 vesicle->vlist->vtx[gvtxi]->grafted_poly = poly_list->poly[i];
174                                 l++;
175                                 k=0;
90882f 176                                 xnorm=0.0;
SP 177                                 ynorm=0.0;
178                                 znorm=0.0;
179                                 int o;
180                                 for (o=0;o<poly_list->poly[i]->grafted_vtx->tristar_no;o++){
181                                     xnorm-=poly_list->poly[i]->grafted_vtx->tristar[o]->xnorm;
182                                     ynorm-=poly_list->poly[i]->grafted_vtx->tristar[o]->ynorm;
183                                     znorm-=poly_list->poly[i]->grafted_vtx->tristar[o]->znorm;    
184                                 }
185                                 normlength=sqrt(xnorm*xnorm+ynorm*ynorm+znorm*znorm);
186                                 if(intpoly && i%2){
187                                     normlength=-normlength;
188                                 }
189                                 xnorm=xnorm/normlength;
190                                 ynorm=ynorm/normlength;
191                                 znorm=znorm/normlength;
192                                 prevPosX=poly_list->poly[i]->grafted_vtx->x;
193                                 prevPosY=poly_list->poly[i]->grafted_vtx->y;
194                                 prevPosZ=poly_list->poly[i]->grafted_vtx->z;
195                                 //prepare starting position for building the polymeres
196                                 posX=prevPosX+xnorm*(vesicle->clist->dmin_interspecies);
197                                 posY=prevPosY+ynorm*(vesicle->clist->dmin_interspecies);
198                                 posZ=prevPosZ+znorm*(vesicle->clist->dmin_interspecies);
fc6f3e 199                                 break;
SP 200                             }
201                         }
202                         if(l>1000)
203                             fatal("Cannot make internal polymeres. No space inside the vesicle?",1001);
204                     }
205                 }
206                 prevPosX=posX;
207                 prevPosY=posY;
208                 prevPosZ=posZ;
624f81 209             }
8db569 210         }
624f81 211     }
M 212     else
213     {
214     /* Make filaments inside the vesicle. Helix with radius... Dist. between poly vertices put to 1*/
fe24d2 215     ts_double a,R,H,tantheta,h,r,phi,A0=1.2;
M 216
217         a = A0*(ts_double)vesicle->nshell;
218         R = A0*((ts_double)vesicle->nshell)/(2.0*sin(M_PI/5.0));
219         H = sqrt(a*a - R*R);
220         tantheta = sqrt(R*R - a*a/4.0)/H;
221         
222         h = -H + sqrt(vesicle->clist->dmin_interspecies)*1.5;
223         r = (H-fabs(h))*tantheta - sqrt(vesicle->clist->dmin_interspecies)*1.5;
224         dphi = 2.0*asin(1.0/2.0/r)*1.001;
402e8f 225         dh = dphi/2.0/M_PI*1.001;
fe24d2 226         phi=0.0;
624f81 227         for(i=0;i<poly_list->n;i++){
M 228             for (j=0;j<poly_list->poly[i]->vlist->n;j++){
fe24d2 229                 h = h + dh;
M 230                 r = (H-fabs(h))*tantheta - sqrt(vesicle->clist->dmin_interspecies)*1.5;
231                 dphi = 2.0*asin(1.0/2.0/r)*1.001;
232                 dh = dphi/2.0/M_PI*1.001;
233                 phi+=dphi;
234                 //ji = j + i*poly_list->poly[i]->vlist->n;
235                 poly_list->poly[i]->vlist->vtx[j]->x = r*cos(phi);
236                 poly_list->poly[i]->vlist->vtx[j]->y = r*sin(phi);
237                 poly_list->poly[i]->vlist->vtx[j]->z = h;// ji*dh - (dh*poly_list->n*poly_list->poly[i]->vlist->n/2.0);
624f81 238             }
8db569 239         }
M 240     }
241
40aa5b 242         //index correction for polymeres. Important, since each vtx has to have unique id
3c1ac1 243 /*    idx=vlist->n;
40aa5b 244     for(i=0;i<n_poly;i++){
SP 245         for(j=0;j<n_mono;j++,idx++){
246
247             poly_list->poly[i]->vlist->vtx[j]->idx=idx;
248
249         }
250     }
3c1ac1 251 */
90882f 252     
1d5dff 253     return poly_list;
M 254 }
255
8db569 256
M 257 ts_bool poly_free(ts_poly *poly){
258
259     if (poly->grafted_vtx!=NULL){
260         poly->grafted_vtx->grafted_poly=NULL;
261     }
262     vtx_list_free(poly->vlist);
263     bond_list_free(poly->blist);
264     free(poly);
265
266     return TS_SUCCESS;
267 }
268
269 ts_bool poly_list_free(ts_poly_list *poly_list){
270     ts_uint i;
271
272     for(i=0;i<poly_list->n;i++){
273         poly_free(poly_list->poly[i]);
274     }
275     free(poly_list->poly);
276     free(poly_list);
277     
278     return TS_SUCCESS;
279 }
a752b5 280
SP 281
282 ts_poly *remove_poly_with_index(ts_poly_list *poly_list, ts_uint idx){
283     ts_uint i;
284     ts_poly *removed_poly=poly_list->poly[idx];
285
286     poly_list->n--; //decrease the total number of polymeres
287     for(i=idx;i<poly_list->n;i++){ //move the rest of the polymeres up.
288         poly_list->poly[i]=poly_list->poly[i+1];
289 //        poly_list->poly[idx]->idx=idx;
290     }
291     
292     return removed_poly;
293 }
294
295
296 ts_bool remove_random_polymeres(ts_poly_list *poly_list, ts_uint number){
297
298     ts_uint i, idx;
299     ts_poly *poly;
300
301     ts_poly **new_poly_array;
302     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);
303     for(i=number;i>0;i--){
304         idx=rand() % poly_list->n;
305         poly=remove_poly_with_index(poly_list, idx);
306         poly_free(poly);
307     }
308     printf("Addr before %ld\n", (long)poly_list->poly);
309     new_poly_array=(ts_poly **)calloc(poly_list->n,sizeof(ts_poly *));
310     for(i=0;i<poly_list->n;i++){
311         new_poly_array[i]=poly_list->poly[i];
312     }
313     free(poly_list->poly);
314     poly_list->poly=new_poly_array;
315     printf("Addr after %ld\n", (long)poly_list->poly);
316     return TS_SUCCESS;
317 }
318