Trisurf Monte Carlo simulator
Samo Penic
2017-09-19 e6efc61995516174081e923004477e3ff0853515
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    
SP 102  
c0ae90 103 //if all the tests are successful, then energy for vtx and neighbours is calculated
1ad6d1 104     for(i=0;i<vtx->neigh_no;i++){
dcd350 105     memcpy((void *)&backupvtx[i+1],(void *)vtx->neigh[i],sizeof(ts_vertex));
1ad6d1 106     }
aec47d 107
1121fa 108     if(vesicle->pswitch == 1 || vesicle->tape->constvolswitch>0){
414b8a 109         for(i=0;i<vtx->tristar_no;i++) dvol-=vtx->tristar[i]->volume;
c0ae90 110     }
SP 111
112     if(vesicle->tape->constareaswitch==2){
113         for(i=0;i<vtx->tristar_no;i++) darea-=vtx->tristar[i]->area;
114     
115     }
a63f17 116
aec47d 117     delta_energy=0;
fe5069 118     
SP 119 //    vesicle_volume(vesicle);
120 //    fprintf(stderr,"Volume in the beginning=%1.16e\n", vesicle->volume);
43c042 121
aec47d 122     //update the normals of triangles that share bead i.
8f6a69 123     for(i=0;i<vtx->tristar_no;i++) triangle_normal_vector(vtx->tristar[i]);
a63f17 124     oenergy=vtx->energy;
aec47d 125     energy_vertex(vtx);
a63f17 126     delta_energy=vtx->xk*(vtx->energy - oenergy);
aec47d 127     //the same is done for neighbouring vertices
8f6a69 128     for(i=0;i<vtx->neigh_no;i++){
SP 129         oenergy=vtx->neigh[i]->energy;
130         energy_vertex(vtx->neigh[i]);
131         delta_energy+=vtx->neigh[i]->xk*(vtx->neigh[i]->energy-oenergy);
aec47d 132     }
414b8a 133
1121fa 134     if(vesicle->pswitch == 1 || vesicle->tape->constvolswitch >0){
414b8a 135         for(i=0;i<vtx->tristar_no;i++) dvol+=vtx->tristar[i]->volume;
fbcbdf 136         if(vesicle->pswitch==1) delta_energy-=vesicle->pressure*dvol;
414b8a 137     };
43c042 138
c0ae90 139     if(vesicle->tape->constareaswitch==2){
SP 140         /* check whether the darea is gt epsarea */
141         for(i=0;i<vtx->tristar_no;i++) darea+=vtx->tristar[i]->area;
142         if(fabs(vesicle->area+darea-A0)>epsarea){
143             //restore old state.
144              vtx=memcpy((void *)vtx,(void *)&backupvtx[0],sizeof(ts_vertex));
145                 for(i=0;i<vtx->neigh_no;i++){
146                     vtx->neigh[i]=memcpy((void *)vtx->neigh[i],(void *)&backupvtx[i+1],sizeof(ts_vertex));
147                 }
148                     for(i=0;i<vtx->tristar_no;i++) triangle_normal_vector(vtx->tristar[i]); 
149                     //fprintf(stderr,"fajlam!\n");
150                     return TS_FAIL;
151         }
152
153
154     }
1121fa 155
SP 156     if(vesicle->tape->constvolswitch==2){
157         /*check whether the dvol is gt than epsvol */
158             //fprintf(stderr,"DVOL=%1.16e\n",dvol);
159         if(fabs(vesicle->volume+dvol-V0)>epsvol){
160             //restore old state.
161              vtx=memcpy((void *)vtx,(void *)&backupvtx[0],sizeof(ts_vertex));
162                 for(i=0;i<vtx->neigh_no;i++){
163                     vtx->neigh[i]=memcpy((void *)vtx->neigh[i],(void *)&backupvtx[i+1],sizeof(ts_vertex));
164                 }
165                     for(i=0;i<vtx->tristar_no;i++) triangle_normal_vector(vtx->tristar[i]); 
166                     //fprintf(stderr,"fajlam!\n");
167                     return TS_FAIL;
168         }
169
170     } else
fe5069 171 //    vesicle_volume(vesicle);
SP 172 //    fprintf(stderr,"Volume before=%1.16e\n", vesicle->volume);
fbcbdf 173    if(vesicle->tape->constvolswitch == 1){
fe5069 174         retval=constvolume(vesicle, vtx, -dvol, &delta_energy_cv, &constvol_vtx_moved,&constvol_vtx_backup);
fbcbdf 175         if(retval==TS_FAIL){ // if we couldn't move the vertex to assure constant volume
SP 176             vtx=memcpy((void *)vtx,(void *)&backupvtx[0],sizeof(ts_vertex));
177             for(i=0;i<vtx->neigh_no;i++){
178                 vtx->neigh[i]=memcpy((void *)vtx->neigh[i],(void *)&backupvtx[i+1],sizeof(ts_vertex));
179             }
180             for(i=0;i<vtx->tristar_no;i++) triangle_normal_vector(vtx->tristar[i]); 
fe5069 181  //           fprintf(stderr,"fajlam!\n");
fbcbdf 182             return TS_FAIL;
SP 183         }
fe5069 184 //    vesicle_volume(vesicle);
SP 185 //    fprintf(stderr,"Volume after=%1.16e\n", vesicle->volume);
186 //    fprintf(stderr,"Volume after-dvol=%1.16e\n", vesicle->volume-dvol);
187 //    fprintf(stderr,"Denergy before=%e\n",delta_energy);
188     
fbcbdf 189     delta_energy+=delta_energy_cv;
fe5069 190 //    fprintf(stderr,"Denergy after=%e\n",delta_energy);
fbcbdf 191     }
250de4 192 /* Vertices with spontaneous curvature may have spontaneous force perpendicular to the surface of the vesicle. additional delta energy is calculated in this function */
SP 193     delta_energy+=direct_force_energy(vesicle,vtx,backupvtx);
304510 194 /* No poly-bond energy for now!
fedf2b 195     if(vtx->grafted_poly!=NULL){
M 196         delta_energy+=
197             (pow(sqrt(vtx_distance_sq(vtx, vtx->grafted_poly->vlist->vtx[0])-1),2)-
198             pow(sqrt(vtx_distance_sq(&backupvtx[0], vtx->grafted_poly->vlist->vtx[0])-1),2)) *vtx->grafted_poly->k;
199     }
304510 200 */
314f2d 201 //   fprintf(stderr, "DE=%f\n",delta_energy);
aec47d 202     //MONTE CARLOOOOOOOO
e5858f 203 //    if(vtx->c!=0.0) printf("DE=%f\n",delta_energy);
aec47d 204     if(delta_energy>=0){
SP 205 #ifdef TS_DOUBLE_DOUBLE
3de289 206         if(exp(-delta_energy)< drand48())
aec47d 207 #endif
SP 208 #ifdef TS_DOUBLE_FLOAT
209         if(expf(-delta_energy)< (ts_float)drand48())
210 #endif
211 #ifdef TS_DOUBLE_LONGDOUBLE
212         if(expl(-delta_energy)< (ts_ldouble)drand48())
213 #endif
214     {
215     //not accepted, reverting changes
fbcbdf 216   //  fprintf(stderr,"MC failed\n");
dcd350 217     vtx=memcpy((void *)vtx,(void *)&backupvtx[0],sizeof(ts_vertex));
1ad6d1 218     for(i=0;i<vtx->neigh_no;i++){
a63f17 219         vtx->neigh[i]=memcpy((void *)vtx->neigh[i],(void *)&backupvtx[i+1],sizeof(ts_vertex));
1ad6d1 220     }
SP 221     
aec47d 222     //update the normals of triangles that share bead i.
dcd350 223    for(i=0;i<vtx->tristar_no;i++) triangle_normal_vector(vtx->tristar[i]);
1ad6d1 224
fe5069 225 //    fprintf(stderr, "before vtx(x,y,z)=%e,%e,%e\n",constvol_vtx_moved->x, constvol_vtx_moved->y, constvol_vtx_moved->z);
43c042 226     if(vesicle->tape->constvolswitch == 1){
958e0e 227         constvolumerestore(constvol_vtx_moved,constvol_vtx_backup);
43c042 228     }
fe5069 229 //    fprintf(stderr, "after vtx(x,y,z)=%e,%e,%e\n",constvol_vtx_moved->x, constvol_vtx_moved->y, constvol_vtx_moved->z);
dd5aca 230 //    vesicle_volume(vesicle);
SP 231 //    fprintf(stderr,"Volume after fail=%1.16e\n", vesicle->volume);
aec47d 232     return TS_FAIL; 
SP 233     }
234 }
2b14da 235     //accepted    
fbcbdf 236  //   fprintf(stderr,"MC accepted\n");
a63f17 237 //    oldcellidx=vertex_self_avoidance(vesicle, &backupvtx[0]);
SP 238     if(vtx->cell!=vesicle->clist->cell[cellidx]){
239         retval=cell_add_vertex(vesicle->clist->cell[cellidx],vtx);
240 //        if(retval==TS_SUCCESS) cell_remove_vertex(vesicle->clist->cell[oldcellidx],vtx);
241         if(retval==TS_SUCCESS) cell_remove_vertex(backupvtx[0].cell,vtx);
242         
243     }
2b14da 244
1121fa 245     if(vesicle->tape->constvolswitch == 2){
SP 246     vesicle->volume+=dvol;
247     } else
43c042 248     if(vesicle->tape->constvolswitch == 1){
fbcbdf 249         constvolumeaccept(vesicle,constvol_vtx_moved,constvol_vtx_backup);
43c042 250     }
c0ae90 251
SP 252     if(vesicle->tape->constareaswitch==2){
253         vesicle->area+=darea;
254     }
a63f17 255 //    if(oldcellidx);
aec47d 256     //END MONTE CARLOOOOOOO
dd5aca 257 //    vesicle_volume(vesicle);
SP 258 //    fprintf(stderr,"Volume after success=%1.16e\n", vesicle->volume);
aec47d 259     return TS_SUCCESS;
SP 260 }
261
fedf2b 262
M 263 ts_bool single_poly_vertex_move(ts_vesicle *vesicle,ts_poly *poly,ts_vertex *vtx,ts_double *rn){
264     ts_uint i;
265     ts_bool retval; 
266     ts_uint cellidx; 
304510 267 //    ts_double delta_energy;
fedf2b 268     ts_double costheta,sintheta,phi,r;
304510 269     ts_double dist;
fedf2b 270     //This will hold all the information of vtx and its neighbours
M 271     ts_vertex backupvtx;
304510 272 //    ts_bond backupbond[2];
fedf2b 273     memcpy((void *)&backupvtx,(void *)vtx,sizeof(ts_vertex));
M 274
275     //random move in a sphere with radius stepsize:
276     r=vesicle->stepsize*rn[0];
277     phi=rn[1]*2*M_PI;
278     costheta=2*rn[2]-1;
279     sintheta=sqrt(1-pow(costheta,2));
280     vtx->x=vtx->x+r*sintheta*cos(phi);
281     vtx->y=vtx->y+r*sintheta*sin(phi);
282     vtx->z=vtx->z+r*costheta;
283
284
285     //distance with neighbours check
304510 286     for(i=0;i<vtx->neigh_no;i++){
M 287         dist=vtx_distance_sq(vtx,vtx->neigh[i]);
288         if(dist<1.0 || dist>vesicle->dmax) {
289             vtx=memcpy((void *)vtx,(void *)&backupvtx,sizeof(ts_vertex));
290             return TS_FAIL;
291         }
292     }
293
294 // Distance with grafted vesicle-vertex check:    
295     if(vtx==poly->vlist->vtx[0]){
296         dist=vtx_distance_sq(vtx,poly->grafted_vtx);
297         if(dist<1.0 || dist>vesicle->dmax) {
298         vtx=memcpy((void *)vtx,(void *)&backupvtx,sizeof(ts_vertex));
299         return TS_FAIL;
300         }
301     }
302
fedf2b 303
M 304     //self avoidance check with distant vertices
305     cellidx=vertex_self_avoidance(vesicle, vtx);
306     //check occupation number
307     retval=cell_occupation_number_and_internal_proximity(vesicle->clist,cellidx,vtx);
308     
309     if(retval==TS_FAIL){
310         vtx=memcpy((void *)vtx,(void *)&backupvtx,sizeof(ts_vertex));
311         return TS_FAIL;
312     } 
313
314
315     //if all the tests are successful, then energy for vtx and neighbours is calculated
304510 316 /* Energy ignored for now!
fedf2b 317     delta_energy=0;
M 318     for(i=0;i<vtx->bond_no;i++){
319         memcpy((void *)&backupbond[i],(void *)vtx->bond[i],sizeof(ts_bond));
320
321         vtx->bond[i]->bond_length=sqrt(vtx_distance_sq(vtx->bond[i]->vtx1,vtx->bond[i]->vtx2));
322         bond_energy(vtx->bond[i],poly);
323         delta_energy+= vtx->bond[i]->energy - backupbond[i].energy;
324     }
325
326     if(vtx==poly->vlist->vtx[0]){
327         delta_energy+=
328             (pow(sqrt(vtx_distance_sq(vtx, poly->grafted_vtx)-1),2)-
329             pow(sqrt(vtx_distance_sq(&backupvtx, poly->grafted_vtx)-1),2)) *poly->k;
330         
331     }
332
333
334     if(delta_energy>=0){
335 #ifdef TS_DOUBLE_DOUBLE
336         if(exp(-delta_energy)< drand48() )
337 #endif
338 #ifdef TS_DOUBLE_FLOAT
339         if(expf(-delta_energy)< (ts_float)drand48())
340 #endif
341 #ifdef TS_DOUBLE_LONGDOUBLE
342         if(expl(-delta_energy)< (ts_ldouble)drand48())
343 #endif
344         {
345     //not accepted, reverting changes
346     vtx=memcpy((void *)vtx,(void *)&backupvtx,sizeof(ts_vertex));
347     for(i=0;i<vtx->bond_no;i++){
348     vtx->bond[i]=memcpy((void *)vtx->bond[i],(void *)&backupbond[i],sizeof(ts_bond));
349     }
350
351     return TS_FAIL; 
352     }
353     }
304510 354 */
fedf2b 355         
M 356 //    oldcellidx=vertex_self_avoidance(vesicle, &backupvtx[0]);
357     if(vtx->cell!=vesicle->clist->cell[cellidx]){
358         retval=cell_add_vertex(vesicle->clist->cell[cellidx],vtx);
359 //        if(retval==TS_SUCCESS) cell_remove_vertex(vesicle->clist->cell[oldcellidx],vtx);
360         if(retval==TS_SUCCESS) cell_remove_vertex(backupvtx.cell,vtx);    
361     }
362 //    if(oldcellidx);
363     //END MONTE CARLOOOOOOO
364     return TS_SUCCESS;
365 }
58230a 366
M 367
368
369
370 ts_bool single_filament_vertex_move(ts_vesicle *vesicle,ts_poly *poly,ts_vertex *vtx,ts_double *rn){
371     ts_uint i;
372     ts_bool retval; 
373     ts_uint cellidx; 
b30f45 374     ts_double delta_energy;
58230a 375     ts_double costheta,sintheta,phi,r;
M 376     ts_double dist[2];
377     //This will hold all the information of vtx and its neighbours
b30f45 378     ts_vertex backupvtx,backupneigh[2];
58230a 379     ts_bond backupbond[2];
b30f45 380
M 381     //backup vertex:        
58230a 382     memcpy((void *)&backupvtx,(void *)vtx,sizeof(ts_vertex));
M 383
384     //random move in a sphere with radius stepsize:
385     r=vesicle->stepsize*rn[0];
386     phi=rn[1]*2*M_PI;
387     costheta=2*rn[2]-1;
388     sintheta=sqrt(1-pow(costheta,2));
389     vtx->x=vtx->x+r*sintheta*cos(phi);
390     vtx->y=vtx->y+r*sintheta*sin(phi);
391     vtx->z=vtx->z+r*costheta;
392
393
394     //distance with neighbours check
395     for(i=0;i<vtx->bond_no;i++){
396         dist[i]=vtx_distance_sq(vtx->bond[i]->vtx1,vtx->bond[i]->vtx2);
397         if(dist[i]<1.0 || dist[i]>vesicle->dmax) {
398             vtx=memcpy((void *)vtx,(void *)&backupvtx,sizeof(ts_vertex));
399             return TS_FAIL;
400         }
401     }
402
fe24d2 403 // TODO: Maybe faster if checks only nucleus-neighboring cells
M 404 // Nucleus penetration check:
405     if (vtx->x*vtx->x + vtx->y*vtx->y + vtx->z*vtx->z < vesicle->R_nucleus){
406         vtx=memcpy((void *)vtx,(void *)&backupvtx,sizeof(ts_vertex));
407         return TS_FAIL;
408     }
409
58230a 410
M 411     //self avoidance check with distant vertices
412     cellidx=vertex_self_avoidance(vesicle, vtx);
413     //check occupation number
414     retval=cell_occupation_number_and_internal_proximity(vesicle->clist,cellidx,vtx);
415     if(retval==TS_FAIL){
416         vtx=memcpy((void *)vtx,(void *)&backupvtx,sizeof(ts_vertex));
417         return TS_FAIL;
418     } 
419
420     //backup bonds
421     for(i=0;i<vtx->bond_no;i++){
422         memcpy(&backupbond[i],vtx->bond[i], sizeof(ts_bond));
423         vtx->bond[i]->bond_length=sqrt(dist[i]);
424         bond_vector(vtx->bond[i]);
b30f45 425     }
M 426
427     //backup neighboring vertices:
428     for(i=0;i<vtx->neigh_no;i++){
429         memcpy(&backupneigh[i],vtx->neigh[i], sizeof(ts_vertex));
58230a 430     }
M 431     
432     //if all the tests are successful, then energy for vtx and neighbours is calculated
b30f45 433     delta_energy=0;
M 434     
435     if(vtx->bond_no == 2){
436         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;
437         delta_energy += vtx->energy - backupvtx.energy;
58230a 438     }
M 439
b30f45 440     for(i=0;i<vtx->neigh_no;i++){
M 441         if(vtx->neigh[i]->bond_no == 2){
442             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;
443             delta_energy += vtx->neigh[i]->energy - backupneigh[i].energy;
444         }
58230a 445     }
M 446
b30f45 447     // poly->k is filament persistence length (in units l_min)
M 448     delta_energy *= poly->k;
58230a 449
M 450     if(delta_energy>=0){
451 #ifdef TS_DOUBLE_DOUBLE
452         if(exp(-delta_energy)< drand48() )
453 #endif
454 #ifdef TS_DOUBLE_FLOAT
455         if(expf(-delta_energy)< (ts_float)drand48())
456 #endif
457 #ifdef TS_DOUBLE_LONGDOUBLE
458         if(expl(-delta_energy)< (ts_ldouble)drand48())
459 #endif
460         {
461     //not accepted, reverting changes
462     vtx=memcpy((void *)vtx,(void *)&backupvtx,sizeof(ts_vertex));
b30f45 463     for(i=0;i<vtx->neigh_no;i++){
M 464         memcpy(vtx->neigh[i],&backupneigh[i],sizeof(ts_vertex));
465     }
58230a 466     for(i=0;i<vtx->bond_no;i++){
b30f45 467         vtx->bond[i]=memcpy((void *)vtx->bond[i],(void *)&backupbond[i],sizeof(ts_bond));
58230a 468     }
M 469
470     return TS_FAIL; 
471     }
472     }
473     
b30f45 474     
58230a 475 //    oldcellidx=vertex_self_avoidance(vesicle, &backupvtx[0]);
M 476     if(vtx->cell!=vesicle->clist->cell[cellidx]){
477         retval=cell_add_vertex(vesicle->clist->cell[cellidx],vtx);
478 //        if(retval==TS_SUCCESS) cell_remove_vertex(vesicle->clist->cell[oldcellidx],vtx);
479         if(retval==TS_SUCCESS) cell_remove_vertex(backupvtx.cell,vtx);    
480     }
481 //    if(oldcellidx);
482     //END MONTE CARLOOOOOOO
483     return TS_SUCCESS;
484 }