-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfire.hpp
165 lines (140 loc) · 5.16 KB
/
fire.hpp
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
#ifndef FIRE_HPP
#define FIRE_HPP
#include <math.h>
#include <algorithm>
#include <vector>
#include "iteration.hpp"
#include "particle.hpp"
#ifdef DEBUG
#include <iostream>
#endif
////////////////
// PROTOTYPES //
////////////////
template<class SystemClass> void FIRE_WCA(
SystemClass* system, long double Emin, int iterMax, long double dtmin = 0,
long double dt0 = 0, long double dtmax = 0,
long double finc = 1.1, long double fdec = 0.5,
long double alpha0 = 0.1, long double falpha = 0.99,
int Nmin = 5) {
// Uses the FIRE algorithm to minimise the WCA potential energy of a given
// system.
// (see https://yketa.github.io/DAMTP_MSC_2019_Wiki/#FIRE%20algorithm)
#ifdef DEBUG
std::cout << "##FIRE minimisation algorithm" << std::endl;
std::cout << "#Emin: " << Emin << " iterMax: " << iterMax;
std::cout << " dtmin: " << dtmin << " Nmin: " << Nmin;
std::cout << std::endl;
#endif
// INITIALISATION
std::vector<double> velocities (2*system->getNumberParticles(), 0.0); // array of global velocities
long double normVelocities; // norm of global velocities
long double normForces; // norm of global forces
double power; // total power of interacting forces
if ( dt0 <= 0 ) dt0 = system->getTimeStep(); // initial time step
if ( dtmax <= 0 ) dtmax = 10*dt0; // maximum time step
#ifdef DEBUG
std::cout << "#dt0: " << dt0 << " dtmax: " << dtmax;
std::cout << " finc: " << finc << " fdec: " << fdec << std::endl;
std::cout << "#alpha0: " << alpha0 << " falpha: " << falpha << std::endl;
std::cout << std::endl;
#endif
long double dt = dt0; // time step
long double alpha = alpha0; // strength of force towards steepest descent
// MINIMISATION LOOP
double NPpos = 0; // number of consecutive steps with positive power
double iter = 0; // number of iterations
double potential = WCA_potential(system);
while ( potential > Emin && iter < iterMax && dt > dtmin ) { // while energy is not minimised and time step is still big for a maximum of iterMax iterations
iter++;
#ifdef DEBUG
std::cout << "#iter: " << iter << " E: " << potential << std::endl;
#endif
// COMPUTE FORCES
for (int i = 0; i < system->getNumberParticles(); i++) {
for (int dim = 0; dim < 2; dim++) {
(system->getParticle(i))->force()[dim] = 0.0; // reset force
}
}
ABP_WCA<SystemClass>(system);
// COMPUTE POWER
power = 0;
for (int i = 0; i < system->getNumberParticles(); i++) { // loop over particles
for (int dim = 0; dim < 2; dim++) { // loop over dimensions
power += (system->getParticle(i))->force()[dim]*velocities[2*i + dim];
}
}
#ifdef DEBUG
std::cout << "#power: " << power << std::endl;
#endif
// P > 0
if ( power > 0 ) {
#ifdef DEBUG
std::cout << "## P > 0" << std::endl;
#endif
NPpos++;
for (int i = 0; i < system->getNumberParticles(); i++) {
// compute norms of velocity and force
normVelocities = 0;
normForces = 0;
for (int dim = 0; dim < 2; dim++) {
normVelocities += pow(velocities[2*i + dim], 2);
normForces += pow((system->getParticle(i))->force()[dim], 2);
}
normVelocities = sqrt(normVelocities);
normForces = sqrt(normForces);
// modify velocity
if ( normForces > 0 ) {
for (int dim = 0; dim < 2; dim++) {
velocities[2*i + dim] = // v_{t+1} =
(1.0 - alpha)*velocities[2*i + dim] // (1 - alpha)*v_t
+ alpha*(system->getParticle(i))->force()[dim] // + alpha*F(r_t)
*normVelocities/normForces; // *|v_t|/|F(r_t)|
}
}
else {
for (int dim = 0; dim < 2; dim++) {
velocities[2*i + dim] = // v_{t+1} =
(1.0 - alpha)*velocities[2*i + dim]; // (1 - alpha)*v_t
}
}
}
// maximum number of iterations at positive power
if ( NPpos > Nmin ) {
NPpos = 0;
dt = std::min(dt*finc, dtmax);
alpha = falpha*alpha;
}
}
// P <= 0
else {
#ifdef DEBUG
std::cout << "## P <= 0" << std::endl;
#endif
NPpos = 0;
std::vector<double> velocities (2*system->getNumberParticles(), 0.0);
dt *= fdec;
alpha = alpha0;
}
#ifdef DEBUG
std::cout << "#dt: " << dt << " alpha: " << alpha << std::endl << std::endl;
#endif
// INTEGRATION TO NEXT TIME STEP [EULER EXPLICIT METHOD]
for (int i = 0; i < system->getNumberParticles(); i++) {
for (int dim = 0; dim < 2; dim++) {
// positions
(system->getParticle(i))->position()[dim] += dt*velocities[2*i + dim];
(system->getParticle(i))->position()[dim] =
_wrapCoordinate<SystemClass>(system, // taking boundary condition into account
(system->getParticle(i))->position()[dim]);
// velocities
velocities[2*i + dim] += dt*(system->getParticle(i))->force()[dim];
}
}
// UPDATE CELL LIST
(system->getCellList())->template update<SystemClass>(system);
// POTENTIAL ENERGY AT NEXT TIME STEP
potential = WCA_potential(system);
}
}
#endif