forked from computacionparalela/tiny_md
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.c
194 lines (150 loc) · 5.45 KB
/
core.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
187
188
189
190
191
192
193
194
#include "core.h"
#include "parameters.h"
#include <math.h>
#include <stdlib.h> // rand()
#define ECUT (4.0 * (pow(RCUT, -12) - pow(RCUT, -6)))
void init_pos(double* rxyz, const double rho)
{
// inicialización de las posiciones de los átomos en un cristal FCC
double a = cbrt(4.0 / rho);
int nucells = ceil(cbrt((double)N / 4.0));
int idx = 0;
for (int i = 0; i < nucells; i++) {
for (int j = 0; j < nucells; j++) {
for (int k = 0; k < nucells; k++) {
rxyz[idx + 0] = i * a; // x
rxyz[idx + 1] = j * a; // y
rxyz[idx + 2] = k * a; // z
// del mismo átomo
rxyz[idx + 3] = (i + 0.5) * a;
rxyz[idx + 4] = (j + 0.5) * a;
rxyz[idx + 5] = k * a;
rxyz[idx + 6] = (i + 0.5) * a;
rxyz[idx + 7] = j * a;
rxyz[idx + 8] = (k + 0.5) * a;
rxyz[idx + 9] = i * a;
rxyz[idx + 10] = (j + 0.5) * a;
rxyz[idx + 11] = (k + 0.5) * a;
idx += 12;
}
}
}
}
void init_vel(double* vxyz, double* temp, double* ekin)
{
// inicialización de velocidades aleatorias
double sf, sumvx = 0.0, sumvy = 0.0, sumvz = 0.0, sumv2 = 0.0;
for (int i = 0; i < 3 * N; i += 3) {
vxyz[i + 0] = rand() / (double)RAND_MAX - 0.5;
vxyz[i + 1] = rand() / (double)RAND_MAX - 0.5;
vxyz[i + 2] = rand() / (double)RAND_MAX - 0.5;
sumvx += vxyz[i + 0];
sumvy += vxyz[i + 1];
sumvz += vxyz[i + 2];
sumv2 += vxyz[i + 0] * vxyz[i + 0] + vxyz[i + 1] * vxyz[i + 1]
+ vxyz[i + 2] * vxyz[i + 2];
}
sumvx /= (double)N;
sumvy /= (double)N;
sumvz /= (double)N;
*temp = sumv2 / (3.0 * N);
*ekin = 0.5 * sumv2;
sf = sqrt(T0 / *temp);
for (int i = 0; i < 3 * N; i += 3) { // elimina la velocidad del centro de masa
// y ajusta la temperatura
vxyz[i + 0] = (vxyz[i + 0] - sumvx) * sf;
vxyz[i + 1] = (vxyz[i + 1] - sumvy) * sf;
vxyz[i + 2] = (vxyz[i + 2] - sumvz) * sf;
}
}
static double minimum_image(double cordi, const double cell_length)
{
// imagen más cercana
if (cordi <= -0.5 * cell_length) {
cordi += cell_length;
} else if (cordi > 0.5 * cell_length) {
cordi -= cell_length;
}
return cordi;
}
void forces(const double* rxyz, double* fxyz, double* epot, double* pres,
const double* temp, const double rho, const double V, const double L)
{
// calcula las fuerzas LJ (12-6)
for (int i = 0; i < 3 * N; i++) {
fxyz[i] = 0.0;
}
double pres_vir = 0.0;
double rcut2 = RCUT * RCUT;
*epot = 0.0;
for (int i = 0; i < 3 * (N - 1); i += 3) {
double xi = rxyz[i + 0];
double yi = rxyz[i + 1];
double zi = rxyz[i + 2];
for (int j = i + 3; j < 3 * N; j += 3) {
double xj = rxyz[j + 0];
double yj = rxyz[j + 1];
double zj = rxyz[j + 2];
// distancia mínima entre r_i y r_j
double rx = xi - xj;
rx = minimum_image(rx, L);
double ry = yi - yj;
ry = minimum_image(ry, L);
double rz = zi - zj;
rz = minimum_image(rz, L);
double rij2 = rx * rx + ry * ry + rz * rz;
if (rij2 <= rcut2) {
double r2inv = 1.0 / rij2;
double r6inv = r2inv * r2inv * r2inv;
double fr = 24.0 * r2inv * r6inv * (2.0 * r6inv - 1.0);
fxyz[i + 0] += fr * rx;
fxyz[i + 1] += fr * ry;
fxyz[i + 2] += fr * rz;
fxyz[j + 0] -= fr * rx;
fxyz[j + 1] -= fr * ry;
fxyz[j + 2] -= fr * rz;
*epot += 4.0 * r6inv * (r6inv - 1.0) - ECUT;
pres_vir += fr * rij2;
}
}
}
pres_vir /= (V * 3.0);
*pres = *temp * rho + pres_vir;
}
static double pbc(double cordi, const double cell_length)
{
// condiciones periodicas de contorno coordenadas entre [0,L)
if (cordi <= 0) {
cordi += cell_length;
} else if (cordi > cell_length) {
cordi -= cell_length;
}
return cordi;
}
void velocity_verlet(double* rxyz, double* vxyz, double* fxyz, double* epot,
double* ekin, double* pres, double* temp, const double rho,
const double V, const double L)
{
for (int i = 0; i < 3 * N; i += 3) { // actualizo posiciones
rxyz[i + 0] += vxyz[i + 0] * DT + 0.5 * fxyz[i + 0] * DT * DT;
rxyz[i + 1] += vxyz[i + 1] * DT + 0.5 * fxyz[i + 1] * DT * DT;
rxyz[i + 2] += vxyz[i + 2] * DT + 0.5 * fxyz[i + 2] * DT * DT;
rxyz[i + 0] = pbc(rxyz[i + 0], L);
rxyz[i + 1] = pbc(rxyz[i + 1], L);
rxyz[i + 2] = pbc(rxyz[i + 2], L);
vxyz[i + 0] += 0.5 * fxyz[i + 0] * DT;
vxyz[i + 1] += 0.5 * fxyz[i + 1] * DT;
vxyz[i + 2] += 0.5 * fxyz[i + 2] * DT;
}
forces(rxyz, fxyz, epot, pres, temp, rho, V, L); // actualizo fuerzas
double sumv2 = 0.0;
for (int i = 0; i < 3 * N; i += 3) { // actualizo velocidades
vxyz[i + 0] += 0.5 * fxyz[i + 0] * DT;
vxyz[i + 1] += 0.5 * fxyz[i + 1] * DT;
vxyz[i + 2] += 0.5 * fxyz[i + 2] * DT;
sumv2 += vxyz[i + 0] * vxyz[i + 0] + vxyz[i + 1] * vxyz[i + 1]
+ vxyz[i + 2] * vxyz[i + 2];
}
*ekin = 0.5 * sumv2;
*temp = sumv2 / (3.0 * N);
}