-
Notifications
You must be signed in to change notification settings - Fork 1
/
many_body.cpp
325 lines (273 loc) · 7.96 KB
/
many_body.cpp
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
//#include <stdio.h>
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <math.h>
#include <string>
#include <omp.h>
#define ll long long int
/////System Configuration//////////
#define number_bodies 1000
#define mass 1
#define length 100
#define breadth 200
#define height 400
#define delta_t 0.01
#define radius 0.5
#define timestep 720000
////////////////////////////////////
#define print_pose false
#define print_force false
#define print_distance false
#define DOUBLE_MAX 999999.99
#define COLLISION_CHECK
#define OPENMP
using namespace std;
float euclidean(double x1, double y1, double z1, double x2, double y2, double z2){
return sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) + (z1-z2)*(z1-z2));
}
void print_matrix(double **X)
{
for (int i = 0; i < 1000; ++i)
{
cout<< X[i][0]<< " " <<X[i][1]<< " " <<X[i][2]<< " "<<endl;
}
return;
}
void find_force(double **F,double **R)
{
ll i,j;
double x,y,z,dis;
#pragma omp parallel for private(j,dis) schedule(guided) reduction(+:x,y,z)
for(i=0;i<number_bodies;i++)
{
x=y=z=0.0;
for(j=0;j<number_bodies; j++)
{
dis = euclidean(R[j][0],R[j][1],R[j][2],R[i][0],R[i][1],R[i][2]);
if(dis!=0)
{
x=x+(R[j][0]-R[i][0])/dis;
y=y+(R[j][1]-R[i][1])/dis;
z=z+(R[j][2]-R[i][2])/dis;
}
}
F[i][0]=x*mass*mass;
F[i][1]=y*mass*mass;
F[i][2]=z*mass*mass;
}
}
void velocity_update(double **F,double **V)
{
int i;
#pragma omp parallel for private(i) schedule(guided)
for(i=0;i<number_bodies;i++)
{
V[i][0] = V[i][0]+(F[i][0]*delta_t)/(2.0*mass);
V[i][1] = V[i][1]+(F[i][1]*delta_t)/(2.0*mass);
V[i][2] = V[i][2]+(F[i][2]*delta_t)/(2.0*mass);
}
}
void initialise_distance(double **D)
{
int i = 0 , j = 0;
#pragma omp parallel for collapse(2)
for( i = 0 ; i < 1000; i++)
for ( j = 0 ; j < 1000 ; j++)
D[i][j] = DOUBLE_MAX;
return;
}
void compute_distance(double **D, double **R , double **V)
{
int i = 0 , j = 0;
float tolerance = 2*radius;
#ifndef OPENMP
{
for( i = 0 ; i < 1000; i++)
{
for ( j = i+1 ; j < 1000 ; j++)
{
////////Updating Position if distance is less than tolerance////
if(euclidean(R[j][0],R[j][1],R[j][2],R[i][0],R[i][1],R[i][2]) <= tolerance)
{
swap(V[i][0],V[j][0]);
swap(V[i][1],V[j][1]);
swap(V[i][2],V[j][2]);
swap(R[i][0],R[j][0]);
swap(R[i][1],R[j][1]);
swap(R[i][2],R[j][2]);
}
///////////////////////////////////////////////////
}
}
}
#endif
#ifdef OPENMP
{
#pragma omp parallel for private(i,j) shared(R,V) schedule(dynamic)
for( i = 0 ; i < 1000; i++)
{
for ( j = i+1 ; j < 1000 ; j++)
{
//if( i!=j) //check if it will work upon parallezing
////////Updating Position and Velocity if distance is less than tolerance////
if(euclidean(R[j][0],R[j][1],R[j][2],R[i][0],R[i][1],R[i][2]) <= tolerance)
{
swap(V[i][0],V[j][0]);
swap(V[i][1],V[j][1]);
swap(V[i][2],V[j][2]);
swap(R[i][0],R[j][0]);
swap(R[i][1],R[j][1]);
swap(R[i][2],R[j][2]);
}
///////////////////////////////////////////////////////////////
}
}
}
#endif
return;
}
void collision_and_position_update(double **R,double **V, double **D)
{
//////position update//////////
int i;
//#pragma omp parallel for private(i)
for(i=0;i<number_bodies;i++)
{
R[i][0] += V[i][0]*delta_t;
R[i][1] += V[i][1]*delta_t;
R[i][2] += V[i][2]*delta_t;
/////Boundary Collision Check/////
// #pragma omp critical
// {
if( R[i][0] >= breadth || R[i][0] <= 0)
{
R[i][0] = (R[i][0] <= 0)?(-R[i][0]) : (2*breadth - R[i][0]);
V[i][0] = -V[i][0];
}
if( R[i][1] >= length || R[i][1] <= 0)
{
R[i][1] = (R[i][1] <= 0)?(-R[i][1]) : (2*length - R[i][1]);
V[i][1] = -V[i][1];
}
if( R[i][2] >= height || R[i][2] <= 0)
{
R[i][2] = (R[i][2] <= 0)?(-R[i][2]) : (2*height - R[i][2]);
V[i][2] = -V[i][2];
}
// }
/////////////////////////////////
}
/////////Collision Check between bodies//////////
#ifdef COLLISION_CHECK
{
compute_distance(D,R,V);
if(print_distance) print_matrix(D);
}
#endif
/////////////////////////////////////////////////
}
void generate_bin_file(double **R,int num)
{
fstream file;
string s = "trajectory"+to_string(num)+".bin";
file.open(s,ios::out|ios::binary);
int i;
for(i=0 ; i < number_bodies ; i++)
{
file << R[i][0] << " ";
file << R[i][1] << " ";
file << R[i][2] << " ";
file <<"\n";
}
file.close();
}
//Reads the initial coordinates of 1000 balls//
void read_txt_file(double **R){
fstream init("trajectory.txt", std::ios_base::in);
if(init.is_open())
{
int idx = 0;
while (idx < 1000)
{
init >> R[idx][0] >> R[idx][1] >> R[idx][2];
idx++;
}
init.close();
return;
}
else
{ cout << "Unable to open file!" << endl;
exit (EXIT_FAILURE);
}
}
int main(int argc, char** argv)
{
int i;
double **R,**F,**V,**D;
omp_set_num_threads(stoi(argv[1]));
cout << argv[1] << "\n";
fstream out;
string filename = "sim_log.txt";
out.open(filename,ios::out|ios::binary);
///Divide each initialissation into sections in openmp///////
#pragma omp parallel sections num_threads(4) private(i)
{
#pragma omp section
{
////////////////initialise Position matrix////////////////
R = (double**)malloc(sizeof(double*)*1000);
for(i = 0; i <1000; i++)
R[i] = (double*)malloc(sizeof(double)*3);
read_txt_file(R);
/////////////////////////////////////////////////////////
}
#pragma omp section
{
///////////////Initialise Force matrix////////////////
F = (double**)malloc(sizeof(double*)*1000);
for( i = 0; i <1000; i++)
F[i] = (double*)malloc(sizeof(double)*3);
///////////////////////////////////////////////////////
}
#pragma omp section
{
///Initialise Velocity matrix assuming intial rest conditions///
V = (double**)calloc(1000, sizeof(double*));
for( i = 0; i <1000; i++)
V[i] = (double*)calloc(3, sizeof(double));
///////////////////////////////////////////////////////
}
#pragma omp section
{
////////////////Initialise Distance matrix//////////////
D = (double**)malloc(sizeof(double*)*1000);
for(i = 0; i <1000; i++)
D[i] = (double*)malloc(sizeof(double)*1000);
initialise_distance(D);
///////////////////////////////////////////////////////
}
}
double wt;
double wtime=omp_get_wtime();
for( i = 0 ;i <timestep ; i++){ //has to be sequential
if(i%100 == 0) wt = omp_get_wtime();
find_force(F,R);
velocity_update(F,V);
collision_and_position_update(R,V,D);
velocity_update(F,V);
if(i%100 == 0)
{
generate_bin_file(R,i); //cannot be parallized
out << "Iteration "<<i <<" took "<< (omp_get_wtime()-wt) << "s \n";
}
}
out << "Total Iteration Time : " << (omp_get_wtime()-wtime) << "\n";
out.close();
cout<<"Done!"<<endl;
if(print_pose) //set print_pose to view position matrix
print_matrix(R);
else if (print_force) //set print_pose to view position matrix
print_matrix(F);
return 0;
}