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-0.cpp
314 lines (249 loc) · 9.11 KB
/
step-0.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
// 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;
/**
* 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;
x = new double*[NumberOfBodies];
v = new double*[NumberOfBodies];
mass = 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];
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();
// force0 = force along x direction
// force1 = force along y direction
// force2 = force along z direction
double* force0 = new double[NumberOfBodies];
double* force1 = new double[NumberOfBodies];
double* force2 = new double[NumberOfBodies];
force0[0] = 0.0;
force1[0] = 0.0;
force2[0] = 0.0;
for (int i=1; i<NumberOfBodies; i++) {
const double distance = sqrt(
(x[0][0]-x[i][0]) * (x[0][0]-x[i][0]) +
(x[0][1]-x[i][1]) * (x[0][1]-x[i][1]) +
(x[0][2]-x[i][2]) * (x[0][2]-x[i][2])
);
// x,y,z forces acting on particle 0
force0[0] += (x[i][0]-x[0][0]) * mass[i]*mass[0] / distance / distance / distance ;
force1[0] += (x[i][1]-x[0][1]) * mass[i]*mass[0] / distance / distance / distance ;
force2[0] += (x[i][2]-x[0][2]) * mass[i]*mass[0] / distance / distance / distance ;
minDx = std::min( minDx,distance );
}
x[0][0] = x[0][0] + timeStepSize * v[0][0];
x[0][1] = x[0][1] + timeStepSize * v[0][1];
x[0][2] = x[0][2] + timeStepSize * v[0][2];
// These are three buggy lines of code that we will use in one of the labs
// x[0][3] = x[0][2] + timeStepSize * v[0][2];
// x[0][2] = x[0][2] + timeStepSize * v[0][2] / 0.0;
// x[50000000][1] = x[0][2] + timeStepSize * v[0][2] / 0.0;
v[0][0] = v[0][0] + timeStepSize * force0[0] / mass[0];
v[0][1] = v[0][1] + timeStepSize * force1[0] / mass[0];
v[0][2] = v[0][2] + timeStepSize * force2[0] / mass[0];
maxV = std::sqrt( v[0][0]*v[0][0] + v[0][1]*v[0][1] + v[0][2]*v[0][2] );
t += timeStepSize;
delete[] force0;
delete[] force1;
delete[] force2;
}
/**
* 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;
}
}
std::cout << "Number of remaining objects: " << NumberOfBodies << std::endl;
std::cout << "Position of first remaining object: " << x[0][0] << ", " << x[0][1] << ", " << x[0][2] << std::endl;
closeParaviewVideoFile();
return 0;
}