Chris Parmer — home

Streamlines for 2D potential flow around a circular cylinder

Fluid Dynamics

Example from the compendium of canonical charts

Fluid Dynamics — Streamlines for 2D potential flow around a circular cylinder

Python Code

"""Fluid Dynamics — Streamlines for 2D potential flow around a circular cylinder."""


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


U_INF = 1.0
A = 1.0


def generate():
    x = np.linspace(-3.5, 3.5, 60)
    y = np.linspace(-3.5, 3.5, 60)
    X, Y = np.meshgrid(x, y)

    r2 = X**2 + Y**2

    # Outside cylinder: potential flow; inside: uniform freestream (cylinder drawn on top)
    outside = r2 > A**2
    U = np.where(outside, U_INF * (1 - A**2 * (X**2 - Y**2) / np.where(r2 > 0, r2**2, 1.0)), U_INF)
    V = np.where(outside, U_INF * (-2 * A**2 * X * Y / np.where(r2 > 0, r2**2, 1.0)), 0.0)

    fig = ff.create_streamline(
        x, y, U, V,
        density=1.5,
        arrow_scale=0.1,
    )
    for trace in fig.data:
        trace.line.color = VIOLET
        trace.opacity = 0.7

    # Cylinder drawn on top to hide streamlines passing through interior
    theta = np.linspace(0, 2 * np.pi, 200)
    cx, cy = np.cos(theta), np.sin(theta)
    fig.add_trace(go.Scatter(
        x=cx, y=cy,
        mode="lines",
        line=dict(color=TEAL, width=2),
        fill="toself",
        fillcolor="rgba(255,255,255,1.0)",
        showlegend=False,
        hoverinfo="skip",
    ))
    # Cylinder border on top
    fig.add_trace(go.Scatter(
        x=cx, y=cy,
        mode="lines",
        line=dict(color=TEAL, width=2.5),
        showlegend=False,
        hoverinfo="skip",
    ))

    # Stagnation points
    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