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