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