The result is:
This page shows my exercise to visualize air flow past three cylinders.
The flow is calculated by commercial finite elemental method software i.e. COMSOL Multiphysics.
In this code, matplotlib.animation.ArtistAnimation function is used.
See also:
- Draw flow past cylinder with continuous stream line using Python & Matplotlib.pyplot
URL: https://pythonmatplotlibtips.blogspot.jp/2017/12/draw-flow-past-cylinder-with-continuous.html - Draw cycloid animation using matplotlib.animation.ArtistAnimation
URL: https://pythonmatplotlibtips.blogspot.jp/2017/12/draw-cycloid-animation-using.html - Draw 3D line animation using Python & Matplotlib.ArtistAnimation
URL: https://pythonmatplotlibtips.blogspot.jp/2017/12/draw-3d-line-animation-using-python-matplotlib-artistanimation.html
In [1]:
import matplotlib.pyplot as plt
import scipy as sp
import numpy as np
import pandas as pd
from matplotlib import cm
from matplotlib.animation import ArtistAnimation
from scipy.integrate import ode as ode
plt.rcParams['animation.html'] = 'html5'
Firstly, load the data calculated by COMSOL Multiphysics.
In [2]:
df0 = pd.read_csv('./air_flow_multi_cylinder.txt',skiprows=9,delim_whitespace=True,header=None)
dfs = []
ntime = 0
while True:
try:
df1 = df0.loc[:,:1]
df2 = df0.loc[:,2+3*ntime:2+3*ntime+2]
df = pd.concat([df1,df2],axis=1)
df.columns = ['x','y','p','u','v']
dfs.append(df)
ntime += 1
except:
break
Secondary, process the data to visualize.
In [3]:
dfs[-1]
Out[3]:
Check the data.
In [4]:
ntime,len(dfs)
Out[4]:
In [5]:
x = dfs[0].loc[:,'x']
y = dfs[0].loc[:,'y']
nx = int(len(x)**0.5)
ny = int(len(y)**0.5)
x = x[:nx]
y = y[::ny]
In [6]:
x
Out[6]:
In [7]:
y
Out[7]:
In [8]:
nx,ny
Out[8]:
In [9]:
# p: pressure
plt.contourf(x,y,dfs[-1]['p'].values.reshape(nx,ny))
plt.axes().set_aspect('equal')
plt.show()
# u: velocity in x direction
plt.contourf(x,y,dfs[-1]['u'].values.reshape(nx,ny))
plt.axes().set_aspect('equal')
plt.show()
# v: velocity in y direction
plt.contourf(x,y,dfs[-1]['v'].values.reshape(nx,ny))
plt.axes().set_aspect('equal')
plt.show()
# (u**2+u**2)**0.5: velocity magnitude
z = ((dfs[-1]['u']**2+dfs[-1]['v']**2)**0.5).values.reshape(nx,ny)
plt.contourf(x,y,z)
plt.axes().set_aspect('equal')
plt.show()
Thirdly, plot the data for each grame and store them to the array.
In [10]:
fig = plt.figure(figsize=(6.1,5),facecolor='w')
ims = []
levs = np.linspace(0,6,100)
for i in range(ntime):
z = ((dfs[i]['u']**2+dfs[i]['v']**2)**0.5).values.reshape(nx,ny)
im = plt.contourf(x,y,z,levs,cmap=cm.jet,vmax=6,vmin=0)
ims.append(im.collections)
cbar = plt.colorbar(im)
cbar.set_clim(0,6)
cbar.set_ticks(np.linspace(0,6,7))
cbar.set_label('Velocity magnitude [m/s]')
plt.xlim(0,1)
plt.ylim(0,1)
plt.xlabel('x [m]')
plt.ylabel('y [m]')
plt.axes().set_aspect('equal')
Finaly, create animation using ArtistAnimation function
In [11]:
ani = ArtistAnimation(fig,ims,interval=200,repeat=True)
In [12]:
ani
Out[12]:
In [13]:
ani.save('air_flow_multi_cylinder.mp4',writer='ffmpeg')
ani.save('air_flow_multi_cylinder.gif',writer='imagemagick')