Trisurf Monte Carlo simulator
Samo Penic
2013-11-30 64c113ed06cb749fdc513162a1cddf4ec024fd72
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;
82 //  for(i=0;i<k->neigh_no;i++) oldenergy+=k->neigh[i]->xk*k->neigh[i]->energy;
83 //  for(i=0;i<kp->neigh_no;i++) oldenergy+=kp->neigh[i]->xk*kp->neigh[i]->energy;
84 //  for(i=0;i<km->neigh_no;i++) oldenergy+=km->neigh[i]->xk*km->neigh[i]->energy;
85 //  for(i=0;i<it->neigh_no;i++) oldenergy+=it->neigh[i]->xk*it->neigh[i]->energy;
e19e79 86 /*
306559 87 fprintf(stderr,"*** Naslov k=%ld\n",(long)k);
SP 88 fprintf(stderr,"*** Naslov it=%ld\n",(long)it);
89 fprintf(stderr,"*** Naslov km=%ld\n",(long)km);
90 fprintf(stderr,"*** Naslov kp=%ld\n",(long)kp);
aec47d 91
SP 92 for(i=0;i<k->neigh_no;i++)
306559 93     fprintf(stderr,"k sosed=%ld\n",(long)k->neigh[i]);
aec47d 94 for(i=0;i<it->neigh_no;i++)
306559 95     fprintf(stderr,"it sosed=%ld\n",(long)it->neigh[i]);
aec47d 96
SP 97 for(i=0;i<km->neigh_no;i++)
306559 98     fprintf(stderr,"km sosed=%ld\n",(long)km->neigh[i]);
aec47d 99 for(i=0;i<kp->neigh_no;i++)
306559 100     fprintf(stderr,"kp sosed=%ld\n",(long)kp->neigh[i]);
aec47d 101
SP 102
e19e79 103 */
SP 104 //    fprintf(stderr,"I WAS HERE! Before bondflip!\n");
aec47d 105     ts_flip_bond(k,it,km,kp, bond);
e19e79 106 //    fprintf(stderr,"I WAS HERE! Bondflip successful!\n");
aec47d 107
SP 108 /* Calculating the new energy */
109   delta_energy=0;
110   for(i=0;i<k->neigh_no;i++) energy_vertex(k->neigh[i]);
111   for(i=0;i<kp->neigh_no;i++) energy_vertex(kp->neigh[i]);
112   for(i=0;i<km->neigh_no;i++) energy_vertex(km->neigh[i]);
113   for(i=0;i<it->neigh_no;i++) energy_vertex(it->neigh[i]);
114   delta_energy+=k->xk* k->energy;
115   delta_energy+=kp->xk* kp->energy;
116   delta_energy+=km->xk* km->energy;
117   delta_energy+=it->xk* it->energy;
118 //  for(i=0;i<k->neigh_no;i++) delta_energy+=k->neigh[i]->xk*k->neigh[i]->energy;
119 //  for(i=0;i<kp->neigh_no;i++) delta_energy+=kp->neigh[i]->xk*kp->neigh[i]->energy;
120 //  for(i=0;i<km->neigh_no;i++) delta_energy+=km->neigh[i]->xk*km->neigh[i]->energy;
121 //  for(i=0;i<it->neigh_no;i++) delta_energy+=it->neigh[i]->xk*it->neigh[i]->energy;
122   delta_energy-=oldenergy;
123  // fprintf(stderr,"I WAS HERE! Got energy!\n");
124 /* MONTE CARLO */
125     if(delta_energy>=0){
126 #ifdef TS_DOUBLE_DOUBLE
127         if(exp(-delta_energy)< drand48() )
128 #endif
129 #ifdef TS_DOUBLE_FLOAT
130         if(expf(-delta_energy)< (ts_float)drand48())
131 #endif
132 #ifdef TS_DOUBLE_LONGDOUBLE
133         if(expl(-delta_energy)< (ts_ldouble)drand48())
134 #endif
135         {
136             //not accepted, reverting changes
137        //     fprintf(stderr,"Failed to move, due to MC\n");
138
139 //            ts_flip_bond(km,kp,it,k, bond);
140             ts_flip_bond(kp,km,k,it, bond);
141                 
142
143 /*
144 fprintf(stderr,"*** Naslov k=%d\n",k);
145 fprintf(stderr,"*** Naslov it=%d\n",it);
146 fprintf(stderr,"*** Naslov km=%d\n",km);
147 fprintf(stderr,"*** Naslov kp=%d\n",kp);
148 for(i=0;i<k->neigh_no;i++)
149     fprintf(stderr,"k sosed=%d\n",k->neigh[i]);
150 for(i=0;i<it->neigh_no;i++)
151     fprintf(stderr,"it sosed=%d\n",it->neigh[i]);
152
153
154 for(i=0;i<km->neigh_no;i++)
155     fprintf(stderr,"km sosed=%d\n",km->neigh[i]);
156 for(i=0;i<kp->neigh_no;i++)
157     fprintf(stderr,"kp sosed=%d\n",kp->neigh[i]);
158 */
159
160
161
162         //    fprintf(stderr,"Reverted condition!\n");
163             return TS_FAIL;
164         }
165     }
166         //    fprintf(stderr,"Success\n");
167
168
169 /* IF BONDFLIP ACCEPTED, THEN RETURN SUCCESS! */
170     return TS_SUCCESS;
171 }
172
173
174 ts_bool ts_flip_bond(ts_vertex *k,ts_vertex *it,ts_vertex *km, ts_vertex *kp,
175 ts_bond *bond){
176
177     ts_triangle *lm=NULL,*lp=NULL, *lp1=NULL, *lm2=NULL;
30ee9c 178     ts_uint i,j; //lmidx, lpidx;
aec47d 179 if(k==NULL || it==NULL || km==NULL || kp==NULL){
SP 180     fatal("ts_flip_bond: You called me with invalid pointers to vertices",999);
181 }
182 // 1. step. We find lm and lp from k->tristar !
183     for(i=0;i<it->tristar_no;i++){
184         for(j=0;j<k->tristar_no;j++){
185             if((it->tristar[i] == k->tristar[j])){ //ce gre za skupen trikotnik
186                 if((it->tristar[i]->vertex[0] == km || it->tristar[i]->vertex[1]
187 == km || it->tristar[i]->vertex[2]== km )){
188                 lm=it->tristar[i];
189          //       lmidx=i;
190                 }
191                 else
192                 {
193                 lp=it->tristar[i];
194          //       lpidx=i;
195                 }
196
197             }
198         }
199     }
200 if(lm==NULL || lp==NULL) fatal("ts_flip_bond: Cannot find triangles lm and lp!",999);
201
202 //we look for important triangles lp1 and lm2.
203
204  for(i=0;i<k->tristar_no;i++){
205         for(j=0;j<kp->tristar_no;j++){
206                 if((k->tristar[i] == kp->tristar[j]) && k->tristar[i]!=lp){ //ce gre za skupen trikotnik
207                     lp1=k->tristar[i];
208             }
209         }
210 }
211
212  for(i=0;i<it->tristar_no;i++){
213         for(j=0;j<km->tristar_no;j++){
214             if((it->tristar[i] == km->tristar[j]) && it->tristar[i]!=lm){ //ce gre za skupen trikotnik
215                     lm2=it->tristar[i];
216             } 
217         }
218     }
219 /*
220 // DEBUG TESTING!
221 fprintf(stderr,"*** Naslov k=%d\n",k);
222 fprintf(stderr,"*** Naslov it=%d\n",it);
223 fprintf(stderr,"*** Naslov km=%d\n",km);
224 fprintf(stderr,"*** Naslov kp=%d\n",kp);
225
226 for(i=0;i<k->neigh_no;i++)
227     fprintf(stderr,"k sosed=%d\n",k->neigh[i]);
228 for(i=0;i<it->neigh_no;i++)
229     fprintf(stderr,"it sosed=%d\n",it->neigh[i]);
230
231
232 // END DEBUG TESTING!
233 */
234 if(lm2==NULL || lp1==NULL) fatal("ts_flip_bond: Cannot find triangles lm2 and lp1!",999);
235
e19e79 236 /*
aec47d 237 //DEBUG TESTING
306559 238 fprintf(stderr,"1. step: lm, lm2, lp1 and lp found!\n");
SP 239 fprintf(stderr,"--- Naslov lm=%ld",(long)lm);
aec47d 240
SP 241
306559 242 fprintf(stderr,"   vtxs(%ld, %ld, %ld)\n",(long)lm->vertex[0],(long)lm->vertex[1], (long)lm->vertex[2]);
SP 243 fprintf(stderr,"--- Naslov lp=%ld",(long)lp);
244 fprintf(stderr,"   vtxs(%ld, %ld, %ld)\n",(long)lp->vertex[0],(long)lp->vertex[1], (long)lp->vertex[2]);
245 fprintf(stderr,"--- Naslov lm2=%ld",(long)lm2);
246 fprintf(stderr,"   vtxs(%ld, %ld, %ld)\n",(long)lm2->vertex[0],(long)lm2->vertex[1], (long)lm2->vertex[2]);
247 fprintf(stderr,"--- Naslov lp1=%ld",(long)lp1);
248 fprintf(stderr,"   vtxs(%ld, %ld, %ld)\n",(long)lp1->vertex[0],(long)lp1->vertex[1], (long)lp1->vertex[2]);
aec47d 249
SP 250 for(i=0;i<lm->neigh_no;i++)
306559 251     fprintf(stderr,"lm sosed=%ld\n",(long)lm->neigh[i]);
aec47d 252 for(i=0;i<lp->neigh_no;i++)
306559 253     fprintf(stderr,"lp sosed=%ld\n",(long)lp->neigh[i]);
aec47d 254 // END DEBUG TESTING
e19e79 255 */
aec47d 256 /*
SP 257 // DEBUG TESTING!
258
259 for(i=0;i<3;i++){
260
261     if(lp1->neigh[i]==lp) fprintf(stderr,"Nasel sem par lp1->lp\n");
262     if(lp->neigh[i]==lp1) fprintf(stderr,"Nasel sem par lp->lp1\n");
263     if(lm2->neigh[i]==lm) fprintf(stderr,"Nasel sem par lm2->lm\n");
264     if(lm->neigh[i]==lm2) fprintf(stderr,"Nasel sem par lm->lm2\n");
265 }
266 // END DEBUG TESTING!
267 */
268
269
270 // 2. step. We change the triangle vertices... (actual bond flip)
271     for(i=0;i<3;i++) if(lm->vertex[i]== it) lm->vertex[i]= kp;
272     for(i=0;i<3;i++) if(lp->vertex[i]== k) lp->vertex[i]= km;
e19e79 273 //fprintf(stderr,"2. step: actual bondflip made\n");
aec47d 274 // 2a. step. If any changes in triangle calculations must be done, do it here!
SP 275 //   * normals are recalculated here
276     triangle_normal_vector(lp);
277     triangle_normal_vector(lm);
e19e79 278 //fprintf(stderr,"2a. step: triangle normals recalculated\n");
aec47d 279 // 3. step. Correct neighbours in vertex_list
SP 280
281
30ee9c 282             vtx_remove_neighbour(k,it);
306559 283 //            vtx_remove_neighbour(it,k);
e19e79 284 //fprintf(stderr,"3. step (PROGRESS): removed k and it neighbours\n");
306559 285     
aec47d 286             //Tukaj pa nastopi tezava... Kam dodati soseda?
30ee9c 287             vtx_insert_neighbour(km,kp,k);
SP 288             vtx_insert_neighbour(kp,km,it);
aec47d 289 //            vertex_add_neighbour(km,kp); //pazi na vrstni red.
SP 290 //            vertex_add_neighbour(kp,km);
e19e79 291 //fprintf(stderr,"3. step: vertex neighbours corrected\n");
aec47d 292
SP 293 // 3a. step. If any changes to ts_vertex, do it here!
294 //   bond_length calculatons not required for it is done in energy.c
295
296 // 4. step. Correct bond_list (don't know why I still have it!)
297             bond->vtx1=km;
298             bond->vtx2=kp;
299 //fprintf(stderr,"4. step: bondlist corrected\n");
300
301
302 // 5. step. Correct neighbouring triangles 
303    
304     triangle_remove_neighbour(lp,lp1);
e19e79 305   //  fprintf(stderr,".\n");
aec47d 306     triangle_remove_neighbour(lp1,lp);
SP 307   //  fprintf(stderr,".\n");
308     triangle_remove_neighbour(lm,lm2);
309   //  fprintf(stderr,".\n");
310     triangle_remove_neighbour(lm2,lm);
311    
312     triangle_add_neighbour(lm,lp1);    
313     triangle_add_neighbour(lp1,lm);
314     triangle_add_neighbour(lp,lm2);  //Vrstni red?!
315     triangle_add_neighbour(lm2,lp);
316
317 //fprintf(stderr,"5. step: triangle neigbours corrected\n");
318
319
320 // 6. step. Correct tristar for vertices km, kp, k and it
321             vertex_add_tristar(km,lp);  // Preveri vrstni red!
322             vertex_add_tristar(kp,lm);
30ee9c 323             vtx_remove_tristar(it,lm);
SP 324             vtx_remove_tristar(k,lp);
aec47d 325 //fprintf(stderr,"6. step: tristar corrected\n");
SP 326
327 /*
328 //DEBUG TESTING
329 fprintf(stderr,"--- Naslov lm=%d",lm);
330
331
332 fprintf(stderr,"   vtxs(%d, %d, %d)\n",lm->vertex[0],lm->vertex[1], lm->vertex[2]);
333 fprintf(stderr,"--- Naslov lp=%d",lp);
334 fprintf(stderr,"   vtxs(%d, %d, %d)\n",lp->vertex[0],lp->vertex[1], lp->vertex[2]);
335 fprintf(stderr,"--- Naslov lm2=%d",lm2);
336 fprintf(stderr,"   vtxs(%d, %d, %d)\n",lm2->vertex[0],lm2->vertex[1], lm2->vertex[2]);
337 fprintf(stderr,"--- Naslov lp1=%d",lp1);
338 fprintf(stderr,"   vtxs(%d, %d, %d)\n",lp1->vertex[0],lp1->vertex[1], lp1->vertex[2]);
339
340 for(i=0;i<lm->neigh_no;i++)
341     fprintf(stderr,"lm sosed=%d\n",lm->neigh[i]);
342 for(i=0;i<lp->neigh_no;i++)
343     fprintf(stderr,"lp sosed=%d\n",lp->neigh[i]);
344 // END DEBUG TESTING
345 */
346   energy_vertex(k);
347   energy_vertex(kp);
348   energy_vertex(km);
349   energy_vertex(it);
350
351
352 // END modifications to data structure!
353
354
355     return TS_SUCCESS;
356 }