-
Notifications
You must be signed in to change notification settings - Fork 493
Expand file tree
/
Copy pathexample_wave.py
More file actions
257 lines (198 loc) · 7.66 KB
/
example_wave.py
File metadata and controls
257 lines (198 loc) · 7.66 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
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
# SPDX-FileCopyrightText: Copyright (c) 2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
###########################################################################
# Example Wave
#
# Shows how to implement a simple 2D wave-equation solver with collision
# against a moving sphere.
#
###########################################################################
import math
import warp as wp
import warp.render
@wp.func
def sample(f: wp.array[float], x: int, y: int, width: int, height: int):
# clamp texture coords
x = wp.clamp(x, 0, width - 1)
y = wp.clamp(y, 0, height - 1)
s = f[y * width + x]
return s
@wp.func
def laplacian(f: wp.array[float], x: int, y: int, width: int, height: int):
ddx = sample(f, x + 1, y, width, height) - 2.0 * sample(f, x, y, width, height) + sample(f, x - 1, y, width, height)
ddy = sample(f, x, y + 1, width, height) - 2.0 * sample(f, x, y, width, height) + sample(f, x, y - 1, width, height)
return ddx + ddy
@wp.kernel
def wave_displace(
hcurrent: wp.array[float],
hprevious: wp.array[float],
width: int,
height: int,
center_x: float,
center_y: float,
r: float,
mag: float,
t: float,
):
tid = wp.tid()
x = tid % width
y = tid // width
dx = float(x) - center_x
dy = float(y) - center_y
dist_sq = float(dx * dx + dy * dy)
if dist_sq < r * r:
h = mag * wp.sin(t)
hcurrent[tid] = h
hprevious[tid] = h
@wp.kernel
def wave_solve(
hprevious: wp.array[float],
hcurrent: wp.array[float],
width: int,
height: int,
inv_cell: float,
k_speed: float,
k_damp: float,
dt: float,
):
tid = wp.tid()
x = tid % width
y = tid // width
l = laplacian(hcurrent, x, y, width, height) * inv_cell * inv_cell
# integrate
h1 = hcurrent[tid]
h0 = hprevious[tid]
h = 2.0 * h1 - h0 + dt * dt * (k_speed * l - k_damp * (h1 - h0))
# buffers get swapped each iteration
hprevious[tid] = h
# simple kernel to apply height deltas to a vertex array
@wp.kernel
def grid_update(heights: wp.array[float], vertices: wp.array[wp.vec3]):
tid = wp.tid()
h = heights[tid]
v = vertices[tid]
v_new = wp.vec3(v[0], h, v[2])
vertices[tid] = v_new
class Example:
def __init__(self, stage_path="example_wave.usd", verbose=False):
self.sim_width = 128
self.sim_height = 128
fps = 60
self.sim_substeps = 16
self.sim_dt = (1.0 / fps) / self.sim_substeps
self.sim_time = 0.0
# wave constants
self.k_speed = 1.0
self.k_damp = 0.0
# grid constants
self.grid_size = 0.1
self.grid_displace = 0.5
self.verbose = verbose
vertices = []
self.indices = []
def grid_index(x, y, stride):
return y * stride + x
for z in range(self.sim_height):
for x in range(self.sim_width):
pos = (
float(x) * self.grid_size,
0.0,
float(z) * self.grid_size,
)
# directly modifies verts_host memory since this is a numpy alias of the same buffer
vertices.append(pos)
if x > 0 and z > 0:
self.indices.append(grid_index(x - 1, z - 1, self.sim_width))
self.indices.append(grid_index(x, z, self.sim_width))
self.indices.append(grid_index(x, z - 1, self.sim_width))
self.indices.append(grid_index(x - 1, z - 1, self.sim_width))
self.indices.append(grid_index(x - 1, z, self.sim_width))
self.indices.append(grid_index(x, z, self.sim_width))
# simulation grids
self.sim_grid0 = wp.zeros(self.sim_width * self.sim_height, dtype=float)
self.sim_grid1 = wp.zeros(self.sim_width * self.sim_height, dtype=float)
self.sim_verts = wp.array(vertices, dtype=wp.vec3)
# create surface displacement around a point
self.cx = self.sim_width / 2 + math.sin(self.sim_time) * self.sim_width / 3
self.cy = self.sim_height / 2 + math.cos(self.sim_time) * self.sim_height / 3
if stage_path:
self.renderer = wp.render.UsdRenderer(stage_path)
else:
self.renderer = None
def step(self):
with wp.ScopedTimer("step"):
for _s in range(self.sim_substeps):
# create surface displacement around a point
self.cx = self.sim_width / 2 + math.sin(self.sim_time) * self.sim_width / 3
self.cy = self.sim_height / 2 + math.cos(self.sim_time) * self.sim_height / 3
wp.launch(
kernel=wave_displace,
dim=self.sim_width * self.sim_height,
inputs=[
self.sim_grid0,
self.sim_grid1,
self.sim_width,
self.sim_height,
self.cx,
self.cy,
10.0,
self.grid_displace,
-math.pi * 0.5,
],
)
# integrate wave equation
wp.launch(
kernel=wave_solve,
dim=self.sim_width * self.sim_height,
inputs=[
self.sim_grid0,
self.sim_grid1,
self.sim_width,
self.sim_height,
1.0 / self.grid_size,
self.k_speed,
self.k_damp,
self.sim_dt,
],
)
# swap grids
(self.sim_grid0, self.sim_grid1) = (self.sim_grid1, self.sim_grid0)
self.sim_time += self.sim_dt
with wp.ScopedTimer("mesh", self.verbose):
# update grid vertices from heights
wp.launch(kernel=grid_update, dim=self.sim_width * self.sim_height, inputs=[self.sim_grid0, self.sim_verts])
def render(self):
if self.renderer is None:
return
with wp.ScopedTimer("render"):
vertices = self.sim_verts.numpy()
self.renderer.begin_frame(self.sim_time)
self.renderer.render_mesh("surface", vertices, self.indices, colors=(0.35, 0.55, 0.9))
self.renderer.render_sphere(
"sphere",
(self.cx * self.grid_size, 0.0, self.cy * self.grid_size),
(0.0, 0.0, 0.0, 1.0),
10.0 * self.grid_size,
color=(1.0, 1.0, 1.0),
)
self.renderer.end_frame()
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("--device", type=str, default=None, help="Override the default Warp device.")
parser.add_argument(
"--stage-path",
type=lambda x: None if x == "None" else str(x),
default="example_wave.usd",
help="Path to the output USD file.",
)
parser.add_argument("--num-frames", type=int, default=300, help="Total number of frames.")
parser.add_argument("--verbose", action="store_true", help="Print out additional status messages during execution.")
args = parser.parse_known_args()[0]
with wp.ScopedDevice(args.device):
example = Example(stage_path=args.stage_path, verbose=args.verbose)
for _ in range(args.num_frames):
example.step()
example.render()
if example.renderer:
example.renderer.save()