Trisurf Monte Carlo simulator
Samo Penic
2014-03-05 719c9febac2eaff9483fda487b57684afbb59bb2
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();
23     poly->grafted_vtx = grafted_vtx;
24     grafted_vtx->grafted_poly = poly;
25
26     ts_uint i;
8db569 27     for(i=0;i<n-1;i++){
1d5dff 28         vtx_add_cneighbour(poly->blist, poly->vlist->vtx[i], poly->vlist->vtx[i+1]);
8db569 29         vtx_add_neighbour(poly->vlist->vtx[i+1], poly->vlist->vtx[i]);
1d5dff 30     }
M 31
48bb92 32     for(i=0;i<poly->blist->n;i++){
M 33     poly->blist->bond[i]->bond_length=sqrt(vtx_distance_sq(poly->blist->bond[i]->vtx1,poly->blist->bond[i]->vtx2));
fedf2b 34     bond_energy(poly->blist->bond[i],poly);
48bb92 35     }
M 36
1d5dff 37     return poly;
M 38 }
39
40
8db569 41 ts_poly_list *init_poly_list(ts_uint n_poly, ts_uint n_mono, ts_vertex_list *vlist){
1d5dff 42     ts_poly_list *poly_list=(ts_poly_list *)calloc(1,sizeof(ts_poly_list));
8db569 43     poly_list->poly    = (ts_poly **)calloc(n_poly,sizeof(ts_poly *));
40aa5b 44     ts_uint i=0,j=0, idx;
8db569 45     ts_uint gvtxi;
M 46     ts_double xnorm,ynorm,znorm,normlength;
1d5dff 47
M 48     if (n_poly > vlist->n){fatal("Number of polymers larger then numbero f vertices on a vesicle.",310);}
49     
50     while(i<n_poly){
8db569 51         gvtxi = rand() % vlist->n;
1d5dff 52         if (vlist->vtx[gvtxi]->grafted_poly == NULL){
8db569 53         poly_list->poly[i] = init_poly(n_mono, vlist->vtx[gvtxi]);
1d5dff 54         i++;
M 55         }
56     }
57     
58     poly_list->n = n_poly;
59
8db569 60 /* Make straight poylmers normal to membrane. Dist. between poly vertices put to 1*/
M 61     for (i=0;i<poly_list->n;i++){
62
63         xnorm=0.0;
64         ynorm=0.0;
65         znorm=0.0;
66         for (j=0;j<poly_list->poly[i]->grafted_vtx->tristar_no;j++){
40aa5b 67             xnorm-=poly_list->poly[i]->grafted_vtx->tristar[j]->xnorm;
SP 68             ynorm-=poly_list->poly[i]->grafted_vtx->tristar[j]->ynorm;
69             znorm-=poly_list->poly[i]->grafted_vtx->tristar[j]->znorm;    
8db569 70         }
M 71         normlength=sqrt(xnorm*xnorm+ynorm*ynorm+znorm*znorm);
72         xnorm=xnorm/normlength;
73         ynorm=ynorm/normlength;
74         znorm=znorm/normlength;
75
76         for (j=0;j<poly_list->poly[i]->vlist->n;j++){
77             poly_list->poly[i]->vlist->vtx[j]->x = poly_list->poly[i]->grafted_vtx->x + xnorm*(ts_double)(j+1);
78             poly_list->poly[i]->vlist->vtx[j]->y = poly_list->poly[i]->grafted_vtx->y + ynorm*(ts_double)(j+1);
79             poly_list->poly[i]->vlist->vtx[j]->z = poly_list->poly[i]->grafted_vtx->z + znorm*(ts_double)(j+1);
80         }
81     }
82
40aa5b 83         //index correction for polymeres. Important, since each vtx has to have unique id
SP 84     idx=vlist->n;
85     for(i=0;i<n_poly;i++){
86         for(j=0;j<n_mono;j++,idx++){
87
88             poly_list->poly[i]->vlist->vtx[j]->idx=idx;
89
90         }
91     }
92
93
1d5dff 94     return poly_list;
M 95 }
96
8db569 97
M 98 ts_bool poly_free(ts_poly *poly){
99
100     if (poly->grafted_vtx!=NULL){
101         poly->grafted_vtx->grafted_poly=NULL;
102     }
103     vtx_list_free(poly->vlist);
104     bond_list_free(poly->blist);
105     free(poly);
106
107     return TS_SUCCESS;
108 }
109
110 ts_bool poly_list_free(ts_poly_list *poly_list){
111     ts_uint i;
112
113     for(i=0;i<poly_list->n;i++){
114         poly_free(poly_list->poly[i]);
115     }
116     free(poly_list->poly);
117     free(poly_list);
118     
119     return TS_SUCCESS;
120 }