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