Trisurf Monte Carlo simulator
Samo Penic
2013-12-09 d06a9476717d788c426e88b6365d7a863928b5c9
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>
14
15 ts_bool single_bondflip_timestep(ts_vesicle *vesicle, ts_bond *bond, ts_double *rn){
16 /*c  Vertex and triangle (lm and lp) indexing for bond flip:
17 c      +----- k-------+              +----- k ------+
18 c      |lm1 / | \ lp1 |              |lm1 /   \ lp1 |
19 c      |  /   |   \   |              |  /       \   |
20 c      |/     |     \ |     FLIP     |/    lm     \ |
21 c     km  lm  | lp   kp    --->      km ---------- kp  
22 c      |\     |     / |              |\    lp     / |  
23 c      |  \   |   /   |              |  \       /   |
24 c      |lm2 \ | / lp2 |              |lm2 \   / lp2 |
25 c      +------it------+              +----- it -----+
26 c
27 */
28     ts_vertex *it=bond->vtx1;
29     ts_vertex *k=bond->vtx2;
d06a94 30   //  ts_uint nei,neip,neim;
SP 31     ts_uint i;
aec47d 32     ts_double oldenergy, delta_energy;
d06a94 33 #ifdef DEBUG
SP 34     if(bond->adjvtx[0]==NULL || bond->adjvtx[1]==NULL) fatal ("single_bondflip_timestep: Adjvertex for called bond is null. This should not happen!",999);
35 #endif
36     ts_vertex *kp=bond->adjvtx[1],*km=bond->adjvtx[0];
37 //    ts_triangle *lm=NULL, *lp=NULL;
aec47d 38
SP 39 /* test if the membrane is wrapped too much, so that kp is nearest neighbour of
40  * km. If it is true, then don't flip! */
d06a94 41 /*TODO: dec2013, i think this never happens!
SP 42       for(i=0;i<km->neigh->n;i++){
20ae83 43         if(km->neigh->vtx[i] == kp) return TS_FAIL;
aec47d 44     }
d06a94 45 */
SP 46
47
aec47d 48  //   fprintf(stderr,"Membrane didn't wrap too much.. Continue.\n");
SP 49 /* if bond would be too long, return... */
d06a94 50     if(vtx_distance_sq(bond->adjvtx[0],bond->adjvtx[1]) > vesicle->dmax ) return TS_FAIL;
SP 51   //  fprintf(stderr,"Bond will not be too long.. Continue.\n");
aec47d 52
SP 53 /* we make a bond flip. this is different than in original fortran */
54 // 0. step. Get memory prior the flip
55   oldenergy=0;
56   oldenergy+=k->xk* k->energy;
57   oldenergy+=kp->xk* kp->energy;
58   oldenergy+=km->xk* km->energy;
59   oldenergy+=it->xk* it->energy;
60 //  for(i=0;i<k->neigh_no;i++) oldenergy+=k->neigh[i]->xk*k->neigh[i]->energy;
61 //  for(i=0;i<kp->neigh_no;i++) oldenergy+=kp->neigh[i]->xk*kp->neigh[i]->energy;
62 //  for(i=0;i<km->neigh_no;i++) oldenergy+=km->neigh[i]->xk*km->neigh[i]->energy;
63 //  for(i=0;i<it->neigh_no;i++) oldenergy+=it->neigh[i]->xk*it->neigh[i]->energy;
e19e79 64 /*
306559 65 fprintf(stderr,"*** Naslov k=%ld\n",(long)k);
SP 66 fprintf(stderr,"*** Naslov it=%ld\n",(long)it);
67 fprintf(stderr,"*** Naslov km=%ld\n",(long)km);
68 fprintf(stderr,"*** Naslov kp=%ld\n",(long)kp);
aec47d 69
SP 70 for(i=0;i<k->neigh_no;i++)
306559 71     fprintf(stderr,"k sosed=%ld\n",(long)k->neigh[i]);
aec47d 72 for(i=0;i<it->neigh_no;i++)
306559 73     fprintf(stderr,"it sosed=%ld\n",(long)it->neigh[i]);
aec47d 74
SP 75 for(i=0;i<km->neigh_no;i++)
306559 76     fprintf(stderr,"km sosed=%ld\n",(long)km->neigh[i]);
aec47d 77 for(i=0;i<kp->neigh_no;i++)
306559 78     fprintf(stderr,"kp sosed=%ld\n",(long)kp->neigh[i]);
aec47d 79
SP 80
e19e79 81 */
d06a94 82  //   fprintf(stderr,"I WAS HERE! Before bondflip!\n");
SP 83     ts_flip_bond(bond);
e19e79 84 //    fprintf(stderr,"I WAS HERE! Bondflip successful!\n");
aec47d 85
SP 86 /* Calculating the new energy */
87   delta_energy=0;
d06a94 88 /* TODO: why this neighs are recalculated? Not necessary! */
20ae83 89   for(i=0;i<k->neigh->n;i++) energy_vertex(k->neigh->vtx[i]);
SP 90   for(i=0;i<kp->neigh->n;i++) energy_vertex(kp->neigh->vtx[i]);
91   for(i=0;i<km->neigh->n;i++) energy_vertex(km->neigh->vtx[i]);
92   for(i=0;i<it->neigh->n;i++) energy_vertex(it->neigh->vtx[i]);
aec47d 93   delta_energy+=k->xk* k->energy;
SP 94   delta_energy+=kp->xk* kp->energy;
95   delta_energy+=km->xk* km->energy;
96   delta_energy+=it->xk* it->energy;
97 //  for(i=0;i<k->neigh_no;i++) delta_energy+=k->neigh[i]->xk*k->neigh[i]->energy;
98 //  for(i=0;i<kp->neigh_no;i++) delta_energy+=kp->neigh[i]->xk*kp->neigh[i]->energy;
99 //  for(i=0;i<km->neigh_no;i++) delta_energy+=km->neigh[i]->xk*km->neigh[i]->energy;
100 //  for(i=0;i<it->neigh_no;i++) delta_energy+=it->neigh[i]->xk*it->neigh[i]->energy;
101   delta_energy-=oldenergy;
102  // fprintf(stderr,"I WAS HERE! Got energy!\n");
103 /* MONTE CARLO */
104     if(delta_energy>=0){
105 #ifdef TS_DOUBLE_DOUBLE
106         if(exp(-delta_energy)< drand48() )
107 #endif
108 #ifdef TS_DOUBLE_FLOAT
109         if(expf(-delta_energy)< (ts_float)drand48())
110 #endif
111 #ifdef TS_DOUBLE_LONGDOUBLE
112         if(expl(-delta_energy)< (ts_ldouble)drand48())
113 #endif
114         {
115             //not accepted, reverting changes
116        //     fprintf(stderr,"Failed to move, due to MC\n");
117
118 //            ts_flip_bond(km,kp,it,k, bond);
d06a94 119             ts_flip_bond(bond);
aec47d 120                 
SP 121
122 /*
123 fprintf(stderr,"*** Naslov k=%d\n",k);
124 fprintf(stderr,"*** Naslov it=%d\n",it);
125 fprintf(stderr,"*** Naslov km=%d\n",km);
126 fprintf(stderr,"*** Naslov kp=%d\n",kp);
127 for(i=0;i<k->neigh_no;i++)
128     fprintf(stderr,"k sosed=%d\n",k->neigh[i]);
129 for(i=0;i<it->neigh_no;i++)
130     fprintf(stderr,"it sosed=%d\n",it->neigh[i]);
131
132
133 for(i=0;i<km->neigh_no;i++)
134     fprintf(stderr,"km sosed=%d\n",km->neigh[i]);
135 for(i=0;i<kp->neigh_no;i++)
136     fprintf(stderr,"kp sosed=%d\n",kp->neigh[i]);
137 */
138
139
140
141         //    fprintf(stderr,"Reverted condition!\n");
142             return TS_FAIL;
143         }
144     }
145         //    fprintf(stderr,"Success\n");
146
147
148 /* IF BONDFLIP ACCEPTED, THEN RETURN SUCCESS! */
149     return TS_SUCCESS;
150 }
151
152
d06a94 153 ts_bool ts_flip_bond(ts_bond *bond){
SP 154 fprintf(stderr,"Called!\n");
aec47d 155     ts_triangle *lm=NULL,*lp=NULL, *lp1=NULL, *lm2=NULL;
d06a94 156     ts_uint i; //lmidx, lpidx;
SP 157 /*This is no longer necessary! if(k==NULL || it==NULL || km==NULL || kp==NULL){
aec47d 158     fatal("ts_flip_bond: You called me with invalid pointers to vertices",999);
d06a94 159 }*/
aec47d 160 // 1. step. We find lm and lp from k->tristar !
d06a94 161 /*    for(i=0;i<it->tristar_no;i++){
aec47d 162         for(j=0;j<k->tristar_no;j++){
SP 163             if((it->tristar[i] == k->tristar[j])){ //ce gre za skupen trikotnik
164                 if((it->tristar[i]->vertex[0] == km || it->tristar[i]->vertex[1]
165 == km || it->tristar[i]->vertex[2]== km )){
166                 lm=it->tristar[i];
167          //       lmidx=i;
168                 }
169                 else
170                 {
171                 lp=it->tristar[i];
172          //       lpidx=i;
173                 }
174
175             }
176         }
177     }
178 if(lm==NULL || lp==NULL) fatal("ts_flip_bond: Cannot find triangles lm and lp!",999);
179
180 //we look for important triangles lp1 and lm2.
181
182  for(i=0;i<k->tristar_no;i++){
183         for(j=0;j<kp->tristar_no;j++){
184                 if((k->tristar[i] == kp->tristar[j]) && k->tristar[i]!=lp){ //ce gre za skupen trikotnik
185                     lp1=k->tristar[i];
186             }
187         }
188 }
189
190  for(i=0;i<it->tristar_no;i++){
191         for(j=0;j<km->tristar_no;j++){
192             if((it->tristar[i] == km->tristar[j]) && it->tristar[i]!=lm){ //ce gre za skupen trikotnik
193                     lm2=it->tristar[i];
194             } 
195         }
196     }
d06a94 197 */
SP 198 //Upper block no longer neccessary.
199 ts_vertex *it=bond->vtx1;
200 ts_vertex *k=bond->vtx2;
201 ts_vertex *km=bond->adjvtx[0];
202 ts_vertex *kp=bond->adjvtx[1];
203 lm=bond->tria[0];
204 lp=bond->tria[1];
aec47d 205 /*
SP 206 // DEBUG TESTING!
207 fprintf(stderr,"*** Naslov k=%d\n",k);
208 fprintf(stderr,"*** Naslov it=%d\n",it);
209 fprintf(stderr,"*** Naslov km=%d\n",km);
210 fprintf(stderr,"*** Naslov kp=%d\n",kp);
211
212 for(i=0;i<k->neigh_no;i++)
213     fprintf(stderr,"k sosed=%d\n",k->neigh[i]);
214 for(i=0;i<it->neigh_no;i++)
215     fprintf(stderr,"it sosed=%d\n",it->neigh[i]);
216
217
218 // END DEBUG TESTING!
219 */
d06a94 220 //find bonds between k and kp (adj[1] vtx)
SP 221 //find bonds between it and km (adj[0] vtx)
222 for(i=0;i<it->tristar_no;i++){
223     if((it->tristar[i]->vertex[0]==km || it->tristar[i]->vertex[1]==km || it->tristar[i]->vertex[2]==km) && (it->tristar[i]->vertex[0]==it || it->tristar[i]->vertex[1]==it || it->tristar[i]->vertex[2]==it)){
224         lm2=it->tristar[i];
225     }
226     else if ((it->tristar[i]->vertex[0]==kp || it->tristar[i]->vertex[1]==kp || it->tristar[i]->vertex[2]==kp) && (it->tristar[i]->vertex[0]==k || it->tristar[i]->vertex[1]==k || it->tristar[i]->vertex[2]==k)){
227         lp1=it->tristar[i];
228     }
229 }
aec47d 230 if(lm2==NULL || lp1==NULL) fatal("ts_flip_bond: Cannot find triangles lm2 and lp1!",999);
e19e79 231 /*
aec47d 232 //DEBUG TESTING
306559 233 fprintf(stderr,"1. step: lm, lm2, lp1 and lp found!\n");
SP 234 fprintf(stderr,"--- Naslov lm=%ld",(long)lm);
aec47d 235
SP 236
306559 237 fprintf(stderr,"   vtxs(%ld, %ld, %ld)\n",(long)lm->vertex[0],(long)lm->vertex[1], (long)lm->vertex[2]);
SP 238 fprintf(stderr,"--- Naslov lp=%ld",(long)lp);
239 fprintf(stderr,"   vtxs(%ld, %ld, %ld)\n",(long)lp->vertex[0],(long)lp->vertex[1], (long)lp->vertex[2]);
240 fprintf(stderr,"--- Naslov lm2=%ld",(long)lm2);
241 fprintf(stderr,"   vtxs(%ld, %ld, %ld)\n",(long)lm2->vertex[0],(long)lm2->vertex[1], (long)lm2->vertex[2]);
242 fprintf(stderr,"--- Naslov lp1=%ld",(long)lp1);
243 fprintf(stderr,"   vtxs(%ld, %ld, %ld)\n",(long)lp1->vertex[0],(long)lp1->vertex[1], (long)lp1->vertex[2]);
aec47d 244
SP 245 for(i=0;i<lm->neigh_no;i++)
306559 246     fprintf(stderr,"lm sosed=%ld\n",(long)lm->neigh[i]);
aec47d 247 for(i=0;i<lp->neigh_no;i++)
306559 248     fprintf(stderr,"lp sosed=%ld\n",(long)lp->neigh[i]);
aec47d 249 // END DEBUG TESTING
e19e79 250 */
aec47d 251 /*
SP 252 // DEBUG TESTING!
253
254 for(i=0;i<3;i++){
255
256     if(lp1->neigh[i]==lp) fprintf(stderr,"Nasel sem par lp1->lp\n");
257     if(lp->neigh[i]==lp1) fprintf(stderr,"Nasel sem par lp->lp1\n");
258     if(lm2->neigh[i]==lm) fprintf(stderr,"Nasel sem par lm2->lm\n");
259     if(lm->neigh[i]==lm2) fprintf(stderr,"Nasel sem par lm->lm2\n");
260 }
261 // END DEBUG TESTING!
262 */
263
264
265 // 2. step. We change the triangle vertices... (actual bond flip)
266     for(i=0;i<3;i++) if(lm->vertex[i]== it) lm->vertex[i]= kp;
267     for(i=0;i<3;i++) if(lp->vertex[i]== k) lp->vertex[i]= km;
e19e79 268 //fprintf(stderr,"2. step: actual bondflip made\n");
aec47d 269 // 2a. step. If any changes in triangle calculations must be done, do it here!
SP 270 //   * normals are recalculated here
271     triangle_normal_vector(lp);
272     triangle_normal_vector(lm);
e19e79 273 //fprintf(stderr,"2a. step: triangle normals recalculated\n");
aec47d 274 // 3. step. Correct neighbours in vertex_list
SP 275
276
20ae83 277             vertex_list_remove_vtx(k->neigh, it);
SP 278             vertex_list_remove_vtx(it->neigh, k);
279     //vtx_remove_neighbour(k,it);
306559 280 //            vtx_remove_neighbour(it,k);
e19e79 281 //fprintf(stderr,"3. step (PROGRESS): removed k and it neighbours\n");
306559 282     
aec47d 283             //Tukaj pa nastopi tezava... Kam dodati soseda?
20ae83 284         
SP 285 //            vtx_insert_neighbour(km,kp,k);
286 //            vtx_insert_neighbour(kp,km,it);
287             vtx_add_neighbour(km,kp); //pazi na vrstni red.
288             vtx_add_neighbour(kp,km);
e19e79 289 //fprintf(stderr,"3. step: vertex neighbours corrected\n");
aec47d 290
SP 291 // 3a. step. If any changes to ts_vertex, do it here!
292 //   bond_length calculatons not required for it is done in energy.c
293
294 // 4. step. Correct bond_list (don't know why I still have it!)
295             bond->vtx1=km;
296             bond->vtx2=kp;
297 //fprintf(stderr,"4. step: bondlist corrected\n");
298
299
300 // 5. step. Correct neighbouring triangles 
301    
302     triangle_remove_neighbour(lp,lp1);
e19e79 303   //  fprintf(stderr,".\n");
aec47d 304     triangle_remove_neighbour(lp1,lp);
SP 305   //  fprintf(stderr,".\n");
306     triangle_remove_neighbour(lm,lm2);
307   //  fprintf(stderr,".\n");
308     triangle_remove_neighbour(lm2,lm);
309    
310     triangle_add_neighbour(lm,lp1);    
311     triangle_add_neighbour(lp1,lm);
312     triangle_add_neighbour(lp,lm2);  //Vrstni red?!
313     triangle_add_neighbour(lm2,lp);
314
315 //fprintf(stderr,"5. step: triangle neigbours corrected\n");
316
317
318 // 6. step. Correct tristar for vertices km, kp, k and it
319             vertex_add_tristar(km,lp);  // Preveri vrstni red!
320             vertex_add_tristar(kp,lm);
30ee9c 321             vtx_remove_tristar(it,lm);
SP 322             vtx_remove_tristar(k,lp);
aec47d 323 //fprintf(stderr,"6. step: tristar corrected\n");
SP 324
325 /*
326 //DEBUG TESTING
327 fprintf(stderr,"--- Naslov lm=%d",lm);
328
329
330 fprintf(stderr,"   vtxs(%d, %d, %d)\n",lm->vertex[0],lm->vertex[1], lm->vertex[2]);
331 fprintf(stderr,"--- Naslov lp=%d",lp);
332 fprintf(stderr,"   vtxs(%d, %d, %d)\n",lp->vertex[0],lp->vertex[1], lp->vertex[2]);
333 fprintf(stderr,"--- Naslov lm2=%d",lm2);
334 fprintf(stderr,"   vtxs(%d, %d, %d)\n",lm2->vertex[0],lm2->vertex[1], lm2->vertex[2]);
335 fprintf(stderr,"--- Naslov lp1=%d",lp1);
336 fprintf(stderr,"   vtxs(%d, %d, %d)\n",lp1->vertex[0],lp1->vertex[1], lp1->vertex[2]);
337
338 for(i=0;i<lm->neigh_no;i++)
339     fprintf(stderr,"lm sosed=%d\n",lm->neigh[i]);
340 for(i=0;i<lp->neigh_no;i++)
341     fprintf(stderr,"lp sosed=%d\n",lp->neigh[i]);
342 // END DEBUG TESTING
343 */
344   energy_vertex(k);
345   energy_vertex(kp);
346   energy_vertex(km);
347   energy_vertex(it);
348
d06a94 349 //Extra steps for new bond data structure
SP 350 bond->adjvtx[0]=k;
351 bond->adjvtx[1]=it;
aec47d 352
SP 353 // END modifications to data structure!
354
355
356     return TS_SUCCESS;
357 }