-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviz.c
299 lines (214 loc) · 6.91 KB
/
viz.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
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
296
297
298
299
#define _XOPEN_SOURCE 500 // M_PI
#include "core.h"
#include "parameters.h"
#include <GL/glut.h> // OpenGL
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
// variables globales
static float Ekin, Epot, Temp, Pres; // variables macroscopicas
static float Rho, V, box_size, tail, Etail, Ptail;
static float *rxyz, *vxyz, *fxyz; // variables microscopicas
static float Rhob, sf, epotm, presm;
static int switcher = 0, frames = 0, mes;
// OpenGL specific drawing routines
static int win_id;
static int win_x = 900, win_y = 900;
static void pre_display(void)
{ // 3D
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (float)win_x / win_y, 1.0, 0.0);
gluLookAt(1.0, 1.0, 1.0, 0.5, 0.5, 0.5, 1.0, 0.0, 0.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
}
static void post_display(void)
{
glutSwapBuffers();
}
static void draw_atoms(void)
{
float glL = cbrtf((float)N / (RHOI - 0.8));
float resize = 0.5;
// grafico las lineas que delimitan la caja de simulación
glBegin(GL_LINES);
float box_line = resize * (box_size / glL);
glColor3d(0.0, 0.0, 1.0);
glVertex3d(0.0, 0.0, 0.0);
glVertex3d(0.0, 0.0, box_line);
glVertex3d(0.0, 0.0, 0.0);
glVertex3d(0.0, box_line, 0.0);
glVertex3d(0.0, 0.0, 0.0);
glVertex3d(box_line, 0.0, 0.0);
glVertex3d(box_line, box_line, box_line);
glVertex3d(box_line, box_line, 0.0);
glVertex3d(box_line, box_line, box_line);
glVertex3d(box_line, 0.0, box_line);
glVertex3d(box_line, box_line, box_line);
glVertex3d(0.0, box_line, box_line);
glVertex3d(0.0, box_line, 0.0);
glVertex3d(box_line, box_line, 0.0);
glVertex3d(0.0, box_line, box_line);
glVertex3d(0.0, 0.0, box_line);
glVertex3d(box_line, 0.0, box_line);
glVertex3d(box_line, 0.0, 0.0);
glVertex3d(box_line, 0.0, box_line);
glVertex3d(0.0, 0.0, box_line);
glVertex3d(0.0, box_line, box_line);
glVertex3d(0.0, box_line, 0.0);
glVertex3d(box_line, box_line, 0.0);
glVertex3d(box_line, 0.0, 0.0);
glEnd();
// grafico las particulas (x, y, z) en el punto (dx, dy, dx), son reescaleadas
// a [0, 1] y luego multiplicadas con un factor que las achica para poder
// apreciar mejor el cambio en el volumen
glBegin(GL_POINTS);
int di;
float dx;
float dy;
float dz;
for (di = 0; di < 3 * N; di += 3) {
dx = (rxyz[di + 0] / glL) * resize;
dy = (rxyz[di + 1] / glL) * resize;
dz = (rxyz[di + 2] / glL) * resize;
glColor3d(0.0, 1.0, 0.0);
glVertex3d(dx, dy, dz);
}
glEnd();
}
/*static void reshape_func ( int width, int height )
{
glutSetWindow ( win_id );
glutReshapeWindow ( width, height );
win_x = width;
win_y = height;
}*/
static void idle_func(void)
{
if (switcher == 3) {
Rho = RHOI;
V = (float)N / Rho;
box_size = cbrtf(V);
tail = 16.0 * M_PI * Rho * ((2.0 / 3.0) * pow(RCUT, -9) - pow(RCUT, -3)) / 3.0;
Etail = tail * (float)N;
Ptail = tail * Rho;
init_pos(rxyz, Rho);
init_vel(vxyz, &Temp, &Ekin);
forces(rxyz, fxyz, &Epot, &Pres, &Temp, Rho, V, box_size);
switcher = 0;
} else if (switcher == 2) { // imprimo propiedades en la terminal y cambio la densidad
printf("%f\t%f\t%f\t%f\n", Rho, V, epotm / (float)mes,
presm / (float)mes);
Rhob = Rho;
Rho = Rho - 0.1;
V = (float)N / Rho;
box_size = cbrtf(V);
tail = 16.0 * M_PI * Rho * ((2.0 / 3.0) * pow(RCUT, -9) - pow(RCUT, -3)) / 3.0;
Etail = tail * (float)N;
Ptail = tail * Rho;
sf = cbrtf(Rhob / Rho);
for (int k = 0; k < 3 * N; k++) { // reescaleo posiciones a nueva densidad
rxyz[k] *= sf;
}
init_vel(vxyz, &Temp, &Ekin);
forces(rxyz, fxyz, &Epot, &Pres, &Temp, Rho, V, box_size);
switcher = 0;
if (fabs(Rho - (RHOI - 0.9f)) < 1e-6) {
printf("\n");
switcher = 3;
}
} else if (switcher == 1) { // loop de medición
for (int i = frames; i < frames + TMES; i++) {
velocity_verlet(rxyz, vxyz, fxyz, &Epot, &Ekin, &Pres, &Temp, Rho,
V, box_size);
sf = sqrtf(T0 / Temp);
for (int k = 0; k < 3 * N; k++) { // reescaleo de velocidades
vxyz[k] *= sf;
}
}
Epot += Etail;
Pres += Ptail;
epotm += Epot;
presm += Pres;
mes++;
frames += TMES;
if (frames % TRUN == 0) {
switcher = 2;
}
} else if (switcher == 0) { // loop de equilibración
while (frames % TEQ != 0) {
velocity_verlet(rxyz, vxyz, fxyz, &Epot, &Ekin, &Pres, &Temp, Rho,
V, box_size);
sf = sqrtf(T0 / Temp);
for (int k = 0; k < 3 * N; k++) { // reescaleo de velocidades
vxyz[k] *= sf;
}
frames++;
}
mes = 0;
epotm = 0.0;
presm = 0.0;
switcher = 1;
}
glutSetWindow(win_id);
glutPostRedisplay();
}
static void display_func(void)
{
pre_display();
draw_atoms();
post_display();
}
static void open_glut_window(void)
{
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowPosition(0, 0);
glutInitWindowSize(win_x, win_y);
win_id = glutCreateWindow("tiny molecular dynamics | visualization");
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glutSwapBuffers();
glClear(GL_COLOR_BUFFER_BIT);
glutSwapBuffers();
pre_display();
// glutKeyboardFunc ( key_func );
// glutMouseFunc ( mouse_func );
// glutMotionFunc ( motion_func );
//glutReshapeFunc ( reshape_func );
glutIdleFunc(idle_func);
glutDisplayFunc(display_func);
}
// viz main
int main(int argc, char** argv)
{
glutInit(&argc, argv);
rxyz = (float*)malloc(3 * N * sizeof(float));
vxyz = (float*)malloc(3 * N * sizeof(float));
fxyz = (float*)malloc(3 * N * sizeof(float));
// parametros iniciales para que los pueda usar (antes de modificar)
// `idle_func`
srand(SEED);
Rho = RHOI;
Rhob = Rho;
V = (float)N / Rho;
box_size = cbrtf(V);
tail = 16.0 * M_PI * Rho * ((2.0 / 3.0) * pow(RCUT, -9) - pow(RCUT, -3)) / 3.0;
Etail = tail * (float)N;
Ptail = tail * Rho;
init_pos(rxyz, Rho);
init_vel(vxyz, &Temp, &Ekin);
forces(rxyz, fxyz, &Epot, &Pres, &Temp, Rho, V, box_size);
//
//
printf("# Número de partículas: %d\n", N);
printf("# Temperatura de referencia: %.2f\n", T0);
printf("# Pasos de equilibración: %d\n", TEQ);
printf("# Pasos de medición: %d\n", TRUN - TEQ);
printf("# (mediciones cada %d pasos)\n", TMES);
open_glut_window();
glutMainLoop();
exit(0);
}