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; |
ded11e
|
34 |
ts_vertex *kp=NULL,*km=NULL; |
SP |
35 |
ts_triangle *lm=NULL, *lp=NULL; |
aec47d
|
36 |
|
20ae83
|
37 |
if(it->neigh->n< 3) return TS_FAIL; |
SP |
38 |
if(k->neigh->n< 3) return TS_FAIL; |
ded11e
|
39 |
|
aec47d
|
40 |
if(k==NULL || it==NULL){ |
SP |
41 |
fatal("In bondflip, number of neighbours of k or it is less than 3!",999); |
|
42 |
} |
ded11e
|
43 |
|
30ee9c
|
44 |
nei=0; |
20ae83
|
45 |
for(i=0;i<it->neigh->n;i++){ // Finds the nn of it, that is k |
SP |
46 |
if(it->neigh->vtx[i]==k){ |
aec47d
|
47 |
nei=i; |
SP |
48 |
break; |
|
49 |
} |
|
50 |
} |
|
51 |
neip=nei+1; // I don't like it.. Smells like I must have it in correct order |
|
52 |
neim=nei-1; |
20ae83
|
53 |
if(neip>=it->neigh->n) neip=0; |
SP |
54 |
if((ts_int)neim<0) neim=it->neigh->n-1; /* casting is essential... If not |
aec47d
|
55 |
there the neim is never <0 !!! */ |
SP |
56 |
// fprintf(stderr,"The numbers are: %u %u\n",neip, neim); |
20ae83
|
57 |
km=it->neigh->vtx[neim]; // We located km and kp |
SP |
58 |
kp=it->neigh->vtx[neip]; |
aec47d
|
59 |
|
ded11e
|
60 |
//find lm and lp (triangles that is common with k and it) |
SP |
61 |
for(i=0;i<it->tristar_no;i++){ |
|
62 |
if(it->tristar[i]->vertex[0]==k || it->tristar[i]->vertex[1]==k || it->tristar[i]->vertex[2]==k){ |
|
63 |
if(lm==NULL) lm=it->tristar[i]; |
|
64 |
else lp=it->tristar[i]; |
|
65 |
} |
|
66 |
} |
|
67 |
|
|
68 |
if(lm==NULL || lp==NULL) { |
|
69 |
fatal("In bondflip, cannot determine lp and lm",999); |
|
70 |
} |
|
71 |
|
aec47d
|
72 |
if(km==NULL || kp==NULL){ |
SP |
73 |
fatal("In bondflip, cannot determine km and kp!",999); |
|
74 |
} |
|
75 |
|
|
76 |
// fprintf(stderr,"I WAS HERE! after the 4 vertices are known!\n"); |
|
77 |
|
|
78 |
/* test if the membrane is wrapped too much, so that kp is nearest neighbour of |
|
79 |
* km. If it is true, then don't flip! */ |
20ae83
|
80 |
for(i=0;i<km->neigh->n;i++){ |
SP |
81 |
if(km->neigh->vtx[i] == kp) return TS_FAIL; |
aec47d
|
82 |
} |
SP |
83 |
// fprintf(stderr,"Membrane didn't wrap too much.. Continue.\n"); |
|
84 |
/* if bond would be too long, return... */ |
30ee9c
|
85 |
if(vtx_distance_sq(km,kp) > vesicle->dmax ) return TS_FAIL; |
aec47d
|
86 |
// fprintf(stderr,"Bond will not be too long.. Continue.\n"); |
SP |
87 |
|
|
88 |
/* we make a bond flip. this is different than in original fortran */ |
|
89 |
// 0. step. Get memory prior the flip |
|
90 |
oldenergy=0; |
|
91 |
oldenergy+=k->xk* k->energy; |
|
92 |
oldenergy+=kp->xk* kp->energy; |
|
93 |
oldenergy+=km->xk* km->energy; |
|
94 |
oldenergy+=it->xk* it->energy; |
|
95 |
// for(i=0;i<k->neigh_no;i++) oldenergy+=k->neigh[i]->xk*k->neigh[i]->energy; |
|
96 |
// for(i=0;i<kp->neigh_no;i++) oldenergy+=kp->neigh[i]->xk*kp->neigh[i]->energy; |
|
97 |
// for(i=0;i<km->neigh_no;i++) oldenergy+=km->neigh[i]->xk*km->neigh[i]->energy; |
|
98 |
// for(i=0;i<it->neigh_no;i++) oldenergy+=it->neigh[i]->xk*it->neigh[i]->energy; |
e19e79
|
99 |
/* |
306559
|
100 |
fprintf(stderr,"*** Naslov k=%ld\n",(long)k); |
SP |
101 |
fprintf(stderr,"*** Naslov it=%ld\n",(long)it); |
|
102 |
fprintf(stderr,"*** Naslov km=%ld\n",(long)km); |
|
103 |
fprintf(stderr,"*** Naslov kp=%ld\n",(long)kp); |
aec47d
|
104 |
|
SP |
105 |
for(i=0;i<k->neigh_no;i++) |
306559
|
106 |
fprintf(stderr,"k sosed=%ld\n",(long)k->neigh[i]); |
aec47d
|
107 |
for(i=0;i<it->neigh_no;i++) |
306559
|
108 |
fprintf(stderr,"it sosed=%ld\n",(long)it->neigh[i]); |
aec47d
|
109 |
|
SP |
110 |
for(i=0;i<km->neigh_no;i++) |
306559
|
111 |
fprintf(stderr,"km sosed=%ld\n",(long)km->neigh[i]); |
aec47d
|
112 |
for(i=0;i<kp->neigh_no;i++) |
306559
|
113 |
fprintf(stderr,"kp sosed=%ld\n",(long)kp->neigh[i]); |
aec47d
|
114 |
|
SP |
115 |
|
e19e79
|
116 |
*/ |
SP |
117 |
// fprintf(stderr,"I WAS HERE! Before bondflip!\n"); |
aec47d
|
118 |
ts_flip_bond(k,it,km,kp, bond); |
e19e79
|
119 |
// fprintf(stderr,"I WAS HERE! Bondflip successful!\n"); |
aec47d
|
120 |
|
SP |
121 |
/* Calculating the new energy */ |
|
122 |
delta_energy=0; |
20ae83
|
123 |
for(i=0;i<k->neigh->n;i++) energy_vertex(k->neigh->vtx[i]); |
SP |
124 |
for(i=0;i<kp->neigh->n;i++) energy_vertex(kp->neigh->vtx[i]); |
|
125 |
for(i=0;i<km->neigh->n;i++) energy_vertex(km->neigh->vtx[i]); |
|
126 |
for(i=0;i<it->neigh->n;i++) energy_vertex(it->neigh->vtx[i]); |
aec47d
|
127 |
delta_energy+=k->xk* k->energy; |
SP |
128 |
delta_energy+=kp->xk* kp->energy; |
|
129 |
delta_energy+=km->xk* km->energy; |
|
130 |
delta_energy+=it->xk* it->energy; |
|
131 |
// for(i=0;i<k->neigh_no;i++) delta_energy+=k->neigh[i]->xk*k->neigh[i]->energy; |
|
132 |
// for(i=0;i<kp->neigh_no;i++) delta_energy+=kp->neigh[i]->xk*kp->neigh[i]->energy; |
|
133 |
// for(i=0;i<km->neigh_no;i++) delta_energy+=km->neigh[i]->xk*km->neigh[i]->energy; |
|
134 |
// for(i=0;i<it->neigh_no;i++) delta_energy+=it->neigh[i]->xk*it->neigh[i]->energy; |
|
135 |
delta_energy-=oldenergy; |
|
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 |
|
20ae83
|
295 |
vertex_list_remove_vtx(k->neigh, it); |
SP |
296 |
vertex_list_remove_vtx(it->neigh, k); |
|
297 |
//vtx_remove_neighbour(k,it); |
306559
|
298 |
// vtx_remove_neighbour(it,k); |
e19e79
|
299 |
//fprintf(stderr,"3. step (PROGRESS): removed k and it neighbours\n"); |
306559
|
300 |
|
aec47d
|
301 |
//Tukaj pa nastopi tezava... Kam dodati soseda? |
20ae83
|
302 |
|
SP |
303 |
// vtx_insert_neighbour(km,kp,k); |
|
304 |
// vtx_insert_neighbour(kp,km,it); |
|
305 |
vtx_add_neighbour(km,kp); //pazi na vrstni red. |
|
306 |
vtx_add_neighbour(kp,km); |
e19e79
|
307 |
//fprintf(stderr,"3. step: vertex neighbours corrected\n"); |
aec47d
|
308 |
|
SP |
309 |
// 3a. step. If any changes to ts_vertex, do it here! |
|
310 |
// bond_length calculatons not required for it is done in energy.c |
|
311 |
|
|
312 |
// 4. step. Correct bond_list (don't know why I still have it!) |
|
313 |
bond->vtx1=km; |
|
314 |
bond->vtx2=kp; |
|
315 |
//fprintf(stderr,"4. step: bondlist corrected\n"); |
|
316 |
|
|
317 |
|
|
318 |
// 5. step. Correct neighbouring triangles |
|
319 |
|
|
320 |
triangle_remove_neighbour(lp,lp1); |
e19e79
|
321 |
// fprintf(stderr,".\n"); |
aec47d
|
322 |
triangle_remove_neighbour(lp1,lp); |
SP |
323 |
// fprintf(stderr,".\n"); |
|
324 |
triangle_remove_neighbour(lm,lm2); |
|
325 |
// fprintf(stderr,".\n"); |
|
326 |
triangle_remove_neighbour(lm2,lm); |
|
327 |
|
|
328 |
triangle_add_neighbour(lm,lp1); |
|
329 |
triangle_add_neighbour(lp1,lm); |
|
330 |
triangle_add_neighbour(lp,lm2); //Vrstni red?! |
|
331 |
triangle_add_neighbour(lm2,lp); |
|
332 |
|
|
333 |
//fprintf(stderr,"5. step: triangle neigbours corrected\n"); |
|
334 |
|
|
335 |
|
|
336 |
// 6. step. Correct tristar for vertices km, kp, k and it |
|
337 |
vertex_add_tristar(km,lp); // Preveri vrstni red! |
|
338 |
vertex_add_tristar(kp,lm); |
30ee9c
|
339 |
vtx_remove_tristar(it,lm); |
SP |
340 |
vtx_remove_tristar(k,lp); |
aec47d
|
341 |
//fprintf(stderr,"6. step: tristar corrected\n"); |
SP |
342 |
|
|
343 |
/* |
|
344 |
//DEBUG TESTING |
|
345 |
fprintf(stderr,"--- Naslov lm=%d",lm); |
|
346 |
|
|
347 |
|
|
348 |
fprintf(stderr," vtxs(%d, %d, %d)\n",lm->vertex[0],lm->vertex[1], lm->vertex[2]); |
|
349 |
fprintf(stderr,"--- Naslov lp=%d",lp); |
|
350 |
fprintf(stderr," vtxs(%d, %d, %d)\n",lp->vertex[0],lp->vertex[1], lp->vertex[2]); |
|
351 |
fprintf(stderr,"--- Naslov lm2=%d",lm2); |
|
352 |
fprintf(stderr," vtxs(%d, %d, %d)\n",lm2->vertex[0],lm2->vertex[1], lm2->vertex[2]); |
|
353 |
fprintf(stderr,"--- Naslov lp1=%d",lp1); |
|
354 |
fprintf(stderr," vtxs(%d, %d, %d)\n",lp1->vertex[0],lp1->vertex[1], lp1->vertex[2]); |
|
355 |
|
|
356 |
for(i=0;i<lm->neigh_no;i++) |
|
357 |
fprintf(stderr,"lm sosed=%d\n",lm->neigh[i]); |
|
358 |
for(i=0;i<lp->neigh_no;i++) |
|
359 |
fprintf(stderr,"lp sosed=%d\n",lp->neigh[i]); |
|
360 |
// END DEBUG TESTING |
|
361 |
*/ |
|
362 |
energy_vertex(k); |
|
363 |
energy_vertex(kp); |
|
364 |
energy_vertex(km); |
|
365 |
energy_vertex(it); |
|
366 |
|
|
367 |
|
|
368 |
// END modifications to data structure! |
|
369 |
|
|
370 |
|
|
371 |
return TS_SUCCESS; |
|
372 |
} |