Trisurf Monte Carlo simulator
mihaf
2014-03-21 bcf4557ddd61890f130247430267feac5b05c5b7
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>
aec47d 15
SP 16 ts_bool single_bondflip_timestep(ts_vesicle *vesicle, ts_bond *bond, ts_double *rn){
17 /*c  Vertex and triangle (lm and lp) indexing for bond flip:
18 c      +----- k-------+              +----- k ------+
19 c      |lm1 / | \ lp1 |              |lm1 /   \ lp1 |
20 c      |  /   |   \   |              |  /       \   |
21 c      |/     |     \ |     FLIP     |/    lm     \ |
22 c     km  lm  | lp   kp    --->      km ---------- kp  
23 c      |\     |     / |              |\    lp     / |  
24 c      |  \   |   /   |              |  \       /   |
25 c      |lm2 \ | / lp2 |              |lm2 \   / lp2 |
26 c      +------it------+              +----- it -----+
27 c
28 */
29     ts_vertex *it=bond->vtx1;
30     ts_vertex *k=bond->vtx2;
31     ts_uint nei,neip,neim;
b866cf 32     ts_uint i,j;
414b8a 33     ts_double oldenergy, delta_energy, dvol=0.0;
b866cf 34     ts_triangle *lm=NULL,*lp=NULL, *lp1=NULL, *lm2=NULL;
aec47d 35
SP 36     ts_vertex *kp,*km;
37
38     if(it->neigh_no< 3) return TS_FAIL;
39     if(k->neigh_no< 3) return TS_FAIL;
40     if(k==NULL || it==NULL){
41         fatal("In bondflip, number of neighbours of k or it is less than 3!",999);
42     }
43
30ee9c 44     nei=0;
aec47d 45     for(i=0;i<it->neigh_no;i++){ // Finds the nn of it, that is k 
SP 46         if(it->neigh[i]==k){
47             nei=i;
48             break;
49         }
50     }
51     neip=nei+1;  // I don't like it.. Smells like I must have it in correct order
52     neim=nei-1;
53     if(neip>=it->neigh_no) neip=0;
54     if((ts_int)neim<0) neim=it->neigh_no-1; /* casting is essential... If not
55 there the neim is never <0 !!! */
56   //  fprintf(stderr,"The numbers are: %u %u\n",neip, neim);
57     km=it->neigh[neim];  // We located km and kp
58     kp=it->neigh[neip];
59
60     if(km==NULL || kp==NULL){
61         fatal("In bondflip, cannot determine km and kp!",999);
62     }
63
64   //  fprintf(stderr,"I WAS HERE! after the 4 vertices are known!\n");
65
66 /* test if the membrane is wrapped too much, so that kp is nearest neighbour of
67  * km. If it is true, then don't flip! */
68     for(i=0;i<km->neigh_no;i++){
69         if(km->neigh[i] == kp) return TS_FAIL;
70     }
71  //   fprintf(stderr,"Membrane didn't wrap too much.. Continue.\n");
72 /* if bond would be too long, return... */
30ee9c 73     if(vtx_distance_sq(km,kp) > vesicle->dmax ) return TS_FAIL;
aec47d 74  //   fprintf(stderr,"Bond will not be too long.. Continue.\n");
SP 75
76 /* we make a bond flip. this is different than in original fortran */
b866cf 77 // find lm, lp
aec47d 78 // 1. step. We find lm and lp from k->tristar !
SP 79     for(i=0;i<it->tristar_no;i++){
80         for(j=0;j<k->tristar_no;j++){
81             if((it->tristar[i] == k->tristar[j])){ //ce gre za skupen trikotnik
82                 if((it->tristar[i]->vertex[0] == km || it->tristar[i]->vertex[1]
83 == km || it->tristar[i]->vertex[2]== km )){
84                 lm=it->tristar[i];
85          //       lmidx=i;
86                 }
87                 else
88                 {
89                 lp=it->tristar[i];
90          //       lpidx=i;
91                 }
92
93             }
94         }
95     }
96 if(lm==NULL || lp==NULL) fatal("ts_flip_bond: Cannot find triangles lm and lp!",999);
97
98 //we look for important triangles lp1 and lm2.
99
100  for(i=0;i<k->tristar_no;i++){
101         for(j=0;j<kp->tristar_no;j++){
102                 if((k->tristar[i] == kp->tristar[j]) && k->tristar[i]!=lp){ //ce gre za skupen trikotnik
103                     lp1=k->tristar[i];
104             }
105         }
106 }
107
108  for(i=0;i<it->tristar_no;i++){
109         for(j=0;j<km->tristar_no;j++){
110             if((it->tristar[i] == km->tristar[j]) && it->tristar[i]!=lm){ //ce gre za skupen trikotnik
111                     lm2=it->tristar[i];
112             } 
113         }
114     }
115
116 if(lm2==NULL || lp1==NULL) fatal("ts_flip_bond: Cannot find triangles lm2 and lp1!",999);
2870ab 117
SP 118
119 /* backup old structure */
120 /* need to backup:
121  * vertices k, kp, km, it
122  * triangles lm, lp, lm2, lp1
123  * bond
124  */
125 ts_vertex *bck_vtx[4];
126 ts_triangle *bck_tria[4];
127 ts_bond *bck_bond;
128 ts_vertex *orig_vtx[]={k,it,kp,km};
7efbb1 129 ts_triangle *orig_tria[]={lm,lp,lm2,lp1};
2870ab 130
7efbb1 131 //fprintf(stderr,"Backuping!!!\n");
SP 132     bck_bond=(ts_bond *)malloc(sizeof(ts_bond));
2870ab 133 for(i=0;i<4;i++){
7efbb1 134 /*    fprintf(stderr,"vtx neigh[%d]=",i);
SP 135     for(j=0;j<orig_vtx[i]->neigh_no;j++) fprintf(stderr," %d", orig_vtx[i]->neigh[j]->idx);
136     fprintf(stderr,"\n");
137 */
2870ab 138     bck_vtx[i]=(ts_vertex *)malloc(sizeof(ts_vertex));
SP 139     bck_tria[i]=(ts_triangle *)malloc(sizeof(ts_triangle));
7efbb1 140     memcpy((void *)bck_vtx[i],(void *)orig_vtx[i],sizeof(ts_vertex));
SP 141     memcpy((void *)bck_tria[i],(void *)orig_tria[i],sizeof(ts_triangle));
2870ab 142     /* level 2 pointers */
7efbb1 143
2870ab 144     bck_vtx[i]->neigh=(ts_vertex **)malloc(orig_vtx[i]->neigh_no*sizeof(ts_vertex *));
SP 145     bck_vtx[i]->tristar=(ts_triangle **)malloc(orig_vtx[i]->tristar_no*sizeof(ts_triangle *));
146     bck_vtx[i]->bond=(ts_bond **)malloc(orig_vtx[i]->bond_no*sizeof(ts_bond *));
147     bck_tria[i]->neigh=(ts_triangle **)malloc(orig_tria[i]->neigh_no*sizeof(ts_triangle *));
148
7efbb1 149     memcpy((void *)bck_vtx[i]->neigh,(void *)orig_vtx[i]->neigh,orig_vtx[i]->neigh_no*sizeof(ts_vertex *));
SP 150     memcpy((void *)bck_vtx[i]->tristar,(void *)orig_vtx[i]->tristar,orig_vtx[i]->tristar_no*sizeof(ts_triangle *));
151     memcpy((void *)bck_vtx[i]->bond,(void *)orig_vtx[i]->bond,orig_vtx[i]->bond_no*sizeof(ts_bond *));
b866cf 152     
7efbb1 153     memcpy((void *)bck_tria[i]->neigh,(void *)orig_tria[i]->neigh,orig_tria[i]->neigh_no*sizeof(ts_triangle *));    
SP 154 }
155     memcpy(bck_bond,bond,sizeof(ts_bond));
156 //fprintf(stderr,"Backup complete!!!\n");
157 /* end backup vertex */
158
414b8a 159 /* Save old energy */
b866cf 160   oldenergy=0;
M 161   oldenergy+=k->xk* k->energy;
162   oldenergy+=kp->xk* kp->energy;
163   oldenergy+=km->xk* km->energy;
164   oldenergy+=it->xk* it->energy;
414b8a 165   //Neigbours of k, it, km, kp don't change its energy.
aec47d 166
414b8a 167     if(vesicle->pswitch == 1){dvol = -lm->volume - lp->volume;}
M 168
169 /* fix data structure for flipped bond */
170     ts_flip_bond(k,it,km,kp, bond,lm, lp, lm2, lp1);
aec47d 171
SP 172
b866cf 173 /* Calculating the new energy */
M 174   delta_energy=0;
175   delta_energy+=k->xk* k->energy;
176   delta_energy+=kp->xk* kp->energy;
177   delta_energy+=km->xk* km->energy;
178   delta_energy+=it->xk* it->energy;
414b8a 179   //Neigbours of k, it, km, kp don't change its energy.
M 180
b866cf 181   delta_energy-=oldenergy;
414b8a 182     if(vesicle->pswitch == 1){
M 183         dvol = dvol + lm->volume + lp->volume;
184         delta_energy-= vesicle->pressure*dvol;
185     }
186
b866cf 187 /* MONTE CARLO */
M 188     if(delta_energy>=0){
189 #ifdef TS_DOUBLE_DOUBLE
190         if(exp(-delta_energy)< drand48() )
191 #endif
192 #ifdef TS_DOUBLE_FLOAT
193         if(expf(-delta_energy)< (ts_float)drand48())
194 #endif
195 #ifdef TS_DOUBLE_LONGDOUBLE
196         if(expl(-delta_energy)< (ts_ldouble)drand48())
197 #endif
198         {
199             //not accepted, reverting changes
7efbb1 200         //restore all backups
SP 201 //        fprintf(stderr,"Restoring!!!\n");
aec47d 202
7efbb1 203         for(i=0;i<4;i++){
SP 204 //            fprintf(stderr,"Restoring vtx neigh[%d] with neighbours %d\n",i, orig_vtx[i]->neigh_no );
205             free(orig_vtx[i]->neigh);
206             free(orig_vtx[i]->tristar);
207             free(orig_vtx[i]->bond);
208             free(orig_tria[i]->neigh);
209             memcpy((void *)orig_vtx[i],(void *)bck_vtx[i],sizeof(ts_vertex));
210             memcpy((void *)orig_tria[i],(void *)bck_tria[i],sizeof(ts_triangle));
211 //            fprintf(stderr,"Restored vtx neigh[%d] with neighbours %d\n",i, orig_vtx[i]->neigh_no );
212             /* level 2 pointers are redirected*/
213         }
214         memcpy(bond,bck_bond,sizeof(ts_bond));
2870ab 215
7efbb1 216         for(i=0;i<4;i++){
SP 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         }
2870ab 223
SP 224         free(bck_bond);
7efbb1 225 //        fprintf(stderr,"Restoration complete!!!\n");
2870ab 226
7efbb1 227         return TS_FAIL;
b866cf 228         }
M 229     }
230      /* IF BONDFLIP ACCEPTED, THEN RETURN SUCCESS! */
7efbb1 231   //          fprintf(stderr,"SUCCESS!!!\n");
2870ab 232
SP 233     // delete all backups
234     for(i=0;i<4;i++){
7efbb1 235     free(bck_vtx[i]->neigh);
SP 236     free(bck_vtx[i]->bond);
237     free(bck_vtx[i]->tristar);
238     free(bck_vtx[i]);
2870ab 239      free(bck_tria[i]->neigh);
SP 240         free(bck_tria[i]);
7efbb1 241 /*    fprintf(stderr,"Afret backup deletion vtx neigh[%d]=",i);
SP 242     for(j=0;j<orig_vtx[i]->neigh_no;j++) fprintf(stderr," %d", orig_vtx[i]->neigh[j]->idx);
243     fprintf(stderr,"\n");
244 */    
2870ab 245     }
7efbb1 246     free(bck_bond);
SP 247
b866cf 248     return TS_SUCCESS;
aec47d 249 }
SP 250
251
b866cf 252 ts_bool ts_flip_bond(ts_vertex *k,ts_vertex *it,ts_vertex *km, ts_vertex *kp,
M 253 ts_bond *bond, ts_triangle *lm, ts_triangle *lp, ts_triangle *lm2, ts_triangle *lp1){
254
255     ts_uint i; //lmidx, lpidx;
256 if(k==NULL || it==NULL || km==NULL || kp==NULL){
257     fatal("ts_flip_bond: You called me with invalid pointers to vertices",999);
258 }
aec47d 259 // 2. step. We change the triangle vertices... (actual bond flip)
SP 260     for(i=0;i<3;i++) if(lm->vertex[i]== it) lm->vertex[i]= kp;
261     for(i=0;i<3;i++) if(lp->vertex[i]== k) lp->vertex[i]= km;
e19e79 262 //fprintf(stderr,"2. step: actual bondflip made\n");
aec47d 263 // 2a. step. If any changes in triangle calculations must be done, do it here!
SP 264 //   * normals are recalculated here
265     triangle_normal_vector(lp);
266     triangle_normal_vector(lm);
e19e79 267 //fprintf(stderr,"2a. step: triangle normals recalculated\n");
aec47d 268 // 3. step. Correct neighbours in vertex_list
SP 269
270
30ee9c 271             vtx_remove_neighbour(k,it);
306559 272 //            vtx_remove_neighbour(it,k);
e19e79 273 //fprintf(stderr,"3. step (PROGRESS): removed k and it neighbours\n");
306559 274     
aec47d 275             //Tukaj pa nastopi tezava... Kam dodati soseda?
30ee9c 276             vtx_insert_neighbour(km,kp,k);
SP 277             vtx_insert_neighbour(kp,km,it);
aec47d 278 //            vertex_add_neighbour(km,kp); //pazi na vrstni red.
SP 279 //            vertex_add_neighbour(kp,km);
e19e79 280 //fprintf(stderr,"3. step: vertex neighbours corrected\n");
aec47d 281
SP 282 // 3a. step. If any changes to ts_vertex, do it here!
283 //   bond_length calculatons not required for it is done in energy.c
284
285 // 4. step. Correct bond_list (don't know why I still have it!)
286             bond->vtx1=km;
287             bond->vtx2=kp;
288 //fprintf(stderr,"4. step: bondlist corrected\n");
289
290
291 // 5. step. Correct neighbouring triangles 
292    
293     triangle_remove_neighbour(lp,lp1);
e19e79 294   //  fprintf(stderr,".\n");
aec47d 295     triangle_remove_neighbour(lp1,lp);
SP 296   //  fprintf(stderr,".\n");
297     triangle_remove_neighbour(lm,lm2);
298   //  fprintf(stderr,".\n");
299     triangle_remove_neighbour(lm2,lm);
300    
301     triangle_add_neighbour(lm,lp1);    
302     triangle_add_neighbour(lp1,lm);
303     triangle_add_neighbour(lp,lm2);  //Vrstni red?!
304     triangle_add_neighbour(lm2,lp);
305
306 //fprintf(stderr,"5. step: triangle neigbours corrected\n");
307
308
309 // 6. step. Correct tristar for vertices km, kp, k and it
310             vertex_add_tristar(km,lp);  // Preveri vrstni red!
311             vertex_add_tristar(kp,lm);
30ee9c 312             vtx_remove_tristar(it,lm);
SP 313             vtx_remove_tristar(k,lp);
aec47d 314 //fprintf(stderr,"6. step: tristar corrected\n");
SP 315   energy_vertex(k);
316   energy_vertex(kp);
317   energy_vertex(km);
318   energy_vertex(it);
319 // END modifications to data structure!
320     return TS_SUCCESS;
321 }