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