-
Notifications
You must be signed in to change notification settings - Fork 0
/
iteration.cpp
295 lines (258 loc) · 10.7 KB
/
iteration.cpp
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include <cmath>
#include <math.h>
#include <vector>
#include "iteration.hpp"
#include "particle.hpp"
///////////////////////////////
// ACTIVE BROWNIAN PARTICLES //
///////////////////////////////
template<> void iterate_ABP_WCA(
System* system, int Niter) {
// Updates system to next step according to the dynamics of active Brownian
// particles with WCA potential, using custom dimensionless parameters
// relations.
Parameters* parameters = system->getParameters();
bool const considerTorque = ( system->getTorqueParameter() != 0 );
std::vector<Particle> newParticles(parameters->getNumberParticles());
double selfPropulsion; // self-propulsion force
double noise; // noise realisation
#if HEUN // HEUN'S SCHEME
double selfPropulsionCorrection; // correction to the self-propulsion force
std::vector<double> positions (2*parameters->getNumberParticles(), 0.0); // positions backup
std::vector<double> forces (2*parameters->getNumberParticles(), 0.0); // forces backup
std::vector<double> orientations; // orientations backup
std::vector<double> torques; // torques backup
if ( considerTorque ) { // only need to use this memory when torque parameter is not 0
orientations.assign(parameters->getNumberParticles(), 0.0);
torques.assign(parameters->getNumberParticles(), 0.0);
}
#endif
for (int iter=0; iter < Niter; iter++) {
// COMPUTATION
for (int i=0; i < parameters->getNumberParticles(); i++) {
// POSITIONS
for (int dim=0; dim < 2; dim++) {
// initialise velocity
(system->getParticle(i))->velocity()[dim] = 0.0;
// initialise new positions with previous ones
newParticles[i].position()[dim] =
(system->getParticle(i))->position()[dim];
// add self-propulsion
selfPropulsion =
#if CONTROLLED_DYNAMICS
(1.0 - 2.0*system->getBiasingParameter()
/3.0/parameters->getPersistenceLength())*
#endif
cos((system->getParticle(i))->orientation()[0] - dim*M_PI/2);
(system->getParticle(i))->velocity()[dim] += selfPropulsion;
newParticles[i].position()[dim] +=
parameters->getTimeStep()*selfPropulsion;
// add noise
noise = (system->getRandomGenerator())->gauss();
(system->getParticle(i))->velocity()[dim] +=
sqrt(2.0/3.0/parameters->getPersistenceLength())
*noise;
newParticles[i].position()[dim] +=
sqrt(parameters->getTimeStep()
*2.0/3.0/parameters->getPersistenceLength())
*noise;
// initialise force
(system->getParticle(i))->force()[dim] = 0.0;
}
// ORIENTATIONS
// initialise new orientation with previous one
newParticles[i].orientation()[0] =
(system->getParticle(i))->orientation()[0];
// add noise
newParticles[i].orientation()[0] +=
sqrt(parameters->getTimeStep()*2.0/parameters->getPersistenceLength())
*(system->getRandomGenerator())->gauss();
if ( considerTorque ) {
// initialise torque
(system->getParticle(i))->torque()[0] = 0.0;
}
}
// FORCES AND ALIGNING TORQUES
system_WCA<System>(system); // compute forces
if ( considerTorque ) {
aligningTorque<System>(system,
[&system](int index) {
return (system->getParticle(index))->orientation(); },
[&system](int index) {
return (system->getParticle(index))->torque(); }); // compute torques
}
for (int i=0; i < parameters->getNumberParticles(); i++) {
for (int dim=0; dim < 2; dim++) {
(system->getParticle(i))->velocity()[dim] +=
(system->getParticle(i))->force()[dim]
/3.0/parameters->getPersistenceLength(); // add force
newParticles[i].position()[dim] +=
(system->getParticle(i))->force()[dim]
*parameters->getTimeStep()/3.0/parameters->getPersistenceLength(); // add force displacement
}
if ( considerTorque ) {
newParticles[i].orientation()[0] +=
(system->getParticle(i))->torque()[0]*parameters->getTimeStep(); // add torque rotation
}
}
// HEUN'S SCHEME
#if HEUN
for (int i=0; i < parameters->getNumberParticles(); i++) {
for (int dim=0; dim < 2; dim++) {
// POSITIONS
positions[2*i + dim] = (system->getParticle(i))->position()[dim]; // save initial position
(system->getParticle(i))->position()[dim] =
newParticles[i].position()[dim]; // integrate position as if using Euler's scheme
// FORCES
forces[2*i + dim] = (system->getParticle(i))->force()[dim]; // save computed force at initial position
(system->getParticle(i))->force()[dim] = 0.0; // re-initialise force
}
if ( considerTorque ) {
// ORIENTATIONS
orientations[i] = (system->getParticle(i))->orientation()[0]; // save initial orientation
(system->getParticle(i))->orientation()[0] =
newParticles[i].orientation()[0]; // integrate position as if using Euler's scheme
// TORQUES
torques[i] = (system->getParticle(i))->torque()[0]; // save computed force at initial position
(system->getParticle(i))->torque()[0] = 0.0; // re-initialise torque
}
}
// FORCES AND ALIGNING TORQUES
system->updateCellList(); // update cell list since positions have changed
system_WCA<System>(system); // re-compute forces
if ( considerTorque ) {
aligningTorque<System>(system,
[&system](int index) {
return (system->getParticle(index))->orientation(); },
[&system](int index) {
return (system->getParticle(index))->torque(); }); // re-compute torques
}
for (int i=0; i < parameters->getNumberParticles(); i++) {
// CORRECTION TO INTERPARTICLE FORCE
for (int dim=0; dim < 2; dim++) {
(system->getParticle(i))->velocity()[dim] +=
((system->getParticle(i))->force()[dim] - forces[2*i + dim])
/3.0/parameters->getPersistenceLength()/2; // velocity
newParticles[i].position()[dim] +=
((system->getParticle(i))->force()[dim] - forces[2*i + dim])
*parameters->getTimeStep()/3.0/parameters->getPersistenceLength()/2; // position
(system->getParticle(i))->force()[dim] =
((system->getParticle(i))->force()[dim] + forces[2*i + dim])/2; // force
}
// CORRECTION TO SELF-PROPULSION FORCE
for (int dim=0; dim < 2; dim++) {
selfPropulsionCorrection = 1.0;
#if CONTROLLED_DYNAMICS
selfPropulsionCorrection *=
(1.0 - 2.0*system->getBiasingParameter()
/3.0/parameters->getPersistenceLength());
#endif
if ( considerTorque ) {
selfPropulsionCorrection *=
(cos(newParticles[i].orientation()[0] - dim*M_PI/2)
- cos(orientations[2*i + dim] - dim*M_PI/2));
}
else {
selfPropulsionCorrection *=
(cos(newParticles[i].orientation()[0] - dim*M_PI/2)
- cos((system->getParticle(i))->orientation()[0] - dim*M_PI/2));
}
selfPropulsionCorrection /= 2;
(system->getParticle(i))->velocity()[dim] +=
selfPropulsionCorrection; // velocity
newParticles[i].position()[dim] +=
parameters->getTimeStep()*selfPropulsionCorrection; // position
}
// CORRECTION TO TORQUE
if ( considerTorque ) {
newParticles[i].orientation()[0] +=
((system->getParticle(i))->torque()[0] - torques[i])
*parameters->getTimeStep()/2; // orientation
(system->getParticle(i))->torque()[0] =
((system->getParticle(i))->torque()[0] + torques[i])/2; // torque
}
// RESET INITIAL POSITIONS AND ORIENTATION
for (int dim=0; dim < 2; dim++) {
(system->getParticle(i))->position()[dim] = positions[2*i + dim]; // position
}
if ( considerTorque ) {
(system->getParticle(i))->orientation()[0] = orientations[i]; // orientation
}
}
#endif
// SAVE AND COPY
system->saveNewState(newParticles);
}
}
/////////////////////////////////
// INTERACTING BROWNIAN ROTORS //
/////////////////////////////////
void iterate_rotors(Rotors* rotors, int Niter) {
// Updates system to next step according to the dynamics of interacting
// Brownian rotors.
bool const considerTorque = ( rotors->getTorqueParameter() != 0 );
std::vector<double> newOrientations(rotors->getNumberParticles());
#if HEUN // HEUN'S SCHEME
std::vector<double> orientations(rotors->getNumberParticles(), 0.0); // orientations backup
std::vector<double> torques(rotors->getNumberParticles(), 0.0); // torques backup
#endif
for (int iter=0; iter < Niter; iter++) {
// COMPUTATION
for (int i=0; i < rotors->getNumberParticles(); i++) {
// initialise new orientations with previous ones
newOrientations[i] = rotors->getOrientation(i)[0];
// reset torques
rotors->getTorque(i)[0] = 0.0;
// add noise
newOrientations[i] +=
sqrt(2.0*rotors->getRotDiffusivity()*rotors->getTimeStep())
*(rotors->getRandomGenerator())->gauss();
}
// compute aligning torques
if ( considerTorque ) {
aligningTorque<Rotors>(rotors,
[&rotors](int index) {
return rotors->getOrientation(index); },
[&rotors](int index) {
return rotors->getTorque(index); }); // compute torques
}
// add torque
for (int i=0; i < rotors->getNumberParticles(); i++) {
newOrientations[i] +=
rotors->getTorque(i)[0]*rotors->getTimeStep();
}
// HEUN'S SCHEME
#if HEUN
for (int i=0; i < rotors->getNumberParticles(); i++) {
// ORIENTATIONS
orientations[i] = rotors->getOrientation(i)[0]; // save initial orientation
rotors->getOrientation(i)[0] = newOrientations[i]; // integration orientation as if using Euler's scheme
// TORQUES
torques[i] = rotors->getTorque(i)[0]; // save computed torque at initial orientation
rotors->getTorque(i)[0] = 0; // re-initialise torques
}
// re-compute aligning torques
if ( considerTorque ) {
aligningTorque<Rotors>(rotors,
[&rotors](int index) {
return rotors->getOrientation(index); },
[&rotors](int index) {
return rotors->getTorque(index); }); // compute torques
}
for (int i=0; i < rotors->getNumberParticles(); i++) {
// correction to orientations
newOrientations[i] +=
(rotors->getTorque(i)[0] - torques[i])*rotors->getTimeStep()
/2;
// correction to torques
rotors->getTorque(i)[0] =
(rotors->getTorque(i)[0] + torques[i])
/2;
// reset initial orientations
rotors->getOrientation(i)[0] = orientations[i];
}
#endif
// SAVE
rotors->saveNewState(newOrientations);
}
}