Trisurf Monte Carlo simulator
Miha
2014-02-06 fedf2b217113800a15f367e111e2dbcad4a3c92c
commit | author | age
aec47d 1 #include<stdlib.h>
SP 2 #include<math.h>
3 #include "general.h"
4 #include "vertex.h"
5 #include "bond.h"
6 #include "triangle.h"
7 #include "vesicle.h"
8 #include "energy.h"
9 #include "timestep.h"
10 #include "cell.h"
11 //#include "io.h"
12 #include<stdio.h>
13 #include "vertexmove.h"
1ad6d1 14 #include <string.h>
aec47d 15
fedf2b 16 ts_bool single_verticle_timestep(ts_vesicle *vesicle,ts_vertex *vtx,ts_double *rn){
aec47d 17     ts_uint i;
SP 18     ts_double dist;
19     ts_bool retval; 
20     ts_uint cellidx; 
21     ts_double delta_energy,oenergy;
ed31fe 22     ts_double costheta,sintheta,phi,r;
1ad6d1 23     //This will hold all the information of vtx and its neighbours
dcd350 24     ts_vertex backupvtx[20];
SP 25     memcpy((void *)&backupvtx[0],(void *)vtx,sizeof(ts_vertex));
a63f17 26
SP 27     //Some stupid tests for debugging cell occupation!
28 /*         cellidx=vertex_self_avoidance(vesicle, vtx);
29     if(vesicle->clist->cell[cellidx]==vtx->cell){
30         fprintf(stderr,"Idx match!\n");
31     } else {
32         fprintf(stderr,"***** Idx don't match!\n");
33         fatal("ENding.",1);
34     }
35 */
36
352fad 37         //temporarly moving the vertex
672ae4 38 //    vtx->x=vtx->x+vesicle->stepsize*(2.0*rn[0]-1.0);
SP 39 //        vtx->y=vtx->y+vesicle->stepsize*(2.0*rn[1]-1.0);
40 //        vtx->z=vtx->z+vesicle->stepsize*(2.0*rn[2]-1.0);
41
ed31fe 42     //random move in a sphere with radius stepsize:
M 43     r=vesicle->stepsize*rn[0];
44     phi=rn[1]*2*M_PI;
45     costheta=2*rn[2]-1;
46     sintheta=sqrt(1-pow(costheta,2));
672ae4 47     vtx->x=vtx->x+r*sintheta*cos(phi);
SP 48     vtx->y=vtx->y+r*sintheta*sin(phi);
49     vtx->z=vtx->z+r*costheta;
50
51
a63f17 52         //distance with neighbours check
8f6a69 53     for(i=0;i<vtx->neigh_no;i++){
352fad 54         dist=vtx_distance_sq(vtx,vtx->neigh[i]);
8f6a69 55         if(dist<1.0 || dist>vesicle->dmax) {
dcd350 56         vtx=memcpy((void *)vtx,(void *)&backupvtx[0],sizeof(ts_vertex));
8f6a69 57         return TS_FAIL;
SP 58         }
aec47d 59     }
SP 60     //self avoidance check with distant vertices
352fad 61      cellidx=vertex_self_avoidance(vesicle, vtx);
aec47d 62     //check occupation number
a63f17 63      retval=cell_occupation_number_and_internal_proximity(vesicle->clist,cellidx,vtx);
SP 64     
aec47d 65     if(retval==TS_FAIL){
dcd350 66         vtx=memcpy((void *)vtx,(void *)&backupvtx[0],sizeof(ts_vertex));
aec47d 67         return TS_FAIL;
SP 68     } 
1ad6d1 69    
SP 70  
352fad 71     //if all the tests are successful, then energy for vtx and neighbours is calculated
1ad6d1 72     for(i=0;i<vtx->neigh_no;i++){
dcd350 73     memcpy((void *)&backupvtx[i+1],(void *)vtx->neigh[i],sizeof(ts_vertex));
1ad6d1 74     }
aec47d 75
a63f17 76
SP 77
aec47d 78     delta_energy=0;
SP 79     //update the normals of triangles that share bead i.
8f6a69 80     for(i=0;i<vtx->tristar_no;i++) triangle_normal_vector(vtx->tristar[i]);
a63f17 81     oenergy=vtx->energy;
aec47d 82     energy_vertex(vtx);
a63f17 83     delta_energy=vtx->xk*(vtx->energy - oenergy);
aec47d 84     //the same is done for neighbouring vertices
8f6a69 85     for(i=0;i<vtx->neigh_no;i++){
SP 86         oenergy=vtx->neigh[i]->energy;
87         energy_vertex(vtx->neigh[i]);
88         delta_energy+=vtx->neigh[i]->xk*(vtx->neigh[i]->energy-oenergy);
aec47d 89     }
fedf2b 90
M 91     if(vtx->grafted_poly!=NULL){
92         delta_energy+=
93             (pow(sqrt(vtx_distance_sq(vtx, vtx->grafted_poly->vlist->vtx[0])-1),2)-
94             pow(sqrt(vtx_distance_sq(&backupvtx[0], vtx->grafted_poly->vlist->vtx[0])-1),2)) *vtx->grafted_poly->k;
95         
96     
97     }
98
314f2d 99 //   fprintf(stderr, "DE=%f\n",delta_energy);
aec47d 100     //MONTE CARLOOOOOOOO
SP 101     if(delta_energy>=0){
102 #ifdef TS_DOUBLE_DOUBLE
103         if(exp(-delta_energy)< drand48() )
104 #endif
105 #ifdef TS_DOUBLE_FLOAT
106         if(expf(-delta_energy)< (ts_float)drand48())
107 #endif
108 #ifdef TS_DOUBLE_LONGDOUBLE
109         if(expl(-delta_energy)< (ts_ldouble)drand48())
110 #endif
111     {
112     //not accepted, reverting changes
dcd350 113     vtx=memcpy((void *)vtx,(void *)&backupvtx[0],sizeof(ts_vertex));
1ad6d1 114     for(i=0;i<vtx->neigh_no;i++){
a63f17 115         vtx->neigh[i]=memcpy((void *)vtx->neigh[i],(void *)&backupvtx[i+1],sizeof(ts_vertex));
1ad6d1 116     }
SP 117     
aec47d 118     //update the normals of triangles that share bead i.
dcd350 119    for(i=0;i<vtx->tristar_no;i++) triangle_normal_vector(vtx->tristar[i]);
1ad6d1 120
aec47d 121     return TS_FAIL; 
SP 122     }
123 }
a63f17 124         
SP 125 //    oldcellidx=vertex_self_avoidance(vesicle, &backupvtx[0]);
126     if(vtx->cell!=vesicle->clist->cell[cellidx]){
127         retval=cell_add_vertex(vesicle->clist->cell[cellidx],vtx);
128 //        if(retval==TS_SUCCESS) cell_remove_vertex(vesicle->clist->cell[oldcellidx],vtx);
129         if(retval==TS_SUCCESS) cell_remove_vertex(backupvtx[0].cell,vtx);
130         
131     }
132 //    if(oldcellidx);
aec47d 133     //END MONTE CARLOOOOOOO
SP 134     return TS_SUCCESS;
135 }
136
fedf2b 137
M 138 ts_bool single_poly_vertex_move(ts_vesicle *vesicle,ts_poly *poly,ts_vertex *vtx,ts_double *rn){
139     ts_uint i;
140     ts_bool retval; 
141     ts_uint cellidx; 
142     ts_double delta_energy;
143     ts_double costheta,sintheta,phi,r;
144     //This will hold all the information of vtx and its neighbours
145     ts_vertex backupvtx;
146     ts_bond backupbond[2];
147     memcpy((void *)&backupvtx,(void *)vtx,sizeof(ts_vertex));
148
149     //random move in a sphere with radius stepsize:
150     r=vesicle->stepsize*rn[0];
151     phi=rn[1]*2*M_PI;
152     costheta=2*rn[2]-1;
153     sintheta=sqrt(1-pow(costheta,2));
154     vtx->x=vtx->x+r*sintheta*cos(phi);
155     vtx->y=vtx->y+r*sintheta*sin(phi);
156     vtx->z=vtx->z+r*costheta;
157
158
159     //distance with neighbours check
160 //    for(i=0;i<vtx->neigh_no;i++){
161 //    dist=vtx_distance_sq(vtx,vtx->neigh[i]);
162 //    if(dist<1.0 || dist>vesicle->dmax) {
163 //    vtx=memcpy((void *)vtx,(void *)&backupvtx[0],sizeof(ts_vertex));
164 //    return TS_FAIL;
165 //        }
166 //    }
167
168     //self avoidance check with distant vertices
169     cellidx=vertex_self_avoidance(vesicle, vtx);
170     //check occupation number
171     retval=cell_occupation_number_and_internal_proximity(vesicle->clist,cellidx,vtx);
172     
173     if(retval==TS_FAIL){
174         vtx=memcpy((void *)vtx,(void *)&backupvtx,sizeof(ts_vertex));
175         return TS_FAIL;
176     } 
177
178
179     //if all the tests are successful, then energy for vtx and neighbours is calculated
180     delta_energy=0;
181     for(i=0;i<vtx->bond_no;i++){
182         memcpy((void *)&backupbond[i],(void *)vtx->bond[i],sizeof(ts_bond));
183
184         vtx->bond[i]->bond_length=sqrt(vtx_distance_sq(vtx->bond[i]->vtx1,vtx->bond[i]->vtx2));
185         bond_energy(vtx->bond[i],poly);
186         delta_energy+= vtx->bond[i]->energy - backupbond[i].energy;
187     }
188
189     if(vtx==poly->vlist->vtx[0]){
190         delta_energy+=
191             (pow(sqrt(vtx_distance_sq(vtx, poly->grafted_vtx)-1),2)-
192             pow(sqrt(vtx_distance_sq(&backupvtx, poly->grafted_vtx)-1),2)) *poly->k;
193         
194     }
195
196
197     if(delta_energy>=0){
198 #ifdef TS_DOUBLE_DOUBLE
199         if(exp(-delta_energy)< drand48() )
200 #endif
201 #ifdef TS_DOUBLE_FLOAT
202         if(expf(-delta_energy)< (ts_float)drand48())
203 #endif
204 #ifdef TS_DOUBLE_LONGDOUBLE
205         if(expl(-delta_energy)< (ts_ldouble)drand48())
206 #endif
207         {
208     //not accepted, reverting changes
209     vtx=memcpy((void *)vtx,(void *)&backupvtx,sizeof(ts_vertex));
210     for(i=0;i<vtx->bond_no;i++){
211     vtx->bond[i]=memcpy((void *)vtx->bond[i],(void *)&backupbond[i],sizeof(ts_bond));
212     }
213
214     return TS_FAIL; 
215     }
216     }
217         
218 //    oldcellidx=vertex_self_avoidance(vesicle, &backupvtx[0]);
219     if(vtx->cell!=vesicle->clist->cell[cellidx]){
220         retval=cell_add_vertex(vesicle->clist->cell[cellidx],vtx);
221 //        if(retval==TS_SUCCESS) cell_remove_vertex(vesicle->clist->cell[oldcellidx],vtx);
222         if(retval==TS_SUCCESS) cell_remove_vertex(backupvtx.cell,vtx);    
223     }
224 //    if(oldcellidx);
225     //END MONTE CARLOOOOOOO
226     return TS_SUCCESS;
227 }