Trisurf Monte Carlo simulator
Samo Penic
2016-05-18 f4d6ca0fa0915bd12f42fd79c57f3d0aabff50f0
commit | author | age
7f6076 1 /* vim: set ts=4 sts=4 sw=4 noet : */
d7639a 2 #include<stdlib.h>
SP 3 #include "general.h"
4 #include "energy.h"
5 #include "vertex.h"
6 #include<math.h>
7 #include<stdio.h>
a00f10 8
SP 9
10 /** @brief Wrapper that calculates energy of every vertex in vesicle
11  *  
12  *  Function calculated energy of every vertex in vesicle. It can be used in
13  *  initialization procedure or in recalculation of the energy after non-MCsweep *  operations. However, when random move of vertex or flip of random bond occur *  call to this function is not necessary nor recommended. 
14  *  @param *vesicle is a pointer to vesicle.
15  *  @returns TS_SUCCESS on success.
16 */
d7639a 17 ts_bool mean_curvature_and_energy(ts_vesicle *vesicle){
SP 18
f74313 19     ts_uint i;
d7639a 20     
f74313 21     ts_vertex_list *vlist=vesicle->vlist;
SP 22     ts_vertex **vtx=vlist->vtx;
d7639a 23
SP 24     for(i=0;i<vlist->n;i++){
f74313 25         energy_vertex(vtx[i]);
b01cc1 26         
d7639a 27     }
SP 28
29     return TS_SUCCESS;
30 }
31
a00f10 32 /** @brief Calculate energy of a bond (in models where energy is bond related)
SP 33  *
34  *  This function is experimental and currently only used in polymeres calculation (PEGs or polymeres inside the vesicle).
35  *
36  *  @param *bond is a pointer to a bond between two vertices in polymere
37  *  @param *poly is a pointer to polymere in which we calculate te energy of the bond
38  *  @returns TS_SUCCESS on successful calculation
39 */
fedf2b 40 inline ts_bool bond_energy(ts_bond *bond,ts_poly *poly){
304510 41 //TODO: This value to be changed and implemented in data structure:
M 42     ts_double d_relaxed=1.0;
43     bond->energy=poly->k*pow(bond->bond_length-d_relaxed,2);
fedf2b 44     return TS_SUCCESS;
M 45 };
46
a00f10 47 /** @brief Calculation of energy of the vertex
SP 48  *  
49  *  Main function that calculates energy of the vertex \f$i\f$. Nearest neighbors (NN) must be ordered in counterclockwise direction for this function to work.
50  *  Firstly NNs that form two neighboring triangles are found (\f$j_m\f$, \f$j_p\f$ and common \f$j\f$). Later, the scalar product of vectors \f$x_1=(\mathbf{i}-\mathbf{j_p})\cdot (\mathbf{i}-\mathbf{j_p})(\mathbf{i}-\mathbf{j_p})\f$, \f$x_2=(\mathbf{j}-\mathbf{j_p})\cdot  (\mathbf{j}-\mathbf{j_p})\f$  and \f$x_3=(\mathbf{j}-\mathbf{j_p})\cdot (\mathbf{i}-\mathbf{j_p})\f$  are calculated. From these three vectors the \f$c_{tp}=\frac{1}{\tan(\varphi_p)}\f$ is calculated, where \f$\varphi_p\f$ is the inner angle at vertex \f$j_p\f$. The procedure is repeated for \f$j_m\f$ instead of \f$j_p\f$ resulting in \f$c_{tn}\f$.
51  *  
854cb6 52 \begin{tikzpicture}{
a00f10 53 \coordinate[label=below:$i$] (i) at (2,0);
SP 54 \coordinate[label=left:$j_m$] (jm) at (0,3.7);
55 \coordinate[label=above:$j$] (j) at (2.5,6.4);
56 \coordinate[label=right:$j_p$] (jp) at (4,2.7);
d7639a 57
a00f10 58 \draw (i) -- (jm) -- (j) -- (jp) -- (i) -- (j);
SP 59
60 \begin{scope}
61 \path[clip] (jm)--(i)--(j);
62 \draw (jm) circle (0.8);
63 \node[right] at (jm) {$\varphi_m$};
64 \end{scope}
65
66 \begin{scope}
67 \path[clip] (jp)--(i)--(j);
68 \draw (jp) circle (0.8);
69 \node[left] at (jp) {$\varphi_p$};
70 \end{scope}
71
72 %%vertices
73 \draw [fill=gray] (i) circle (0.1);
74 \draw [fill=white] (j) circle (0.1);
75 \draw [fill=white] (jp) circle (0.1);
76 \draw [fill=white] (jm) circle (0.1);
77 %\node[draw,circle,fill=white] at (i) {};
854cb6 78 \end{tikzpicture}
a00f10 79
SP 80  * The curvature is then calculated as \f$\mathbf{h}=\frac{1}{2}\Sigma_{k=0}^{\mathrm{neigh\_no}} c_{tp}^{(k)}+c_{tm}^{(k)} (\mathbf{j_k}-\mathbf{i})\f$, where \f$c_{tp}^{(k)}+c_{tm}^k=2\sigma^{(k)}\f$ (length in dual lattice?) and the previous equation can be written as \f$\mathbf{h}=\Sigma_{k=0}^{\mathrm{neigh\_no}}\sigma^{(k)}\cdot(\mathbf{j}-\mathbf{i})\f$ (See Kroll, p. 384, eq 70).
81  *
82  * From the curvature the enery is calculated by equation \f$E=\frac{1}{2}\mathbf{h}\cdot\mathbf{h}\f$.
83  * @param *vtx is a pointer to vertex at which we want to calculate the energy
84  * @returns TS_SUCCESS on successful calculation.
85 */
d7639a 86 inline ts_bool energy_vertex(ts_vertex *vtx){
SP 87     ts_uint jj;
88     ts_uint jjp,jjm;
89     ts_vertex *j,*jp, *jm;
90     ts_triangle *jt;
a63f17 91     ts_double s=0.0,xh=0.0,yh=0.0,zh=0.0,txn=0.0,tyn=0.0,tzn=0.0;
d7639a 92     ts_double x1,x2,x3,ctp,ctm,tot,xlen;
SP 93     ts_double h,ht;
8f6a69 94     for(jj=1; jj<=vtx->neigh_no;jj++){
d7639a 95         jjp=jj+1;
8f6a69 96         if(jjp>vtx->neigh_no) jjp=1;
d7639a 97         jjm=jj-1;
8f6a69 98         if(jjm<1) jjm=vtx->neigh_no;
SP 99         j=vtx->neigh[jj-1];
100         jp=vtx->neigh[jjp-1];
101         jm=vtx->neigh[jjm-1];
102         jt=vtx->tristar[jj-1];
f74313 103         x1=vtx_distance_sq(vtx,jp); //shouldn't be zero!
SP 104         x2=vtx_distance_sq(j,jp); // shouldn't be zero!
8f6a69 105         x3=(j->x-jp->x)*(vtx->x-jp->x)+
SP 106            (j->y-jp->y)*(vtx->y-jp->y)+
107            (j->z-jp->z)*(vtx->z-jp->z);
d7639a 108         
SP 109 #ifdef TS_DOUBLE_DOUBLE
110         ctp=x3/sqrt(x1*x2-x3*x3);
111 #endif
112 #ifdef TS_DOUBLE_FLOAT
113         ctp=x3/sqrtf(x1*x2-x3*x3);
114 #endif
115 #ifdef TS_DOUBLE_LONGDOUBLE
116         ctp=x3/sqrtl(x1*x2-x3*x3);
117 #endif
f74313 118         x1=vtx_distance_sq(vtx,jm);
SP 119         x2=vtx_distance_sq(j,jm);
8f6a69 120         x3=(j->x-jm->x)*(vtx->x-jm->x)+
SP 121            (j->y-jm->y)*(vtx->y-jm->y)+
122            (j->z-jm->z)*(vtx->z-jm->z);
d7639a 123 #ifdef TS_DOUBLE_DOUBLE
SP 124         ctm=x3/sqrt(x1*x2-x3*x3);
125 #endif
126 #ifdef TS_DOUBLE_FLOAT
127         ctm=x3/sqrtf(x1*x2-x3*x3);
128 #endif
129 #ifdef TS_DOUBLE_LONGDOUBLE
130         ctm=x3/sqrtl(x1*x2-x3*x3);
131 #endif
132         tot=ctp+ctm;
133         tot=0.5*tot;
a63f17 134
f74313 135         xlen=vtx_distance_sq(j,vtx);
a63f17 136 /*
d7639a 137 #ifdef  TS_DOUBLE_DOUBLE 
8f6a69 138         vtx->bond[jj-1]->bond_length=sqrt(xlen); 
d7639a 139 #endif
SP 140 #ifdef  TS_DOUBLE_FLOAT
8f6a69 141         vtx->bond[jj-1]->bond_length=sqrtf(xlen); 
d7639a 142 #endif
SP 143 #ifdef  TS_DOUBLE_LONGDOUBLE 
8f6a69 144         vtx->bond[jj-1]->bond_length=sqrtl(xlen); 
d7639a 145 #endif
SP 146
8f6a69 147         vtx->bond[jj-1]->bond_length_dual=tot*vtx->bond[jj-1]->bond_length;
a63f17 148 */
d7639a 149         s+=tot*xlen;
8f6a69 150         xh+=tot*(j->x - vtx->x);
SP 151         yh+=tot*(j->y - vtx->y);
152         zh+=tot*(j->z - vtx->z);
41a035 153         txn+=jt->xnorm;
SP 154         tyn+=jt->ynorm;
155         tzn+=jt->znorm;
d7639a 156     }
SP 157     
158     h=xh*xh+yh*yh+zh*zh;
159     ht=txn*xh+tyn*yh + tzn*zh;
160     s=s/4.0; 
161 #ifdef TS_DOUBLE_DOUBLE
162     if(ht>=0.0) {
8f6a69 163         vtx->curvature=sqrt(h);
d7639a 164     } else {
8f6a69 165         vtx->curvature=-sqrt(h);
d7639a 166     }
SP 167 #endif
168 #ifdef TS_DOUBLE_FLOAT
169     if(ht>=0.0) {
8f6a69 170         vtx->curvature=sqrtf(h);
d7639a 171     } else {
8f6a69 172         vtx->curvature=-sqrtf(h);
d7639a 173     }
SP 174 #endif
175 #ifdef TS_DOUBLE_LONGDOUBLE
176     if(ht>=0.0) {
8f6a69 177         vtx->curvature=sqrtl(h);
d7639a 178     } else {
8f6a69 179         vtx->curvature=-sqrtl(h);
d7639a 180     }
SP 181 #endif
dac2e5 182 // c is forced curvature energy for each vertex. Should be set to zero for
8f6a69 183 // normal circumstances.
SP 184     vtx->energy=0.5*s*(vtx->curvature/s-vtx->c)*(vtx->curvature/s-vtx->c);
d7639a 185
SP 186     return TS_SUCCESS;
187 }