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."""
from pathlib import Path

# ── Palette + theme (matching the Plotly Studio gallery these charts ship in) ──
VIOLET, TEAL, GREEN, PINK, ORANGE = "#845EEE", "#52B3D0", "#55B685", "#DA5597", "#E9A23B"
PRIMARY, SECONDARY = VIOLET, TEAL
COLORWAY = [VIOLET, TEAL, GREEN, PINK, ORANGE]
BG, TEXT, GRID, MUTED = "#ffffff", "#1c2024", "#d9d9e0", "#60646c"
FONT = "Inter, -apple-system, BlinkMacSystemFont, sans-serif"
COLORSCALE = [[0, "rgba(132, 94, 238, 0.05)"], [1, "rgba(132, 94, 238, 0.9)"]]


def apply_theme(fig):
    """Light gallery theme: white background, Inter font, soft gridlines."""
    fig.update_layout(
        paper_bgcolor=BG, plot_bgcolor=BG, colorway=COLORWAY,
        font=dict(family=FONT, color=TEXT, size=12),
        legend=dict(font=dict(color=TEXT)),
        hoverlabel=dict(bgcolor="#f0f0f3", font=dict(color=TEXT, family=FONT), bordercolor=GRID),
    )
    fig.update_xaxes(gridcolor=GRID, linecolor=GRID, zerolinecolor=GRID)
    fig.update_yaxes(gridcolor=GRID, linecolor=GRID, zerolinecolor=GRID)


def fetch_csv(url, **kwargs):
    import io
    import pandas as pd
    import requests
    r = requests.get(url, timeout=60)
    r.raise_for_status()
    return pd.read_csv(io.StringIO(r.text), **kwargs)


def fetch_json(url):
    import requests
    r = requests.get(url, timeout=60)
    r.raise_for_status()
    return r.json()

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,
    )
    apply_theme(fig)
    return fig


fig = generate()
fig.show()

Made with Plotly