-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmoldyn_functions.cpp
More file actions
253 lines (205 loc) · 7.53 KB
/
moldyn_functions.cpp
File metadata and controls
253 lines (205 loc) · 7.53 KB
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
#include "moldyn_functions.hpp"
double get_time() {
return (double) chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count()/1000.0;
}
void printray(vec3 ray) {
printf("%.18f %.18f %.18f\n", ray.x, ray.y, ray.z);
}
double get_length_sqrd(vec3 ray) {
return pow(ray.x, 2) + pow(ray.y, 2) + pow(ray.z, 2);
}
void get_atom_combinations(vector<pair<Atom*, Atom*>> &combinations, vector<Atom> &atoms) {
string bitmask(2, 1); // 2 leading 1's
bitmask.resize(atoms.size(), 0); // N-2 trailing 0's
int i = 0;
do {
bool first = true;
for (int j = 0; j < atoms.size(); j++) {
if (bitmask[j]) {
if (first) {
combinations[i].first = &atoms[j];
first = false;
} else {
combinations[i].second = &atoms[j];
}
}
}
i++;
} while (prev_permutation(bitmask.begin(), bitmask.end()));
}
double U(double r_sqrd) {
return 4*(pow(r_sqrd, -6) - pow(r_sqrd, -3)) + 2912/531441.0;
}
vec3 get_force(vec3 &between_vec, double r_sqrd) {
vec3 direction_vec = between_vec/r_sqrd;
double force = 24*(2*pow(r_sqrd, -6) - pow(r_sqrd, -3));
return direction_vec*force;
}
tuple<vec3, double> periodic_boundry(vec3 &between_ray, double L) {
double dx = between_ray[0];
dx = dx - round(dx/L)*L;
double dy = between_ray[1];
dy = dy - round(dy/L)*L;
double dz = between_ray[2];
dz = dz - round(dz/L)*L;
vec3 direction_ray = {dx, dy, dz};
double r_sqrd = pow(dx, 2) + pow(dy, 2) + pow(dz, 2);
return tuple<vec3, double>(direction_ray, r_sqrd);
}
tuple<double, double, double> get_energy(vector<Atom> &atoms, vector<pair<Atom*, Atom*>> &atom_combinations, double L) {
// Calculate potential energy:
double potential_energy = 0;
for (pair<Atom*, Atom*> &atom_combination : atom_combinations) {
Atom *atom1 = atom_combination.first;
Atom *atom2 = atom_combination.second;
vec3 between_vec = atom1->pos - atom2->pos;
auto [direction_vec, r_sqrd] = periodic_boundry(between_vec, L);
potential_energy += U(r_sqrd);
}
// Calculate kinetic energy:
double kinetic_energy = 0;
for (Atom &atom : atoms) {
kinetic_energy += 0.5*get_length_sqrd(atom.vel);
}
return tuple<double, double, double>(potential_energy, kinetic_energy, potential_energy+kinetic_energy);
}
void ez_simulate(vector<Atom> &atoms, vector<pair<Atom*, Atom*>> &atom_combinations, double dt, double t_max, double L) {
vector<double> t_list((int) t_max/dt, 0);
FILE* datafile = fopen("data/null", "w");
for (size_t i = 0; i < t_list.size(); i++) {
t_list[i] = i*dt;
// Fancy progress indicator
if ((int) (t_list[i]*6/t_max*10) % 10 == 0) {
cout << "\r";
for (int j = 0; j < t_list[i]*6/t_max; j++) {
cout << ".";
}
cout << flush;
}
step(atoms, atom_combinations, dt, L, datafile);
}
fclose(datafile);
}
tuple<vector<double>, vector<double>, vector<double>, vector<double>, vector<double>, vector<double>, vector<double>> simulate(
vector<Atom> &atoms, vector<pair<Atom*, Atom*>> &atom_combinations, double dt, double t_max, string filename, double L, int completion, int total_runs
) {
// Declare variables
int n = t_max/dt;
vector<double> t_list(n, 0);
vector<double> pot_list(n, 0);
vector<double> kin_list(n, 0);
vector<double> tot_list(n, 0);
vector<double> tmp_list(n, 0);
vector<double> vac_list(n, 0);
vector<double> msd_list(n, 0);
FILE *datafile = fopen(("data/"+filename+".xyz").c_str(), "w");
for (size_t i = 0; i < t_list.size(); i++) {
t_list[i] = i*dt;
// Fancy progress indicator, now even more complicated
double run_percent = (100*completion + t_list[i]*100/t_max)/total_runs;
if ((int) (run_percent*10) % 10 == 0) {
printf("\r...... %i/%i : %3.0f %%", completion+1, total_runs, run_percent);
fflush(stdout);
}
// Get and store energy
auto [pot, kin, tot] = get_energy(atoms, atom_combinations, L);
pot_list[i] = pot;
kin_list[i] = kin;
tot_list[i] = tot;
// Calculate and store temperature
double temperature = (2.0/(3*atoms.size()))*kin;
tmp_list[i] = temperature;
// Calculate and store velocity autocorrelation
double vac = 0;
for (Atom &atom : atoms) {
double vac_emum = glm::dot(atom.vel, atom.vel0);
double vac_denom = get_length_sqrd(atom.vel0);
vac += vac_emum/vac_denom;
}
vac = vac/((double) atoms.size());
vac_list[i] = vac;
// Calculate and store mean squared displacement
double msd = 0;
for (Atom &atom : atoms) {
msd += get_length_sqrd(atom.dist_traveled(L) - atom.pos0);
}
msd = msd/((double) atoms.size());
msd_list[i] = msd;
step(atoms, atom_combinations, dt, L, datafile);
}
fclose(datafile);
return make_tuple(t_list, pot_list, kin_list, tot_list, tmp_list, vac_list, msd_list);
}
void step(vector<Atom> &atoms, vector<pair<Atom*, Atom*>> &atom_combinations, double dt, double L, FILE *datafile) {
// Add the force acting on the particles efficiently using pairs
for (pair<Atom*, Atom*> &atom_combination : atom_combinations) {
Atom *atom1 = atom_combination.first;
Atom *atom2 = atom_combination.second;
vec3 between_ray = atom1->pos - atom2->pos;
auto [direction_ray, r_sqrd] = periodic_boundry(between_ray, L);
if (r_sqrd < 3*3) {
vec3 force = get_force(direction_ray, r_sqrd);
atom1->force += force;
atom2->force -= force;
}
}
fprintf(datafile, "%zi\ntype x y z\n", atoms.size());
for (Atom &atom : atoms) {
// Save current atom positions to file
atom.save_state(datafile);
// Update atom positions using given method
atom.update(dt, L);
}
}
vector<vec3> box_positions(int n, double d) {
vector<vec3> positions;
positions.reserve(4*pow(n, 3));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
positions.push_back(vec3(i, j, k)*d);
positions.push_back(vec3(i, 0.5 + j, 0.5 + k)*d);
positions.push_back(vec3(0.5 + i, j, 0.5 + k)*d);
positions.push_back(vec3(0.5 + i, 0.5 + j, k)*d);
}
}
}
return positions;
}
tuple<double, vector<Atom>, vector<pair<Atom*, Atom*>>> create_atoms(int atom_count, double d, double temperature) {
int n = cbrt(atom_count/4.0);
double L = d*n;
vector<Atom> atoms;
atoms.reserve(atom_count);
default_random_engine generator(chrono::system_clock::now().time_since_epoch().count());
normal_distribution<double> normal_temperature(0, sqrt(temperature/119.7));
// string line;
// ifstream normal_speeds("data/normal_speeds.dat");
// vector<string> str_list;
// while (getline(normal_speeds, line)) {
// str_list.push_back(line);
// }
vector<vec3> positions = box_positions(n, d);
for (int i = 0; i < atom_count; i++) {
// vector<string> elements = split(str_list[i], ", ");
vec3 velocitiy = vec3(normal_temperature(generator), normal_temperature(generator), normal_temperature(generator));
atoms.push_back(Atom(positions[i], velocitiy));
}
// normal_speeds.close();
int atom_combination_count = (atom_count*(atom_count-1))/2;
vector<pair<Atom*, Atom*>> atom_combinations(atom_combination_count);
get_atom_combinations(atom_combinations, atoms);
// for (Atom &atom : atoms) {
// printray(atom.vel);
// }
return tuple<double, vector<Atom>, vector<pair<Atom*, Atom*>>>(L, move(atoms), move(atom_combinations));
}
tuple<double, vector<Atom>, vector<pair<Atom*, Atom*>>> create_equalibrium_atoms(int atom_count, double d, double temperature, double dt, double t_max) {
auto [L, warm_atoms, atom_combinations] = create_atoms(atom_count, d, temperature);
ez_simulate(warm_atoms, atom_combinations, dt, t_max, L);
for (Atom &atom : warm_atoms) {
atom.vel0 = atom.vel;
atom.pos0 = atom.pos;
}
return tuple<double, vector<Atom>, vector<pair<Atom*, Atom*>>>(L, move(warm_atoms), move(atom_combinations));
}