matplotlib can also make streamline plots, which are sometimes called field line plots. The matplotlib function call to make such plots is streamplot, and its use is illustrated in the following program to plot the streamlines of the velocity field of a viscous liquid around a sphere falling through it at constant velocity u.:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Circle
def v(u, a, x, z):
"""Return the velocity vector field v = (vx, vy)
around sphere at r = 0."""
r = np.sqrt(x * x + z * z)
R = a / r
RR = R * R
cs, sn = z / r, x / r
vr = u * cs * (1.0 - 0.5 * R * (3.0 - RR))
vtheta = -u * sn * (1.0 - 0.25 * R * (3.0 + RR))
vx = vr * sn + vtheta * cs
vz = vr * cs - vtheta * sn
return vx, vz
# Grid of x, y points
xlim, zlim = 12, 12
nx, nz = 100, 100
x = np.linspace(-xlim, xlim, nx)
z = np.linspace(-zlim, zlim, nz)
X, Z = np.meshgrid(x, z)
# Set particle radius and velocity
a, u = 1.0, 1.0
# Velocity field vector, V=(Vx, Vz) as separate components
Vx, Vz = v(u, a, X, Z)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(9, 4.5))
# Plot the streamlines using colormap and arrow style
color = np.log(np.sqrt(Vx * Vx + Vz * Vz))
seedx = np.linspace(-xlim, xlim, 18) # Seed streamlines evenly
seedz = -zlim * np.ones(len(seedx)) # far from particle
seed = np.array([seedx, seedz])
ax1.streamplot(x, z, Vx, Vz, color=color, linewidth=1,
cmap='afmhot', density=5, arrowstyle='-|>',
arrowsize=1.0, minlength=0.4, start_points=seed.T)
ax2.streamplot(x, z, Vx, Vz - u, color=color, linewidth=1,
cmap='afmhot', density=5, arrowstyle='-|>',
arrowsize=1.0, minlength=0.4, start_points=seed.T)
for ax in (ax1, ax2):
# Add filled circle for sphere
ax.add_patch(Circle((0, 0), a, color='C0', zorder=2))
ax.set_xlabel('$x$')
ax.set_ylabel('$z$')
ax.set_aspect('equal')
ax.set_xlim(-0.7 * xlim, 0.7 * xlim)
ax.set_ylim(-0.7 * zlim, 0.7 * zlim)
fig.tight_layout()
fig.savefig('./figures/stokesFlowStream.pdf')
plt.show()
The resulting plot is shown below:
The left plot is in the reference frame of the falling sphere and the right plot is in the laboratory frame
where the liquid very far from the sphere is at rest.
The program starts by defining a function that calculates the velocity field as a function of the lateral distance x and the vertical distance z. The function is a solution to the Stokes equation, which describes flow in viscous liquids at very low (zero) Reynolds number. The velocity field serves as the primary input into the matplotlib streamplot function.
The next step is to use NumPy’s meshgrid program to define the 2D grid of points at which the velocity field will be calculated, just as we did for the contour plots. After setting up the meshgrid arrays X and Z, we call the function we defined v(u, a, X, Z) to calculate the velocity field (line 31).
The streamplot functions are set up in lines 36–39 and called in lines 40–47. Note that for the streamplot function the input x-z coordinate arrays are 1D arrays but the velocity arrays Vx-Vz are 2D arrays.
The arrays seedx and seedx set up the starting points (seeds) for the streamlines. You can leave them out and streamplot will make its own choices based on the values you set for the density and minlength keywords. Here we have chosen them, along with the seed settings, so that all the streamlines are continuous across the plot. The other keywords set the properties for the arrow size and style, the width of the streamlines, and the coloring of the streamlines, in this case according to the speed at a given point.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Circle
def v(u, a, x, z):
"""Return the velocity vector field v = (vx, vy)
around sphere at r = 0."""
r = np.sqrt(x * x + z * z)
R = a / r
RR = R * R
cs, sn = z / r, x / r
vr = u * cs * (1.0 - 0.5 * R * (3.0 - RR))
vtheta = -u * sn * (1.0 - 0.25 * R * (3.0 + RR))
vx = vr * sn + vtheta * cs
vz = vr * cs - vtheta * sn
return vx, vz
# Grid of x, y points
xlim, zlim = 12, 12
nx, nz = 100, 100
x = np.linspace(-xlim, xlim, nx)
z = np.linspace(-zlim, zlim, nz)
X, Z = np.meshgrid(x, z)
# Set particle radius and velocity
a, u = 1.0, 1.0
# Velocity field vector, V=(Vx, Vz) as separate components
Vx, Vz = v(u, a, X, Z)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(9, 4.5))
# Plot the streamlines using colormap and arrow style
color = np.log(np.sqrt(Vx * Vx + Vz * Vz))
seedx = np.linspace(-xlim, xlim, 18) # Seed streamlines evenly
seedz = -zlim * np.ones(len(seedx)) # far from particle
seed = np.array([seedx, seedz])
ax1.streamplot(x, z, Vx, Vz, color=color, linewidth=1,
cmap='afmhot', density=5, arrowstyle='-|>',
arrowsize=1.0, minlength=0.4, start_points=seed.T)
ax2.streamplot(x, z, Vx, Vz - u, color=color, linewidth=1,
cmap='afmhot', density=5, arrowstyle='-|>',
arrowsize=1.0, minlength=0.4, start_points=seed.T)
for ax in (ax1, ax2):
# Add filled circle for sphere
ax.add_patch(Circle((0, 0), a, color='C0', zorder=2))
ax.set_xlabel('$x$')
ax.set_ylabel('$z$')
ax.set_aspect('equal')
ax.set_xlim(-0.7 * xlim, 0.7 * xlim)
ax.set_ylim(-0.7 * zlim, 0.7 * zlim)
fig.tight_layout()
fig.savefig('./figures/stokesFlowStream.pdf')
plt.show()
The resulting plot is shown below:
The left plot is in the reference frame of the falling sphere and the right plot is in the laboratory frame
where the liquid very far from the sphere is at rest.
The program starts by defining a function that calculates the velocity field as a function of the lateral distance x and the vertical distance z. The function is a solution to the Stokes equation, which describes flow in viscous liquids at very low (zero) Reynolds number. The velocity field serves as the primary input into the matplotlib streamplot function.
The next step is to use NumPy’s meshgrid program to define the 2D grid of points at which the velocity field will be calculated, just as we did for the contour plots. After setting up the meshgrid arrays X and Z, we call the function we defined v(u, a, X, Z) to calculate the velocity field (line 31).
The streamplot functions are set up in lines 36–39 and called in lines 40–47. Note that for the streamplot function the input x-z coordinate arrays are 1D arrays but the velocity arrays Vx-Vz are 2D arrays.
The arrays seedx and seedx set up the starting points (seeds) for the streamlines. You can leave them out and streamplot will make its own choices based on the values you set for the density and minlength keywords. Here we have chosen them, along with the seed settings, so that all the streamlines are continuous across the plot. The other keywords set the properties for the arrow size and style, the width of the streamlines, and the coloring of the streamlines, in this case according to the speed at a given point.
0 comments:
Post a Comment