Trisurf Monte Carlo simulator
Samo Penic
2014-06-13 0af0cf6ac748ba9c135c9ca0a23cdd6cbf50caf1
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"
9166cb 12 #include "io.h"
aec47d 13 #include<stdio.h>
SP 14 #include "vertexmove.h"
1ad6d1 15 #include <string.h>
43c042 16 #include "constvol.h"
aec47d 17
fedf2b 18 ts_bool single_verticle_timestep(ts_vesicle *vesicle,ts_vertex *vtx,ts_double *rn){
aec47d 19     ts_uint i;
SP 20     ts_double dist;
21     ts_bool retval; 
22     ts_uint cellidx; 
fbcbdf 23     ts_double delta_energy, delta_energy_cv,oenergy,dvol=0.0;
ed31fe 24     ts_double costheta,sintheta,phi,r;
1ad6d1 25     //This will hold all the information of vtx and its neighbours
958e0e 26     ts_vertex backupvtx[20], *constvol_vtx_moved=NULL, *constvol_vtx_backup=NULL;
dcd350 27     memcpy((void *)&backupvtx[0],(void *)vtx,sizeof(ts_vertex));
a63f17 28
SP 29     //Some stupid tests for debugging cell occupation!
30 /*         cellidx=vertex_self_avoidance(vesicle, vtx);
31     if(vesicle->clist->cell[cellidx]==vtx->cell){
32         fprintf(stderr,"Idx match!\n");
33     } else {
34         fprintf(stderr,"***** Idx don't match!\n");
35         fatal("ENding.",1);
36     }
37 */
38
352fad 39         //temporarly moving the vertex
672ae4 40 //    vtx->x=vtx->x+vesicle->stepsize*(2.0*rn[0]-1.0);
SP 41 //        vtx->y=vtx->y+vesicle->stepsize*(2.0*rn[1]-1.0);
42 //        vtx->z=vtx->z+vesicle->stepsize*(2.0*rn[2]-1.0);
43
ed31fe 44     //random move in a sphere with radius stepsize:
M 45     r=vesicle->stepsize*rn[0];
46     phi=rn[1]*2*M_PI;
47     costheta=2*rn[2]-1;
48     sintheta=sqrt(1-pow(costheta,2));
672ae4 49     vtx->x=vtx->x+r*sintheta*cos(phi);
SP 50     vtx->y=vtx->y+r*sintheta*sin(phi);
51     vtx->z=vtx->z+r*costheta;
52
53
a63f17 54         //distance with neighbours check
8f6a69 55     for(i=0;i<vtx->neigh_no;i++){
352fad 56         dist=vtx_distance_sq(vtx,vtx->neigh[i]);
8f6a69 57         if(dist<1.0 || dist>vesicle->dmax) {
dcd350 58         vtx=memcpy((void *)vtx,(void *)&backupvtx[0],sizeof(ts_vertex));
8f6a69 59         return TS_FAIL;
SP 60         }
aec47d 61     }
304510 62
M 63 // Distance with grafted poly-vertex check:    
64     if(vtx->grafted_poly!=NULL){
65         dist=vtx_distance_sq(vtx,vtx->grafted_poly->vlist->vtx[0]);
66         if(dist<1.0 || dist>vesicle->dmax) {
67         vtx=memcpy((void *)vtx,(void *)&backupvtx[0],sizeof(ts_vertex));
68         return TS_FAIL;
69         }
70     }
71
fe24d2 72 // TODO: Maybe faster if checks only nucleus-neighboring cells
M 73 // Nucleus penetration check:
74     if (vtx->x*vtx->x + vtx->y*vtx->y + vtx->z*vtx->z < vesicle->R_nucleus){
75         vtx=memcpy((void *)vtx,(void *)&backupvtx[0],sizeof(ts_vertex));
76         return TS_FAIL;
77     }
78
79 //self avoidance check with distant vertices
80     cellidx=vertex_self_avoidance(vesicle, vtx);
81     //check occupation number
82     retval=cell_occupation_number_and_internal_proximity(vesicle->clist,cellidx,vtx);
83
aec47d 84     if(retval==TS_FAIL){
dcd350 85         vtx=memcpy((void *)vtx,(void *)&backupvtx[0],sizeof(ts_vertex));
aec47d 86         return TS_FAIL;
SP 87     } 
1ad6d1 88    
SP 89  
352fad 90     //if all the tests are successful, then energy for vtx and neighbours is calculated
1ad6d1 91     for(i=0;i<vtx->neigh_no;i++){
dcd350 92     memcpy((void *)&backupvtx[i+1],(void *)vtx->neigh[i],sizeof(ts_vertex));
1ad6d1 93     }
aec47d 94
fbcbdf 95     if(vesicle->pswitch == 1 || vesicle->tape->constvolswitch==1){
414b8a 96         for(i=0;i<vtx->tristar_no;i++) dvol-=vtx->tristar[i]->volume;
M 97     };
a63f17 98
aec47d 99     delta_energy=0;
fe5069 100     
SP 101 //    vesicle_volume(vesicle);
102 //    fprintf(stderr,"Volume in the beginning=%1.16e\n", vesicle->volume);
43c042 103
aec47d 104     //update the normals of triangles that share bead i.
8f6a69 105     for(i=0;i<vtx->tristar_no;i++) triangle_normal_vector(vtx->tristar[i]);
a63f17 106     oenergy=vtx->energy;
aec47d 107     energy_vertex(vtx);
a63f17 108     delta_energy=vtx->xk*(vtx->energy - oenergy);
aec47d 109     //the same is done for neighbouring vertices
8f6a69 110     for(i=0;i<vtx->neigh_no;i++){
SP 111         oenergy=vtx->neigh[i]->energy;
112         energy_vertex(vtx->neigh[i]);
113         delta_energy+=vtx->neigh[i]->xk*(vtx->neigh[i]->energy-oenergy);
aec47d 114     }
414b8a 115
fbcbdf 116     if(vesicle->pswitch == 1 || vesicle->tape->constvolswitch == 1){
414b8a 117         for(i=0;i<vtx->tristar_no;i++) dvol+=vtx->tristar[i]->volume;
fbcbdf 118         if(vesicle->pswitch==1) delta_energy-=vesicle->pressure*dvol;
414b8a 119     };
43c042 120
fe5069 121 //    vesicle_volume(vesicle);
SP 122 //    fprintf(stderr,"Volume before=%1.16e\n", vesicle->volume);
fbcbdf 123    if(vesicle->tape->constvolswitch == 1){
fe5069 124         retval=constvolume(vesicle, vtx, -dvol, &delta_energy_cv, &constvol_vtx_moved,&constvol_vtx_backup);
fbcbdf 125         if(retval==TS_FAIL){ // if we couldn't move the vertex to assure constant volume
SP 126             vtx=memcpy((void *)vtx,(void *)&backupvtx[0],sizeof(ts_vertex));
127             for(i=0;i<vtx->neigh_no;i++){
128                 vtx->neigh[i]=memcpy((void *)vtx->neigh[i],(void *)&backupvtx[i+1],sizeof(ts_vertex));
129             }
130             for(i=0;i<vtx->tristar_no;i++) triangle_normal_vector(vtx->tristar[i]); 
fe5069 131  //           fprintf(stderr,"fajlam!\n");
fbcbdf 132             return TS_FAIL;
SP 133         }
fe5069 134 //    vesicle_volume(vesicle);
SP 135 //    fprintf(stderr,"Volume after=%1.16e\n", vesicle->volume);
136 //    fprintf(stderr,"Volume after-dvol=%1.16e\n", vesicle->volume-dvol);
137 //    fprintf(stderr,"Denergy before=%e\n",delta_energy);
138     
fbcbdf 139     delta_energy+=delta_energy_cv;
fe5069 140 //    fprintf(stderr,"Denergy after=%e\n",delta_energy);
fbcbdf 141     }
304510 142 /* No poly-bond energy for now!
fedf2b 143     if(vtx->grafted_poly!=NULL){
M 144         delta_energy+=
145             (pow(sqrt(vtx_distance_sq(vtx, vtx->grafted_poly->vlist->vtx[0])-1),2)-
146             pow(sqrt(vtx_distance_sq(&backupvtx[0], vtx->grafted_poly->vlist->vtx[0])-1),2)) *vtx->grafted_poly->k;
147     }
304510 148 */
314f2d 149 //   fprintf(stderr, "DE=%f\n",delta_energy);
aec47d 150     //MONTE CARLOOOOOOOO
SP 151     if(delta_energy>=0){
152 #ifdef TS_DOUBLE_DOUBLE
3de289 153         if(exp(-delta_energy)< drand48())
aec47d 154 #endif
SP 155 #ifdef TS_DOUBLE_FLOAT
156         if(expf(-delta_energy)< (ts_float)drand48())
157 #endif
158 #ifdef TS_DOUBLE_LONGDOUBLE
159         if(expl(-delta_energy)< (ts_ldouble)drand48())
160 #endif
161     {
162     //not accepted, reverting changes
fbcbdf 163   //  fprintf(stderr,"MC failed\n");
dcd350 164     vtx=memcpy((void *)vtx,(void *)&backupvtx[0],sizeof(ts_vertex));
1ad6d1 165     for(i=0;i<vtx->neigh_no;i++){
a63f17 166         vtx->neigh[i]=memcpy((void *)vtx->neigh[i],(void *)&backupvtx[i+1],sizeof(ts_vertex));
1ad6d1 167     }
SP 168     
aec47d 169     //update the normals of triangles that share bead i.
dcd350 170    for(i=0;i<vtx->tristar_no;i++) triangle_normal_vector(vtx->tristar[i]);
1ad6d1 171
fe5069 172 //    fprintf(stderr, "before vtx(x,y,z)=%e,%e,%e\n",constvol_vtx_moved->x, constvol_vtx_moved->y, constvol_vtx_moved->z);
43c042 173     if(vesicle->tape->constvolswitch == 1){
958e0e 174         constvolumerestore(constvol_vtx_moved,constvol_vtx_backup);
43c042 175     }
fe5069 176 //    fprintf(stderr, "after vtx(x,y,z)=%e,%e,%e\n",constvol_vtx_moved->x, constvol_vtx_moved->y, constvol_vtx_moved->z);
dd5aca 177 //    vesicle_volume(vesicle);
SP 178 //    fprintf(stderr,"Volume after fail=%1.16e\n", vesicle->volume);
aec47d 179     return TS_FAIL; 
SP 180     }
181 }
2b14da 182     //accepted    
fbcbdf 183  //   fprintf(stderr,"MC accepted\n");
a63f17 184 //    oldcellidx=vertex_self_avoidance(vesicle, &backupvtx[0]);
SP 185     if(vtx->cell!=vesicle->clist->cell[cellidx]){
186         retval=cell_add_vertex(vesicle->clist->cell[cellidx],vtx);
187 //        if(retval==TS_SUCCESS) cell_remove_vertex(vesicle->clist->cell[oldcellidx],vtx);
188         if(retval==TS_SUCCESS) cell_remove_vertex(backupvtx[0].cell,vtx);
189         
190     }
2b14da 191
43c042 192     if(vesicle->tape->constvolswitch == 1){
fbcbdf 193         constvolumeaccept(vesicle,constvol_vtx_moved,constvol_vtx_backup);
43c042 194     }
a63f17 195 //    if(oldcellidx);
aec47d 196     //END MONTE CARLOOOOOOO
dd5aca 197 //    vesicle_volume(vesicle);
SP 198 //    fprintf(stderr,"Volume after success=%1.16e\n", vesicle->volume);
aec47d 199     return TS_SUCCESS;
SP 200 }
201
fedf2b 202
M 203 ts_bool single_poly_vertex_move(ts_vesicle *vesicle,ts_poly *poly,ts_vertex *vtx,ts_double *rn){
204     ts_uint i;
205     ts_bool retval; 
206     ts_uint cellidx; 
304510 207 //    ts_double delta_energy;
fedf2b 208     ts_double costheta,sintheta,phi,r;
304510 209     ts_double dist;
fedf2b 210     //This will hold all the information of vtx and its neighbours
M 211     ts_vertex backupvtx;
304510 212 //    ts_bond backupbond[2];
fedf2b 213     memcpy((void *)&backupvtx,(void *)vtx,sizeof(ts_vertex));
M 214
215     //random move in a sphere with radius stepsize:
216     r=vesicle->stepsize*rn[0];
217     phi=rn[1]*2*M_PI;
218     costheta=2*rn[2]-1;
219     sintheta=sqrt(1-pow(costheta,2));
220     vtx->x=vtx->x+r*sintheta*cos(phi);
221     vtx->y=vtx->y+r*sintheta*sin(phi);
222     vtx->z=vtx->z+r*costheta;
223
224
225     //distance with neighbours check
304510 226     for(i=0;i<vtx->neigh_no;i++){
M 227         dist=vtx_distance_sq(vtx,vtx->neigh[i]);
228         if(dist<1.0 || dist>vesicle->dmax) {
229             vtx=memcpy((void *)vtx,(void *)&backupvtx,sizeof(ts_vertex));
230             return TS_FAIL;
231         }
232     }
233
234 // Distance with grafted vesicle-vertex check:    
235     if(vtx==poly->vlist->vtx[0]){
236         dist=vtx_distance_sq(vtx,poly->grafted_vtx);
237         if(dist<1.0 || dist>vesicle->dmax) {
238         vtx=memcpy((void *)vtx,(void *)&backupvtx,sizeof(ts_vertex));
239         return TS_FAIL;
240         }
241     }
242
fedf2b 243
M 244     //self avoidance check with distant vertices
245     cellidx=vertex_self_avoidance(vesicle, vtx);
246     //check occupation number
247     retval=cell_occupation_number_and_internal_proximity(vesicle->clist,cellidx,vtx);
248     
249     if(retval==TS_FAIL){
250         vtx=memcpy((void *)vtx,(void *)&backupvtx,sizeof(ts_vertex));
251         return TS_FAIL;
252     } 
253
254
255     //if all the tests are successful, then energy for vtx and neighbours is calculated
304510 256 /* Energy ignored for now!
fedf2b 257     delta_energy=0;
M 258     for(i=0;i<vtx->bond_no;i++){
259         memcpy((void *)&backupbond[i],(void *)vtx->bond[i],sizeof(ts_bond));
260
261         vtx->bond[i]->bond_length=sqrt(vtx_distance_sq(vtx->bond[i]->vtx1,vtx->bond[i]->vtx2));
262         bond_energy(vtx->bond[i],poly);
263         delta_energy+= vtx->bond[i]->energy - backupbond[i].energy;
264     }
265
266     if(vtx==poly->vlist->vtx[0]){
267         delta_energy+=
268             (pow(sqrt(vtx_distance_sq(vtx, poly->grafted_vtx)-1),2)-
269             pow(sqrt(vtx_distance_sq(&backupvtx, poly->grafted_vtx)-1),2)) *poly->k;
270         
271     }
272
273
274     if(delta_energy>=0){
275 #ifdef TS_DOUBLE_DOUBLE
276         if(exp(-delta_energy)< drand48() )
277 #endif
278 #ifdef TS_DOUBLE_FLOAT
279         if(expf(-delta_energy)< (ts_float)drand48())
280 #endif
281 #ifdef TS_DOUBLE_LONGDOUBLE
282         if(expl(-delta_energy)< (ts_ldouble)drand48())
283 #endif
284         {
285     //not accepted, reverting changes
286     vtx=memcpy((void *)vtx,(void *)&backupvtx,sizeof(ts_vertex));
287     for(i=0;i<vtx->bond_no;i++){
288     vtx->bond[i]=memcpy((void *)vtx->bond[i],(void *)&backupbond[i],sizeof(ts_bond));
289     }
290
291     return TS_FAIL; 
292     }
293     }
304510 294 */
fedf2b 295         
M 296 //    oldcellidx=vertex_self_avoidance(vesicle, &backupvtx[0]);
297     if(vtx->cell!=vesicle->clist->cell[cellidx]){
298         retval=cell_add_vertex(vesicle->clist->cell[cellidx],vtx);
299 //        if(retval==TS_SUCCESS) cell_remove_vertex(vesicle->clist->cell[oldcellidx],vtx);
300         if(retval==TS_SUCCESS) cell_remove_vertex(backupvtx.cell,vtx);    
301     }
302 //    if(oldcellidx);
303     //END MONTE CARLOOOOOOO
304     return TS_SUCCESS;
305 }
58230a 306
M 307
308
309
310 ts_bool single_filament_vertex_move(ts_vesicle *vesicle,ts_poly *poly,ts_vertex *vtx,ts_double *rn){
311     ts_uint i;
312     ts_bool retval; 
313     ts_uint cellidx; 
b30f45 314     ts_double delta_energy;
58230a 315     ts_double costheta,sintheta,phi,r;
M 316     ts_double dist[2];
317     //This will hold all the information of vtx and its neighbours
b30f45 318     ts_vertex backupvtx,backupneigh[2];
58230a 319     ts_bond backupbond[2];
b30f45 320
M 321     //backup vertex:        
58230a 322     memcpy((void *)&backupvtx,(void *)vtx,sizeof(ts_vertex));
M 323
324     //random move in a sphere with radius stepsize:
325     r=vesicle->stepsize*rn[0];
326     phi=rn[1]*2*M_PI;
327     costheta=2*rn[2]-1;
328     sintheta=sqrt(1-pow(costheta,2));
329     vtx->x=vtx->x+r*sintheta*cos(phi);
330     vtx->y=vtx->y+r*sintheta*sin(phi);
331     vtx->z=vtx->z+r*costheta;
332
333
334     //distance with neighbours check
335     for(i=0;i<vtx->bond_no;i++){
336         dist[i]=vtx_distance_sq(vtx->bond[i]->vtx1,vtx->bond[i]->vtx2);
337         if(dist[i]<1.0 || dist[i]>vesicle->dmax) {
338             vtx=memcpy((void *)vtx,(void *)&backupvtx,sizeof(ts_vertex));
339             return TS_FAIL;
340         }
341     }
342
fe24d2 343 // TODO: Maybe faster if checks only nucleus-neighboring cells
M 344 // Nucleus penetration check:
345     if (vtx->x*vtx->x + vtx->y*vtx->y + vtx->z*vtx->z < vesicle->R_nucleus){
346         vtx=memcpy((void *)vtx,(void *)&backupvtx,sizeof(ts_vertex));
347         return TS_FAIL;
348     }
349
58230a 350
M 351     //self avoidance check with distant vertices
352     cellidx=vertex_self_avoidance(vesicle, vtx);
353     //check occupation number
354     retval=cell_occupation_number_and_internal_proximity(vesicle->clist,cellidx,vtx);
355     if(retval==TS_FAIL){
356         vtx=memcpy((void *)vtx,(void *)&backupvtx,sizeof(ts_vertex));
357         return TS_FAIL;
358     } 
359
360     //backup bonds
361     for(i=0;i<vtx->bond_no;i++){
362         memcpy(&backupbond[i],vtx->bond[i], sizeof(ts_bond));
363         vtx->bond[i]->bond_length=sqrt(dist[i]);
364         bond_vector(vtx->bond[i]);
b30f45 365     }
M 366
367     //backup neighboring vertices:
368     for(i=0;i<vtx->neigh_no;i++){
369         memcpy(&backupneigh[i],vtx->neigh[i], sizeof(ts_vertex));
58230a 370     }
M 371     
372     //if all the tests are successful, then energy for vtx and neighbours is calculated
b30f45 373     delta_energy=0;
M 374     
375     if(vtx->bond_no == 2){
376         vtx->energy = -(vtx->bond[0]->x*vtx->bond[1]->x + vtx->bond[0]->y*vtx->bond[1]->y + vtx->bond[0]->z*vtx->bond[1]->z)/vtx->bond[0]->bond_length/vtx->bond[1]->bond_length;
377         delta_energy += vtx->energy - backupvtx.energy;
58230a 378     }
M 379
b30f45 380     for(i=0;i<vtx->neigh_no;i++){
M 381         if(vtx->neigh[i]->bond_no == 2){
382             vtx->neigh[i]->energy = -(vtx->neigh[i]->bond[0]->x*vtx->neigh[i]->bond[1]->x + vtx->neigh[i]->bond[0]->y*vtx->neigh[i]->bond[1]->y + vtx->neigh[i]->bond[0]->z*vtx->neigh[i]->bond[1]->z)/vtx->neigh[i]->bond[0]->bond_length/vtx->neigh[i]->bond[1]->bond_length;
383             delta_energy += vtx->neigh[i]->energy - backupneigh[i].energy;
384         }
58230a 385     }
M 386
b30f45 387     // poly->k is filament persistence length (in units l_min)
M 388     delta_energy *= poly->k;
58230a 389
M 390     if(delta_energy>=0){
391 #ifdef TS_DOUBLE_DOUBLE
392         if(exp(-delta_energy)< drand48() )
393 #endif
394 #ifdef TS_DOUBLE_FLOAT
395         if(expf(-delta_energy)< (ts_float)drand48())
396 #endif
397 #ifdef TS_DOUBLE_LONGDOUBLE
398         if(expl(-delta_energy)< (ts_ldouble)drand48())
399 #endif
400         {
401     //not accepted, reverting changes
402     vtx=memcpy((void *)vtx,(void *)&backupvtx,sizeof(ts_vertex));
b30f45 403     for(i=0;i<vtx->neigh_no;i++){
M 404         memcpy(vtx->neigh[i],&backupneigh[i],sizeof(ts_vertex));
405     }
58230a 406     for(i=0;i<vtx->bond_no;i++){
b30f45 407         vtx->bond[i]=memcpy((void *)vtx->bond[i],(void *)&backupbond[i],sizeof(ts_bond));
58230a 408     }
M 409
410     return TS_FAIL; 
411     }
412     }
413     
b30f45 414     
58230a 415 //    oldcellidx=vertex_self_avoidance(vesicle, &backupvtx[0]);
M 416     if(vtx->cell!=vesicle->clist->cell[cellidx]){
417         retval=cell_add_vertex(vesicle->clist->cell[cellidx],vtx);
418 //        if(retval==TS_SUCCESS) cell_remove_vertex(vesicle->clist->cell[oldcellidx],vtx);
419         if(retval==TS_SUCCESS) cell_remove_vertex(backupvtx.cell,vtx);    
420     }
421 //    if(oldcellidx);
422     //END MONTE CARLOOOOOOO
423     return TS_SUCCESS;
424 }