Skip to content

Commit 211f0e3

Browse files
authored
Don't have streamplot error on a zero field (#4188)
* Don't have streamplot error on a zero field * Use approximately zero.
1 parent 5f4a387 commit 211f0e3

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

firedrake/pyplot/mpl.py

+17-5
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,12 @@ def streamline(function, point, direction=+1, tolerance=3e-3, loc_tolerance=1e-1
392392
x = np.array(point)
393393
v1 = toreal(direction * function.at(x, tolerance=loc_tolerance), complex_component)
394394
r = toreal(cell_sizes.at(x, tolerance=loc_tolerance), "real")
395-
dt = 0.5 * r / np.sqrt(np.sum(v1**2))
395+
v1norm = np.sqrt(np.sum(v1**2))
396+
if np.isclose(v1norm, 0.0):
397+
# Bail early for zero fields.
398+
return
399+
400+
dt = 0.5 * r / v1norm
396401

397402
while True:
398403
try:
@@ -622,7 +627,10 @@ def streamplot(function, resolution=None, min_length=None, max_time=None,
622627
if max_time is None:
623628
area = assemble(Constant(1) * dx(mesh))
624629
average_speed = np.sqrt(assemble(inner(function, function) * dx) / area)
625-
max_time = 50 * min_length / average_speed
630+
if np.isclose(average_speed, 0.0):
631+
max_time = 50 * min_length / average_speed
632+
else:
633+
max_time = 0.
626634

627635
streamplotter = Streamplotter(function, resolution, min_length, max_time,
628636
tolerance, loc_tolerance,
@@ -666,9 +674,13 @@ def streamplot(function, resolution=None, min_length=None, max_time=None,
666674
widths = np.array(widths)
667675

668676
points = np.asarray(points)
669-
vmin = kwargs.pop("vmin", speeds.min())
670-
vmax = kwargs.pop("vmax", speeds.max())
671-
norm = kwargs.pop("norm", matplotlib.colors.Normalize(vmin=vmin, vmax=vmax))
677+
if speeds.size > 0:
678+
vmin = kwargs.pop("vmin", speeds.min())
679+
vmax = kwargs.pop("vmax", speeds.max())
680+
norm = kwargs.pop("norm", matplotlib.colors.Normalize(vmin=vmin, vmax=vmax))
681+
else:
682+
norm = None
683+
672684
cmap = plt.get_cmap(kwargs.pop("cmap", None))
673685

674686
collection = LineCollection(points, cmap=cmap, norm=norm, linewidth=widths)

tests/firedrake/output/test_plotting.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -191,18 +191,20 @@ def test_streamplot():
191191
saddle = assemble(interpolate(2 * as_vector((v[0], -v[1])), V))
192192
r = Constant(.5)
193193
sink = assemble(interpolate(center - r * v, V))
194+
zero = Function(V)
194195

195-
fig, axes = plt.subplots(ncols=1, nrows=3, sharex=True, sharey=True)
196+
fig, axes = plt.subplots(ncols=1, nrows=4, sharex=True, sharey=True)
196197
for ax in axes:
197198
ax.set_aspect("equal")
198199

199200
color_norm = matplotlib.colors.PowerNorm(gamma=0.5)
200201
kwargses = [
201202
{'resolution': 1/48, 'tolerance': 2e-2, 'norm': color_norm, 'seed': 0},
202203
{'loc_tolerance': 1e-5, 'cmap': 'bone', 'vmax': 1., 'seed': 0},
203-
{'min_length': 1/4, 'max_time': 5., 'seed': 0}
204+
{'min_length': 1/4, 'max_time': 5., 'seed': 0},
205+
{}
204206
]
205-
for ax, function, kwargs in zip(axes, [center, saddle, sink], kwargses):
207+
for ax, function, kwargs in zip(axes, [center, saddle, sink, zero], kwargses):
206208
lines = streamplot(function, axes=ax, **kwargs)
207209
colorbar = fig.colorbar(lines, ax=ax)
208210
assert lines is not None

0 commit comments

Comments
 (0)