This repository has been archived by the owner on Oct 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
step-1.cpp
374 lines (296 loc) · 10.7 KB
/
step-1.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// Translate this file with
//
// g++ -O3 assignment-code.cpp -o assignment-code
//
// Run it with
//
// ./demo-code
//
// There should be a result.pvd file that you can open with Paraview.
// Sometimes, Paraview requires to select the representation "Point Gaussian"
// to see something meaningful.
//
// (C) 2018-2020 Tobias Weinzierl
#include <fstream>
#include <sstream>
#include <iostream>
#include <string>
#include <math.h>
#include <limits>
#include <iomanip>
#include <cmath>
double t = 0;
double tFinal = 0;
double tPlot = 0;
double tPlotDelta = 0;
int NumberOfBodies = 0;
/**
* Pointer to pointers. Each pointer in turn points to three coordinates, i.e.
* each pointer represents one molecule/particle/body.
*/
double** x;
/**
* Equivalent to x storing the velocities.
*/
double** v;
/**
* One mass entry per molecule/particle.
*/
double* mass;
/**
* Global time step size used.
*/
double timeStepSize = 0.0;
/**
* Maximum velocity of all particles.
*/
double maxV;
/**
* Minimum distance between two elements.
*/
double minDx;
double C;
int* merged;
double* force0;
double* force1;
double* force2;
/**
* Set up scenario from the command line.
*
* If you need additional helper data structures, you can
* initialise them here. Alternatively, you can introduce a
* totally new function to initialise additional data fields and
* call this new function from main after setUp(). Either way is
* fine.
*
* This operation's semantics is not to be changed in the assignment.
*/
void setUp(int argc, char** argv) {
NumberOfBodies = (argc-4) / 7;
C = 0.01 * NumberOfBodies;
merged = new int[NumberOfBodies];
x = new double*[NumberOfBodies];
v = new double*[NumberOfBodies];
mass = new double [NumberOfBodies];
// force0 = force along x direction
// force1 = force along y direction
// force2 = force along z direction
force0 = new double[NumberOfBodies];
force1 = new double[NumberOfBodies];
force2 = new double[NumberOfBodies];
int readArgument = 1;
tPlotDelta = std::stof(argv[readArgument]); readArgument++;
tFinal = std::stof(argv[readArgument]); readArgument++;
timeStepSize = std::stof(argv[readArgument]); readArgument++;
for (int i=0; i<NumberOfBodies; i++) {
x[i] = new double[3];
v[i] = new double[3];
merged[i] = -1;
x[i][0] = std::stof(argv[readArgument]); readArgument++;
x[i][1] = std::stof(argv[readArgument]); readArgument++;
x[i][2] = std::stof(argv[readArgument]); readArgument++;
v[i][0] = std::stof(argv[readArgument]); readArgument++;
v[i][1] = std::stof(argv[readArgument]); readArgument++;
v[i][2] = std::stof(argv[readArgument]); readArgument++;
mass[i] = std::stof(argv[readArgument]); readArgument++;
if (mass[i]<=0.0 ) {
std::cerr << "invalid mass for body " << i << std::endl;
exit(-2);
}
}
std::cout << "created setup with " << NumberOfBodies << " bodies" << std::endl;
if (tPlotDelta<=0.0) {
std::cout << "plotting switched off" << std::endl;
tPlot = tFinal + 1.0;
}
else {
std::cout << "plot initial setup plus every " << tPlotDelta << " time units" << std::endl;
tPlot = 0.0;
}
}
std::ofstream videoFile;
/**
* This operation is not to be changed in the assignment.
*/
void openParaviewVideoFile() {
videoFile.open( "result.pvd" );
videoFile << "<?xml version=\"1.0\"?>" << std::endl
<< "<VTKFile type=\"Collection\" version=\"0.1\" byte_order=\"LittleEndian\" compressor=\"vtkZLibDataCompressor\">" << std::endl
<< "<Collection>";
}
/**
* This operation is not to be changed in the assignment.
*/
void closeParaviewVideoFile() {
videoFile << "</Collection>"
<< "</VTKFile>" << std::endl;
}
/**
* The file format is documented at http://www.vtk.org/wp-content/uploads/2015/04/file-formats.pdf
*
* This operation is not to be changed in the assignment.
*/
void printParaviewSnapshot() {
static int counter = -1;
counter++;
std::stringstream filename;
filename << "result-" << counter << ".vtp";
std::ofstream out( filename.str().c_str() );
out << "<VTKFile type=\"PolyData\" >" << std::endl
<< "<PolyData>" << std::endl
<< " <Piece NumberOfPoints=\"" << NumberOfBodies << "\">" << std::endl
<< " <Points>" << std::endl
<< " <DataArray type=\"Float64\" NumberOfComponents=\"3\" format=\"ascii\">";
// << " <DataArray type=\"Float32\" NumberOfComponents=\"3\" format=\"ascii\">";
for (int i=0; i<NumberOfBodies; i++) {
out << x[i][0]
<< " "
<< x[i][1]
<< " "
<< x[i][2]
<< " ";
}
out << " </DataArray>" << std::endl
<< " </Points>" << std::endl
<< " </Piece>" << std::endl
<< "</PolyData>" << std::endl
<< "</VTKFile>" << std::endl;
videoFile << "<DataSet timestep=\"" << counter << "\" group=\"\" part=\"0\" file=\"" << filename.str() << "\"/>" << std::endl;
}
/**
* This is the main operation you should change in the assignment. You might
* want to add a few more variables or helper functions, but this is where the
* magic happens.
*/
void updateBody() {
maxV = 0.0;
minDx = std::numeric_limits<double>::max();
// Compute forces for each particle
for (int i=0; i<NumberOfBodies; i++) {
// If this particle has been merged, don't compute its force
if (merged[i] != -1) {
continue;
}
force0[i] = 0.0;
force1[i] = 0.0;
force2[i] = 0.0;
for (int j=0; j<NumberOfBodies; j++) {
// Filter out the current particle and merged particles
if (j != i && merged[j] == -1) {
// Compute Euclidian distances to other particles (this is just pythag)
// Could stand to be pre-computed
const double distance = sqrt(
(x[i][0]-x[j][0]) * (x[i][0]-x[j][0]) +
(x[i][1]-x[j][1]) * (x[i][1]-x[j][1]) +
(x[i][2]-x[j][2]) * (x[i][2]-x[j][2])
);
// If particles should be merged, merge them
if (distance < (C * (mass[i] + mass[j]))) {
merged[j] = i;
// Merge any particles that were merged to k into i
for (int k=0; k<NumberOfBodies; k++) {
if (merged[k] == j) {
merged[k] = i;
}
}
v[i][0] = mass[i] * v[i][0] / (mass[i] + mass[j]) + mass[j] * v[j][0] / (mass[i] + mass[j]);
v[i][1] = mass[i] * v[i][1] / (mass[i] + mass[j]) + mass[j] * v[j][1] / (mass[i] + mass[j]);
v[i][2] = mass[i] * v[i][2] / (mass[i] + mass[j]) + mass[j] * v[j][2] / (mass[i] + mass[j]);
mass[i] = mass[i] + mass[j];
x[i][0] = (mass[i] * x[i][0] + mass[j] * x[j][0]) / (mass[i] + mass[j]);
x[i][1] = (mass[i] * x[i][1] + mass[j] * x[j][1]) / (mass[i] + mass[j]);
x[i][2] = (mass[i] * x[i][2] + mass[j] * x[j][2]) / (mass[i] + mass[j]);
continue;
}
// x,y,z forces acting on particle i
force0[i] += (x[j][0]-x[i][0]) * mass[j]*mass[i] / distance / distance / distance ;
force1[i] += (x[j][1]-x[i][1]) * mass[j]*mass[i] / distance / distance / distance ;
force2[i] += (x[j][2]-x[i][2]) * mass[j]*mass[i] / distance / distance / distance ;
minDx = std::min( minDx,distance );
}
}
}
for (int i=0; i<NumberOfBodies; i++) {
if (merged[i] == -1) {
x[i][0] = x[i][0] + timeStepSize * v[i][0];
x[i][1] = x[i][1] + timeStepSize * v[i][1];
x[i][2] = x[i][2] + timeStepSize * v[i][2];
v[i][0] = v[i][0] + timeStepSize * force0[i] / mass[i];
v[i][1] = v[i][1] + timeStepSize * force1[i] / mass[i];
v[i][2] = v[i][2] + timeStepSize * force2[i] / mass[i];
maxV = std::max(maxV, v[i][0]*v[i][0] + v[i][1]*v[i][1] + v[i][2]*v[i][2]);
}
else {
x[i][0] = x[merged[i]][0];
x[i][1] = x[merged[i]][1];
x[i][2] = x[merged[i]][2];
}
}
maxV = std::sqrt(maxV);
t += timeStepSize;
}
/**
* Main routine.
*
* No major changes in assignment. You can add a few initialisation
* or stuff if you feel the need to do so. But keep in mind that you
* may not alter what the program plots to the terminal.
*/
int main(int argc, char** argv) {
if (argc==1) {
std::cerr << "usage: " + std::string(argv[0]) + " snapshot final-time dt objects" << std::endl
<< " snapshot interval after how many time units to plot. Use 0 to switch off plotting" << std::endl
<< " final-time simulated time (greater 0)" << std::endl
<< " dt time step size (greater 0)" << std::endl
<< std::endl
<< "Examples:" << std::endl
<< "0.01 100.0 0.001 0.0 0.0 0.0 1.0 0.0 0.0 1.0 \t One body moving form the coordinate system's centre along x axis with speed 1" << std::endl
<< "0.01 100.0 0.001 0.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 1.0 0.0 1.0 0.0 0.0 1.0 \t One spiralling around the other one" << std::endl
<< "0.01 100.0 0.001 3.0 0.0 0.0 0.0 1.0 0.0 0.4 0.0 0.0 0.0 0.0 0.0 0.0 0.2 2.0 0.0 0.0 0.0 0.0 0.0 1.0 \t Three body setup from first lecture" << std::endl
<< "0.01 100.0 0.001 3.0 0.0 0.0 0.0 1.0 0.0 0.4 0.0 0.0 0.0 0.0 0.0 0.0 0.2 2.0 0.0 0.0 0.0 0.0 0.0 1.0 2.0 1.0 0.0 0.0 0.0 0.0 1.0 2.0 0.0 1.0 0.0 0.0 0.0 1.0 \t Five body setup" << std::endl
<< std::endl
<< "In this naive code, only the first body moves" << std::endl;
return -1;
}
else if ( (argc-4)%7!=0 ) {
std::cerr << "error in arguments: each planet is given by seven entries (position, velocity, mass)" << std::endl;
std::cerr << "got " << argc << " arguments (three of them are reserved)" << std::endl;
std::cerr << "run without arguments for usage instruction" << std::endl;
return -2;
}
std::cout << std::setprecision(15);
setUp(argc,argv);
openParaviewVideoFile();
int snapshotCounter = 0;
if (t > tPlot) {
printParaviewSnapshot();
std::cout << "plotted initial setup" << std::endl;
tPlot = tPlotDelta;
}
int timeStepCounter = 0;
while (t<=tFinal) {
updateBody();
timeStepCounter++;
if (t >= tPlot) {
printParaviewSnapshot();
std::cout << "plot next snapshot"
<< ",\t time step=" << timeStepCounter
<< ",\t t=" << t
<< ",\t dt=" << timeStepSize
<< ",\t v_max=" << maxV
<< ",\t dx_min=" << minDx
<< std::endl;
tPlot += tPlotDelta;
}
}
int remainingBodies = 0;
for (int i=0; i<NumberOfBodies; i++) {
if (merged[i] == -1) {
remainingBodies++;
}
}
std::cout << "Number of remaining objects: " << remainingBodies << std::endl;
std::cout << "Position of first remaining object: " << x[0][0] << ", " << x[0][1] << ", " << x[0][2] << std::endl;
closeParaviewVideoFile();
return 0;
}