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