Trisurf Monte Carlo simulator
Samo Penic
2014-09-11 5a19989febd5054ff64c5a45a906ca033cdf67cc
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;
414b8a 34     ts_double oldenergy, delta_energy, dvol=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;}
dd5aca 173       
SP 174 /*    vesicle_volume(vesicle);
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     }
fbcbdf 195
1121fa 196     if(vesicle->tape->constvolswitch == 2){
SP 197         /*check whether the dvol is gt than epsvol */
198         if(fabs(vesicle->volume+dvol-V0)>epsvol){
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     } else
fbcbdf 226     if(vesicle->tape->constvolswitch == 1){
dd5aca 227         retval=constvolume(vesicle, it, -dvol, &delta_energy_cv, &constvol_vtx_moved,&constvol_vtx_backup);
fbcbdf 228         if(retval==TS_FAIL){
SP 229 /* restoration procedure copied from few lines below */
dd5aca 230             for(i=0;i<4;i++){
SP 231     //            fprintf(stderr,"Restoring vtx neigh[%d] with neighbours %d\n",i, orig_vtx[i]->neigh_no );
232                 free(orig_vtx[i]->neigh);
233                 free(orig_vtx[i]->tristar);
234                 free(orig_vtx[i]->bond);
235                 free(orig_tria[i]->neigh);
236                 memcpy((void *)orig_vtx[i],(void *)bck_vtx[i],sizeof(ts_vertex));
237                 memcpy((void *)orig_tria[i],(void *)bck_tria[i],sizeof(ts_triangle));
238     //            fprintf(stderr,"Restored vtx neigh[%d] with neighbours %d\n",i, orig_vtx[i]->neigh_no );
239                 /* level 2 pointers are redirected*/
240             }
241             memcpy(bond,bck_bond,sizeof(ts_bond));
242             for(i=0;i<4;i++){
243                 free(bck_vtx[i]);
244                 free(bck_tria[i]);
245     /*            fprintf(stderr,"Restoring vtx neigh[%d] with neighbours %d =",i, orig_vtx[i]->neigh_no );
246                 for(j=0;j<orig_vtx[i]->neigh_no;j++) fprintf(stderr," %d", orig_vtx[i]->neigh[j]->idx);
247                 fprintf(stderr,"\n"); */
248             }
249             free(bck_bond);
250             return TS_FAIL;
fbcbdf 251         }
b2fa8c 252         delta_energy+=delta_energy_cv;
fbcbdf 253     }
SP 254
414b8a 255
b866cf 256 /* MONTE CARLO */
M 257     if(delta_energy>=0){
258 #ifdef TS_DOUBLE_DOUBLE
fbcbdf 259         if(exp(-delta_energy)< drand48())
b866cf 260 #endif
M 261 #ifdef TS_DOUBLE_FLOAT
262         if(expf(-delta_energy)< (ts_float)drand48())
263 #endif
264 #ifdef TS_DOUBLE_LONGDOUBLE
265         if(expl(-delta_energy)< (ts_ldouble)drand48())
266 #endif
267         {
268             //not accepted, reverting changes
7efbb1 269         //restore all backups
SP 270 //        fprintf(stderr,"Restoring!!!\n");
b2fa8c 271         if(vesicle->tape->constvolswitch == 1){
SP 272             constvolumerestore(constvol_vtx_moved,constvol_vtx_backup);
273         }
aec47d 274
7efbb1 275         for(i=0;i<4;i++){
SP 276 //            fprintf(stderr,"Restoring vtx neigh[%d] with neighbours %d\n",i, orig_vtx[i]->neigh_no );
277             free(orig_vtx[i]->neigh);
278             free(orig_vtx[i]->tristar);
279             free(orig_vtx[i]->bond);
280             free(orig_tria[i]->neigh);
281             memcpy((void *)orig_vtx[i],(void *)bck_vtx[i],sizeof(ts_vertex));
282             memcpy((void *)orig_tria[i],(void *)bck_tria[i],sizeof(ts_triangle));
283 //            fprintf(stderr,"Restored vtx neigh[%d] with neighbours %d\n",i, orig_vtx[i]->neigh_no );
284             /* level 2 pointers are redirected*/
285         }
286         memcpy(bond,bck_bond,sizeof(ts_bond));
2870ab 287
7efbb1 288         for(i=0;i<4;i++){
SP 289             free(bck_vtx[i]);
290             free(bck_tria[i]);
291 /*            fprintf(stderr,"Restoring vtx neigh[%d] with neighbours %d =",i, orig_vtx[i]->neigh_no );
292             for(j=0;j<orig_vtx[i]->neigh_no;j++) fprintf(stderr," %d", orig_vtx[i]->neigh[j]->idx);
293             fprintf(stderr,"\n"); */
294         }
2870ab 295
SP 296         free(bck_bond);
fbcbdf 297
7efbb1 298 //        fprintf(stderr,"Restoration complete!!!\n");
dd5aca 299 //    vesicle_volume(vesicle);
SP 300 //    fprintf(stderr,"Volume after fail=%1.16e\n", vesicle->volume);
2870ab 301
7efbb1 302         return TS_FAIL;
b866cf 303         }
M 304     }
305      /* IF BONDFLIP ACCEPTED, THEN RETURN SUCCESS! */
fbcbdf 306 //            fprintf(stderr,"SUCCESS!!!\n");
2870ab 307
1121fa 308  if(vesicle->tape->constvolswitch == 2){
SP 309     vesicle->volume+=dvol;
310     } else
b2fa8c 311     if(vesicle->tape->constvolswitch == 1){
SP 312         constvolumeaccept(vesicle,constvol_vtx_moved,constvol_vtx_backup);
313     }
2870ab 314     // delete all backups
SP 315     for(i=0;i<4;i++){
7efbb1 316     free(bck_vtx[i]->neigh);
SP 317     free(bck_vtx[i]->bond);
318     free(bck_vtx[i]->tristar);
319     free(bck_vtx[i]);
2870ab 320      free(bck_tria[i]->neigh);
SP 321         free(bck_tria[i]);
7efbb1 322 /*    fprintf(stderr,"Afret backup deletion vtx neigh[%d]=",i);
SP 323     for(j=0;j<orig_vtx[i]->neigh_no;j++) fprintf(stderr," %d", orig_vtx[i]->neigh[j]->idx);
324     fprintf(stderr,"\n");
325 */    
2870ab 326     }
7efbb1 327     free(bck_bond);
SP 328
dd5aca 329 //    vesicle_volume(vesicle);
SP 330 //    fprintf(stderr,"Volume after success=%1.16e\n", vesicle->volume);
b866cf 331     return TS_SUCCESS;
aec47d 332 }
SP 333
334
b866cf 335 ts_bool ts_flip_bond(ts_vertex *k,ts_vertex *it,ts_vertex *km, ts_vertex *kp,
M 336 ts_bond *bond, ts_triangle *lm, ts_triangle *lp, ts_triangle *lm2, ts_triangle *lp1){
337
338     ts_uint i; //lmidx, lpidx;
339 if(k==NULL || it==NULL || km==NULL || kp==NULL){
340     fatal("ts_flip_bond: You called me with invalid pointers to vertices",999);
341 }
aec47d 342 // 2. step. We change the triangle vertices... (actual bond flip)
SP 343     for(i=0;i<3;i++) if(lm->vertex[i]== it) lm->vertex[i]= kp;
344     for(i=0;i<3;i++) if(lp->vertex[i]== k) lp->vertex[i]= km;
e19e79 345 //fprintf(stderr,"2. step: actual bondflip made\n");
aec47d 346 // 2a. step. If any changes in triangle calculations must be done, do it here!
SP 347 //   * normals are recalculated here
348     triangle_normal_vector(lp);
349     triangle_normal_vector(lm);
e19e79 350 //fprintf(stderr,"2a. step: triangle normals recalculated\n");
aec47d 351 // 3. step. Correct neighbours in vertex_list
SP 352
353
30ee9c 354             vtx_remove_neighbour(k,it);
306559 355 //            vtx_remove_neighbour(it,k);
e19e79 356 //fprintf(stderr,"3. step (PROGRESS): removed k and it neighbours\n");
306559 357     
aec47d 358             //Tukaj pa nastopi tezava... Kam dodati soseda?
30ee9c 359             vtx_insert_neighbour(km,kp,k);
SP 360             vtx_insert_neighbour(kp,km,it);
aec47d 361 //            vertex_add_neighbour(km,kp); //pazi na vrstni red.
SP 362 //            vertex_add_neighbour(kp,km);
e19e79 363 //fprintf(stderr,"3. step: vertex neighbours corrected\n");
aec47d 364
SP 365 // 3a. step. If any changes to ts_vertex, do it here!
366 //   bond_length calculatons not required for it is done in energy.c
367
368 // 4. step. Correct bond_list (don't know why I still have it!)
369             bond->vtx1=km;
370             bond->vtx2=kp;
371 //fprintf(stderr,"4. step: bondlist corrected\n");
372
373
374 // 5. step. Correct neighbouring triangles 
375    
376     triangle_remove_neighbour(lp,lp1);
e19e79 377   //  fprintf(stderr,".\n");
aec47d 378     triangle_remove_neighbour(lp1,lp);
SP 379   //  fprintf(stderr,".\n");
380     triangle_remove_neighbour(lm,lm2);
381   //  fprintf(stderr,".\n");
382     triangle_remove_neighbour(lm2,lm);
383    
384     triangle_add_neighbour(lm,lp1);    
385     triangle_add_neighbour(lp1,lm);
386     triangle_add_neighbour(lp,lm2);  //Vrstni red?!
387     triangle_add_neighbour(lm2,lp);
388
389 //fprintf(stderr,"5. step: triangle neigbours corrected\n");
390
391
392 // 6. step. Correct tristar for vertices km, kp, k and it
393             vertex_add_tristar(km,lp);  // Preveri vrstni red!
394             vertex_add_tristar(kp,lm);
30ee9c 395             vtx_remove_tristar(it,lm);
SP 396             vtx_remove_tristar(k,lp);
aec47d 397 //fprintf(stderr,"6. step: tristar corrected\n");
SP 398   energy_vertex(k);
399   energy_vertex(kp);
400   energy_vertex(km);
401   energy_vertex(it);
402 // END modifications to data structure!
403     return TS_SUCCESS;
404 }