Recent Changes - Search:

HomePage

PmWiki

pmwiki.org

MakingAnimations

Making animations with XMGRACE

Xmgrace is pretty easy to script. Construct a text file that contains the commands you want common for all frames of the animation. This should set the axes, the line styles and colors, the plot title and so on.

For example,

TITLE "My example animation"
WORLD XMIN 1
WORLD XMAX 32
WORLD YMIN 0.2
WORLD YMAX 1.2
XAXIS TICK MAJOR 10
XAXIS TICK MINOR 1
YAXIS TICK MAJOR 0.2
YAXIS TICK MINOR 0.1
s0 LINE LINEWIDTH 2.0

Save this as a file, say setup.batch. Now, for every data set we want to construct an image. For example,

for i in dataset.t* ; do gracebat $i -batch setup.batch -printfile fig-$i.png -hdevice PNG -hardcopy ; done

Now we use convert to make an animated gif (or an mng file, but these seem less well supported on web browsers).

convert -delay 50 fig-*.png animation.gif

Most likely, the delay parameter will need tweaking to get an animation that moves at the right speed. If it turns out that the order of files that is produced by the shell glob is not correct, then write the list of files to a text-file (ls fig-*.png > files), and sort it (either by hand, or using some utility such as sort), then use

convert -delay 50 $(cat files) animation.gif

The usual output of the time evolution program produces files that do not appear in the proper sorting order. eg, time t=10 would be listed before time t=2, for example. One way to produce a sorted list is to specify the location of the key to the sort command.

 $ ls -1
density.t1
density.t10
density.t5

 $ ls | sort -k 1.10 -n
density.t1
density.t5
density.t10

The -k 1.N option means start reading the first sort field from position N in the string. The -n option means sort the field numerically, rather than alphabetically.

What we want to do now is set the title of the graph to t=N, where N is the actual simulation time of the figure. However, one big disadvantage with xmgrace is that you cannot specify batch commands on the command line itself, only in a file. (There are a few commands you can put on the command line, but it is far from a complete set; you cannot set the title of the graph, for instance.) This is a big annoyance, which we can get around only with a bit of shell scripting. This is wrong, it turns out there is a gracebat option to do this: -pexec "command". We can use this to set elements of the figure that need to change from frame to frame.

Firstly, we set the title font to a fixed-width font. Remove the TITLE line from the setup.batch, and replace it with

TITLE FONT "Courier"

Now, our one-liner for generating the figures from the data is

for i in $(cat files) ; do gracebat $i -batch setup.batch -pexec $(printf "TITLE \"t=%4.1f\"\n" $(echo $i | sed 's/.*t//')) -printfile fig-$i.png -hdevice PNG -hardcopy ; done

An example output is at http://web.physik.rwth-aachen.de/~ianmcc/hubbard-animation-test.gif, that was produced with this scheme. This is the charge density profile of a single-particle excitation added to the left-most site at t=0. **WARNING: this is an almost 2MB .gif file.

Making a movie from a sequence of frames

This is a simple script to generate a movie from a sequence of png images. here, pics is a text file that contains a list of png images. The script converts them into an avi file in MPEG-4 format.

#!/bin/bash

# list pictures that we want to encode, in order.  Assumed to be stored in file "pics"
pics=$(cat pics)
# picture type, png, jpeg etc
type=png
# output file name
file_out=movie.avi
# frames per second
fps=20
# average bitrate in Mbps.  Default is 6, but for a line animation we want much more
obr=600
# options for the encoding:
# keyint: insert a keyframe every n frames.  This increases the size of the file,
# but seeking is only possible to a key frame.  Since we probably have a fairly
# low framerate, this could be pretty small
opt="keyint=25"
# video codec.  mpeg4 should be pretty safe
codec="msmpeg4v2"

# do it
mencoder -ovc lavc -lavcopts vcodec=$codec:vbitrate=$obr:$opt -nosound -mf fps=$fps:type=$type -o $file_out $(for i in $pics ; do echo mf://$i ; done)

A more complicated example that was generated with this script is http://web.physik.rwth-aachen.de/~ianmcc/hubbard-sc.avi

Making animations with GNUPlot

Someone else will have to add this, I've never used GNUPlot much.

Edit - History - Print - Recent Changes - Search
Page last modified on February 14, 2009, at 02:42 AM