Trisurf Monte Carlo simulator
Samo Penic
2018-09-25 5eefcc281fda031d322599fd7094da4fc8450948
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){
5eefcc 104         if(vtx->z > vesicle->confinement_plane.z_max || vtx->z < vesicle->confinement_plane.z_min){
267cf1 105                 vtx=memcpy((void *)vtx,(void *)&backupvtx[0],sizeof(ts_vertex));
SP 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){    
5eefcc 214             //ts_fprintf(stderr,"force!!!\n");
SP 215             delta_energy-=vesicle->tape->plane_F * ( 1.0/pow(backupvtx->z-vesicle->confinement_plane.z_min,2) + 1.0/pow(-backupvtx->z+vesicle->confinement_plane.z_max,2) );
216             delta_energy+=vesicle->tape->plane_F * ( 1.0/pow(vtx->z-vesicle->confinement_plane.z_min,2) + +1.0/pow(-vtx->z+vesicle->confinement_plane.z_max,2) );
267cf1 217
SP 218         }
219     }
220 // end plane confinement
aec47d 221     if(delta_energy>=0){
SP 222 #ifdef TS_DOUBLE_DOUBLE
3de289 223         if(exp(-delta_energy)< drand48())
aec47d 224 #endif
SP 225 #ifdef TS_DOUBLE_FLOAT
226         if(expf(-delta_energy)< (ts_float)drand48())
227 #endif
228 #ifdef TS_DOUBLE_LONGDOUBLE
229         if(expl(-delta_energy)< (ts_ldouble)drand48())
230 #endif
231     {
232     //not accepted, reverting changes
fbcbdf 233   //  fprintf(stderr,"MC failed\n");
dcd350 234     vtx=memcpy((void *)vtx,(void *)&backupvtx[0],sizeof(ts_vertex));
1ad6d1 235     for(i=0;i<vtx->neigh_no;i++){
a63f17 236         vtx->neigh[i]=memcpy((void *)vtx->neigh[i],(void *)&backupvtx[i+1],sizeof(ts_vertex));
1ad6d1 237     }
SP 238     
aec47d 239     //update the normals of triangles that share bead i.
dcd350 240    for(i=0;i<vtx->tristar_no;i++) triangle_normal_vector(vtx->tristar[i]);
1ad6d1 241
fe5069 242 //    fprintf(stderr, "before vtx(x,y,z)=%e,%e,%e\n",constvol_vtx_moved->x, constvol_vtx_moved->y, constvol_vtx_moved->z);
43c042 243     if(vesicle->tape->constvolswitch == 1){
958e0e 244         constvolumerestore(constvol_vtx_moved,constvol_vtx_backup);
43c042 245     }
fe5069 246 //    fprintf(stderr, "after vtx(x,y,z)=%e,%e,%e\n",constvol_vtx_moved->x, constvol_vtx_moved->y, constvol_vtx_moved->z);
dd5aca 247 //    vesicle_volume(vesicle);
SP 248 //    fprintf(stderr,"Volume after fail=%1.16e\n", vesicle->volume);
aec47d 249     return TS_FAIL; 
SP 250     }
251 }
2b14da 252     //accepted    
fbcbdf 253  //   fprintf(stderr,"MC accepted\n");
a63f17 254 //    oldcellidx=vertex_self_avoidance(vesicle, &backupvtx[0]);
SP 255     if(vtx->cell!=vesicle->clist->cell[cellidx]){
256         retval=cell_add_vertex(vesicle->clist->cell[cellidx],vtx);
257 //        if(retval==TS_SUCCESS) cell_remove_vertex(vesicle->clist->cell[oldcellidx],vtx);
258         if(retval==TS_SUCCESS) cell_remove_vertex(backupvtx[0].cell,vtx);
259         
260     }
2b14da 261
1121fa 262     if(vesicle->tape->constvolswitch == 2){
SP 263     vesicle->volume+=dvol;
264     } else
43c042 265     if(vesicle->tape->constvolswitch == 1){
fbcbdf 266         constvolumeaccept(vesicle,constvol_vtx_moved,constvol_vtx_backup);
43c042 267     }
c0ae90 268
SP 269     if(vesicle->tape->constareaswitch==2){
270         vesicle->area+=darea;
271     }
a63f17 272 //    if(oldcellidx);
aec47d 273     //END MONTE CARLOOOOOOO
dd5aca 274 //    vesicle_volume(vesicle);
SP 275 //    fprintf(stderr,"Volume after success=%1.16e\n", vesicle->volume);
aec47d 276     return TS_SUCCESS;
SP 277 }
278
fedf2b 279
M 280 ts_bool single_poly_vertex_move(ts_vesicle *vesicle,ts_poly *poly,ts_vertex *vtx,ts_double *rn){
281     ts_uint i;
282     ts_bool retval; 
283     ts_uint cellidx; 
304510 284 //    ts_double delta_energy;
fedf2b 285     ts_double costheta,sintheta,phi,r;
304510 286     ts_double dist;
fedf2b 287     //This will hold all the information of vtx and its neighbours
M 288     ts_vertex backupvtx;
304510 289 //    ts_bond backupbond[2];
fedf2b 290     memcpy((void *)&backupvtx,(void *)vtx,sizeof(ts_vertex));
M 291
292     //random move in a sphere with radius stepsize:
293     r=vesicle->stepsize*rn[0];
294     phi=rn[1]*2*M_PI;
295     costheta=2*rn[2]-1;
296     sintheta=sqrt(1-pow(costheta,2));
297     vtx->x=vtx->x+r*sintheta*cos(phi);
298     vtx->y=vtx->y+r*sintheta*sin(phi);
299     vtx->z=vtx->z+r*costheta;
300
301
302     //distance with neighbours check
304510 303     for(i=0;i<vtx->neigh_no;i++){
M 304         dist=vtx_distance_sq(vtx,vtx->neigh[i]);
305         if(dist<1.0 || dist>vesicle->dmax) {
306             vtx=memcpy((void *)vtx,(void *)&backupvtx,sizeof(ts_vertex));
307             return TS_FAIL;
308         }
309     }
310
311 // Distance with grafted vesicle-vertex check:    
312     if(vtx==poly->vlist->vtx[0]){
313         dist=vtx_distance_sq(vtx,poly->grafted_vtx);
314         if(dist<1.0 || dist>vesicle->dmax) {
315         vtx=memcpy((void *)vtx,(void *)&backupvtx,sizeof(ts_vertex));
316         return TS_FAIL;
317         }
318     }
319
fedf2b 320
M 321     //self avoidance check with distant vertices
322     cellidx=vertex_self_avoidance(vesicle, vtx);
323     //check occupation number
324     retval=cell_occupation_number_and_internal_proximity(vesicle->clist,cellidx,vtx);
325     
326     if(retval==TS_FAIL){
327         vtx=memcpy((void *)vtx,(void *)&backupvtx,sizeof(ts_vertex));
328         return TS_FAIL;
329     } 
330
331
332     //if all the tests are successful, then energy for vtx and neighbours is calculated
304510 333 /* Energy ignored for now!
fedf2b 334     delta_energy=0;
M 335     for(i=0;i<vtx->bond_no;i++){
336         memcpy((void *)&backupbond[i],(void *)vtx->bond[i],sizeof(ts_bond));
337
338         vtx->bond[i]->bond_length=sqrt(vtx_distance_sq(vtx->bond[i]->vtx1,vtx->bond[i]->vtx2));
339         bond_energy(vtx->bond[i],poly);
340         delta_energy+= vtx->bond[i]->energy - backupbond[i].energy;
341     }
342
343     if(vtx==poly->vlist->vtx[0]){
344         delta_energy+=
345             (pow(sqrt(vtx_distance_sq(vtx, poly->grafted_vtx)-1),2)-
346             pow(sqrt(vtx_distance_sq(&backupvtx, poly->grafted_vtx)-1),2)) *poly->k;
347         
348     }
349
350
351     if(delta_energy>=0){
352 #ifdef TS_DOUBLE_DOUBLE
353         if(exp(-delta_energy)< drand48() )
354 #endif
355 #ifdef TS_DOUBLE_FLOAT
356         if(expf(-delta_energy)< (ts_float)drand48())
357 #endif
358 #ifdef TS_DOUBLE_LONGDOUBLE
359         if(expl(-delta_energy)< (ts_ldouble)drand48())
360 #endif
361         {
362     //not accepted, reverting changes
363     vtx=memcpy((void *)vtx,(void *)&backupvtx,sizeof(ts_vertex));
364     for(i=0;i<vtx->bond_no;i++){
365     vtx->bond[i]=memcpy((void *)vtx->bond[i],(void *)&backupbond[i],sizeof(ts_bond));
366     }
367
368     return TS_FAIL; 
369     }
370     }
304510 371 */
fedf2b 372         
M 373 //    oldcellidx=vertex_self_avoidance(vesicle, &backupvtx[0]);
374     if(vtx->cell!=vesicle->clist->cell[cellidx]){
375         retval=cell_add_vertex(vesicle->clist->cell[cellidx],vtx);
376 //        if(retval==TS_SUCCESS) cell_remove_vertex(vesicle->clist->cell[oldcellidx],vtx);
377         if(retval==TS_SUCCESS) cell_remove_vertex(backupvtx.cell,vtx);    
378     }
379 //    if(oldcellidx);
380     //END MONTE CARLOOOOOOO
381     return TS_SUCCESS;
382 }
58230a 383
M 384
385
386
387 ts_bool single_filament_vertex_move(ts_vesicle *vesicle,ts_poly *poly,ts_vertex *vtx,ts_double *rn){
388     ts_uint i;
389     ts_bool retval; 
390     ts_uint cellidx; 
b30f45 391     ts_double delta_energy;
58230a 392     ts_double costheta,sintheta,phi,r;
M 393     ts_double dist[2];
394     //This will hold all the information of vtx and its neighbours
b30f45 395     ts_vertex backupvtx,backupneigh[2];
58230a 396     ts_bond backupbond[2];
b30f45 397
M 398     //backup vertex:        
58230a 399     memcpy((void *)&backupvtx,(void *)vtx,sizeof(ts_vertex));
M 400
401     //random move in a sphere with radius stepsize:
402     r=vesicle->stepsize*rn[0];
403     phi=rn[1]*2*M_PI;
404     costheta=2*rn[2]-1;
405     sintheta=sqrt(1-pow(costheta,2));
406     vtx->x=vtx->x+r*sintheta*cos(phi);
407     vtx->y=vtx->y+r*sintheta*sin(phi);
408     vtx->z=vtx->z+r*costheta;
409
410
411     //distance with neighbours check
412     for(i=0;i<vtx->bond_no;i++){
413         dist[i]=vtx_distance_sq(vtx->bond[i]->vtx1,vtx->bond[i]->vtx2);
414         if(dist[i]<1.0 || dist[i]>vesicle->dmax) {
415             vtx=memcpy((void *)vtx,(void *)&backupvtx,sizeof(ts_vertex));
416             return TS_FAIL;
417         }
418     }
419
fe24d2 420 // TODO: Maybe faster if checks only nucleus-neighboring cells
M 421 // Nucleus penetration check:
422     if (vtx->x*vtx->x + vtx->y*vtx->y + vtx->z*vtx->z < vesicle->R_nucleus){
423         vtx=memcpy((void *)vtx,(void *)&backupvtx,sizeof(ts_vertex));
424         return TS_FAIL;
425     }
426
58230a 427
M 428     //self avoidance check with distant vertices
429     cellidx=vertex_self_avoidance(vesicle, vtx);
430     //check occupation number
431     retval=cell_occupation_number_and_internal_proximity(vesicle->clist,cellidx,vtx);
432     if(retval==TS_FAIL){
433         vtx=memcpy((void *)vtx,(void *)&backupvtx,sizeof(ts_vertex));
434         return TS_FAIL;
435     } 
436
437     //backup bonds
438     for(i=0;i<vtx->bond_no;i++){
439         memcpy(&backupbond[i],vtx->bond[i], sizeof(ts_bond));
440         vtx->bond[i]->bond_length=sqrt(dist[i]);
441         bond_vector(vtx->bond[i]);
b30f45 442     }
M 443
444     //backup neighboring vertices:
445     for(i=0;i<vtx->neigh_no;i++){
446         memcpy(&backupneigh[i],vtx->neigh[i], sizeof(ts_vertex));
58230a 447     }
M 448     
449     //if all the tests are successful, then energy for vtx and neighbours is calculated
b30f45 450     delta_energy=0;
M 451     
452     if(vtx->bond_no == 2){
453         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;
454         delta_energy += vtx->energy - backupvtx.energy;
58230a 455     }
M 456
b30f45 457     for(i=0;i<vtx->neigh_no;i++){
M 458         if(vtx->neigh[i]->bond_no == 2){
459             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;
460             delta_energy += vtx->neigh[i]->energy - backupneigh[i].energy;
461         }
58230a 462     }
M 463
b30f45 464     // poly->k is filament persistence length (in units l_min)
M 465     delta_energy *= poly->k;
58230a 466
M 467     if(delta_energy>=0){
468 #ifdef TS_DOUBLE_DOUBLE
469         if(exp(-delta_energy)< drand48() )
470 #endif
471 #ifdef TS_DOUBLE_FLOAT
472         if(expf(-delta_energy)< (ts_float)drand48())
473 #endif
474 #ifdef TS_DOUBLE_LONGDOUBLE
475         if(expl(-delta_energy)< (ts_ldouble)drand48())
476 #endif
477         {
478     //not accepted, reverting changes
479     vtx=memcpy((void *)vtx,(void *)&backupvtx,sizeof(ts_vertex));
b30f45 480     for(i=0;i<vtx->neigh_no;i++){
M 481         memcpy(vtx->neigh[i],&backupneigh[i],sizeof(ts_vertex));
482     }
58230a 483     for(i=0;i<vtx->bond_no;i++){
b30f45 484         vtx->bond[i]=memcpy((void *)vtx->bond[i],(void *)&backupbond[i],sizeof(ts_bond));
58230a 485     }
M 486
487     return TS_FAIL; 
488     }
489     }
490     
b30f45 491     
58230a 492 //    oldcellidx=vertex_self_avoidance(vesicle, &backupvtx[0]);
M 493     if(vtx->cell!=vesicle->clist->cell[cellidx]){
494         retval=cell_add_vertex(vesicle->clist->cell[cellidx],vtx);
495 //        if(retval==TS_SUCCESS) cell_remove_vertex(vesicle->clist->cell[oldcellidx],vtx);
496         if(retval==TS_SUCCESS) cell_remove_vertex(backupvtx.cell,vtx);    
497     }
498 //    if(oldcellidx);
499     //END MONTE CARLOOOOOOO
500     return TS_SUCCESS;
501 }