[python] Streamline drawing

Streamline

Streamline of the flow field represented by the Cartesian coordinate system $ \ boldsymbol {x} = (x, y) $, velocity $ \ boldsymbol {u} = (u, v) $ at a certain time $ t $ (Streamline) </ b> is a mathematical ordinary differential equation $ \frac{\text{d} x}{\text{d} u(x,y,t)} = \frac{\text{d} y}{\text{d} v(x,y,t)} $ It is defined by. Imagine that the streamlines introduced in this article are not drawn by solving this ordinary differential equation, but simply by connecting velocity vectors.

Draw with streamplot built into matplotlib

As an example, a two-dimensional stationary Taylor-Green vortex: $ \begin{aligned} u &= \sin x \cos y \\\\ v &= -\cos x \sin y \end{aligned} $ Is used. You can easily draw streamlines with the matplotlib built-in `` `streamplot```.


import matplotlib.pyplot as plt
import numpy as np
from numpy import sin, cos, pi, sqrt

n = 1000
x = np.linspace(0, 2*pi, n)
y = np.linspace(0, 2*pi, n)
X, Y = np.meshgrid(x, y)

# Taylor-Green Vortices
u = sin(X)*cos(Y)
v = - cos(X)*sin(X)
speed = sqrt(u**2 + v**2)

# Plot
plt.figure(1)
plt.streamplot(X, Y, u, v, density=3, color='k', arrowstyle='-', linewidth=0.6)
plt.contourf(X, Y, speed, 100, cmap='viridis')
plt.show()

I was able to draw a streamline. The background is the flow velocity.

Draw with Line Integral Convolution (LIC)

This is the main subject. The above matplotlib built-in `streamplot``` is beautiful enough, but if you want to draw more beautifully (artistically), use the method <b> Line Integral Convolution (LIC) </ b>. If you use it, you can draw streamlines very beautifully. [Wikipedia](https://en.wikipedia.org/wiki/Line_integral_convolution) has a brief explanation. For the algorithm, see [Cabral and Leedom, 1993](https://dl.acm.org/doi/pdf/10.1145/166117.166151?casa_token=A78GoJh369IAAAAA%3AfWlns5xYUUWiQC09j8IIMBVDZq4jYTRt_uJX2bQhc4jYTRt_uJX2bQhc4V5d. Examples of streamlines drawn using LIC are [Phillips et al., 2015](https://iopscience.iop.org/article/10.1088/1748-3190/10/5/056020/meta) and [Van Der Kindere et al., 2019](https://link.springer.com/article/10.1007/s00348-019-2678-5) and so on. There is a sample code in [SciPy Cookbook](https://scipy-cookbook.readthedocs.io/items/LineIntegralConvolution.html), so I'm grateful to use it. You can use it by building `setup.py. Rewrite the first half of the linked lic_demo.py```. Since I want to display the flow velocity in the background, I multiply the flow velocity by LIC in the last line.

Vector = np.stack([u, v], axis=2)
Vector = np.array(Vector, dtype=np.float32)
texture = np.random.rand(n, n).astype(np.float32)

plt.bone()
frame = 0
dpi = 1000
video = False
if video:
    kernellen = 31
    for t in np.linspace(0,1,16*5):
        kernel = np.sin(np.arange(kernellen)*np.pi/kernellen)*(1+np.sin(2*np.pi*5*(np.arange(kernellen)/float(kernellen)+t)))
        kernel = kernel.astype(np.float32)
        image = lic_internal.line_integral_convolution(Vector, texture, kernel)
        frame += 1
else:
    kernellen = 31
    kernel = np.sin(np.arange(kernellen)*np.pi/kernellen)
    kernel = kernel.astype(np.float32)
    image = lic_internal.line_integral_convolution(Vector, texture, kernel)

speed_LIC = image[1:-1, 1:-1]*speed[1:-1, 1:-1]

It's very beautiful.

By the way, it's easier with Mathematica

Mathematica has a built-in `` `LineIntegralConvolutionPlot```!

LineIntegralConvolutionPlot[{{Sin[x] Cos[y], -Sin[y] Cos[x]}, {"noise", 1000, 1000}}, {x, 0, 2 Pi}, {y, 0, 2 Pi},  
ColorFunction -> "BlueGreenYellow", RasterSize -> 300]

It was just done ...

Recommended Posts

[python] Streamline drawing
Graph drawing in python
Drawing with Python Tinker
Drawing candle charts in python
Python
Streamline web search with python
Drawing a silverstone curve using python
Learn Python by drawing (turtle graphics)
Introduction of python drawing package pygal
kafka python
Python basics ⑤
python + lottery 6
Python Summary
Built-in python
Python comprehension
Python technique
Studying python
Python 2.7 Countdown
Python memorandum
Python FlowFishMaster
Python service
python tips
python function ①
Python basics
Python memo
ufo-> python (3)
Drawing software
Python comprehension
install python
Python Singleton
Python basics ④
Python Memorandum 2
python memo
Try drawing a simple animation in Python
Python Jinja2
Python increment
atCoder 173 Python
[Python] function
[Python] Drawing a swirl pattern with turtle
Python installation
python tips
Installing Python 3.4.3.
Try python
Python memo
Python iterative
Python algorithm
Python2 + word2vec
[Python] Variables
Python functions
Python sys.intern ()
Python tutorial
Python decimals
python underscore
Python summary
Start python
[Python] Sort
Drawing board
Note: Python
Python basics ③
python log
Python basics