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; |
|
86 |
/* |
|
87 |
fprintf(stderr,"*** Naslov k=%d\n",k); |
|
88 |
fprintf(stderr,"*** Naslov it=%d\n",it); |
|
89 |
fprintf(stderr,"*** Naslov km=%d\n",km); |
|
90 |
fprintf(stderr,"*** Naslov kp=%d\n",kp); |
|
91 |
|
|
92 |
for(i=0;i<k->neigh_no;i++) |
|
93 |
fprintf(stderr,"k sosed=%d\n",k->neigh[i]); |
|
94 |
for(i=0;i<it->neigh_no;i++) |
|
95 |
fprintf(stderr,"it sosed=%d\n",it->neigh[i]); |
|
96 |
|
|
97 |
for(i=0;i<km->neigh_no;i++) |
|
98 |
fprintf(stderr,"km sosed=%d\n",km->neigh[i]); |
|
99 |
for(i=0;i<kp->neigh_no;i++) |
|
100 |
fprintf(stderr,"kp sosed=%d\n",kp->neigh[i]); |
|
101 |
|
|
102 |
|
|
103 |
*/ |
|
104 |
// fprintf(stderr,"I WAS HERE! Before bondflip!\n"); |
|
105 |
ts_flip_bond(k,it,km,kp, bond); |
|
106 |
// fprintf(stderr,"I WAS HERE! Bondflip successful!\n"); |
|
107 |
|
|
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 |
|
|
236 |
|
|
237 |
//fprintf(stderr,"1. step: lm, lm2, lp1 and lp found!\n"); |
|
238 |
|
|
239 |
/* |
|
240 |
//DEBUG TESTING |
|
241 |
fprintf(stderr,"--- Naslov lm=%d",lm); |
|
242 |
|
|
243 |
|
|
244 |
fprintf(stderr," vtxs(%d, %d, %d)\n",lm->vertex[0],lm->vertex[1], lm->vertex[2]); |
|
245 |
fprintf(stderr,"--- Naslov lp=%d",lp); |
|
246 |
fprintf(stderr," vtxs(%d, %d, %d)\n",lp->vertex[0],lp->vertex[1], lp->vertex[2]); |
|
247 |
fprintf(stderr,"--- Naslov lm2=%d",lm2); |
|
248 |
fprintf(stderr," vtxs(%d, %d, %d)\n",lm2->vertex[0],lm2->vertex[1], lm2->vertex[2]); |
|
249 |
fprintf(stderr,"--- Naslov lp1=%d",lp1); |
|
250 |
fprintf(stderr," vtxs(%d, %d, %d)\n",lp1->vertex[0],lp1->vertex[1], lp1->vertex[2]); |
|
251 |
|
|
252 |
for(i=0;i<lm->neigh_no;i++) |
|
253 |
fprintf(stderr,"lm sosed=%d\n",lm->neigh[i]); |
|
254 |
for(i=0;i<lp->neigh_no;i++) |
|
255 |
fprintf(stderr,"lp sosed=%d\n",lp->neigh[i]); |
|
256 |
// END DEBUG TESTING |
|
257 |
*/ |
|
258 |
/* |
|
259 |
// DEBUG TESTING! |
|
260 |
|
|
261 |
for(i=0;i<3;i++){ |
|
262 |
|
|
263 |
if(lp1->neigh[i]==lp) fprintf(stderr,"Nasel sem par lp1->lp\n"); |
|
264 |
if(lp->neigh[i]==lp1) fprintf(stderr,"Nasel sem par lp->lp1\n"); |
|
265 |
if(lm2->neigh[i]==lm) fprintf(stderr,"Nasel sem par lm2->lm\n"); |
|
266 |
if(lm->neigh[i]==lm2) fprintf(stderr,"Nasel sem par lm->lm2\n"); |
|
267 |
} |
|
268 |
// END DEBUG TESTING! |
|
269 |
*/ |
|
270 |
|
|
271 |
|
|
272 |
// 2. step. We change the triangle vertices... (actual bond flip) |
|
273 |
for(i=0;i<3;i++) if(lm->vertex[i]== it) lm->vertex[i]= kp; |
|
274 |
for(i=0;i<3;i++) if(lp->vertex[i]== k) lp->vertex[i]= km; |
|
275 |
//fprintf(stderr,"2. step: actual bondflip made\n"); |
|
276 |
// 2a. step. If any changes in triangle calculations must be done, do it here! |
|
277 |
// * normals are recalculated here |
|
278 |
triangle_normal_vector(lp); |
|
279 |
triangle_normal_vector(lm); |
|
280 |
// 3. step. Correct neighbours in vertex_list |
|
281 |
|
|
282 |
|
30ee9c
|
283 |
vtx_remove_neighbour(k,it); |
SP |
284 |
vtx_remove_neighbour(it,k); |
aec47d
|
285 |
//Tukaj pa nastopi tezava... Kam dodati soseda? |
30ee9c
|
286 |
vtx_insert_neighbour(km,kp,k); |
SP |
287 |
vtx_insert_neighbour(kp,km,it); |
aec47d
|
288 |
// vertex_add_neighbour(km,kp); //pazi na vrstni red. |
SP |
289 |
// vertex_add_neighbour(kp,km); |
|
290 |
//fprintf(stderr,"3. step: vertex neighbours corrected\n"); |
|
291 |
|
|
292 |
// 3a. step. If any changes to ts_vertex, do it here! |
|
293 |
// bond_length calculatons not required for it is done in energy.c |
|
294 |
|
|
295 |
// 4. step. Correct bond_list (don't know why I still have it!) |
|
296 |
bond->vtx1=km; |
|
297 |
bond->vtx2=kp; |
|
298 |
//fprintf(stderr,"4. step: bondlist corrected\n"); |
|
299 |
|
|
300 |
|
|
301 |
// 5. step. Correct neighbouring triangles |
|
302 |
|
|
303 |
triangle_remove_neighbour(lp,lp1); |
|
304 |
// fprintf(stderr,".\n"); |
|
305 |
triangle_remove_neighbour(lp1,lp); |
|
306 |
// fprintf(stderr,".\n"); |
|
307 |
triangle_remove_neighbour(lm,lm2); |
|
308 |
// fprintf(stderr,".\n"); |
|
309 |
triangle_remove_neighbour(lm2,lm); |
|
310 |
|
|
311 |
triangle_add_neighbour(lm,lp1); |
|
312 |
triangle_add_neighbour(lp1,lm); |
|
313 |
triangle_add_neighbour(lp,lm2); //Vrstni red?! |
|
314 |
triangle_add_neighbour(lm2,lp); |
|
315 |
|
|
316 |
//fprintf(stderr,"5. step: triangle neigbours corrected\n"); |
|
317 |
|
|
318 |
|
|
319 |
// 6. step. Correct tristar for vertices km, kp, k and it |
|
320 |
vertex_add_tristar(km,lp); // Preveri vrstni red! |
|
321 |
vertex_add_tristar(kp,lm); |
30ee9c
|
322 |
vtx_remove_tristar(it,lm); |
SP |
323 |
vtx_remove_tristar(k,lp); |
aec47d
|
324 |
//fprintf(stderr,"6. step: tristar corrected\n"); |
SP |
325 |
|
|
326 |
/* |
|
327 |
//DEBUG TESTING |
|
328 |
fprintf(stderr,"--- Naslov lm=%d",lm); |
|
329 |
|
|
330 |
|
|
331 |
fprintf(stderr," vtxs(%d, %d, %d)\n",lm->vertex[0],lm->vertex[1], lm->vertex[2]); |
|
332 |
fprintf(stderr,"--- Naslov lp=%d",lp); |
|
333 |
fprintf(stderr," vtxs(%d, %d, %d)\n",lp->vertex[0],lp->vertex[1], lp->vertex[2]); |
|
334 |
fprintf(stderr,"--- Naslov lm2=%d",lm2); |
|
335 |
fprintf(stderr," vtxs(%d, %d, %d)\n",lm2->vertex[0],lm2->vertex[1], lm2->vertex[2]); |
|
336 |
fprintf(stderr,"--- Naslov lp1=%d",lp1); |
|
337 |
fprintf(stderr," vtxs(%d, %d, %d)\n",lp1->vertex[0],lp1->vertex[1], lp1->vertex[2]); |
|
338 |
|
|
339 |
for(i=0;i<lm->neigh_no;i++) |
|
340 |
fprintf(stderr,"lm sosed=%d\n",lm->neigh[i]); |
|
341 |
for(i=0;i<lp->neigh_no;i++) |
|
342 |
fprintf(stderr,"lp sosed=%d\n",lp->neigh[i]); |
|
343 |
// END DEBUG TESTING |
|
344 |
*/ |
|
345 |
energy_vertex(k); |
|
346 |
energy_vertex(kp); |
|
347 |
energy_vertex(km); |
|
348 |
energy_vertex(it); |
|
349 |
|
|
350 |
|
|
351 |
// END modifications to data structure! |
|
352 |
|
|
353 |
|
|
354 |
return TS_SUCCESS; |
|
355 |
} |