-
Notifications
You must be signed in to change notification settings - Fork 10
/
avb.c
186 lines (147 loc) · 5.66 KB
/
avb.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include "avb.h"
#include "MC.h"
#include "output.h"
#include "parse_input.h"
#include "system.h"
#include "utils.h"
#include <math.h>
#include <float.h>
avbmc *avbdata;
void AVBMC_init(input_file *input, System *syst, Output *IO) {
double sin_theta = sin(acos(syst->kf_cosmax));
if(sin_theta >= 1. / (2. * (1. + syst->kf_delta))) {
output_exit(IO, "The AVB move cannot be used with patches that do not fulfill the single-bond-per-patch condition\n");
}
avbdata = malloc(sizeof(avbmc));
int numpatches = syst->n_patches;
avbdata->neighbours = malloc(numpatches * sizeof(PatchyParticle*));
avbdata->num_neighbours = 0;
avbdata->avb_vin = syst->n_patches * syst->n_patches * (M_PI * (syst->kf_delta * syst->kf_delta * syst->kf_delta + 3. * SQR(syst->kf_delta) + 3. * syst->kf_delta) * SQR(1. - syst->kf_cosmax) / 3.);
output_log_msg(IO, "Vavb = %lf\n", avbdata->avb_vin);
avbdata->avb_vout = syst->V - avbdata->avb_vin;
avbdata->avb_p = 0.5;
/**
* the probability with which avb moves are attempted. The probability of regular rototranslations is just 1 - avb_p.
*/
getInputDouble(input, "avb_p", &avbdata->avb_p, 0);
}
void AVBMC_free() {
free(avbdata->neighbours);
free(avbdata);
}
void _set_neighbours(System *syst, PatchyParticle *p) {
int ind[3], loop_ind[3];
cells_fill_and_get_idx_from_particle(syst, p, ind);
avbdata->num_neighbours = 0;
memset(avbdata->neighbours, 0, p->n_patches * sizeof(PatchyParticle*));
int j, k, l, p_patch, q_patch;
for(j = -1; j < 2; j++) {
loop_ind[0] = (ind[0] + j + syst->cells->N_side[0]) % syst->cells->N_side[0];
for(k = -1; k < 2; k++) {
loop_ind[1] = (ind[1] + k + syst->cells->N_side[1]) % syst->cells->N_side[1];
for(l = -1; l < 2; l++) {
loop_ind[2] = (ind[2] + l + syst->cells->N_side[2]) % syst->cells->N_side[2];
int loop_index = (loop_ind[0] * syst->cells->N_side[1] + loop_ind[1]) * syst->cells->N_side[2] + loop_ind[2];
PatchyParticle *q = syst->cells->heads[loop_index];
while(q != NULL) {
if(q->index != p->index) {
int val = MC_interact(syst, p, q, &p_patch, &q_patch);
if(val == PATCH_BOND) {
avbdata->neighbours[p_patch] = q;
avbdata->num_neighbours++;
}
}
q = syst->cells->next[q->index];
}
}
}
}
}
void AVBMC_dynamics(System *syst, Output *IO) {
if(drand48() < avbdata->avb_p || syst->N < 2) MC_move_rototranslate(syst, IO);
else {
syst->tries[AVB]++;
PatchyParticle *receiver = syst->particles + (int) (drand48() * syst->N);
_set_neighbours(syst, receiver);
int i;
PatchyParticle *p;
// the move will attempt to bring p inside the bonding volume of receiver
if(drand48() > 0.5) {
// if all the particles but receiver are in receiver's bonding volume then no move is possible
if(avbdata->num_neighbours == (syst->N - 1)) return;
int found = 0;
// select a particle which is not in receiver's bonding volume
do {
p = syst->particles + (int) (drand48() * syst->N);
found = 1;
for(i = 0; i < receiver->n_patches && found; i++)
if(avbdata->neighbours[i] == p) found = 0;
} while(!found || p == receiver);
vector new_r;
matrix new_orient;
// choose a target patch
int target_patch = (int) (drand48() * p->n_patches);
place_inside_vbonding(syst, receiver, new_r, new_orient, target_patch);
vector disp = { new_r[0] - p->r[0], new_r[1] - p->r[1], new_r[2] - p->r[2] };
double deltaE = -MC_energy(syst, p);
MC_rototraslate_particle(syst, p, disp, new_orient);
deltaE += MC_energy(syst, p);
#ifdef DEBUG
int p_patch = 0, q_patch = 0;
assert(MC_interact(syst, receiver, p, &p_patch, &q_patch) == PATCH_BOND);
#endif
double acc = exp(-deltaE / syst->T) * (syst->N - avbdata->num_neighbours - 1.) * avbdata->avb_vin / ((avbdata->num_neighbours + 1.) * avbdata->avb_vout);
if(!syst->overlap && drand48() < acc) {
syst->accepted[AVB]++;
syst->energy += deltaE;
}
else {
MC_rollback_particle(syst, p);
}
}
// the move will try to take p out out of the bonding volume of receiver
else {
// we need receiver to have a non-zero number of neighbors in order to take one of them out...
if(avbdata->num_neighbours == 0) return;
// pick a random particle from receiver's neighbors
int random_neighbour = (int) (drand48() * avbdata->num_neighbours);
int cn = 0;
int ci = 0;
while(cn <= random_neighbour) {
if(avbdata->neighbours[ci] != NULL) cn++;
ci++;
}
p = avbdata->neighbours[ci - 1];
#ifdef DEBUG
int p_patch = 0, q_patch = 0;
assert(MC_interact(syst, receiver, p, &p_patch, &q_patch) == PATCH_BOND);
#endif
vector new_r;
matrix new_orient;
vector new_patches[syst->n_patches];
int buff;
// choose a random position and a random orientation so that, at the end of the move, p and receiver won't be bonded any more
do {
new_r[0] = drand48() * syst->box[0];
new_r[1] = drand48() * syst->box[1];
new_r[2] = drand48() * syst->box[2];
random_orientation(syst, new_orient);
for(i = 0; i < syst->n_patches; i++) {
MATRIX_VECTOR_MULTIPLICATION(new_orient, syst->base_patches[i], new_patches[i]);
}
} while(MC_would_interact(syst, receiver, new_r, new_patches, &buff, &buff) == PATCH_BOND);
vector disp = { new_r[0] - p->r[0], new_r[1] - p->r[1], new_r[2] - p->r[2] };
double deltaE = -MC_energy(syst, p);
MC_rototraslate_particle(syst, p, disp, new_orient);
deltaE += MC_energy(syst, p);
double acc = exp(-deltaE / syst->T) * avbdata->num_neighbours * avbdata->avb_vout / ((syst->N - avbdata->num_neighbours) * avbdata->avb_vin);
if(!syst->overlap && drand48() < acc) {
syst->accepted[AVB]++;
syst->energy += deltaE;
}
else {
MC_rollback_particle(syst, p);
}
}
}
}