-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
193 lines (164 loc) · 3.82 KB
/
main.cpp
File metadata and controls
193 lines (164 loc) · 3.82 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
#include <cstdio>
#include <vapor/vapor.hpp>
using namespace vapor;
using particle_t = vec_t<double, 6>;
using particle_array_t = memory_backed_array_t<1, particle_t, ref_counted_ptr_t>;
auto G = 1.0;
auto M = 1.0;
struct State
{
double time;
double iter;
particle_array_t part;
};
static auto average(const State& a, const State& b, double x) -> State
{
return x == 1.0 ? b : State{
(a.time * (1.0 - x) + b.time * x),
(a.iter * (1.0 - x) + b.iter * x),
(a.part * (1.0 - x) + b.part * x).cache(),
};
}
auto pos(const particle_t& particle)
{
return vec(particle[0], particle[1], particle[2]);
}
auto vel(const particle_t& particle)
{
return vec(particle[3], particle[4], particle[5]);
}
auto norm(const vec_t<double, 3>& v)
{
return sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
}
auto initial_particles()
{
auto num_particles = 100;
auto r0 = 1.0;
auto r1 = 2.0;
auto dr = (r1 - r0) / (num_particles - 1);
return range(num_particles).map([=] (int i) {
auto r = r0 + i * dr;
auto v = sqrt(G * M / r);
auto p = particle_t{r, 0.0, 0.0, 0.0, v, 0.0};
return p;
}).cache();
}
auto initial_state()
{
return State{
0.0,
0.0,
initial_particles(),
};
}
auto gravity(vec_t<double, 3> x)
{
auto rhat = x / norm(x);
auto r2 = dot(x, x);
auto a = -G * M / r2 * rhat;
return a;
}
auto min_dt(const State& state)
{
return min(state.part.map([] (auto p)
{
auto x = pos(p);
auto v = vel(p);
auto a = gravity(x);
return norm(v) / norm(a);
}));
}
auto time_derivative(particle_array_t particles)
{
return particles.map([] (auto p)
{
auto x = pos(p);
auto v = vel(p);
auto a = gravity(x);
return concat(v, a);
});
}
auto next(State state, double dt)
{
return State{
state.time + dt,
state.iter + 1,
(state.part + time_derivative(state.part) * dt).cache()
};
}
static void update_state(State& state)
{
auto rk_order = 3;
auto s0 = state;
auto dt = min_dt(state) * 0.1;
switch (rk_order)
{
case 1: {
state = next(s0, dt);
break;
}
case 2: {
auto s1 = average(s0, next(s0, dt), 1./1);
auto s2 = average(s0, next(s1, dt), 1./2);
state = s2;
break;
}
case 3: {
auto s1 = average(s0, next(s0, dt), 1./1);
auto s2 = average(s0, next(s1, dt), 1./4);
auto s3 = average(s0, next(s2, dt), 2./3);
state = s3;
break;
}
}
}
void output(particle_array_t particles, const char* filename)
{
FILE* file = fopen(filename, "w");
if (!file)
{
perror("Failed to open file");
return;
}
for (int n = 0; n < particles.size(); ++n)
{
auto p = particles[n];
fprintf(
file, "%+12.10e %+12.10e %+12.10e %+12.10e %+12.10e %+12.10e\n",
p[0], p[1], p[2], p[3], p[4], p[5]
);
}
fclose(file);
}
void output_bin(particle_array_t particles, const char* filename)
{
FILE* file = fopen(filename, "wb");
if (!file)
{
perror("Failed to open file");
return;
}
for (int n = 0; n < particles.size(); ++n)
{
auto p = particles[n];
fwrite(&p, sizeof(particle_t), 1, file);
}
fclose(file);
}
int main()
{
auto state = initial_state();
auto num_outputs = 0;
while (state.time < 2 * M_PI)
{
update_state(state);
if (int(state.iter) % 1 == 0) {
auto fname = format("dust.%04d.bin", num_outputs);
output_bin(state.part, fname.data);
num_outputs++;
}
printf("[%04d] t=%.3f\n", int(state.iter), state.time);
}
return 0;
}