Chris Parmer — home

Quiver plot of 2D potential flow around a circular cylinder

Fluid Dynamics

Example from the compendium of canonical charts

Fluid Dynamics — Quiver plot of 2D potential flow around a circular cylinder

Python Code

"""Fluid Dynamics — Quiver plot of 2D potential flow around a circular cylinder."""


import numpy as np
import plotly.figure_factory as ff
import plotly.graph_objects as go


# Inviscid, irrotational (potential) flow around a unit-radius cylinder.
# Uniform freestream U∞=1, cylinder radius a=1 at origin.
# Velocity field (outside cylinder):
#   u = U∞ [ 1 - a²(x²-y²)/(x²+y²)² ]
#   v = U∞ [ -2a²·x·y / (x²+y²)² ]
U_INF = 1.0
A = 1.0


def velocity(x, y):
    r2 = x**2 + y**2
    # Mask interior of cylinder
    outside = r2 > (A + 0.01)**2
    r2 = np.where(outside, r2, np.nan)
    u = U_INF * (1 - A**2 * (x**2 - y**2) / r2**2)
    v = U_INF * (-2 * A**2 * x * y / r2**2)
    return np.where(outside, u, 0.0), np.where(outside, v, 0.0)


def generate():
    # Coarser grid for quiver (arrows would overlap on a dense grid)
    x = np.linspace(-3.5, 3.5, 18)
    y = np.linspace(-3.5, 3.5, 18)
    X, Y = np.meshgrid(x, y)

    U, V = velocity(X, Y)

    # Mask points inside (or very near) cylinder for display
    r2 = X**2 + Y**2
    mask = r2 > (A + 0.2)**2
    X_q = np.where(mask, X, np.nan).ravel()
    Y_q = np.where(mask, Y, np.nan).ravel()
    U_q = np.where(mask, U, np.nan).ravel()
    V_q = np.where(mask, V, np.nan).ravel()

    # Drop nan rows
    keep = ~np.isnan(X_q)
    X_q, Y_q, U_q, V_q = X_q[keep], Y_q[keep], U_q[keep], V_q[keep]

    # Reshape back to 2D for ff.create_quiver (requires 2D arrays)
    # Build 2D arrays by filtering rows/cols that are fully masked
    xi = np.linspace(-3.5, 3.5, 18)
    yi = np.linspace(-3.5, 3.5, 18)
    Xi, Yi = np.meshgrid(xi, yi)
    Ui, Vi = velocity(Xi, Yi)
    ri2 = Xi**2 + Yi**2
    inside = ri2 <= (A + 0.15)**2
    Ui[inside] = np.nan
    Vi[inside] = np.nan

    # Normalize arrow lengths to show direction uniformly, scaled by magnitude
    mag = np.sqrt(Ui**2 + Vi**2)
    max_mag = np.nanmax(mag)
    Ui_n = Ui / max_mag
    Vi_n = Vi / max_mag

    fig = ff.create_quiver(
        Xi, Yi, Ui_n, Vi_n,
        scale=0.22,
        arrow_scale=0.35,
    )
    for trace in fig.data:
        trace.line.color = VIOLET
        trace.opacity = 0.75

    # Cylinder outline
    theta = np.linspace(0, 2 * np.pi, 200)
    fig.add_trace(go.Scatter(
        x=np.cos(theta), y=np.sin(theta),
        mode="lines",
        line=dict(color=TEAL, width=2),
        fill="toself",
        fillcolor="rgba(82,179,208,0.15)",
        showlegend=False,
        hoverinfo="skip",
    ))

    # Stagnation-point markers
    fig.add_trace(go.Scatter(
        x=[-1, 1], y=[0, 0],
        mode="markers",
        marker=dict(size=8, color="#E9A23B", symbol="circle",
                    line=dict(color="white", width=1.5)),
        name="Stagnation points",
        hovertemplate="Stagnation point<br>(%{x:.0f}, %{y:.0f})<extra></extra>",
    ))

    fig.update_layout(
        xaxis=dict(
            title="x / a",
            range=[-3.8, 3.8],
            scaleanchor="y",
            showgrid=True,
            zeroline=True,
        ),
        yaxis=dict(
            title="y / a",
            range=[-3.8, 3.8],
        ),
        legend=dict(orientation="h", y=1.06),
        margin=dict(t=20, b=60, l=70, r=40),
        height=580,
        showlegend=True,
    )
    return fig


if __name__ == "__main__":
    generate()

Made with Plotly