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