Trisurf Monte Carlo simulator
Samo Penic
2014-03-05 719c9febac2eaff9483fda487b57684afbb59bb2
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 ------+
719c9f 18 c      |lm1 / ^ \ lp1 |              |lm1 /   \ lp1 |
aec47d 19 c      |  /   |   \   |              |  /       \   |
SP 20 c      |/     |     \ |     FLIP     |/    lm     \ |
719c9f 21 c     km  lm  | lp   kp    --->      km ---------> kp  
aec47d 22 c      |\     |     / |              |\    lp     / |  
SP 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
719c9f 116             fprintf(stderr,"Failed to move, due to MC\n");
aec47d 117
SP 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     }
719c9f 145             fprintf(stderr,"Success\n");
aec47d 146
SP 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;
30ee9c 156     ts_uint i,j; //lmidx, lpidx;
aec47d 157
d06a94 158 ts_vertex *it=bond->vtx1;
SP 159 ts_vertex *k=bond->vtx2;
160 ts_vertex *km=bond->adjvtx[0];
161 ts_vertex *kp=bond->adjvtx[1];
162 lm=bond->tria[0];
163 lp=bond->tria[1];
aec47d 164 /*
SP 165 // DEBUG TESTING!
166 fprintf(stderr,"*** Naslov k=%d\n",k);
167 fprintf(stderr,"*** Naslov it=%d\n",it);
168 fprintf(stderr,"*** Naslov km=%d\n",km);
169 fprintf(stderr,"*** Naslov kp=%d\n",kp);
170
171 for(i=0;i<k->neigh_no;i++)
172     fprintf(stderr,"k sosed=%d\n",k->neigh[i]);
173 for(i=0;i<it->neigh_no;i++)
174     fprintf(stderr,"it sosed=%d\n",it->neigh[i]);
175
176
177 // END DEBUG TESTING!
178 */
d06a94 179 //find bonds between k and kp (adj[1] vtx)
SP 180 //find bonds between it and km (adj[0] vtx)
719c9f 181
SP 182 for(i=0;i<3;i++){
183 if(lm->neigh[i]==lp) break;
184     for(j=0;j<3;j++){
185         if(lm->neigh[i]->vertex[j]==it) lm2=lm->neigh[i];
186     }
187 }
188
189 for(i=0;i<3;i++){
190 if(lp->neigh[i]==lm) break;
191     for(j=0;j<3;j++){
192         if(lp->neigh[i]->vertex[j]==k) lp1=lp->neigh[i];
193     }
194 }
195
196 /*
d06a94 197 for(i=0;i<it->tristar_no;i++){
SP 198     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)){
199         lm2=it->tristar[i];
200     }
201     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)){
202         lp1=it->tristar[i];
203     }
204 }
719c9f 205 */
SP 206 if(lm2==NULL ) fatal("ts_flip_bond: Cannot find triangles lm2!",999);
207 if(lp1==NULL ) fatal("ts_flip_bond: Cannot find triangles lp1!",999);
aec47d 208 if(lm2==NULL || lp1==NULL) fatal("ts_flip_bond: Cannot find triangles lm2 and lp1!",999);
SP 209
e19e79 210 /*
aec47d 211 //DEBUG TESTING
306559 212 fprintf(stderr,"1. step: lm, lm2, lp1 and lp found!\n");
SP 213 fprintf(stderr,"--- Naslov lm=%ld",(long)lm);
aec47d 214
SP 215
306559 216 fprintf(stderr,"   vtxs(%ld, %ld, %ld)\n",(long)lm->vertex[0],(long)lm->vertex[1], (long)lm->vertex[2]);
SP 217 fprintf(stderr,"--- Naslov lp=%ld",(long)lp);
218 fprintf(stderr,"   vtxs(%ld, %ld, %ld)\n",(long)lp->vertex[0],(long)lp->vertex[1], (long)lp->vertex[2]);
219 fprintf(stderr,"--- Naslov lm2=%ld",(long)lm2);
220 fprintf(stderr,"   vtxs(%ld, %ld, %ld)\n",(long)lm2->vertex[0],(long)lm2->vertex[1], (long)lm2->vertex[2]);
221 fprintf(stderr,"--- Naslov lp1=%ld",(long)lp1);
222 fprintf(stderr,"   vtxs(%ld, %ld, %ld)\n",(long)lp1->vertex[0],(long)lp1->vertex[1], (long)lp1->vertex[2]);
aec47d 223
SP 224 for(i=0;i<lm->neigh_no;i++)
306559 225     fprintf(stderr,"lm sosed=%ld\n",(long)lm->neigh[i]);
aec47d 226 for(i=0;i<lp->neigh_no;i++)
306559 227     fprintf(stderr,"lp sosed=%ld\n",(long)lp->neigh[i]);
aec47d 228 // END DEBUG TESTING
e19e79 229 */
aec47d 230 /*
SP 231 // DEBUG TESTING!
232
233 for(i=0;i<3;i++){
234
235     if(lp1->neigh[i]==lp) fprintf(stderr,"Nasel sem par lp1->lp\n");
236     if(lp->neigh[i]==lp1) fprintf(stderr,"Nasel sem par lp->lp1\n");
237     if(lm2->neigh[i]==lm) fprintf(stderr,"Nasel sem par lm2->lm\n");
238     if(lm->neigh[i]==lm2) fprintf(stderr,"Nasel sem par lm->lm2\n");
239 }
240 // END DEBUG TESTING!
241 */
242
243
244 // 2. step. We change the triangle vertices... (actual bond flip)
245     for(i=0;i<3;i++) if(lm->vertex[i]== it) lm->vertex[i]= kp;
246     for(i=0;i<3;i++) if(lp->vertex[i]== k) lp->vertex[i]= km;
e19e79 247 //fprintf(stderr,"2. step: actual bondflip made\n");
aec47d 248 // 2a. step. If any changes in triangle calculations must be done, do it here!
SP 249 //   * normals are recalculated here
250     triangle_normal_vector(lp);
251     triangle_normal_vector(lm);
e19e79 252 //fprintf(stderr,"2a. step: triangle normals recalculated\n");
aec47d 253 // 3. step. Correct neighbours in vertex_list
SP 254
255
20ae83 256             vertex_list_remove_vtx(k->neigh, it);
SP 257             vertex_list_remove_vtx(it->neigh, k);
258     //vtx_remove_neighbour(k,it);
306559 259 //            vtx_remove_neighbour(it,k);
e19e79 260 //fprintf(stderr,"3. step (PROGRESS): removed k and it neighbours\n");
306559 261     
aec47d 262             //Tukaj pa nastopi tezava... Kam dodati soseda?
20ae83 263         
SP 264 //            vtx_insert_neighbour(km,kp,k);
265 //            vtx_insert_neighbour(kp,km,it);
266             vtx_add_neighbour(km,kp); //pazi na vrstni red.
267             vtx_add_neighbour(kp,km);
e19e79 268 //fprintf(stderr,"3. step: vertex neighbours corrected\n");
aec47d 269
SP 270 // 3a. step. If any changes to ts_vertex, do it here!
271 //   bond_length calculatons not required for it is done in energy.c
272
273 // 4. step. Correct bond_list (don't know why I still have it!)
274             bond->vtx1=km;
275             bond->vtx2=kp;
276 //fprintf(stderr,"4. step: bondlist corrected\n");
277
278
279 // 5. step. Correct neighbouring triangles 
280    
281     triangle_remove_neighbour(lp,lp1);
e19e79 282   //  fprintf(stderr,".\n");
aec47d 283     triangle_remove_neighbour(lp1,lp);
SP 284   //  fprintf(stderr,".\n");
285     triangle_remove_neighbour(lm,lm2);
286   //  fprintf(stderr,".\n");
287     triangle_remove_neighbour(lm2,lm);
288    
289     triangle_add_neighbour(lm,lp1);    
290     triangle_add_neighbour(lp1,lm);
291     triangle_add_neighbour(lp,lm2);  //Vrstni red?!
292     triangle_add_neighbour(lm2,lp);
293
294 //fprintf(stderr,"5. step: triangle neigbours corrected\n");
295
296
297 // 6. step. Correct tristar for vertices km, kp, k and it
298             vertex_add_tristar(km,lp);  // Preveri vrstni red!
299             vertex_add_tristar(kp,lm);
30ee9c 300             vtx_remove_tristar(it,lm);
SP 301             vtx_remove_tristar(k,lp);
aec47d 302 //fprintf(stderr,"6. step: tristar corrected\n");
SP 303
304 /*
305 //DEBUG TESTING
306 fprintf(stderr,"--- Naslov lm=%d",lm);
307
308
309 fprintf(stderr,"   vtxs(%d, %d, %d)\n",lm->vertex[0],lm->vertex[1], lm->vertex[2]);
310 fprintf(stderr,"--- Naslov lp=%d",lp);
311 fprintf(stderr,"   vtxs(%d, %d, %d)\n",lp->vertex[0],lp->vertex[1], lp->vertex[2]);
312 fprintf(stderr,"--- Naslov lm2=%d",lm2);
313 fprintf(stderr,"   vtxs(%d, %d, %d)\n",lm2->vertex[0],lm2->vertex[1], lm2->vertex[2]);
314 fprintf(stderr,"--- Naslov lp1=%d",lp1);
315 fprintf(stderr,"   vtxs(%d, %d, %d)\n",lp1->vertex[0],lp1->vertex[1], lp1->vertex[2]);
316
317 for(i=0;i<lm->neigh_no;i++)
318     fprintf(stderr,"lm sosed=%d\n",lm->neigh[i]);
319 for(i=0;i<lp->neigh_no;i++)
320     fprintf(stderr,"lp sosed=%d\n",lp->neigh[i]);
321 // END DEBUG TESTING
322 */
323   energy_vertex(k);
324   energy_vertex(kp);
325   energy_vertex(km);
326   energy_vertex(it);
327
d06a94 328 //Extra steps for new bond data structure
aec47d 329
d06a94 330 bond->adjvtx[0]=k;
SP 331 bond->adjvtx[1]=it;
aec47d 332 // END modifications to data structure!
SP 333
334
335     return TS_SUCCESS;
336 }