LBM animation in Gnuplot

I am trying to develop an implementation of the Lattice Boltzmann Method (a way of solving problems of fluids). The case I am studying consists of a 2D fluid with a cylinder inside of it. I would like to create an animation of the problem. To do so, I try to save data in text documentes (one for each time-step) including density and velocity in the x and y axis for each of the points of the whole space. I would like to use Gnuplot for this. Anyone could help me?

1 Like

Hi @xpicabea,

Welcome to Discourse. I hope it is a Fortran LBM code you are using.

Here is an old gnuplot script to help you get started (store this as animation.plt):

#!/usr/bin/gnuplot

set terminal pngcairo enhanced size 640,480

tstart = 0
tmax = 100000
tinterval  = 100

unset tics
unset border
unset colorbox

set xrange [0:300]
set yrange [0:300]

set size ratio -1

infile(i) = sprintf("result_%d.out",i)
outfile(j) = sprintf("pic%06d.png",j)

set cbrange[-0.1:0.1]

# main animation loop
do for [i=tstart:tmax:tinterval]{
    set output outfile(i/tinterval)
    plot infile(i) u ($1-0.5):($2-0.5):4 w image notitle
}

set output
set terminal qt
reset

After you’ve generated the output frames you can use ffmpeg to convert them into a video.

I did my master’s thesis on LBM around back in 2017. Here is an example animation I produced.

3 Likes

I found another animation I made along with the shell script I used to convert the frames to a video:

# https://stackoverflow.com/questions/24961127/how-to-create-a-video-from-images-with-ffmpeg
ffmpeg -framerate 30 -i conc_img%04d.png -c:v libx264 -pix_fmt yuv420p spinodal.mp4

You will want to replace the conc_img%04d.png with your image name, and naturally the output file name (spinodal.mp4 in this case). I remember I had some issue with playing the video on different OS’s due to video codecs, but I never bothered to figure out what was wrong.

1 Like

Wonderful!
Such an animation would be great in the Fortran-lang.org website…

1 Like

If the files are saved with intervals say 500 then use the following gnuplot commands:

set terminal gif animate delay 0.01  
set view map 
set size square 
set xrange [*:*] noextend 
set yrange [*:*] noextend 
set palette defined (0 'blue', 0.5 'grey', 1 'red') 
set pm3d map interpolate 9,9 
set out 'gnuplot_animation_grain.gif' 
n=500 
while (n <=5000) {   
fname = sprintf('grain_%d.dat', n ) 
splot fname matrix with pm3d notitle  
n=n+500 
 } 
unset output 

It will generate the gif file with the name gnuplot_animation_grain.gif. You can try this example with the files in the link.

gnuplot_animation_grain

Alternatively, you can use the dislin graphical library and use the minimum routines:

  call Metafl ( 'cons' )
  call Disini ( )
  call autres ( Nx, Ny )
  
  if ( mod( istep, nprint ) .eq. 0 ) then 
       call erase ( )  
       call graf3 ( 0.d0, 300.d0, 0.d0, 50.d0, 0.d0, 300.d0,&
            & 0.d0, 50.d0, 0.0d0, 1.2d0, 0.0d0, 0.2d0 )
       call crvmat ( phi, Nx, Ny, 1, 1 )   
       call endgrf
       call sendbf ( )
    end if
 
  call Disfin ( )

Nx and Ny are the x and y axis dimensions. nprint is the ouput frequency and the graf3 is the routine to plot the axis system: x-axis, y-axis, and color bar ranges defined there. phi is 2D matrix.

The example code is here:

Compile and run with

f90link -a -r8 main

for double precision

dislin_gif

The dislin-generated animation of the previous example looks like this.

Thanks for the comment. I think a larger community gallery of results produced by Fortran programs could serve as a nice showcase of what the language is used for.

Here’s a video I noticed recently showing results from AVBP - a CFD code for turbulent reacting flows developed at CERFACS:

2 Likes

I definitely agree. Such visualizations can have more impact than words.