Trisurf Monte Carlo simulator
Samo Penic
2016-09-15 3cd5f4a96d445b55da8a7e6da8509bbd7d5d41b8
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"
8db569 9
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 *));
3c1ac1 58     ts_uint i=0,j=0; //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;
624f81 87         for (i=0;i<poly_list->n;i++){
M 88     
89             xnorm=0.0;
90             ynorm=0.0;
91             znorm=0.0;
92             for (j=0;j<poly_list->poly[i]->grafted_vtx->tristar_no;j++){
93                 xnorm-=poly_list->poly[i]->grafted_vtx->tristar[j]->xnorm;
94                 ynorm-=poly_list->poly[i]->grafted_vtx->tristar[j]->ynorm;
95                 znorm-=poly_list->poly[i]->grafted_vtx->tristar[j]->znorm;    
96             }
97             normlength=sqrt(xnorm*xnorm+ynorm*ynorm+znorm*znorm);
e98482 98             if(intpoly && i%2){
SP 99                 normlength=-normlength;
100             }
624f81 101             xnorm=xnorm/normlength;
M 102             ynorm=ynorm/normlength;
103             znorm=znorm/normlength;
8db569 104
624f81 105             for (j=0;j<poly_list->poly[i]->vlist->n;j++){
M 106                 poly_list->poly[i]->vlist->vtx[j]->x = poly_list->poly[i]->grafted_vtx->x + xnorm*(ts_double)(j+1);
107                 poly_list->poly[i]->vlist->vtx[j]->y = poly_list->poly[i]->grafted_vtx->y + ynorm*(ts_double)(j+1);
108                 poly_list->poly[i]->vlist->vtx[j]->z = poly_list->poly[i]->grafted_vtx->z + znorm*(ts_double)(j+1);
109             }
8db569 110         }
624f81 111     }
M 112     else
113     {
114     /* Make filaments inside the vesicle. Helix with radius... Dist. between poly vertices put to 1*/
fe24d2 115     ts_double a,R,H,tantheta,h,r,phi,A0=1.2;
M 116
117         a = A0*(ts_double)vesicle->nshell;
118         R = A0*((ts_double)vesicle->nshell)/(2.0*sin(M_PI/5.0));
119         H = sqrt(a*a - R*R);
120         tantheta = sqrt(R*R - a*a/4.0)/H;
121         
122         h = -H + sqrt(vesicle->clist->dmin_interspecies)*1.5;
123         r = (H-fabs(h))*tantheta - sqrt(vesicle->clist->dmin_interspecies)*1.5;
124         dphi = 2.0*asin(1.0/2.0/r)*1.001;
402e8f 125         dh = dphi/2.0/M_PI*1.001;
fe24d2 126         phi=0.0;
624f81 127         for(i=0;i<poly_list->n;i++){
M 128             for (j=0;j<poly_list->poly[i]->vlist->n;j++){
fe24d2 129                 h = h + dh;
M 130                 r = (H-fabs(h))*tantheta - sqrt(vesicle->clist->dmin_interspecies)*1.5;
131                 dphi = 2.0*asin(1.0/2.0/r)*1.001;
132                 dh = dphi/2.0/M_PI*1.001;
133                 phi+=dphi;
134                 //ji = j + i*poly_list->poly[i]->vlist->n;
135                 poly_list->poly[i]->vlist->vtx[j]->x = r*cos(phi);
136                 poly_list->poly[i]->vlist->vtx[j]->y = r*sin(phi);
137                 poly_list->poly[i]->vlist->vtx[j]->z = h;// ji*dh - (dh*poly_list->n*poly_list->poly[i]->vlist->n/2.0);
624f81 138             }
8db569 139         }
M 140     }
141
40aa5b 142         //index correction for polymeres. Important, since each vtx has to have unique id
3c1ac1 143 /*    idx=vlist->n;
40aa5b 144     for(i=0;i<n_poly;i++){
SP 145         for(j=0;j<n_mono;j++,idx++){
146
147             poly_list->poly[i]->vlist->vtx[j]->idx=idx;
148
149         }
150     }
3c1ac1 151 */
40aa5b 152
1d5dff 153     return poly_list;
M 154 }
155
8db569 156
M 157 ts_bool poly_free(ts_poly *poly){
158
3cd5f4 159 //    if (poly->grafted_vtx!=NULL){
SP 160 //        poly->grafted_vtx->grafted_poly=NULL;
161 //    }
8db569 162     vtx_list_free(poly->vlist);
M 163     bond_list_free(poly->blist);
164     free(poly);
165
166     return TS_SUCCESS;
167 }
168
169 ts_bool poly_list_free(ts_poly_list *poly_list){
170     ts_uint i;
3cd5f4 171     //fprintf(stderr,"no. of polys=%d\n", poly_list->n);
8db569 172     for(i=0;i<poly_list->n;i++){
3cd5f4 173     //    fprintf(stderr,"%d poly address in mem=%ld\n",i+1,(long)&(poly_list->poly[i]));
8db569 174         poly_free(poly_list->poly[i]);
M 175     }
176     free(poly_list->poly);
177     free(poly_list);
178     
179     return TS_SUCCESS;
180 }
a752b5 181
SP 182
183 ts_poly *remove_poly_with_index(ts_poly_list *poly_list, ts_uint idx){
184     ts_uint i;
185     ts_poly *removed_poly=poly_list->poly[idx];
186
187     poly_list->n--; //decrease the total number of polymeres
188     for(i=idx;i<poly_list->n;i++){ //move the rest of the polymeres up.
189         poly_list->poly[i]=poly_list->poly[i+1];
190 //        poly_list->poly[idx]->idx=idx;
191     }
192     
193     return removed_poly;
194 }
195
196
197 ts_bool remove_random_polymeres(ts_poly_list *poly_list, ts_uint number){
198
199     ts_uint i, idx;
200     ts_poly *poly;
201
202     ts_poly **new_poly_array;
203     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);
204     for(i=number;i>0;i--){
205         idx=rand() % poly_list->n;
206         poly=remove_poly_with_index(poly_list, idx);
207         poly_free(poly);
208     }
209     printf("Addr before %ld\n", (long)poly_list->poly);
210     new_poly_array=(ts_poly **)calloc(poly_list->n,sizeof(ts_poly *));
211     for(i=0;i<poly_list->n;i++){
212         new_poly_array[i]=poly_list->poly[i];
213     }
214     free(poly_list->poly);
215     poly_list->poly=new_poly_array;
216     printf("Addr after %ld\n", (long)poly_list->poly);
217     return TS_SUCCESS;
218 }
219