-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver.py
380 lines (306 loc) · 15.6 KB
/
solver.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
370
371
372
373
374
375
376
377
378
379
380
import numpy as np
import taichi as ti
from scene import Scene
@ti.data_oriented
class Solver:
def __init__(self, scene: Scene, h):
self.scene = scene
self.h = h
# function for RK4
@ti.func
def rk4_f(self, pos, L_square):
r = pos.norm()
one_point_five = ti.cast(1.5, ti.f32)
return - (L_square * pos * one_point_five) / (r ** 5)
@ti.func
def determine_color(self, event_horizon_hit, accretion_disk_hit, pos, accretion_disk_hit_x, accretion_disk_hit_y):
color = ti.Vector([0.0, 0.0, 0.0])
# Check if the ray hits the event horizon
if event_horizon_hit:
color = ti.Vector([0.0, 0.0, 0.0]) # Black for event horizon
else:
# Get the skymap color based on the ray's position
color = self.scene.skymap.get_color_from_ray_ti(pos)
# Check if the ray hits the accretion disk
if accretion_disk_hit:
# Blend the accretion disk color with the skymap color
accretion_color = self.scene.get_accretion_disk_color_ti(
accretion_disk_hit_x, accretion_disk_hit_y
)
color = self.scene.accretion_alpha * accretion_color + \
(1 - self.scene.accretion_alpha) * color
return color
# Forward Euler method
@ti.kernel
def solve_forward_euler(self, positions: ti.template(), directions: ti.template(), colors: ti.template()):
one_point_five = ti.cast(1.5, ti.f32)
ad_hit_coord = ti.Vector([0.0, 0.0])
for i, j in positions:
pos = positions[i, j]
dir_ = directions[i, j]
dir_ = dir_.normalized()
L_square = dir_.cross(pos).norm() ** 2
event_horizon_hit = False
while True:
new_pos = pos + self.h * dir_
r = new_pos.norm()
constant = - (L_square * one_point_five) / (r ** 5)
new_dir = dir_ + self.h * constant * pos
# Check for event horizon or accretion disk hit
if pos[2] * new_pos[2] < 0:
t = -pos[2] / (new_pos[2] - pos[2]) # remove the +1e-7
ad_hit_coord = pos[:2] + t * (new_pos[:2] - pos[:2])
if self.scene.accretion_r2 >= ad_hit_coord.norm() >= self.scene.accretion_r1:
colors[i, j] = self.scene.get_accretion_disk_color_ti(
ad_hit_coord[0], ad_hit_coord[1]) + colors[i, j]
pos = new_pos
dir_ = new_dir
if r < self.scene.blackhole_r:
event_horizon_hit = True
break
elif r > self.scene.skymap.r_max:
break
if event_horizon_hit:
colors[i, j] = ti.Vector(
[0.0, 0.0, 0.0]) + self.scene.accretion_alpha * colors[i, j] # Black for event horizon
else:
# Get the skymap color based on the ray's position
colors[i, j] = self.scene.skymap.get_color_from_ray_ti(
pos) + self.scene.accretion_alpha * colors[i, j]
colors[i, j] = ti.math.clamp(colors[i, j], 0.0, 1.0)
# Runge-Kutta 4-step method
@ti.kernel
def solve_rk4(self, positions: ti.template(), directions: ti.template(), colors: ti.template()):
for i, j in positions:
pos = positions[i, j]
dir_ = directions[i, j]
L_square = dir_.cross(pos).norm() ** 2
event_horizon_hit = False
while True:
# RK4 integration for position
k1_pos = self.h * dir_
k1_dir = self.h * self.rk4_f(pos, L_square)
k2_pos = self.h * (dir_ + 0.5 * k1_dir)
k2_dir = self.h * self.rk4_f(pos + 0.5 * k1_pos, L_square)
k3_pos = self.h * (dir_ + 0.5 * k2_dir)
k3_dir = self.h * self.rk4_f(pos + 0.5 * k2_pos, L_square)
k4_pos = self.h * (dir_ + k3_dir)
k4_dir = self.h * self.rk4_f(pos + k3_pos, L_square)
new_pos = pos + (k1_pos + 2 * k2_pos + 2 * k3_pos + k4_pos) / 6
new_dir_ = dir_ + (k1_dir + 2 * k2_dir + 2 * k3_dir + k4_dir) / 6
# Check for event horizon or accretion disk hit
if pos[2] * new_pos[2] < 0:
t = -pos[2] / (new_pos[2] - pos[2]) # remove the +1e-7
ad_hit_coord = pos[:2] + t * (new_pos[:2] - pos[:2])
if self.scene.accretion_r2 >= ad_hit_coord.norm() >= self.scene.accretion_r1:
colors[i, j] = self.scene.get_accretion_disk_color_ti(
ad_hit_coord[0], ad_hit_coord[1]) + colors[i, j]
# Check if the ray hits the event horizon or the skymap
r = pos.norm()
if r < self.scene.blackhole_r:
event_horizon_hit = True
break
elif r > self.scene.skymap.r_max:
break
pos = new_pos
dir_ = new_dir_
if event_horizon_hit:
colors[i, j] = ti.Vector(
[0.0, 0.0, 0.0]) + self.scene.accretion_alpha * colors[i, j] # Black for event horizon
else:
# Get the skymap color based on the ray's position
colors[i, j] = self.scene.skymap.get_color_from_ray_ti(
pos) + self.scene.accretion_alpha * colors[i, j]
colors[i, j] = ti.math.clamp(colors[i, j], 0.0, 1.0)
# Leapfrog method
@ti.kernel
def solve_leapfrog(self, positions: ti.template(), directions: ti.template(), colors: ti.template()):
one_point_five = ti.cast(1.5, ti.f32)
for i, j in positions:
pos = positions[i, j]
dir_ = directions[i, j]
L_square = dir_.cross(pos).norm() ** 2
# Half-step velocity update
r = pos.norm()
constant = - (L_square * one_point_five) / (r ** 5)
dir_ = dir_ + 0.5 * self.h * constant * pos
event_horizon_hit = False
accretion_disk_hit = False
while True:
# Full-step position update
new_pos = pos + self.h * dir_
# Recalculate constants with new position
r = new_pos.norm()
constant = - (L_square * one_point_five) / (r ** 5)
# Full-step velocity update
new_dir_ = dir_ + self.h * constant * pos
# Check for event horizon or accretion disk hit
if pos[2] * new_pos[2] < 0:
t = -pos[2] / (new_pos[2] - pos[2]) # remove the +1e-7
ad_hit_coord = pos[:2] + t * (new_pos[:2] - pos[:2])
if self.scene.accretion_r2 >= ad_hit_coord.norm() >= self.scene.accretion_r1:
colors[i, j] = self.scene.get_accretion_disk_color_ti(
ad_hit_coord[0], ad_hit_coord[1]) + colors[i, j]
pos = new_pos
dir_ = new_dir_
# Check if the ray hits the event horizon or the skymap
r = ti.sqrt(pos.dot(pos))
if r < self.scene.blackhole_r:
event_horizon_hit = True
break
elif r > self.scene.skymap.r_max:
break
if event_horizon_hit:
colors[i, j] = ti.Vector(
[0.0, 0.0, 0.0]) + self.scene.accretion_alpha * colors[i, j] # Black for event horizon
else:
# Get the skymap color based on the ray's position
colors[i, j] = self.scene.skymap.get_color_from_ray_ti(
pos) + self.scene.accretion_alpha * colors[i, j]
colors[i, j] = ti.math.clamp(colors[i, j], 0.0, 1.0)
# Adams-Bashforth 2-step method
@ti.kernel
def solve_ab2(self, positions: ti.template(), directions: ti.template(), colors: ti.template()):
three_over_two = ti.cast(1.5, ti.f32)
one_over_two = ti.cast(0.5, ti.f32)
one_point_five = ti.cast(1.5, ti.f32)
ad_hit_coord = ti.Vector([0.0, 0.0])
for i, j in positions:
pos = positions[i, j]
dir_ = directions[i, j]
L_square = dir_.cross(pos).norm() ** 2
# Initialize f_{n-1}
f_pos_prev = dir_
r = pos.norm()
constant = - (L_square * one_point_five) / (r ** 5)
f_dir_prev = constant * pos
event_horizon_hit = False
accretion_disk_hit = False
while True:
# Compute f_n
f_pos_n = dir_
r = pos.norm()
constant = - (L_square * one_point_five) / (r ** 5)
f_dir_n = constant * pos
new_pos = pos + self.h * (three_over_two * f_pos_n - one_over_two * f_pos_prev)
new_dir_ = dir_ + self.h * (three_over_two * f_dir_n - one_over_two * f_dir_prev)
# Check for event horizon or accretion disk hit
if pos[2] * new_pos[2] < 0:
t = -pos[2] / (new_pos[2] - pos[2]) # remove the +1e-7
ad_hit_coord = pos[:2] + t * (new_pos[:2] - pos[:2])
if self.scene.accretion_r2 >= ad_hit_coord.norm() >= self.scene.accretion_r1:
colors[i, j] = self.scene.get_accretion_disk_color_ti(
ad_hit_coord[0], ad_hit_coord[1]) + colors[i, j]
pos = new_pos
dir_ = new_dir_
# Update previous function evaluations
f_pos_prev = f_pos_n
f_dir_prev = f_dir_n
# Check if the ray hits the event horizon or the skymap
if r < self.scene.blackhole_r:
event_horizon_hit = True
break
elif r > self.scene.skymap.r_max:
break
if event_horizon_hit:
colors[i, j] = ti.Vector(
[0.0, 0.0, 0.0]) + self.scene.accretion_alpha * colors[i, j] # Black for event horizon
else:
# Get the skymap color based on the ray's position
colors[i, j] = self.scene.skymap.get_color_from_ray_ti(
pos) + self.scene.accretion_alpha * colors[i, j]
colors[i, j] = ti.math.clamp(colors[i, j], 0.0, 1.0)
@ti.kernel
def solve_am4(self, positions: ti.template(), directions: ti.template(), colors: ti.template()):
# Store coefficients as constants (arrays are now Taichi-compatible)
coefficients_ab2 = ti.static([3 / 2.0, -1 / 2.0]) # Adams-Bashforth 2-step
coefficients_ab3 = ti.static([23 / 12.0, -16 / 12.0, 5 / 12.0]) # Adams-Bashforth 3-step
coefficients_ab = ti.static([9 / 24.0, -19 / 24.0, 5 / 24.0, 1 / 24.0]) # Adams-Bashforth 4-step
coefficients_am = ti.static([9 / 24.0, 19 / 24.0, -5 / 24.0, -1 / 24.0]) # Adams-Moulton
for i, j in positions:
pos = positions[i, j]
dir_ = directions[i, j]
L_square = dir_.cross(pos).norm() ** 2
# Initialize function evaluations
f_pos_prev = [ti.Vector([0.0, 0.0, 0.0]) for _ in range(4)]
f_dir_prev = [ti.Vector([0.0, 0.0, 0.0]) for _ in range(4)]
# Step 1: Use Euler's method for the first step
f_pos_prev[0] = dir_
f_dir_prev[0] = self.rk4_f(pos, L_square)
pos = pos + self.h * f_pos_prev[0]
dir_ = dir_ + self.h * f_dir_prev[0]
# Step 2: Use Adams-Bashforth 2-step
f_pos_prev[1] = dir_
f_dir_prev[1] = self.rk4_f(pos, L_square)
f_pos_update = ti.Vector([0.0, 0.0, 0.0])
f_dir_update = ti.Vector([0.0, 0.0, 0.0])
for k in ti.static(range(2)): # Use ti.static for Python-style loops
f_pos_update += coefficients_ab2[k] * f_pos_prev[1 - k]
f_dir_update += coefficients_ab2[k] * f_dir_prev[1 - k]
pos = pos + self.h * f_pos_update
dir_ = dir_ + self.h * f_dir_update
# Step 3: Use Adams-Bashforth 3-step
f_pos_prev[2] = dir_
f_dir_prev[2] = self.rk4_f(pos, L_square)
f_pos_update = ti.Vector([0.0, 0.0, 0.0])
f_dir_update = ti.Vector([0.0, 0.0, 0.0])
for k in ti.static(range(3)):
f_pos_update += coefficients_ab3[k] * f_pos_prev[2 - k]
f_dir_update += coefficients_ab3[k] * f_dir_prev[2 - k]
pos = pos + self.h * f_pos_update
dir_ = dir_ + self.h * f_dir_update
# Start Adams-Moulton 4-step method
event_horizon_hit = False
while True:
# Predictor step: Adams-Bashforth 4-step
f_pos_predictor = ti.Vector([0.0, 0.0, 0.0])
f_dir_predictor = ti.Vector([0.0, 0.0, 0.0])
for k in ti.static(range(4)):
f_pos_predictor += coefficients_ab[k] * f_pos_prev[3 - k]
f_dir_predictor += coefficients_ab[k] * f_dir_prev[3 - k]
pos_predictor = pos + self.h * f_pos_predictor
dir_predictor = dir_ + self.h * f_dir_predictor
# Corrector step: Adams-Moulton
f_pos_corrector = dir_predictor
f_dir_corrector = self.rk4_f(pos_predictor, L_square)
f_pos_update = ti.Vector([0.0, 0.0, 0.0])
f_dir_update = ti.Vector([0.0, 0.0, 0.0])
for k in ti.static(range(4)):
if k < 3:
f_pos_update += coefficients_am[k] * f_pos_prev[3 - k]
f_dir_update += coefficients_am[k] * f_dir_prev[3 - k]
else:
f_pos_update += coefficients_am[k] * f_pos_corrector
f_dir_update += coefficients_am[k] * f_dir_corrector
new_pos = pos + self.h * f_pos_update
new_dir_ = dir_ + self.h * f_dir_update
# Check for event horizon or accretion disk hit
if pos[2] * new_pos[2] < 0:
t = -pos[2] / (new_pos[2] - pos[2]) # remove the +1e-7
ad_hit_coord = pos[:2] + t * (new_pos[:2] - pos[:2])
if self.scene.accretion_r2 >= ad_hit_coord.norm() >= self.scene.accretion_r1:
colors[i, j] = self.scene.get_accretion_disk_color_ti(
ad_hit_coord[0], ad_hit_coord[1]) + colors[i, j]
pos = new_pos
dir_ = new_dir_
# Shift previous function evaluations
for k in ti.static(range(3)): # Reverse logic manually
f_pos_prev[3 - k] = f_pos_prev[2 - k]
f_dir_prev[3 - k] = f_dir_prev[2 - k]
f_pos_prev[0] = dir_
f_dir_prev[0] = self.rk4_f(pos, L_square)
# Check if the ray hits the event horizon or the skymap
r = pos.norm()
if r < self.scene.blackhole_r:
event_horizon_hit = True
break
elif r > self.scene.skymap.r_max:
break
if event_horizon_hit:
colors[i, j] = ti.Vector(
[0.0, 0.0, 0.0]) + self.scene.accretion_alpha * colors[i, j] # Black for event horizon
else:
# Get the skymap color based on the ray's position
colors[i, j] = self.scene.skymap.get_color_from_ray_ti(
pos) + self.scene.accretion_alpha * colors[i, j]
colors[i, j] = ti.math.clamp(colors[i, j], 0.0, 1.0)