When producing animation from computed results one is essentially constructing a movie, where individual frames are displayed on the screen in rapid succession. The animation process can be broken down into the following basic steps:
1. Perform the simulation, writing out sufficient data
to create a still image for each frame in the movie.
2. Produce a sequence of still image files, one for each
frame in the movie.
3. Assemble the individual frames into a single movie
file.
It is not uncommon for animations to contain several hundred frames and thus it is usually necessary to automate the task of constructing the individual frames. A wide variety of still image formats can be used to produce animation, and there are also a large number of formats to choose from for the resulting movie file. This note describes a method of combining still gif images into an "animated gif" movie file. Animated gif files have the advantage that they are easily viewed in almost any web browser and result in fairly compact movie files. It should be a fairly straightforward matter to adapt these instructions to a different image format, or convert the final output to an alternative format if desired.
It is probably best so separate the task of producing the movie from the task of running the simulation. This can be done by writing the required data to a file at each time step during the simulation for which an animation frame is desired (a skip factor is generally used since the simulation time steps tend to be much finer than what is required to construct a smooth movie). Once the simulation has finished, a "post-processing" step is undertaken where the data file is read, the still frames made, and the movie constructed. It is this step that should be automated since it is necessary to create a large number of still image files. I handled the post-processing task by writing a fortran program that reads the simulation output data file and then extracts a single specified frame, writing this to a separate file. A plotting program then reads the single frame data file and produces a still image from it. The steps of extracting a single frame of data and converting it to an image file are then repeated over and over in the script file.
My main simulation code contains the following lines in order to produce a single large output data file that holds all of the frames:
open(unit=1,file='frames.dat',form='unformatted')
dt_print = 0.01
i_skip = max(dt_print/dt, 1)
c *** Write a header record containing all relevant parameters for the run
write(1) Nw, CFL, i_fd_type,
&
Nx, xL, dx, x_shift_0, period,
&
nt, t_final, dt, dt_print, i_skip, n_frames
.
.
.
c *** Time stepping loop
do n=1, nt
.
.
.
if( mod(n,i_skip)
.eq. 0 ) then
i_frame = i_frame + 1
write(1) i_frame, n, t
write(1) (u( i),i=1,Nx)
write(1) (u_exact(i),i=1,Nx)
end if
end do
I then used the program extract_frames.f to read the frames.dat file, extract a specified frame number, and write this single frame to the file frame.dat. This latter file is the one that is read by the plotting program.
A good tool for producing the still image files is gnuplot , which is a widely-used, free plotting package. Gnuplot can be driven from a script file, which makes it especially well-suited for automating the task of producing the image files. Gnuplot can also produce gif files, which are necessary if the movie file is to be an animated gif. [Note: Some installations of gnuplot have been compiled without gif support. If your version does not support gif, you will see an error message when you attempt to use the set terminal gif command discussed below. In this case, the best thing to do is to visit the gnuplot site and download a version that has gif support.] In order to automate gnuplot, you must first produce a plotting command file. The easiest way to do this is to run gnuplot from the command line and use it to make a plot of a representative frame in the movie. You can optimize the plot at this stage by setting the axis limits, including axis titles, figure labels, etc. Once the plot is optimized, issue the command set terminal gif large size 320,240 followed by the command set output "frame.gif". Finally save the plotting instructions by typing save "frames.plt" followed quit . The set terminal and set output commands appear in the frames.plt file, but they are commented out. Gnuplot does this so that the next time you use the plotting commands ,the image is displayed on the screen instead of sent to a file. We actually need the image to be written to a file and thus it is necessary to edit the frames.plt file and remove the comments on these two lines. When you have finished this step, your plotting commands file will look something like my frames.plt (if you are impatient just download this file and don't worry about creating your own). An image file will then be created by issuing the command gnuplot frames.plt.
The next step is to produce a script file which will automatically extract the data frames and plot them. I wrote a c-shell script for this purpose. The following commands are enough to produce the sequence of still image files:
#!/bin/csh
set n_frames = $1
/bin/rm -f frame.*
set count=1
while($count <= $n_frames)
echo "$count" | extract_frames
gnuplot frames.plt
if ($count < 10) then
set number = "0$count"
else
set number = $count
endif
mv frame.gif frame.$number
@ count++
end
If these commands are inserted in the file make_images, the command make_images n, where n is the number of frames contained in the large data file frames.dat, will produce a series of image files, named frame.01, frame.02, ..., frame.n.
Finally, the utility gifsicle is used to assemble the individual still images into a single animated gif file. Simply issuing the command gifsicle -d s -O2 frame.* > movie.gif, where s is the inter-frame delay in 100ths of a second, will create the movie. Start with a value s=10 and then adjust it up or down as necessary in order to make the movie run at the desired speed. The movie can ve viewed by reading it into your web browser.
I wrote a larger script file, make_movie, that combines all the above steps, so that a movie file can be created by issuing a single command. This is especially useful for rapidly assessing the simulation results.
If you don't want to bother with installing gnuplot and/or gifsicle on your system, contact me and I will give you an account on one of my machines that have these utilities installed.