-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexperiment_lambda.py
180 lines (147 loc) · 5.9 KB
/
experiment_lambda.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
# main python script for running and solving Blackhole ODE
# Written by
# - JaeHyun Lee: [email protected]
# - Joon Suk Huh: [email protected]
# - Suenggwan Jo: [email protected]
# - Hyeong Kyu Choi: [email protected]
import argparse
import numpy as np
import matplotlib.pyplot as plt
from camera import Camera
from solver import Solver
from skymap import Skymap
from scene import Scene
import taichi as ti
def main():
# Create an argument parser
parser = argparse.ArgumentParser(description="Parse rendering parameters.")
# Camera position
parser.add_argument(
"-pov", "-p", nargs=3, metavar=('x', 'y', 'z'),
help="Camera position in cartesian coordinate (default: [0,3,0.2])",
type=float,
default=[0, 3.0, 0.2]
)
# Focal length (float)
parser.add_argument("-focal", "-f", type=float,
default=1.5,
help="Focal length (default: 1.5)")
# Field of View (FoV) (float between 0 and 180)
parser.add_argument(
"-fov",
type=float,
default=90,
help="Field of View (FoV) in degrees (float between 0 and 180) (default: 90)"
)
# Resolution (string: '4k' or 'fhd')
parser.add_argument(
"-resolution", "-r",
type=str,
default='4k',
choices=["4k", "fhd"],
help="Resolution: '4k' or 'fhd' (default: 4k)"
)
# Texture file path (string)
parser.add_argument("-texture", "-t", type=str,
default='texture/high_res/space_texture_high1.jpg',
help="Texture file path (string)")
# Accretion disk texture file path (string)
parser.add_argument("-at", type=str,
default='texture/ad/adisk.jpg',
help="Accretion disk texture file path (string)")
# GPU or CPU flag (use '--gpu' for GPU, default is GPU)
parser.add_argument(
"--cpu",
action="store_true",
help="Use CPU for rendering (default: use GPU)"
)
# Output file name base
parser.add_argument("-output", "-o", type=str,
default='result',
help="Base output file name. (default: result)")
# accretion r1 r2
parser.add_argument("-ar1", type=float,
default=2,
help="inner radius of accretion disk (default: 2)")
# accretion r1 r2
parser.add_argument("-ar2", type=float,
default=6,
help="outer radius of accretion disk (default: 6)")
args = parser.parse_args()
# Initialize Taichi
if args.cpu:
ti.init(arch=ti.cpu)
else:
# On Apple Silicon, use Metal explicitly
ti.init(arch=ti.metal)
# Determine resolution
if args.resolution == '4k':
resol = np.array([3840, 2160])
img_width, img_height = 3840, 2160
else:
resol = np.array([1920, 1080])
img_width, img_height = 1920, 1080
print('Welcome to Math/CS714 Project')
# Setup camera and get rays as numpy arrays
my_camera = Camera(np.array(args.pov, dtype=np.float32),
np.float32(args.focal),
np.array([0, 0, 0], dtype=np.float32),
resol, fov=np.float32(args.fov % 180))
print('Generating rays...')
# Generate and convert rays into numpy arrays
original_positions_taichi, original_directions_taichi = my_camera.get_all_rays()
# Convert Taichi fields to numpy arrays
original_positions = original_positions_taichi.to_numpy()
original_directions = original_directions_taichi.to_numpy()
# Initialize positions and directions Taichi fields
positions = ti.Vector.field(3, dtype=ti.f32, shape=(img_width, img_height))
directions = ti.Vector.field(3, dtype=ti.f32, shape=(img_width, img_height))
colors = ti.Vector.field(3, dtype=ti.f32, shape=(img_width, img_height))
# Use the numpy arrays to reset Taichi fields before each experiment
# Initialize scene
scene = Scene(
blackhole_r=ti.cast(1.0, ti.f32),
accretion_r1=ti.cast(args.ar1, ti.f32),
accretion_r2=ti.cast(args.ar2, ti.f32),
accretion_temp=ti.cast(400., ti.f32),
accretion_alpha=ti.cast(1, ti.f32),
skymap=Skymap(args.texture, r_max=10)
)
scene.set_accretion_disk_texture(args.at)
# Lists of integrators and lambdas to experiment with
# integrators = ["euler", "rk4", "leapfrog", "ab2", "am4"]
# lamb_values = [0.1, 0.05, 0.01, 0.001]
h = 0.001
integrator = "am4"
my_solver = Solver(scene, h=ti.cast(h, ti.f32))
print(f"Running integrator: {integrator}, lambda: {h}")
# Reinitialize solver
# Reset fields before each run
colors.fill(0.0) # start from a clean color field
positions.from_numpy(original_positions) # Reload original ray positions
directions.from_numpy(original_directions) # Reload original ray directions
# Solve ODE
print('Solving ODE...')
if integrator == "euler":
my_solver.solve_forward_euler(positions, directions, colors)
elif integrator == 'rk4':
my_solver.solve_rk4(positions, directions, colors)
elif integrator == 'leapfrog':
my_solver.solve_leapfrog(positions, directions, colors)
elif integrator == 'ab2':
my_solver.solve_ab2(positions, directions, colors)
elif integrator == 'am4':
my_solver.solve_am4(positions, directions, colors)
# Render and save
print('Rendering...')
img = my_camera.render(colors)
output_filename = f"experiment_lambda_size/result_{integrator}_lambda_{h}.png"
plt.figure(figsize=(img_width / 100, img_height / 100), dpi=100)
plt.imshow(np.transpose(img, (1, 0, 2)))
plt.axis('off')
plt.savefig(output_filename, dpi=100, bbox_inches='tight', pad_inches=0)
plt.close()
print(f"Saved: {output_filename}")
# Create an argument parser
if __name__ == '__main__':
main()