Trisurf Monte Carlo simulator
Samo Penic
2016-04-07 b122c46c95b96febda4ebf3c8ddfa79998d599f9
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;
414b8a 171   //Neigbours of k, it, km, kp don't change its energy.
aec47d 172
1121fa 173     if(vesicle->pswitch == 1 || vesicle->tape->constvolswitch>0){dvol = -lm->volume - lp->volume;}
c0ae90 174     if(vesicle->tape->constareaswitch==2){darea=-lm->area-lp->area;} 
dd5aca 175 /*    vesicle_volume(vesicle);
SP 176     fprintf(stderr,"Volume in the beginning=%1.16e\n", vesicle->volume);
177 */
414b8a 178
M 179 /* fix data structure for flipped bond */
180     ts_flip_bond(k,it,km,kp, bond,lm, lp, lm2, lp1);
aec47d 181
SP 182
b866cf 183 /* Calculating the new energy */
M 184   delta_energy=0;
185   delta_energy+=k->xk* k->energy;
186   delta_energy+=kp->xk* kp->energy;
187   delta_energy+=km->xk* km->energy;
188   delta_energy+=it->xk* it->energy;
414b8a 189   //Neigbours of k, it, km, kp don't change its energy.
M 190
fbcbdf 191     delta_energy-=oldenergy;
1121fa 192     if(vesicle->pswitch == 1 || vesicle->tape->constvolswitch>0){
414b8a 193         dvol = dvol + lm->volume + lp->volume;
fbcbdf 194         if(vesicle->pswitch==1) delta_energy-= vesicle->pressure*dvol;
414b8a 195     }
c0ae90 196     if(vesicle->tape->constareaswitch==2){
SP 197         darea=darea+lm->area+lp->area; 
198 /*check whether the dvol is gt than epsvol */
199         if(fabs(vesicle->area+darea-A0)>epsarea){
200             //restore old state.
201             /* restoration procedure copied from few lines below */
202                 for(i=0;i<4;i++){
203             //            fprintf(stderr,"Restoring vtx neigh[%d] with neighbours %d\n",i, orig_vtx[i]->neigh_no );
204                 free(orig_vtx[i]->neigh);
205                 free(orig_vtx[i]->tristar);
206                 free(orig_vtx[i]->bond);
207                 free(orig_tria[i]->neigh);
208                 memcpy((void *)orig_vtx[i],(void *)bck_vtx[i],sizeof(ts_vertex));
209                 memcpy((void *)orig_tria[i],(void *)bck_tria[i],sizeof(ts_triangle));
210             //            fprintf(stderr,"Restored vtx neigh[%d] with neighbours %d\n",i, orig_vtx[i]->neigh_no );
211                 /* level 2 pointers are redirected*/
212                 }
213                 memcpy(bond,bck_bond,sizeof(ts_bond));
214                 for(i=0;i<4;i++){
215                 free(bck_vtx[i]);
216                 free(bck_tria[i]);
217             /*            fprintf(stderr,"Restoring vtx neigh[%d] with neighbours %d =",i, orig_vtx[i]->neigh_no );
218                 for(j=0;j<orig_vtx[i]->neigh_no;j++) fprintf(stderr," %d", orig_vtx[i]->neigh[j]->idx);
219                 fprintf(stderr,"\n"); */
220                 }
221                 free(bck_bond);
222                 return TS_FAIL;
223
224         }
225     }
226
227
228
fbcbdf 229
1121fa 230     if(vesicle->tape->constvolswitch == 2){
SP 231         /*check whether the dvol is gt than epsvol */
232         if(fabs(vesicle->volume+dvol-V0)>epsvol){
233             //restore old state.
234             /* restoration procedure copied from few lines below */
235                 for(i=0;i<4;i++){
236             //            fprintf(stderr,"Restoring vtx neigh[%d] with neighbours %d\n",i, orig_vtx[i]->neigh_no );
237                 free(orig_vtx[i]->neigh);
238                 free(orig_vtx[i]->tristar);
239                 free(orig_vtx[i]->bond);
240                 free(orig_tria[i]->neigh);
241                 memcpy((void *)orig_vtx[i],(void *)bck_vtx[i],sizeof(ts_vertex));
242                 memcpy((void *)orig_tria[i],(void *)bck_tria[i],sizeof(ts_triangle));
243             //            fprintf(stderr,"Restored vtx neigh[%d] with neighbours %d\n",i, orig_vtx[i]->neigh_no );
244                 /* level 2 pointers are redirected*/
245                 }
246                 memcpy(bond,bck_bond,sizeof(ts_bond));
247                 for(i=0;i<4;i++){
248                 free(bck_vtx[i]);
249                 free(bck_tria[i]);
250             /*            fprintf(stderr,"Restoring vtx neigh[%d] with neighbours %d =",i, orig_vtx[i]->neigh_no );
251                 for(j=0;j<orig_vtx[i]->neigh_no;j++) fprintf(stderr," %d", orig_vtx[i]->neigh[j]->idx);
252                 fprintf(stderr,"\n"); */
253                 }
254                 free(bck_bond);
255                 return TS_FAIL;
256
257         }
258
259     } else
fbcbdf 260     if(vesicle->tape->constvolswitch == 1){
dd5aca 261         retval=constvolume(vesicle, it, -dvol, &delta_energy_cv, &constvol_vtx_moved,&constvol_vtx_backup);
fbcbdf 262         if(retval==TS_FAIL){
SP 263 /* restoration procedure copied from few lines below */
dd5aca 264             for(i=0;i<4;i++){
SP 265     //            fprintf(stderr,"Restoring vtx neigh[%d] with neighbours %d\n",i, orig_vtx[i]->neigh_no );
266                 free(orig_vtx[i]->neigh);
267                 free(orig_vtx[i]->tristar);
268                 free(orig_vtx[i]->bond);
269                 free(orig_tria[i]->neigh);
270                 memcpy((void *)orig_vtx[i],(void *)bck_vtx[i],sizeof(ts_vertex));
271                 memcpy((void *)orig_tria[i],(void *)bck_tria[i],sizeof(ts_triangle));
272     //            fprintf(stderr,"Restored vtx neigh[%d] with neighbours %d\n",i, orig_vtx[i]->neigh_no );
273                 /* level 2 pointers are redirected*/
274             }
275             memcpy(bond,bck_bond,sizeof(ts_bond));
276             for(i=0;i<4;i++){
277                 free(bck_vtx[i]);
278                 free(bck_tria[i]);
279     /*            fprintf(stderr,"Restoring vtx neigh[%d] with neighbours %d =",i, orig_vtx[i]->neigh_no );
280                 for(j=0;j<orig_vtx[i]->neigh_no;j++) fprintf(stderr," %d", orig_vtx[i]->neigh[j]->idx);
281                 fprintf(stderr,"\n"); */
282             }
283             free(bck_bond);
284             return TS_FAIL;
fbcbdf 285         }
b2fa8c 286         delta_energy+=delta_energy_cv;
fbcbdf 287     }
SP 288
414b8a 289
b866cf 290 /* MONTE CARLO */
M 291     if(delta_energy>=0){
292 #ifdef TS_DOUBLE_DOUBLE
fbcbdf 293         if(exp(-delta_energy)< drand48())
b866cf 294 #endif
M 295 #ifdef TS_DOUBLE_FLOAT
296         if(expf(-delta_energy)< (ts_float)drand48())
297 #endif
298 #ifdef TS_DOUBLE_LONGDOUBLE
299         if(expl(-delta_energy)< (ts_ldouble)drand48())
300 #endif
301         {
302             //not accepted, reverting changes
7efbb1 303         //restore all backups
SP 304 //        fprintf(stderr,"Restoring!!!\n");
b2fa8c 305         if(vesicle->tape->constvolswitch == 1){
SP 306             constvolumerestore(constvol_vtx_moved,constvol_vtx_backup);
307         }
aec47d 308
7efbb1 309         for(i=0;i<4;i++){
SP 310 //            fprintf(stderr,"Restoring vtx neigh[%d] with neighbours %d\n",i, orig_vtx[i]->neigh_no );
311             free(orig_vtx[i]->neigh);
312             free(orig_vtx[i]->tristar);
313             free(orig_vtx[i]->bond);
314             free(orig_tria[i]->neigh);
315             memcpy((void *)orig_vtx[i],(void *)bck_vtx[i],sizeof(ts_vertex));
316             memcpy((void *)orig_tria[i],(void *)bck_tria[i],sizeof(ts_triangle));
317 //            fprintf(stderr,"Restored vtx neigh[%d] with neighbours %d\n",i, orig_vtx[i]->neigh_no );
318             /* level 2 pointers are redirected*/
319         }
320         memcpy(bond,bck_bond,sizeof(ts_bond));
2870ab 321
7efbb1 322         for(i=0;i<4;i++){
SP 323             free(bck_vtx[i]);
324             free(bck_tria[i]);
325 /*            fprintf(stderr,"Restoring vtx neigh[%d] with neighbours %d =",i, orig_vtx[i]->neigh_no );
326             for(j=0;j<orig_vtx[i]->neigh_no;j++) fprintf(stderr," %d", orig_vtx[i]->neigh[j]->idx);
327             fprintf(stderr,"\n"); */
328         }
2870ab 329
SP 330         free(bck_bond);
fbcbdf 331
7efbb1 332 //        fprintf(stderr,"Restoration complete!!!\n");
dd5aca 333 //    vesicle_volume(vesicle);
SP 334 //    fprintf(stderr,"Volume after fail=%1.16e\n", vesicle->volume);
2870ab 335
7efbb1 336         return TS_FAIL;
b866cf 337         }
M 338     }
339      /* IF BONDFLIP ACCEPTED, THEN RETURN SUCCESS! */
fbcbdf 340 //            fprintf(stderr,"SUCCESS!!!\n");
2870ab 341
c0ae90 342     if(vesicle->tape->constvolswitch == 2){
SP 343         vesicle->volume+=dvol;
344     } else if(vesicle->tape->constvolswitch == 1){
b2fa8c 345         constvolumeaccept(vesicle,constvol_vtx_moved,constvol_vtx_backup);
SP 346     }
c0ae90 347     if(vesicle->tape->constareaswitch==2){
SP 348         vesicle->area+=darea;
349     }
2870ab 350     // delete all backups
SP 351     for(i=0;i<4;i++){
7efbb1 352     free(bck_vtx[i]->neigh);
SP 353     free(bck_vtx[i]->bond);
354     free(bck_vtx[i]->tristar);
355     free(bck_vtx[i]);
2870ab 356      free(bck_tria[i]->neigh);
SP 357         free(bck_tria[i]);
7efbb1 358 /*    fprintf(stderr,"Afret backup deletion vtx neigh[%d]=",i);
SP 359     for(j=0;j<orig_vtx[i]->neigh_no;j++) fprintf(stderr," %d", orig_vtx[i]->neigh[j]->idx);
360     fprintf(stderr,"\n");
361 */    
2870ab 362     }
7efbb1 363     free(bck_bond);
SP 364
dd5aca 365 //    vesicle_volume(vesicle);
SP 366 //    fprintf(stderr,"Volume after success=%1.16e\n", vesicle->volume);
b866cf 367     return TS_SUCCESS;
aec47d 368 }
SP 369
370
b866cf 371 ts_bool ts_flip_bond(ts_vertex *k,ts_vertex *it,ts_vertex *km, ts_vertex *kp,
M 372 ts_bond *bond, ts_triangle *lm, ts_triangle *lp, ts_triangle *lm2, ts_triangle *lp1){
373
374     ts_uint i; //lmidx, lpidx;
375 if(k==NULL || it==NULL || km==NULL || kp==NULL){
376     fatal("ts_flip_bond: You called me with invalid pointers to vertices",999);
377 }
aec47d 378 // 2. step. We change the triangle vertices... (actual bond flip)
SP 379     for(i=0;i<3;i++) if(lm->vertex[i]== it) lm->vertex[i]= kp;
380     for(i=0;i<3;i++) if(lp->vertex[i]== k) lp->vertex[i]= km;
e19e79 381 //fprintf(stderr,"2. step: actual bondflip made\n");
aec47d 382 // 2a. step. If any changes in triangle calculations must be done, do it here!
SP 383 //   * normals are recalculated here
384     triangle_normal_vector(lp);
385     triangle_normal_vector(lm);
e19e79 386 //fprintf(stderr,"2a. step: triangle normals recalculated\n");
aec47d 387 // 3. step. Correct neighbours in vertex_list
SP 388
389
30ee9c 390             vtx_remove_neighbour(k,it);
306559 391 //            vtx_remove_neighbour(it,k);
e19e79 392 //fprintf(stderr,"3. step (PROGRESS): removed k and it neighbours\n");
306559 393     
aec47d 394             //Tukaj pa nastopi tezava... Kam dodati soseda?
30ee9c 395             vtx_insert_neighbour(km,kp,k);
SP 396             vtx_insert_neighbour(kp,km,it);
aec47d 397 //            vertex_add_neighbour(km,kp); //pazi na vrstni red.
SP 398 //            vertex_add_neighbour(kp,km);
e19e79 399 //fprintf(stderr,"3. step: vertex neighbours corrected\n");
aec47d 400
SP 401 // 3a. step. If any changes to ts_vertex, do it here!
402 //   bond_length calculatons not required for it is done in energy.c
403
404 // 4. step. Correct bond_list (don't know why I still have it!)
405             bond->vtx1=km;
406             bond->vtx2=kp;
407 //fprintf(stderr,"4. step: bondlist corrected\n");
408
409
410 // 5. step. Correct neighbouring triangles 
411    
412     triangle_remove_neighbour(lp,lp1);
e19e79 413   //  fprintf(stderr,".\n");
aec47d 414     triangle_remove_neighbour(lp1,lp);
SP 415   //  fprintf(stderr,".\n");
416     triangle_remove_neighbour(lm,lm2);
417   //  fprintf(stderr,".\n");
418     triangle_remove_neighbour(lm2,lm);
419    
420     triangle_add_neighbour(lm,lp1);    
421     triangle_add_neighbour(lp1,lm);
422     triangle_add_neighbour(lp,lm2);  //Vrstni red?!
423     triangle_add_neighbour(lm2,lp);
424
425 //fprintf(stderr,"5. step: triangle neigbours corrected\n");
426
427
428 // 6. step. Correct tristar for vertices km, kp, k and it
429             vertex_add_tristar(km,lp);  // Preveri vrstni red!
430             vertex_add_tristar(kp,lm);
30ee9c 431             vtx_remove_tristar(it,lm);
SP 432             vtx_remove_tristar(k,lp);
aec47d 433 //fprintf(stderr,"6. step: tristar corrected\n");
SP 434   energy_vertex(k);
435   energy_vertex(kp);
436   energy_vertex(km);
437   energy_vertex(it);
438 // END modifications to data structure!
439     return TS_SUCCESS;
440 }