-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMD.c
More file actions
206 lines (178 loc) · 5.92 KB
/
Copy pathMD.c
File metadata and controls
206 lines (178 loc) · 5.92 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
//
// Created by sorak on 11/30/2024.
//
#include "MD.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <omp.h>
#include <time.h>
#include <string.h>
#define NUM_PARTICLES_UNIVERSE 10000
#define NUM_TIMESTEP 1
#define NUM_THREADS 16
#define BSIZE 1
#define BLOCKED 1
void velocityUpdateBlocked(Particle *particleList);
void velocityUpdate(Particle *particleList);
void printData(Particle *particleList);
void plot_particles(Particle *particleList,FILE *fp);
int main()
{
double start, end;
double cpu_time;
Particle *particleList = malloc(sizeof(Particle) * NUM_PARTICLES_UNIVERSE);
init_ParticleList(particleList);
Particle current;
//FILE *fpBefore = fopen("Before.dat", "w");
//plot_particles(particleList, fpBefore);
start = omp_get_wtime();
for (int i = 0; i < NUM_TIMESTEP; i++)
{
if (BLOCKED)
{
velocityUpdateBlocked(particleList);
}
else
{
velocityUpdate(particleList);
}
for (int ref = 0; ref < NUM_PARTICLES_UNIVERSE; ref++)
{
current = particleList[ref];
current.x = fmod(current.x + current.vX * DT, L);
current.y = fmod(current.y + current.vY * DT, L);
current.z = fmod(current.z + current.vZ * DT, L);
current.x = current.x < 0 ? current.x + L : current.x;
current.y = current.y < 0 ? current.y + L : current.y;
current.z = current.z < 0 ? current.z + L : current.z;
current.x = current.x > L ? current.x - L : current.x;
current.y = current.y > L ? current.y - L : current.y;
current.z = current.z > L ? current.z - L : current.z;
}
}
end = omp_get_wtime();
cpu_time = (double)(end - start);
//FILE *fpAfter = fopen("After.dat", "w");
//plot_particles(particleList, fpAfter);
printf("Time Taken: %f seconds\n", cpu_time);
return 0;
}
void init_ParticleList(Particle *particleList)
{
srand(SEED);
int min = -10;
int max = 10;
for (int i = 0; i < NUM_PARTICLES_UNIVERSE; i++)
{
particleList[i].x = ((float)rand() / (float)(RAND_MAX)) * UNIVERSE_SIZE;
particleList[i].y = ((float)rand() / (float)(RAND_MAX)) * UNIVERSE_SIZE;
particleList[i].z = ((float)rand() / (float)(RAND_MAX)) * UNIVERSE_SIZE;
particleList[i].vX = ((float)(min + rand() % (max - min + 1)) / (float)max) * UNIVERSE_SIZE / 2;
particleList[i].vY = ((float)(min + rand() % (max - min + 1)) / (float)max) * UNIVERSE_SIZE / 2;
particleList[i].vZ = ((float)(min + rand() % (max - min + 1)) / (float)max) * UNIVERSE_SIZE / 2;
particleList[i].particleId = i;
}
}
void velocityUpdateBlocked(Particle *particleList)
{
Particle vec;
float r, f;
#pragma omp parallel for num_threads(NUM_THREADS)
for (int ii = 0; ii < NUM_PARTICLES_UNIVERSE; ii += BSIZE)
{
for (int jj = 0; jj < NUM_PARTICLES_UNIVERSE; jj += BSIZE)
{
for (int ref = ii; ref < ii + BSIZE; ref++)
{
for (int neighbor = jj; neighbor < jj + BSIZE; neighbor++)
{
if (ref == neighbor)
{
continue;
}
else
{
modr(&vec, &particleList[ref], &particleList[neighbor]);
r = norm(&vec);
if (r > CUTOFF)
{
continue;
}
if (vec.x < 0)
{
continue;
}
f = lj(r);
scalar_mul(&vec, DT * f / r);
vec_add(&particleList[ref], &vec);
scalar_mul(&vec, -1.);
vec_add(&particleList[neighbor], &vec);
}
}
}
}
}
}
void velocityUpdate(Particle *particleList)
{
Particle vec;
float r, f;
#pragma omp parallel for num_threads(NUM_THREADS)
for (int ref = 0; ref < NUM_PARTICLES_UNIVERSE; ref++)
{
for (int neighbor = 0; neighbor < NUM_PARTICLES_UNIVERSE; neighbor++)
{
if (ref == neighbor)
{
continue;
}
else
{
modr(&vec, &particleList[ref], &particleList[neighbor]);
r = norm(&vec);
if (r > CUTOFF)
{
continue;
}
if (vec.x < 0)
{
continue;
}
f = lj(r);
scalar_mul(&vec, DT * f / r);
vec_add(&particleList[ref], &vec);
scalar_mul(&vec, -1.);
vec_add(&particleList[neighbor], &vec);
}
}
}
}
void printData(Particle *particleList){
printf("Printing Particle List!!\n --------------------\n");
for (int i = 0; i < NUM_PARTICLES_UNIVERSE; i++)
{
printf("Particle:%d\n", particleList[i].particleId);
printf("X: %f\n", particleList[i].x);
printf("Y: %f\n", particleList[i].y);
printf("Z: %f\n", particleList[i].z);
printf("\n");
}
printf("Printing Particle Velocity!!\n --------------------\n");
for (int i = 0; i < NUM_PARTICLES_UNIVERSE; i++)
{
printf("Particle:%d\n", particleList[i].particleId);
printf("X: %f\n", particleList[i].vX);
printf("Y: %f\n", particleList[i].vY);
printf("Z: %f\n", particleList[i].vZ);
printf("\n");
}
}
void plot_particles(Particle *particleList, FILE *fp)
{
for (int i = 0; i < NUM_PARTICLES_UNIVERSE; i++)
{
fprintf(fp, "%lf %lf %lf\n", particleList[i].x, particleList[i].y, particleList[i].z);
}
fclose(fp);
}