Trisurf Monte Carlo simulator
mihaf
2014-03-18 58230a2591414fb38b9ec8d3a76439b290cb0a6f
commit | author | age
8db569 1 #include"general.h"
M 2 #include"poly.h"
3 #include<stdlib.h>
4 #include"vertex.h"
5 #include"bond.h"
6 #include<math.h>
fedf2b 7 #include"energy.h"
8db569 8
48bb92 9 ts_bool poly_assign_spring_const(ts_vesicle *vesicle){
M 10     ts_uint i;
11
12     for(i=0;i<vesicle->poly_list->n;i++){
13      vesicle->poly_list->poly[i]->k = vesicle->spring_constant;
14         }
15     
16     return TS_SUCCESS;
17 }
8db569 18
1d5dff 19 ts_poly    *init_poly(ts_uint n, ts_vertex *grafted_vtx){
M 20     ts_poly    *poly=(ts_poly *)calloc(1,sizeof(ts_poly));
21     poly->vlist = init_vertex_list(n);
22     poly->blist = init_bond_list();
624f81 23     if (grafted_vtx!=NULL){
M 24         poly->grafted_vtx = grafted_vtx;
25         grafted_vtx->grafted_poly = poly;
26     }
1d5dff 27
M 28     ts_uint i;
8db569 29     for(i=0;i<n-1;i++){
1d5dff 30         vtx_add_cneighbour(poly->blist, poly->vlist->vtx[i], poly->vlist->vtx[i+1]);
8db569 31         vtx_add_neighbour(poly->vlist->vtx[i+1], poly->vlist->vtx[i]);
1d5dff 32     }
M 33
48bb92 34     for(i=0;i<poly->blist->n;i++){
M 35     poly->blist->bond[i]->bond_length=sqrt(vtx_distance_sq(poly->blist->bond[i]->vtx1,poly->blist->bond[i]->vtx2));
fedf2b 36     bond_energy(poly->blist->bond[i],poly);
48bb92 37     }
M 38
1d5dff 39     return poly;
M 40 }
41
42
624f81 43 ts_poly_list *init_poly_list(ts_uint n_poly, ts_uint n_mono, ts_vertex_list *vlist, ts_vesicle *vesicle){
1d5dff 44     ts_poly_list *poly_list=(ts_poly_list *)calloc(1,sizeof(ts_poly_list));
8db569 45     poly_list->poly    = (ts_poly **)calloc(n_poly,sizeof(ts_poly *));
3c1ac1 46     ts_uint i=0,j=0; //idx;
8db569 47     ts_uint gvtxi;
M 48     ts_double xnorm,ynorm,znorm,normlength;
624f81 49     ts_double dphi,dh;
M 50     ts_uint ji;
1d5dff 51
624f81 52     // Grafting polymers:
M 53     if (vlist!=NULL){
54         if (n_poly > vlist->n){fatal("Number of polymers larger then numbero f vertices on a vesicle.",310);}
1d5dff 55     
624f81 56         while(i<n_poly){
M 57             gvtxi = rand() % vlist->n;
58             if (vlist->vtx[gvtxi]->grafted_poly == NULL){
59             poly_list->poly[i] = init_poly(n_mono, vlist->vtx[gvtxi]);
60             i++;
61             }
1d5dff 62         }
M 63     }
624f81 64     else
M 65     {
66         for(i=0;i<n_poly;i++){
67             poly_list->poly[i] = init_poly(n_mono, NULL);
68         }
69     }
70
1d5dff 71     poly_list->n = n_poly;
M 72
624f81 73     if (vlist!=NULL){
M 74     /* Make straight grafted poylmers normal to membrane (polymer brush). Dist. between poly vertices put to 1*/
75         for (i=0;i<poly_list->n;i++){
76     
77             xnorm=0.0;
78             ynorm=0.0;
79             znorm=0.0;
80             for (j=0;j<poly_list->poly[i]->grafted_vtx->tristar_no;j++){
81                 xnorm-=poly_list->poly[i]->grafted_vtx->tristar[j]->xnorm;
82                 ynorm-=poly_list->poly[i]->grafted_vtx->tristar[j]->ynorm;
83                 znorm-=poly_list->poly[i]->grafted_vtx->tristar[j]->znorm;    
84             }
85             normlength=sqrt(xnorm*xnorm+ynorm*ynorm+znorm*znorm);
86             xnorm=xnorm/normlength;
87             ynorm=ynorm/normlength;
88             znorm=znorm/normlength;
8db569 89
624f81 90             for (j=0;j<poly_list->poly[i]->vlist->n;j++){
M 91                 poly_list->poly[i]->vlist->vtx[j]->x = poly_list->poly[i]->grafted_vtx->x + xnorm*(ts_double)(j+1);
92                 poly_list->poly[i]->vlist->vtx[j]->y = poly_list->poly[i]->grafted_vtx->y + ynorm*(ts_double)(j+1);
93                 poly_list->poly[i]->vlist->vtx[j]->z = poly_list->poly[i]->grafted_vtx->z + znorm*(ts_double)(j+1);
94             }
8db569 95         }
624f81 96     }
M 97     else
98     {
99     /* Make filaments inside the vesicle. Helix with radius... Dist. between poly vertices put to 1*/
58230a 100         dphi = 2.0*asin(1.0/2.0/vesicle->R_nucleus)*1.001;
M 101         dh = dphi/2.0/M_PI*1.001;
624f81 102         for(i=0;i<poly_list->n;i++){
M 103             for (j=0;j<poly_list->poly[i]->vlist->n;j++){
104                 ji = j + i*poly_list->poly[i]->vlist->n;
105                 poly_list->poly[i]->vlist->vtx[j]->x = vesicle->R_nucleus*cos(ji*dphi);
106                 poly_list->poly[i]->vlist->vtx[j]->y = vesicle->R_nucleus*sin(ji*dphi);
58230a 107                 poly_list->poly[i]->vlist->vtx[j]->z = ji*dh - (dh*poly_list->n*poly_list->poly[i]->vlist->n/2.0);
624f81 108             }
8db569 109         }
M 110     }
111
40aa5b 112         //index correction for polymeres. Important, since each vtx has to have unique id
3c1ac1 113 /*    idx=vlist->n;
40aa5b 114     for(i=0;i<n_poly;i++){
SP 115         for(j=0;j<n_mono;j++,idx++){
116
117             poly_list->poly[i]->vlist->vtx[j]->idx=idx;
118
119         }
120     }
3c1ac1 121 */
40aa5b 122
1d5dff 123     return poly_list;
M 124 }
125
8db569 126
M 127 ts_bool poly_free(ts_poly *poly){
128
129     if (poly->grafted_vtx!=NULL){
130         poly->grafted_vtx->grafted_poly=NULL;
131     }
132     vtx_list_free(poly->vlist);
133     bond_list_free(poly->blist);
134     free(poly);
135
136     return TS_SUCCESS;
137 }
138
139 ts_bool poly_list_free(ts_poly_list *poly_list){
140     ts_uint i;
141
142     for(i=0;i<poly_list->n;i++){
143         poly_free(poly_list->poly[i]);
144     }
145     free(poly_list->poly);
146     free(poly_list);
147     
148     return TS_SUCCESS;
149 }