-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.c
165 lines (129 loc) · 4.81 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
#include "core.h"
#include "parameters.h"
#include <math.h>
#include <omp.h>
#include <stdlib.h> // rand()
#include <omp.h>
#define ECUT (4.0 * (powf(RCUT, -12) - powf(RCUT, -6)))
void init_pos(float* restrict rxyz, const float rho)
{
// inicialización de las posiciones de los átomos en un cristal FCC
float a = cbrtf(4.0 / rho);
int nucells = ceilf(cbrtf((float)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[N + idx + 0] = j * a; // y
rxyz[2 * N + idx + 0] = k * a; // z
// del mismo átomo
rxyz[idx + 1] = (i + 0.5) * a;
rxyz[N + idx + 1] = (j + 0.5) * a;
rxyz[2 * N + idx + 1] = k * a;
rxyz[idx + 2] = (i + 0.5) * a;
rxyz[N + idx + 2] = j * a;
rxyz[2 * N + idx + 2] = (k + 0.5) * a;
rxyz[idx + 3] = i * a;
rxyz[N + idx + 3] = (j + 0.5) * a;
rxyz[2 * N + idx + 3] = (k + 0.5) * a;
idx += 4;
}
}
}
}
void init_vel(float* restrict vxyz, float* restrict temp, float* restrict ekin)
{
float sf, sumv2 = 0.0;
float sumv[3] = { 0.0, 0.0, 0.0 };
for (int i = 0; i < N; i++) {
for (int j = 0; j < 3; j++) {
vxyz[j * N + i] = rand() / (float)RAND_MAX - 0.5;
sumv[j] += vxyz[j * N + i];
sumv2 += vxyz[j * N + i] * vxyz[j * N + i];
}
}
for (int j = 0; j < 3; j++)
sumv[j] /= (float)N;
*temp = sumv2 / (3.0 * N);
*ekin = 0.5 * sumv2;
sf = sqrtf(T0 / *temp);
for (int i = 0; i < N; i++) { // elimina la velocidad del centro de masa
for (int j = 0; j < 3; j++) { // y ajusta la temperatura
vxyz[j * N + i] = sf * (vxyz[j * N + i] - sumv[j]);
}
}
}
static float minimum_image(float cordi, const float cell_length, const float cell_length_r)
{
return cordi - cell_length * roundf(cordi * cell_length_r);
}
void forces(const float* restrict rxyz, float* restrict fxyz, float* restrict epot, float* restrict pres,
const float* restrict temp, const float rho, const float V, const float L)
{
for (int i = 0; i < 3 * N; i++) {
fxyz[i] = 0.0;
}
const float L_r = 1.0 / L;
float _epot = 0.0;
float pres_vir = 0.0;
#pragma omp parallel reduction(+ : _epot, pres_vir, fxyz[:3 * N])
{
float ri[4 * N];
#pragma omp for nowait
for (int i = 0; i < N - 1; i++) {
for (int j = i + 1; j < N; j++) {
ri[j] = minimum_image(rxyz[i + 0] - rxyz[j], L, L_r);
ri[j + N] = minimum_image(rxyz[i + N] - rxyz[j + N], L, L_r);
ri[j + 2 * N] = minimum_image(rxyz[i + 2 * N] - rxyz[j + 2 * N], L, L_r);
ri[j + 3 * N] = ri[j] * ri[j] + ri[j + N] * ri[j + N] + ri[j + 2 * N] * ri[j + 2 * N];
}
for (int j = i + 1; j < N; j++) {
if (ri[j + 3 * N] <= RCUT2) {
float r2inv = 1.0 / ri[j + 3 * N];
float r6inv = r2inv * r2inv * r2inv;
float fr = 24.0 * r2inv * r6inv * (2.0 * r6inv - 1.0);
fxyz[i + 0] += fr * ri[j];
fxyz[i + N] += fr * ri[j + N];
fxyz[i + 2 * N] += fr * ri[j + 2 * N];
fxyz[j] -= fr * ri[j];
fxyz[j + N] -= fr * ri[j + N];
fxyz[j + 2 * N] -= fr * ri[j + 2 * N];
_epot += 4.0 * r6inv * (r6inv - 1.0) - ECUT;
pres_vir += fr * ri[j + 3 * N];
}
}
}
}
*epot = _epot;
pres_vir /= (3.0 * V);
*pres = *temp * rho + pres_vir;
}
static float pbc(float cordi, const float cell_length, const float cell_length_r)
{
return cordi - cell_length * floorf(cordi * cell_length_r);
}
void velocity_verlet(float* restrict rxyz, float* restrict vxyz, float* restrict fxyz, float* restrict epot,
float* restrict ekin, float* restrict pres, float* restrict temp, const float rho,
const float V, const float L)
{
const float L_r = 1.0 / L;
const float DT_2 = 0.5 * DT;
for (int i = 0; i < 3 * N; i++) {
vxyz[i] += fxyz[i] * DT_2;
}
for (int i = 0; i < 3 * N; i++) {
rxyz[i] += vxyz[i] * DT;
}
for (int i = 0; i < 3 * N; i++) {
rxyz[i] = pbc(rxyz[i], L, L_r);
}
forces(rxyz, fxyz, epot, pres, temp, rho, V, L);
float sumv2 = 0.0;
for (int i = 0; i < 3 * N; i++) {
vxyz[i] += 0.5 * fxyz[i] * DT;
sumv2 += vxyz[i] * vxyz[i];
}
*ekin = 0.5 * sumv2;
*temp = sumv2 / (3.0 * N);
}