-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheuler.c
279 lines (241 loc) · 6.46 KB
/
euler.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
struct body {
char name[16];
double r[4];
double v[4];
double a[4];
double m;
int fixed;
};
struct data {
int nbodies;
struct body* bodies;
double G;
double dt;
};
void euler_next(struct data* data) {
int n = data->nbodies;
double G = data->G;
double dt = data->dt;
#pragma omp parallel for
for (int i = 0; i < n; i++) {
double rij[4];
struct body* b1 = &data->bodies[i];
if (b1->fixed) continue;
for (int k = 0; k < 4; k++) {
b1->a[k] = 0;
}
for (int j = 0; j < n; j++) {
if (i == j) continue;
struct body* b2 = &data->bodies[j];
for (int k = 0; k < 4; k++) {
rij[k] = (b2->r[k] - b1->r[k]);
}
double R = 0;
for (int k = 0; k < 4; k++) {
R += rij[k] * rij[k];
}
R = sqrt(R);
double Koef = G * b2->m / (R*R*R);
for (int k = 0; k < 4; k++) {
b1->a[k] += Koef * rij[k];
}
}
}
#pragma omp parallel for
for (int i = 0; i < n; i++) {
struct body* b = &data->bodies[i];
for (int k = 0; k < 4; k++) {
b->v[k] = b->v[k] + dt * b->a[k];
b->r[k] = b->r[k] + dt * b->v[k];
}
}
}
double kepler(double dt) {
double G = 1;
double MM = 1e5;
struct body bodies[] = {
{
.r = {0, 0, 0, 0},
.v = {0, 0, 0, 0},
.a = {0, 0, 0, 0},
.m = MM,
.fixed = 1
},
{
.r = {0, 1, 0, 0},
.v = {sqrt(G * MM), 0, 0, 0},
.a = {0, 0, 0, 0},
.m = 1,
.fixed = 0
},
};
struct data data = {
.nbodies = 2,
.bodies = &bodies[0],
.G = G,
.dt = dt
};
double max_err = 0;
double T = 0.1;
double t = 0;
while (t < T) {
euler_next(&data);
double r = 0;
for (int k = 0; k < 4; k++) {
r += data.bodies[1].r[k] * data.bodies[1].r[k];
}
r = sqrt(r);
double err = fabs(r - 1.0);
if (max_err < err) {
max_err = err;
}
t += dt;
}
return max_err;
}
void run_test() {
double err1 = kepler(0.001);
double err2 = kepler(0.0001);
double err3 = kepler(0.00001);
printf("%f %f %f\n", err1, err2, err3);
if (err1 / 10 < err2) {
printf("Error1\n");
exit(1);
}
if (err1 / 100 < err3) {
printf("Error2\n");
exit(2);
}
printf("Ok\n");
exit(0);
}
/*
file format:
G
N
Body1 r0 r1 r2 v0 v1 v2 Mass
Body2 r0 r1 r2 v0 v1 v2 Mass
...
BodyN r0 r1 r2 v0 v1 v2 Mass
*/
void load(struct data* data, const char* fn) {
FILE* f = fopen(fn, "rb");
if (!f) { goto err; }
if (fscanf(f, "%lf %d", &data->G, &data->nbodies) != 2) { goto err; }
data->bodies = calloc(data->nbodies, sizeof(struct body));
for (int i = 0; i < data->nbodies; i++) {
if (fscanf(
f, "%15s %lf %lf %lf %lf %lf %lf %lf",
data->bodies[i].name,
&data->bodies[i].r[0], &data->bodies[i].r[1], &data->bodies[i].r[2],
&data->bodies[i].v[0], &data->bodies[i].v[1], &data->bodies[i].v[2],
&data->bodies[i].m) != 8)
{
goto err;
}
}
fclose(f);
return;
err:
fprintf(stderr, "Cannot open or parse file: '%s'\n", fn);
exit(1);
}
void print_header(struct data* data) {
// column names
printf("t ");
for (int i = 0; i < data->nbodies; i++) {
for (int j = 0; j < 3; j++) {
printf("r%d,%d ", i, j);
}
for (int j = 0; j < 3; j++) {
printf("v%d,%d ", i, j);
}
}
printf("\n");
// comment
for (int i = 0; i < data->nbodies; i++) {
printf("# %s %le\n", data->bodies[i].name, data->bodies[i].m);
}
}
void print(struct data* data, double t) {
printf("%e ", t);
for (int i = 0; i < data->nbodies; i++) {
printf(
"%e %e %e %e %e %e ",
data->bodies[i].r[0], data->bodies[i].r[1], data->bodies[i].r[2],
data->bodies[i].v[0], data->bodies[i].v[1], data->bodies[i].v[2]);
}
printf("\n");
}
void solve(struct data* data, double T) {
double t = 0;
print_header(data);
print(data, t);
while (t < T) {
euler_next(data);
t += data->dt;
print(data, t);
}
}
double get_seconds_elapsed(struct timespec start, struct timespec end) {
return (double)(end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) / 1.0e9;
}
void bench(struct data* data) {
double t = 0;
struct timespec start_time, end_time;
double elapsed_time;
clock_gettime(CLOCK_MONOTONIC, &start_time);
while (t < 0.1) {
euler_next(data);
t += data->dt;
}
clock_gettime(CLOCK_MONOTONIC, &end_time);
elapsed_time = get_seconds_elapsed(start_time, end_time);
printf("Elapsed: %fs\n", elapsed_time);
}
void usage(const char* name) {
fprintf(stderr, "%s --input file.txt [--dt 0.001] [--T 10] [--test]\n", name);
exit(0);
}
int main(int argc, char** argv) {
const char* fn = NULL;
double dt = 0.0001;
double T = 10.0;
int test_mode = 0;
int bench_mode = 0;
for (int i = 1; i < argc; i++) {
if (i < argc - 1 && !strcmp(argv[i], "--input")) {
fn = argv[++i];
} else if (i < argc - 1 && !strcmp(argv[i], "--dt")) {
dt = atof(argv[++i]);
} else if (i < argc - 1 && !strcmp(argv[i], "--T")) {
T = atof(argv[++i]);
} else if (!strcmp(argv[i], "--test")) {
test_mode = 1;
} else if (!strcmp(argv[i], "--bench")) {
bench_mode = 1;
} else {
usage(argv[0]);
}
}
if (test_mode) {
run_test(); return 0;
}
if (!fn) {
usage(argv[0]);
}
struct data data = {.dt = dt};
load(&data, fn);
if (bench_mode) {
bench(&data);
} else {
solve(&data, T);
}
free(data.bodies);
return 0;
}