Create images and then a movie or a .gif animation from your data

Create images and then a movie or a .gif animation from your data

• Make sure you save your data in a proper way (i.e. a way for which any tilmestep you need is saved and for which you save the position of the particles and their physical characteristics you’re interested in). Here I save one .txt file for each iteration I want to save from the computation (of flow of 2D beads). Each file is named save1.txt, save2.txt etc. until the last one : save101.txt for me.

Each of these text file is composed by the following columns (but there are no headers):
it,radius[i],rx[i], ry[i],vx[i], vy[i],ome[i],ax[i], ay[i],domedt[i] (i being the particle)

Therefore it looks like:
1.000000e+02 4.411078e-01 -5.310373e-01 8.232963e+00 3.710539e+00 -1.537021e-01 0.000000e+00 4.204679e-01 -5.028706e-01 0.000000e+00
1.000000e+02 5.043521e-01 1.579150e+01 2.389012e+01 7.111336e+00 -2.592077e-01 0.000000e+00 1.986693e-01 -9.800666e-01 0.000000e+00
1.000000e+02 4.639842e-01 1.709093e+01 9.843280e+00 3.501103e+00 4.201602e-01 0.000000e+00 -1.247290e+01 -1.373425e+01 0.000000e+00
1.000000e+02 4.598597e-01 -1.441409e+01 9.238946e+00 4.124428e+00 -9.097106e-01 0.000000e+00 9.960587e-01 -4.897665e+00 0.000000e+00
1.000000e+02 5.051099e-01 -2.714866e+00 1.688822e+00 1.288142e-01 6.407148e-02 0.000000e+00 2.723070e+00 5.263489e+00 0.000000e+00
1.000000e+02 5.797257e-01 -1.710212e+01 1.670530e+01 5.672349e+00 -1.402118e-01 0.000000e+00 -1.754910e+01 -9.859782e+00 0.000000e+00
1.000000e+02 4.439925e-01 8.605122e+00 1.071265e+01 3.753931e+00 -5.498020e-01 0.000000e+00 2.538151e+00 1.270680e-01 0.000000e+00
1.000000e+02 5.372175e-01 -1.175561e+01 1.120167e+01 3.876558e+00 -6.042894e-01 0.000000e+00 -2.538811e+00 1.648526e+00 0.000000e+00
1.000000e+02 4.408521e-01 1.036803e+01 1.629499e+01 5.747330e+00 -7.071506e-02 0.000000e+00 4.726249e+00 -3.825003e+00 0.000000e+00

Once you have all your save%%%.txt files, you can generate the corresponding images (one for each save%%%.txt file, i.e. one for each saved iteration of your computation).

• Generate the images:
You can make your proper routine that plots circles of proper radius at the proper position, and choosing their face color according to a physical quantity loaded from the save%%%.txt file.

Here is an example of a Python routine that does want you need (I is very basic but does the job 😉
#!/usr/bin/env python
import matplotlib
matplotlib.rcParams['legend.fancybox'] = True
import numpy as np
import matplotlib.pyplot as plt
import pylab
from matplotlib import rc
rc('text', usetex=True)
rc('font', family='serif')
from pylab import *
from scipy.integrate import ode
from scipy.special import airy
from scipy.optimize import leastsq
from scipy.optimize import fmin_slsqp
from scipy.optimize import curve_fit
from operator import itemgetter, attrgetter
inFile = sys.argv[1]
endfilm= int(sys.argv[2])
#upscale= int(sys.argv[3])
for T in range(1,endfilm):
Al= np.genfromtxt(inFile+str(T)+'.txt')
fig = plt.figure()
figure(figsize=(5.0,7.8),dpi=20000)
allo = Al[:,4]
area = np.pi * (8* Al[:,1])**2
im=plt.scatter(Al[:,2], Al[:,3], s=area,c=allo, alpha=1,linewidths=0.5,vmin = -2, vmax = 10)
cb=plt.colorbar(im,orientation="horizontal")
plt.xlabel(r"$x/d$",fontsize='14',labelpad=2)
plt.ylabel(r"$y/d$",fontsize='14',labelpad=2)
plt.tick_params(axis='y', labelsize=10,pad=4)
plt.tick_params(axis='x', labelsize=10,pad=4)
file= int(Al[0,0])
plt.ylim([-5,40])
plt.xlim([-16,16])
cb.set_label(r"$Vx$", fontname='Arial', fontsize=20)
plt.savefig("fig"+str('{:05.0f}'.format(T))+".png")
# plt.savefig("fig"+str(file)+".png")

I save it as plot.py
To use it, type the following command in your terminal (you need to ave all the images and this .py code in the save file):
python plot.py save 101 1.
Here, « save » is the generic name of your save%%%.txt files, 101 is the maximum number and 1 is a dilatation coefficient to adjust the plot (I commented the line in the code here so you don’t need it a priori).
Here you go! You now have your 101 images, numbered with 5 digits (to be adjusted if need, see last code line).

• Create a GIF animation:
– install ImageMagick using Homebrew (to install Homebrew on your mac: see http://brew.sh). To do so, type the following command line in your Terminal:
brew install ImageMagick
– then create the GIF from your images: go in the directory in which you stored the images and type in the Terminal:
convert fig*.png filmGIF.gif
– way a bit and you’ll have your .gif file, that you want visualize using your internet browser for instance.

• Create a movie from your images:
– if not already done, install FFMPEG on your mac by typing the following in your Terminal:
brew install ffmpeg
– then compute the film (you can check the ffmpeg website for more options, the most important is the framerate -25 here-, the definition -1920×1080- and the -vcodec libx264 to make sure you can play the movie on any platform):
ffmpeg -r 25 -f image2 -s 1920x1080 -i fig%05d.png -vcodec libx264 -crf 25 -pix_fmt yuv420p myMOVIE.mp4

Here you go! Please share and comment 😉
Thanks!

No comments yet. Be the first.

Laisser un commentaire