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