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