-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimizing_particle.py
305 lines (239 loc) · 8.21 KB
/
optimizing_particle.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
import logging
import sys
import taichi as ti
from taichi import _logging as ti_logging
from utils import Printer, parse_common_sim_args, plot_losses
ti.init(arch=ti.cpu, debug=True, flatten_if=True, verbose=False)
logging.getLogger().setLevel(logging.INFO)
printer = Printer()
args = parse_common_sim_args()
if not args.info:
ti_logging.set_logging_level(ti.WARN)
if not args.trace:
sys.tracebacklimit = 0
# Environment setup
dt = 4e-3
simulation_steps = args.sim_steps
gravity = ti.Vector([0, -9.8, 0])
drag_damping = 1
elasticity_coef = 0.6
friction_coef = 0.22
# Plane
plane_boundary = -1
plane_origin = ti.Vector([0.5, plane_boundary, 0.5])
plane_p1 = plane_origin - [0.1, plane_boundary, 0.1]
plane_p2 = plane_origin - [0.6, plane_boundary, 0.3]
# so that the normal points toward the ball
plane_normal = -plane_p1.cross(plane_p2)
plane_normal = plane_normal.normalized()
# Ball setup
n_balls = 2
ball_radius = 0.3
ball_center = ti.Vector.field(3, dtype=float, shape=(1,))
helper_ball_center = ti.Vector.field(3, dtype=float, shape=(simulation_steps))
contact_ball_center = ti.Vector.field(3, dtype=float, shape=(1,))
target_ball_center = ti.Vector.field(3, dtype=float, shape=(1,))
target_ball_idx = 0
contact_ball_idx = 1
# Fields setup
x = ti.Vector.field(3, dtype=float)
v = ti.Vector.field(3, dtype=float)
init_x = ti.Vector.field(3, dtype=float)
init_v = ti.Vector.field(3, dtype=float)
impulse = ti.Vector.field(3, dtype=float)
# Optimization setup
optimization_steps = args.opt_steps
loss = ti.field(dtype=ti.float32)
# total number of contacts
num_contacts = ti.field(dtype=ti.float32)
# count iterations with sequential contact
n_sequential_contacts = ti.field(dtype=ti.float32)
speed_loss = ti.field(dtype=ti.float32)
lr = 0.5
# Fields finalization
ti.root.dense(ti.i, simulation_steps).dense(ti.j, n_balls).place(x, v, impulse)
ti.root.place(init_x, init_v)
ti.root.place(loss, num_contacts, n_sequential_contacts, speed_loss)
ti.root.lazy_grad()
# Init params setup
# collision with ground
init_x[None] = [0.75, -0.3, 0.0]
init_v[None] = [-1.5, -0.5, -1.5]
@ti.kernel
def init_points():
x[0, contact_ball_idx] = [0.7, 0.7, -1.0]
target_ball_center[0] = [-0.5, -0.5, 0.5]
x[0, target_ball_idx] = init_x[None]
v[0, target_ball_idx] = init_v[None]
num_contacts[None] = 0
n_sequential_contacts[None] = 0
def forward(do_visualize=False):
init_points()
final_step = simulation_steps - 1
for t in range(1, simulation_steps):
substep(t)
advance(t)
if not args.exclude_collisions:
handle_collisions(t, target_ball_idx)
if do_visualize:
visualize(t)
if num_contacts[None] > 5.0:
final_step = t
break
if args.do_optim:
compute_speed_loss(final_step)
compute_loss(final_step)
return final_step
@ti.kernel
def substep(t: ti.int32):
imp = ti.Vector([0.0, 0.0, 0.0])
force = compute_force()
imp += force * dt
imp *= ti.exp(-drag_damping * dt)
impulse[t, target_ball_idx] += imp
@ti.func
def compute_force():
force = ti.Vector([0.0, 0.0, 0.0])
force += gravity
return force
@ti.kernel
def advance(t: ti.i32):
for i in range(n_balls):
v[t, i] = v[t - 1, i] + impulse[t, i]
x[t, i] = x[t - 1, i] + dt * v[t, i]
@ti.kernel
def handle_collisions(t: ti.int32, i: ti.int32):
detect_and_handle_plane_collision(t)
@ti.func
def detect_and_handle_plane_collision(t):
next_tangent_v = ti.Vector([0.0, 0.0, 0.0])
next_normal_v = ti.Vector([0.0, 0.0, 0.0])
point_on_the_plane = plane_origin
normal_proj = (x[t, target_ball_idx] - point_on_the_plane).dot(plane_normal)
plane_dist = ti.abs(normal_proj)
result = 0
eps = 1e-8
if normal_proj > 0:
if plane_dist - ball_radius <= eps:
result = 1
normal_v = (v[t, target_ball_idx].dot(plane_normal)) * plane_normal
tangent_v = v[t, target_ball_idx] - normal_v + eps
next_normal_v = -elasticity_coef * normal_v
next_tangent_v = (
tangent_v
- min(friction_coef * normal_v.norm(), tangent_v.norm())
* tangent_v.normalized()
)
# simpler, less accurate alternative
# next_tangent_v2 = (1 - friction_coef) * tangent_v
v[t, target_ball_idx] = next_normal_v + next_tangent_v
num_contacts[None] += 1
n_sequential_contacts[None] += 1
else:
n_sequential_contacts[None] = 0
elif normal_proj == 0:
result = 1
else:
if -plane_dist + ball_radius >= eps:
result = 1
assert 0, "The plane is non-penetrating"
return result
@ti.kernel
def compute_speed_loss(final_step: ti.i32):
for i in range(final_step):
if (v[i, target_ball_idx].norm()) > speed_loss[None]:
speed_loss[None] = v[i, target_ball_idx].norm()
@ti.kernel
def compute_loss(t: ti.i32):
contact_loss_scaler = (
0.25
if 3 > (num_contacts[None]) > 1
else max(num_contacts[None] ** (2 / 1), 1.25)
)
dist_loss = (
x[t, target_ball_idx] - target_ball_center[0]
).norm() ** 2 * contact_loss_scaler
loss[None] = 0.9 * contact_loss_scaler * dist_loss + 0.1 * speed_loss[None]
def main():
losses = []
grads = []
for i in range(optimization_steps):
clear()
with ti.ad.Tape(loss):
is_final_iter = i == (optimization_steps - 1)
final_step = forward(
do_visualize=(args.do_visualize and not args.do_optim)
or (is_final_iter and args.do_visualize)
)
losses.append(loss[None])
grads.append(init_v.grad[None].norm())
if args.do_optim:
printer.print_iter_stats(
i, loss=loss[None], pos=x[final_step, target_ball_idx]
)
update_inits()
draw_trajectory(final_step)
if args.do_optim:
printer.print_final_optim_stats(
pos=x[final_step, target_ball_idx], target_pos=target_ball_center[0]
)
if args.do_plot:
plot_losses(losses, ylabel="Loss", fig_title="Rigid ball. Loss")
plot_losses(
grads,
ylabel="V.grad.norm()",
fig_title="Rigid ball. Velocity gradient (unclipped) norm",
)
def draw_trajectory(final_step):
helper_ball_center.fill([-1, -2, -1])
for t in range(0, final_step, 5):
helper_ball_center[t] = x[t, target_ball_idx]
scene.particles(
helper_ball_center, radius=ball_radius * 0.15, color=(0.25, 0.62, 0.18)
)
aux_update_scene()
def update_inits():
cum_v_grad = 0.0
for i in range(3):
# init_x[None][i] -= lr * init_x.grad[None][i]
init_v.grad[None][i] = ti.min(ti.max(init_v.grad[None][i], -1), 1)
init_v[None][i] -= lr * init_v.grad[None][i]
cum_v_grad += init_v.grad[None][i]
assert abs(init_v.grad[None][i]) < 100, "Exploding init_v.grad"
assert abs(cum_v_grad) > 0, "init_v.grad is zero"
printer.print_grad_stats(init_x=init_x, init_v=init_v)
@ti.kernel
def clear():
for t, i in ti.ndrange(simulation_steps, n_balls):
impulse[t, i] = ti.Vector([0.0, 0.0, 0.0])
window: ti.ui.Window = None
canvas: ti.ui.Canvas = None
scene: ti.ui.Scene = None
camera: ti.ui.Camera = None
def visualize(t):
global window, canvas, scene, camera
if window is None:
window = ti.ui.Window("Rigid ball", (600, 600), vsync=True)
canvas = window.get_canvas()
canvas.set_background_color((1, 1, 1))
scene = ti.ui.Scene()
camera = ti.ui.make_camera()
aux_update_scene()
render_ball(t)
def aux_update_scene():
camera.position(0.0, 0.0, 3)
camera.lookat(0.0, 0.0, 0)
scene.set_camera(camera)
scene.point_light(pos=(0, 1, 2), color=(1, 1, 1))
scene.ambient_light((0.5, 0.5, 0.5))
canvas.scene(scene)
window.show()
def render_ball(t):
ball_center[0] = x[t, target_ball_idx]
contact_ball_center[0] = x[t, contact_ball_idx]
scene.particles(ball_center, radius=ball_radius * 0.95, color=(0.5, 0.42, 0.8))
scene.particles(
target_ball_center, radius=ball_radius * 0.25, color=(0.05, 0.12, 0.18)
)
if __name__ == "__main__":
main()