Trisurf Monte Carlo simulator
Samo Penic
2017-01-04 96b0a0ac783d35fbf4e764fe8e610ddc2a79af27
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"
30ee9c 12 #include "bondflip.h"
aec47d 13 //#include "io.h"
SP 14 #include<stdio.h>
2870ab 15 #include<string.h>
fbcbdf 16 #include "constvol.h"
aec47d 17
SP 18 ts_bool single_bondflip_timestep(ts_vesicle *vesicle, ts_bond *bond, ts_double *rn){
19 /*c  Vertex and triangle (lm and lp) indexing for bond flip:
20 c      +----- k-------+              +----- k ------+
21 c      |lm1 / | \ lp1 |              |lm1 /   \ lp1 |
22 c      |  /   |   \   |              |  /       \   |
23 c      |/     |     \ |     FLIP     |/    lm     \ |
24 c     km  lm  | lp   kp    --->      km ---------- kp  
25 c      |\     |     / |              |\    lp     / |  
26 c      |  \   |   /   |              |  \       /   |
27 c      |lm2 \ | / lp2 |              |lm2 \   / lp2 |
28 c      +------it------+              +----- it -----+
29 c
30 */
31     ts_vertex *it=bond->vtx1;
32     ts_vertex *k=bond->vtx2;
33     ts_uint nei,neip,neim;
b866cf 34     ts_uint i,j;
c0ae90 35     ts_double oldenergy, delta_energy, dvol=0.0, darea=0.0;
b866cf 36     ts_triangle *lm=NULL,*lp=NULL, *lp1=NULL, *lm2=NULL;
aec47d 37
SP 38     ts_vertex *kp,*km;
fbcbdf 39
SP 40     ts_double delta_energy_cv;
41     ts_vertex *constvol_vtx_moved, *constvol_vtx_backup;
42     ts_bool retval;
aec47d 43
SP 44     if(it->neigh_no< 3) return TS_FAIL;
45     if(k->neigh_no< 3) return TS_FAIL;
46     if(k==NULL || it==NULL){
47         fatal("In bondflip, number of neighbours of k or it is less than 3!",999);
48     }
49
30ee9c 50     nei=0;
aec47d 51     for(i=0;i<it->neigh_no;i++){ // Finds the nn of it, that is k 
SP 52         if(it->neigh[i]==k){
53             nei=i;
54             break;
55         }
56     }
57     neip=nei+1;  // I don't like it.. Smells like I must have it in correct order
58     neim=nei-1;
59     if(neip>=it->neigh_no) neip=0;
60     if((ts_int)neim<0) neim=it->neigh_no-1; /* casting is essential... If not
61 there the neim is never <0 !!! */
62   //  fprintf(stderr,"The numbers are: %u %u\n",neip, neim);
63     km=it->neigh[neim];  // We located km and kp
64     kp=it->neigh[neip];
65
66     if(km==NULL || kp==NULL){
67         fatal("In bondflip, cannot determine km and kp!",999);
68     }
69
70   //  fprintf(stderr,"I WAS HERE! after the 4 vertices are known!\n");
71
72 /* test if the membrane is wrapped too much, so that kp is nearest neighbour of
73  * km. If it is true, then don't flip! */
74     for(i=0;i<km->neigh_no;i++){
75         if(km->neigh[i] == kp) return TS_FAIL;
76     }
77  //   fprintf(stderr,"Membrane didn't wrap too much.. Continue.\n");
78 /* if bond would be too long, return... */
30ee9c 79     if(vtx_distance_sq(km,kp) > vesicle->dmax ) return TS_FAIL;
aec47d 80  //   fprintf(stderr,"Bond will not be too long.. Continue.\n");
SP 81
82 /* we make a bond flip. this is different than in original fortran */
b866cf 83 // find lm, lp
aec47d 84 // 1. step. We find lm and lp from k->tristar !
SP 85     for(i=0;i<it->tristar_no;i++){
86         for(j=0;j<k->tristar_no;j++){
87             if((it->tristar[i] == k->tristar[j])){ //ce gre za skupen trikotnik
88                 if((it->tristar[i]->vertex[0] == km || it->tristar[i]->vertex[1]
89 == km || it->tristar[i]->vertex[2]== km )){
90                 lm=it->tristar[i];
91          //       lmidx=i;
92                 }
93                 else
94                 {
95                 lp=it->tristar[i];
96          //       lpidx=i;
97                 }
98
99             }
100         }
101     }
102 if(lm==NULL || lp==NULL) fatal("ts_flip_bond: Cannot find triangles lm and lp!",999);
103
104 //we look for important triangles lp1 and lm2.
105
106  for(i=0;i<k->tristar_no;i++){
107         for(j=0;j<kp->tristar_no;j++){
108                 if((k->tristar[i] == kp->tristar[j]) && k->tristar[i]!=lp){ //ce gre za skupen trikotnik
109                     lp1=k->tristar[i];
110             }
111         }
112 }
113
114  for(i=0;i<it->tristar_no;i++){
115         for(j=0;j<km->tristar_no;j++){
116             if((it->tristar[i] == km->tristar[j]) && it->tristar[i]!=lm){ //ce gre za skupen trikotnik
117                     lm2=it->tristar[i];
118             } 
119         }
120     }
121
122 if(lm2==NULL || lp1==NULL) fatal("ts_flip_bond: Cannot find triangles lm2 and lp1!",999);
2870ab 123
SP 124
125 /* backup old structure */
126 /* need to backup:
127  * vertices k, kp, km, it
128  * triangles lm, lp, lm2, lp1
129  * bond
130  */
131 ts_vertex *bck_vtx[4];
132 ts_triangle *bck_tria[4];
133 ts_bond *bck_bond;
134 ts_vertex *orig_vtx[]={k,it,kp,km};
7efbb1 135 ts_triangle *orig_tria[]={lm,lp,lm2,lp1};
2870ab 136
7efbb1 137 //fprintf(stderr,"Backuping!!!\n");
SP 138     bck_bond=(ts_bond *)malloc(sizeof(ts_bond));
2870ab 139 for(i=0;i<4;i++){
7efbb1 140 /*    fprintf(stderr,"vtx neigh[%d]=",i);
SP 141     for(j=0;j<orig_vtx[i]->neigh_no;j++) fprintf(stderr," %d", orig_vtx[i]->neigh[j]->idx);
142     fprintf(stderr,"\n");
143 */
2870ab 144     bck_vtx[i]=(ts_vertex *)malloc(sizeof(ts_vertex));
SP 145     bck_tria[i]=(ts_triangle *)malloc(sizeof(ts_triangle));
7efbb1 146     memcpy((void *)bck_vtx[i],(void *)orig_vtx[i],sizeof(ts_vertex));
SP 147     memcpy((void *)bck_tria[i],(void *)orig_tria[i],sizeof(ts_triangle));
2870ab 148     /* level 2 pointers */
7efbb1 149
2870ab 150     bck_vtx[i]->neigh=(ts_vertex **)malloc(orig_vtx[i]->neigh_no*sizeof(ts_vertex *));
SP 151     bck_vtx[i]->tristar=(ts_triangle **)malloc(orig_vtx[i]->tristar_no*sizeof(ts_triangle *));
152     bck_vtx[i]->bond=(ts_bond **)malloc(orig_vtx[i]->bond_no*sizeof(ts_bond *));
153     bck_tria[i]->neigh=(ts_triangle **)malloc(orig_tria[i]->neigh_no*sizeof(ts_triangle *));
154
7efbb1 155     memcpy((void *)bck_vtx[i]->neigh,(void *)orig_vtx[i]->neigh,orig_vtx[i]->neigh_no*sizeof(ts_vertex *));
SP 156     memcpy((void *)bck_vtx[i]->tristar,(void *)orig_vtx[i]->tristar,orig_vtx[i]->tristar_no*sizeof(ts_triangle *));
157     memcpy((void *)bck_vtx[i]->bond,(void *)orig_vtx[i]->bond,orig_vtx[i]->bond_no*sizeof(ts_bond *));
b866cf 158     
7efbb1 159     memcpy((void *)bck_tria[i]->neigh,(void *)orig_tria[i]->neigh,orig_tria[i]->neigh_no*sizeof(ts_triangle *));    
SP 160 }
161     memcpy(bck_bond,bond,sizeof(ts_bond));
162 //fprintf(stderr,"Backup complete!!!\n");
163 /* end backup vertex */
164
414b8a 165 /* Save old energy */
b866cf 166   oldenergy=0;
M 167   oldenergy+=k->xk* k->energy;
168   oldenergy+=kp->xk* kp->energy;
169   oldenergy+=km->xk* km->energy;
170   oldenergy+=it->xk* it->energy;
032273 171   oldenergy+=bond->energy; /* attraction with neighboring vertices, that have spontaneous curvature */
414b8a 172   //Neigbours of k, it, km, kp don't change its energy.
aec47d 173
1121fa 174     if(vesicle->pswitch == 1 || vesicle->tape->constvolswitch>0){dvol = -lm->volume - lp->volume;}
c0ae90 175     if(vesicle->tape->constareaswitch==2){darea=-lm->area-lp->area;} 
dd5aca 176 /*    vesicle_volume(vesicle);
SP 177     fprintf(stderr,"Volume in the beginning=%1.16e\n", vesicle->volume);
178 */
414b8a 179
M 180 /* fix data structure for flipped bond */
032273 181     ts_flip_bond(k,it,km,kp, bond,lm, lp, lm2, lp1, vesicle->tape->w);
aec47d 182
SP 183
b866cf 184 /* Calculating the new energy */
M 185   delta_energy=0;
186   delta_energy+=k->xk* k->energy;
187   delta_energy+=kp->xk* kp->energy;
188   delta_energy+=km->xk* km->energy;
189   delta_energy+=it->xk* it->energy;
032273 190   delta_energy+=bond->energy; /* attraction with neighboring vertices, that have spontaneous curvature */
414b8a 191   //Neigbours of k, it, km, kp don't change its energy.
M 192
fbcbdf 193     delta_energy-=oldenergy;
1121fa 194     if(vesicle->pswitch == 1 || vesicle->tape->constvolswitch>0){
414b8a 195         dvol = dvol + lm->volume + lp->volume;
fbcbdf 196         if(vesicle->pswitch==1) delta_energy-= vesicle->pressure*dvol;
414b8a 197     }
c0ae90 198     if(vesicle->tape->constareaswitch==2){
SP 199         darea=darea+lm->area+lp->area; 
200 /*check whether the dvol is gt than epsvol */
201         if(fabs(vesicle->area+darea-A0)>epsarea){
202             //restore old state.
203             /* restoration procedure copied from few lines below */
204                 for(i=0;i<4;i++){
205             //            fprintf(stderr,"Restoring vtx neigh[%d] with neighbours %d\n",i, orig_vtx[i]->neigh_no );
206                 free(orig_vtx[i]->neigh);
207                 free(orig_vtx[i]->tristar);
208                 free(orig_vtx[i]->bond);
209                 free(orig_tria[i]->neigh);
210                 memcpy((void *)orig_vtx[i],(void *)bck_vtx[i],sizeof(ts_vertex));
211                 memcpy((void *)orig_tria[i],(void *)bck_tria[i],sizeof(ts_triangle));
212             //            fprintf(stderr,"Restored vtx neigh[%d] with neighbours %d\n",i, orig_vtx[i]->neigh_no );
213                 /* level 2 pointers are redirected*/
214                 }
215                 memcpy(bond,bck_bond,sizeof(ts_bond));
216                 for(i=0;i<4;i++){
217                 free(bck_vtx[i]);
218                 free(bck_tria[i]);
219             /*            fprintf(stderr,"Restoring vtx neigh[%d] with neighbours %d =",i, orig_vtx[i]->neigh_no );
220                 for(j=0;j<orig_vtx[i]->neigh_no;j++) fprintf(stderr," %d", orig_vtx[i]->neigh[j]->idx);
221                 fprintf(stderr,"\n"); */
222                 }
223                 free(bck_bond);
224                 return TS_FAIL;
225
226         }
227     }
228
229
230
fbcbdf 231
1121fa 232     if(vesicle->tape->constvolswitch == 2){
SP 233         /*check whether the dvol is gt than epsvol */
234         if(fabs(vesicle->volume+dvol-V0)>epsvol){
235             //restore old state.
236             /* restoration procedure copied from few lines below */
237                 for(i=0;i<4;i++){
238             //            fprintf(stderr,"Restoring vtx neigh[%d] with neighbours %d\n",i, orig_vtx[i]->neigh_no );
239                 free(orig_vtx[i]->neigh);
240                 free(orig_vtx[i]->tristar);
241                 free(orig_vtx[i]->bond);
242                 free(orig_tria[i]->neigh);
243                 memcpy((void *)orig_vtx[i],(void *)bck_vtx[i],sizeof(ts_vertex));
244                 memcpy((void *)orig_tria[i],(void *)bck_tria[i],sizeof(ts_triangle));
245             //            fprintf(stderr,"Restored vtx neigh[%d] with neighbours %d\n",i, orig_vtx[i]->neigh_no );
246                 /* level 2 pointers are redirected*/
247                 }
248                 memcpy(bond,bck_bond,sizeof(ts_bond));
249                 for(i=0;i<4;i++){
250                 free(bck_vtx[i]);
251                 free(bck_tria[i]);
252             /*            fprintf(stderr,"Restoring vtx neigh[%d] with neighbours %d =",i, orig_vtx[i]->neigh_no );
253                 for(j=0;j<orig_vtx[i]->neigh_no;j++) fprintf(stderr," %d", orig_vtx[i]->neigh[j]->idx);
254                 fprintf(stderr,"\n"); */
255                 }
256                 free(bck_bond);
257                 return TS_FAIL;
258
259         }
260
261     } else
fbcbdf 262     if(vesicle->tape->constvolswitch == 1){
dd5aca 263         retval=constvolume(vesicle, it, -dvol, &delta_energy_cv, &constvol_vtx_moved,&constvol_vtx_backup);
fbcbdf 264         if(retval==TS_FAIL){
SP 265 /* restoration procedure copied from few lines below */
dd5aca 266             for(i=0;i<4;i++){
SP 267     //            fprintf(stderr,"Restoring vtx neigh[%d] with neighbours %d\n",i, orig_vtx[i]->neigh_no );
268                 free(orig_vtx[i]->neigh);
269                 free(orig_vtx[i]->tristar);
270                 free(orig_vtx[i]->bond);
271                 free(orig_tria[i]->neigh);
272                 memcpy((void *)orig_vtx[i],(void *)bck_vtx[i],sizeof(ts_vertex));
273                 memcpy((void *)orig_tria[i],(void *)bck_tria[i],sizeof(ts_triangle));
274     //            fprintf(stderr,"Restored vtx neigh[%d] with neighbours %d\n",i, orig_vtx[i]->neigh_no );
275                 /* level 2 pointers are redirected*/
276             }
277             memcpy(bond,bck_bond,sizeof(ts_bond));
278             for(i=0;i<4;i++){
279                 free(bck_vtx[i]);
280                 free(bck_tria[i]);
281     /*            fprintf(stderr,"Restoring vtx neigh[%d] with neighbours %d =",i, orig_vtx[i]->neigh_no );
282                 for(j=0;j<orig_vtx[i]->neigh_no;j++) fprintf(stderr," %d", orig_vtx[i]->neigh[j]->idx);
283                 fprintf(stderr,"\n"); */
284             }
285             free(bck_bond);
286             return TS_FAIL;
fbcbdf 287         }
b2fa8c 288         delta_energy+=delta_energy_cv;
fbcbdf 289     }
SP 290
414b8a 291
b866cf 292 /* MONTE CARLO */
M 293     if(delta_energy>=0){
294 #ifdef TS_DOUBLE_DOUBLE
fbcbdf 295         if(exp(-delta_energy)< drand48())
b866cf 296 #endif
M 297 #ifdef TS_DOUBLE_FLOAT
298         if(expf(-delta_energy)< (ts_float)drand48())
299 #endif
300 #ifdef TS_DOUBLE_LONGDOUBLE
301         if(expl(-delta_energy)< (ts_ldouble)drand48())
302 #endif
303         {
304             //not accepted, reverting changes
7efbb1 305         //restore all backups
SP 306 //        fprintf(stderr,"Restoring!!!\n");
b2fa8c 307         if(vesicle->tape->constvolswitch == 1){
SP 308             constvolumerestore(constvol_vtx_moved,constvol_vtx_backup);
309         }
aec47d 310
7efbb1 311         for(i=0;i<4;i++){
SP 312 //            fprintf(stderr,"Restoring vtx neigh[%d] with neighbours %d\n",i, orig_vtx[i]->neigh_no );
313             free(orig_vtx[i]->neigh);
314             free(orig_vtx[i]->tristar);
315             free(orig_vtx[i]->bond);
316             free(orig_tria[i]->neigh);
317             memcpy((void *)orig_vtx[i],(void *)bck_vtx[i],sizeof(ts_vertex));
318             memcpy((void *)orig_tria[i],(void *)bck_tria[i],sizeof(ts_triangle));
319 //            fprintf(stderr,"Restored vtx neigh[%d] with neighbours %d\n",i, orig_vtx[i]->neigh_no );
320             /* level 2 pointers are redirected*/
321         }
322         memcpy(bond,bck_bond,sizeof(ts_bond));
2870ab 323
7efbb1 324         for(i=0;i<4;i++){
SP 325             free(bck_vtx[i]);
326             free(bck_tria[i]);
327 /*            fprintf(stderr,"Restoring vtx neigh[%d] with neighbours %d =",i, orig_vtx[i]->neigh_no );
328             for(j=0;j<orig_vtx[i]->neigh_no;j++) fprintf(stderr," %d", orig_vtx[i]->neigh[j]->idx);
329             fprintf(stderr,"\n"); */
330         }
2870ab 331
SP 332         free(bck_bond);
fbcbdf 333
7efbb1 334 //        fprintf(stderr,"Restoration complete!!!\n");
dd5aca 335 //    vesicle_volume(vesicle);
SP 336 //    fprintf(stderr,"Volume after fail=%1.16e\n", vesicle->volume);
2870ab 337
7efbb1 338         return TS_FAIL;
b866cf 339         }
M 340     }
341      /* IF BONDFLIP ACCEPTED, THEN RETURN SUCCESS! */
fbcbdf 342 //            fprintf(stderr,"SUCCESS!!!\n");
2870ab 343
c0ae90 344     if(vesicle->tape->constvolswitch == 2){
SP 345         vesicle->volume+=dvol;
346     } else if(vesicle->tape->constvolswitch == 1){
b2fa8c 347         constvolumeaccept(vesicle,constvol_vtx_moved,constvol_vtx_backup);
SP 348     }
c0ae90 349     if(vesicle->tape->constareaswitch==2){
SP 350         vesicle->area+=darea;
351     }
2870ab 352     // delete all backups
SP 353     for(i=0;i<4;i++){
7efbb1 354     free(bck_vtx[i]->neigh);
SP 355     free(bck_vtx[i]->bond);
356     free(bck_vtx[i]->tristar);
357     free(bck_vtx[i]);
2870ab 358      free(bck_tria[i]->neigh);
SP 359         free(bck_tria[i]);
7efbb1 360 /*    fprintf(stderr,"Afret backup deletion vtx neigh[%d]=",i);
SP 361     for(j=0;j<orig_vtx[i]->neigh_no;j++) fprintf(stderr," %d", orig_vtx[i]->neigh[j]->idx);
362     fprintf(stderr,"\n");
363 */    
2870ab 364     }
7efbb1 365     free(bck_bond);
SP 366
dd5aca 367 //    vesicle_volume(vesicle);
SP 368 //    fprintf(stderr,"Volume after success=%1.16e\n", vesicle->volume);
b866cf 369     return TS_SUCCESS;
aec47d 370 }
SP 371
372
b866cf 373 ts_bool ts_flip_bond(ts_vertex *k,ts_vertex *it,ts_vertex *km, ts_vertex *kp,
032273 374 ts_bond *bond, ts_triangle *lm, ts_triangle *lp, ts_triangle *lm2, ts_triangle *lp1, ts_double w_energy){
b866cf 375
M 376     ts_uint i; //lmidx, lpidx;
377 if(k==NULL || it==NULL || km==NULL || kp==NULL){
378     fatal("ts_flip_bond: You called me with invalid pointers to vertices",999);
379 }
aec47d 380 // 2. step. We change the triangle vertices... (actual bond flip)
SP 381     for(i=0;i<3;i++) if(lm->vertex[i]== it) lm->vertex[i]= kp;
382     for(i=0;i<3;i++) if(lp->vertex[i]== k) lp->vertex[i]= km;
e19e79 383 //fprintf(stderr,"2. step: actual bondflip made\n");
aec47d 384 // 2a. step. If any changes in triangle calculations must be done, do it here!
SP 385 //   * normals are recalculated here
386     triangle_normal_vector(lp);
387     triangle_normal_vector(lm);
e19e79 388 //fprintf(stderr,"2a. step: triangle normals recalculated\n");
aec47d 389 // 3. step. Correct neighbours in vertex_list
SP 390
391
30ee9c 392             vtx_remove_neighbour(k,it);
306559 393 //            vtx_remove_neighbour(it,k);
e19e79 394 //fprintf(stderr,"3. step (PROGRESS): removed k and it neighbours\n");
306559 395     
aec47d 396             //Tukaj pa nastopi tezava... Kam dodati soseda?
30ee9c 397             vtx_insert_neighbour(km,kp,k);
SP 398             vtx_insert_neighbour(kp,km,it);
aec47d 399 //            vertex_add_neighbour(km,kp); //pazi na vrstni red.
SP 400 //            vertex_add_neighbour(kp,km);
e19e79 401 //fprintf(stderr,"3. step: vertex neighbours corrected\n");
aec47d 402
SP 403 // 3a. step. If any changes to ts_vertex, do it here!
404 //   bond_length calculatons not required for it is done in energy.c
405
406 // 4. step. Correct bond_list (don't know why I still have it!)
407             bond->vtx1=km;
408             bond->vtx2=kp;
409 //fprintf(stderr,"4. step: bondlist corrected\n");
410
411
412 // 5. step. Correct neighbouring triangles 
413    
414     triangle_remove_neighbour(lp,lp1);
e19e79 415   //  fprintf(stderr,".\n");
aec47d 416     triangle_remove_neighbour(lp1,lp);
SP 417   //  fprintf(stderr,".\n");
418     triangle_remove_neighbour(lm,lm2);
419   //  fprintf(stderr,".\n");
420     triangle_remove_neighbour(lm2,lm);
421    
422     triangle_add_neighbour(lm,lp1);    
423     triangle_add_neighbour(lp1,lm);
424     triangle_add_neighbour(lp,lm2);  //Vrstni red?!
425     triangle_add_neighbour(lm2,lp);
426
427 //fprintf(stderr,"5. step: triangle neigbours corrected\n");
428
429
430 // 6. step. Correct tristar for vertices km, kp, k and it
431             vertex_add_tristar(km,lp);  // Preveri vrstni red!
432             vertex_add_tristar(kp,lm);
30ee9c 433             vtx_remove_tristar(it,lm);
SP 434             vtx_remove_tristar(k,lp);
aec47d 435 //fprintf(stderr,"6. step: tristar corrected\n");
SP 436   energy_vertex(k);
437   energy_vertex(kp);
438   energy_vertex(km);
439   energy_vertex(it);
032273 440   attraction_bond_energy(bond, w_energy);
aec47d 441 // END modifications to data structure!
SP 442     return TS_SUCCESS;
443 }