-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
369 lines (303 loc) · 10 KB
/
main.py
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
import taichi as ti
from orbit_math import *
@ti.func
def elliptic_params_to_vector(a: ti.f32, e: ti.f32, rotation: ti.math.mat3,
n: ti.f32, t: ti.f32, cM: ti.f32):
E = elliptic_params_to_angle(t, n, e)
return elliptic_angle_to_vector(E, a, e, rotation, cM)
@ti.func
def hyperbolic_params_to_vector(a: ti.f32, e: ti.f32, rotation: ti.math.mat3,
n: ti.f32, t: ti.f32, cM: ti.f32):
H = hyperbolic_params_to_angle(t, n, e)
return hyperbolic_angle_to_vector(H, a, e, rotation, cM)
@ti.func
def vector_to_params(X: vec3, V: vec3, cM: ti.f32, time: ti.f32):
a: ti.f32 = 0
e: ti.f32 = 0
i: ti.f32 = 0
w: ti.f32 = 0
W: ti.f32 = 0
n: ti.f32 = 0
t: ti.f32 = 0
t0: ti.f32 = 0
# rotation:ti.math.mat3 = [1,0,0,0,1,0,0,0,1]
X_magnitude = ti.Vector.norm(X)
mu = cM * G
Ws = V.dot(V) / 2 - mu / X_magnitude
a = -mu / (2 * Ws)
L = ti.Vector.cross(X, V)
L_magnitude = L.norm()
L2 = L_magnitude * L_magnitude
p = L2 / mu
e = ti.sqrt(1 - p / a)
if (ti.abs(a) < 1e-6):
e = 1
E: ti.f32 = 0
if (e == 0):
E = 0
elif (e <= 1):
#cosE = (1 - X_magnitude / SOP.a) / SOP.e
#sinE = Vector3.Dot(X, V) / (SOP.e * ti.sqrt(SOP.a * mu))
cosE = (1 - X_magnitude / a)
sinE = X.dot(V) / ti.sqrt(a * mu)
E = ti.math.atan2(sinE, cosE)
else:
coshE = (1 + X_magnitude / a) / e
sinhE = X.dot(V) / (e * ti.sqrt(-a * mu))
#E = MathF.Atanh(sinhE / coshE)
E = m_asinh(sinhE)
# T = 2 * math.pi * ti.sqrt(a * a * a / mu)
M: ti.f32 = 0
if (e <= 1):
M = (E - e * ti.sin(E))
else:
M = e * m_sinh(E) - E
n = ti.sqrt(mu / ti.abs(a * a * a))
t = M / n
t0 = time - t
sini: ti.f32 = 0
cosi: ti.f32 = 0
if (e != 1):
sini = ti.sqrt(L.x * L.x + L.y * L.y) / L_magnitude
cosi = L.z / L_magnitude
i = ti.math.atan2(sini, cosi)
else:
i = 0
sini = 0
cosi = 1
AscendingAxis = vec3(-L.y, L.x, 0)
if (cosi == 1):
AscendingAxis.x = 1
AscendingAxis.y = 0
elif (cosi == -1):
AscendingAxis.x = -1
AscendingAxis.y = 0
θ: ti.f32 = 0 # 真近点角
bDa2 = (1 - e * e) # b/a^2
if (e <= 1):
# tanθ = √(1-e^2)sinE/(cosE-e)
θ = ti.math.atan2(ti.sqrt(bDa2) * ti.sin(E), ti.cos(E) - e)
else:
sin = ti.sqrt(-bDa2) * m_sinh(E)
cos = -(m_cosh(E) - e)
θ = ti.math.atan2(sin, cos)
#v = ω + θ
# not sinv but sinv*X*A
normalAxis = L.normalized()
if (normalAxis.x == 0 and normalAxis.y == 0 and normalAxis.z == 0):
normalAxis.x = 0
normalAxis.y = 0
normalAxis.z = 1
sinv = ti.Vector.cross(AscendingAxis, X).dot(normalAxis)
cosv = AscendingAxis.dot(X)
v = ti.math.atan2(sinv, cosv)
w = v - θ # 近地点与参考轴的夹角
if (i == 0):
W = 0
else:
W = ti.math.atan2(AscendingAxis.y, AscendingAxis.x)
rotation4 = ti.math.rotation3d(
0.0, 0.0, W)@ti.math.rotation3d(i, 0.0, 0.0)@ti.math.rotation3d(0.0, 0.0, w)
rotation = rotation4[0:3,0:3]
return Orbit(a, e, i, w, W, n, t, t0, cM, rotation)
# 0: Sun
# EllipticObjetcCount = 0
# HyperbolicObjetcCount = 0
# EllipticField = ti.field(dtype=Orbit, shape=field_end[2])
# HyperbolicField = ti.field(dtype=Orbit, shape=field_end[2])
@ti.dataclass
class Orbit:
a: ti.f32
e: ti.f32
i: ti.f32
w: ti.f32
W: ti.f32
n: ti.f32
t: ti.f32
t0: ti.f32
M: ti.f32
rotation: ti.math.mat3
@ti.dataclass
class ObjectVector:
X: vec3
V: vec3
m: ti.f32
r: ti.f32
massiveFlag: ti.i32
center: ti.i32
@ti.dataclass
class VectorLerp:
X1: vec3
X2: vec3
V1: vec3
V2: vec3
t0: ti.f32
t1: ti.f32
capacity = 100000
MassiveIndex = ti.field(dtype=ti.i32, shape=capacity)
field_end = ti.field(dtype=ti.i32, shape=3)
orbit_field = Orbit.field(shape=capacity)
vector_field = ObjectVector.field(shape=capacity)
@ti.func
def field_swap(_from, _to):
orbit_field[_from], orbit_field[_to] = orbit_field[_to], orbit_field[_from]
vector_field[_from], vector_field[_to] = vector_field[_to], vector_field[_from]
@ti.func
def static_add() -> ti.i32:
field_swap(field_end[1], field_end[2])
field_swap(field_end[0], field_end[1])
field_end[0] = field_end[0] + 1
field_end[1] = field_end[1] + 1
field_end[2] = field_end[2] + 1
return field_end[0]-1
@ti.func
def ellicptic_add() -> ti.i32:
field_swap(field_end[1], field_end[2])
field_end[1] = field_end[1] + 1
field_end[2] = field_end[2] + 1
return field_end[1]-1
@ti.func
def hyperbolic_add() -> ti.i32:
field_end[2] = field_end[2] + 1
return field_end[2]-1
@ti.func
def ellicptic_swap(h_index: ti.i32):
field_swap(h_index, field_end[1])
field_end[1] = field_end[1] + 1
@ti.func
def hyperbolic_swap(e_index: ti.i32):
field_end[1] = field_end[1] - 1
field_swap(e_index, field_end[1])
@ti.kernel
def initialize(count: ti.i32):
for i in range(count):
vector_field[i] = ObjectVector(
vec3(ti.randn(), ti.randn(), ti.randn()).normalized()*2,
vec3(ti.randn(), ti.randn(), ti.randn())*2.0,
m=1, r=1, massiveFlag=0, center=0)
# vector_field[i].V = ti.math.cross(vector_field[i].V,vector_field[i].X) * 0.5
static_index = static_add()
MassiveIndex[static_index] = 0
vector_field[static_index].X = vec3(0, 0, 0)
vector_field[static_index].V = vec3(0, 0, 0)
vector_field[static_index].massiveFlag = 1
vector_field[static_index].m = 10
vector_field[static_index].r = 10
ti.loop_config(serialize=True)
for i in range(field_end[0], count):
vecs = vector_field[field_end[2]]
orbit = vector_to_params(
vecs.X, vecs.V, vector_field[vecs.center].m, 0.0)
if (orbit.e <= 1):
orbit_field[ellicptic_add()] = orbit
elif (orbit.e > 1):
orbit_field[hyperbolic_add()] = orbit
@ti.kernel
def update_object_params(begin: ti.i32, end: ti.i32, time: ti.f32):
for i in range(begin, end):
vecs = vector_field[i]
orbit_field[i] = vector_to_params(
vecs.X, vecs.V, vector_field[vecs.center].m, time)
if (orbit_field[i].e <= 1 and i >= field_end[1]):
ellicptic_swap(i)
elif (orbit_field[i].e > 1 and i < field_end[1]):
hyperbolic_swap(i)
@ti.kernel
def update_object_vectors(time: ti.f32):
for i in range(field_end[0], field_end[1]):
orb = orbit_field[i]
orb.t = time - orb.t0
vector_field[i].X, vector_field[i].V = elliptic_params_to_vector(
a=orb.a, e=orb.e, rotation=orb.rotation, n=orb.n, t=orb.t, cM=orb.M)
for i in range(field_end[1], field_end[2]):
orb = orbit_field[i]
orb.t = time - orb.t0
vector_field[i].X, vector_field[i].V = hyperbolic_params_to_vector(
a=orb.a, e=orb.e, rotation=orb.rotation, n=orb.n, t=orb.t, cM=orb.M)
@ti.kernel
def dynamic_update(begin: ti.i32, end: ti.i32, dt: ti.f32):
vf = ti.static(vector_field)
for i in range(begin, end):
cv = vf[vf[i].center]
r = vf[i].X-cv.X
a = -cv.m*G/(r.norm_sqr()) * r.normalized()
vf[i].V += a*dt
vf[i].X += vf[i].V * dt
@ti.kernel
def add_test():
static_add()
delta_time = 0.001
time_scale:float = 1.0
simulate_time = 0
print("initializing...")
initialize(capacity)
update_object_params(field_end[0], field_end[2], simulate_time)
print("initializing gui...")
# gui framework
window = ti.ui.Window("Restricted Gravity", (1024, 1024), vsync=False)
canvas = window.get_canvas()
canvas.set_background_color((0, 0, 0))
scene = ti.ui.Scene()
camera = ti.ui.Camera()
camera.position(0.0, 0.0, 10)
camera.lookat(0.0, 0.0, 0)
print("initialization finished")
print("start simulation")
distance = 10.0
def camera_update(window, dt):
global distance
camera.position(0.0, distance, 1)
camera.lookat(0.0, 0.0, 0.0)
if (window.is_pressed(' ')):
distance = 10.0
if (window.is_pressed('w')):
distance -= 100*dt
if (window.is_pressed('s')):
distance += 100*dt
def force_control(window, dt):
force = vec3(0, 0, 0)
if (window.is_pressed('t')):
force -= 100*delta_time
if (window.is_pressed('g')):
force += 100*delta_time
while window.running:
# physics
simulate_time += delta_time*time_scale
if (window.is_pressed('x')):
time_scale *= 1.01
if (window.is_pressed('z')):
time_scale *= 0.99
if window.is_pressed('r'):
simulate_time = 0
gui = window.get_gui()
with gui.sub_window("info", 0, 0, 0.5, 0.3):
gui.text('w and s to zoom')
gui.text('space to reset zoom')
gui.text('x and z to change time scale')
gui.text('r to reset time')
gui.text(f'time_scale:{time_scale}')
gui.text(f'static:{field_end[0]} ellicptic:{field_end[1]-field_end[0]} hyperbolic:{field_end[2]-field_end[1]}')
gui.text(f'time:{simulate_time}')
update_object_vectors(simulate_time)
# for i in range(10):
# U.dynamic_update(1,U.field_end[2],delta_time/10)
# camera
# camera.position(0.0,0.0, 10)
camera_update(window, delta_time)
scene.set_camera(camera)
# render
scene.point_light(pos=(0, 0, 0), color=(1, 1, 1))
scene.ambient_light((0.5, 0.5, 0.5))
# object render
scene.particles(centers=vector_field.X, radius=0.005, per_vertex_color=vector_field.V)
# gui = window.get_gui()
# with gui.sub_window("name", 0, 0, 0.5, 0.3):
# gui.text(content=f'X: {U.VectorField[0].X.x}')
# new_color = gui.color_edit_3("name", old_color)
# of1 = orbit_field[10]
# print('a: ', of1.a, 'e: ', of1.e, 'i: ', of1.i, 'w: ', of1.w, 'W: ',
# of1.W, 'n: ', of1.n, 't: ', of1.t, 't0: ', of1.t0, 'M: ', of1.M)
# print(vector_field[10].X)
# print("field_end[0]: ", field_end[0], "field_end[1]: ", field_end[1],"field_end[2]: ", field_end[2])
canvas.scene(scene)
window.show()