Spaces:
Sleeping
Sleeping
File size: 8,847 Bytes
66c9c8a | 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 | from typing import Set
import numpy as np
from warp.fem import DiscreteField
def plot_grid_surface(field, axes=None):
import matplotlib.pyplot as plt
from matplotlib import cm
if axes is None:
fig, axes = plt.subplots(subplot_kw={"projection": "3d"})
node_positions = field.space.node_grid()
# Make data.
X = node_positions[0]
Y = node_positions[1]
Z = field.dof_values.numpy().reshape(X.shape)
# Plot the surface.
return axes.plot_surface(X, Y, Z, cmap=cm.coolwarm, linewidth=0, antialiased=False)
def plot_tri_surface(field, axes=None):
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.tri.triangulation import Triangulation
if axes is None:
fig, axes = plt.subplots(subplot_kw={"projection": "3d"})
node_positions = field.space.node_positions().numpy()
triangulation = Triangulation(
x=node_positions[:, 0], y=node_positions[:, 1], triangles=field.space.node_triangulation()
)
Z = field.dof_values.numpy()
# Plot the surface.
return axes.plot_trisurf(triangulation, Z, cmap=cm.coolwarm, linewidth=0, antialiased=False)
def plot_scatter_surface(field, axes=None):
import matplotlib.pyplot as plt
from matplotlib import cm
if axes is None:
fig, axes = plt.subplots(subplot_kw={"projection": "3d"})
X, Y = field.space.node_positions().numpy().T
# Make data.
Z = field.dof_values.numpy().reshape(X.shape)
# Plot the surface.
return axes.scatter(X, Y, Z, c=Z, cmap=cm.coolwarm)
def plot_surface(field, axes=None):
if hasattr(field.space, "node_grid"):
return plot_grid_surface(field, axes)
elif hasattr(field.space, "node_triangulation"):
return plot_tri_surface(field, axes)
else:
return plot_scatter_surface(field, axes)
def plot_grid_color(field, axes=None):
import matplotlib.pyplot as plt
from matplotlib import cm
if axes is None:
fig, axes = plt.subplots()
node_positions = field.space.node_grid()
# Make data.
X = node_positions[0]
Y = node_positions[1]
Z = field.dof_values.numpy().reshape(X.shape)
# Plot the surface.
return axes.pcolormesh(X, Y, Z, cmap=cm.coolwarm)
def plot_velocities(field, axes=None):
import matplotlib.pyplot as plt
if axes is None:
fig, axes = plt.subplots()
node_positions = field.space.node_positions().numpy()
# Make data.
X = node_positions[:, 0]
Y = node_positions[:, 1]
vel = field.dof_values.numpy()
u = np.ascontiguousarray(vel[:, 0])
v = np.ascontiguousarray(vel[:, 1])
u = u.reshape(X.shape)
v = v.reshape(X.shape)
return axes.quiver(X, Y, u, v)
def plot_grid_streamlines(field, axes=None):
import matplotlib.pyplot as plt
if axes is None:
fig, axes = plt.subplots()
node_positions = field.space.node_grid()
# Make data.
X = node_positions[0][:, 0]
Y = node_positions[1][0, :]
vel = field.dof_values.numpy()
u = np.ascontiguousarray(vel[:, 0])
v = np.ascontiguousarray(vel[:, 1])
u = np.transpose(u.reshape(node_positions[0].shape))
v = np.transpose(v.reshape(node_positions[0].shape))
splot = axes.streamplot(X, Y, u, v, density=2)
splot.axes = axes
return splot
def plot_3d_scatter(field, axes=None):
import matplotlib.pyplot as plt
from matplotlib import cm
if axes is None:
fig, axes = plt.subplots(subplot_kw={"projection": "3d"})
X, Y, Z = field.space.node_positions().numpy().T
# Make data.
f = field.dof_values.numpy().reshape(X.shape)
# Plot the surface.
return axes.scatter(X, Y, Z, c=f, cmap=cm.coolwarm)
def plot_3d_velocities(field, axes=None):
import matplotlib.pyplot as plt
if axes is None:
fig, axes = plt.subplots(subplot_kw={"projection": "3d"})
X, Y, Z = field.space.node_positions().numpy().T
vel = field.dof_values.numpy()
u = np.ascontiguousarray(vel[:, 0])
v = np.ascontiguousarray(vel[:, 1])
w = np.ascontiguousarray(vel[:, 2])
u = u.reshape(X.shape)
v = v.reshape(X.shape)
w = w.reshape(X.shape)
return axes.quiver(X, Y, Z, u, v, w, length=1.0 / X.shape[0], normalize=False)
class Plot:
def __init__(self, stage=None, default_point_radius=0.01):
self.default_point_radius = default_point_radius
self._surfaces = {}
self._surface_vectors = {}
self._volumes = {}
if stage is not None:
from warp.render import UsdRenderer
self._usd_renderer = UsdRenderer(stage)
self._plt = None
else:
self._usd_renderer = None
def begin_frame(self, time):
if self._usd_renderer is not None:
self._usd_renderer.begin_frame(time=time)
def end_frame(self):
if self._usd_renderer is not None:
self._usd_renderer.end_frame()
def add_surface(self, name: str, field: DiscreteField):
if self._usd_renderer is not None:
points_2d = field.space.node_positions().numpy()
values = field.dof_values.numpy()
points_3d = np.hstack((points_2d, values.reshape(-1, 1)))
if hasattr(field.space, "node_triangulation"):
indices = field.space.node_triangulation()
self._usd_renderer.render_mesh(name, points=points_3d, indices=indices)
else:
self._usd_renderer.render_points(name, points=points_3d, radius=self.default_point_radius)
if name not in self._surfaces:
field_clone = field.space.make_field(space_partition=field.space_partition)
self._surfaces[name] = (field_clone, [])
self._surfaces[name][1].append(field.dof_values.numpy())
def add_surface_vector(self, name: str, field: DiscreteField):
if self._usd_renderer is not None:
points_2d = field.space.node_positions().numpy()
values = field.dof_values.numpy()
points_3d = np.hstack((points_2d + values, np.zeros_like(points_2d[:, 0]).reshape(-1, 1)))
if hasattr(field.space, "node_triangulation"):
indices = field.space.node_triangulation()
self._usd_renderer.render_mesh(name, points=points_3d, indices=indices)
else:
self._usd_renderer.render_points(name, points=points_3d, radius=self.default_point_radius)
if name not in self._surface_vectors:
field_clone = field.space.make_field(space_partition=field.space_partition)
self._surface_vectors[name] = (field_clone, [])
self._surface_vectors[name][1].append(field.dof_values.numpy())
def add_volume(self, name: str, field: DiscreteField):
if self._usd_renderer is not None:
points_3d = field.space.node_positions().numpy()
values = field.dof_values.numpy()
self._usd_renderer.render_points(name, points_3d, radius=values)
if name not in self._volumes:
field_clone = field.space.make_field(space_partition=field.space_partition)
self._volumes[name] = (field_clone, [])
self._volumes[name][1].append(field.dof_values.numpy())
def plot(self, streamlines: Set[str] = []):
return self._plot_matplotlib(streamlines)
def _plot_matplotlib(self, streamlines: Set[str] = []):
import matplotlib.pyplot as plt
import matplotlib.animation as animation
def make_animation(ax, field, values, plot_func, num_frames: int):
def animate(i):
ax.clear()
field.dof_values = values[i]
return plot_func(field, axes=ax)
return animation.FuncAnimation(
ax.figure,
animate,
interval=30,
blit=False,
frames=len(values),
)
for name, (field, values) in self._surfaces.items():
field.dof_values = values[0]
ax = plot_surface(field).axes
if len(values) > 1:
anim = make_animation(ax, field, values, plot_func=plot_surface, num_frames=len(values))
for name, (field, values) in self._surface_vectors.items():
field.dof_values = values[0]
if name in streamlines and hasattr(field.space, "node_grid"):
ax = plot_grid_streamlines(field).axes
else:
ax = plot_velocities(field).axes
if len(values) > 1:
anim = make_animation(ax, field, values, plot_func=plot_velocities, num_frames=len(values))
for name, (field, values) in self._volumes.items():
field.dof_values = values[0]
ax = plot_3d_scatter(field).axes
plt.show()
|