Making a simulation movie with a running plot

From OxDNA
Revision as of 21:03, 7 November 2016 by Randisi (talk | contribs) (Added to Category:Examples)

oxDNA simulations can be very informative to look at if presented in the form of a movie. Here we'll see how to make one with cogli1, a lightweight visualisation tool that can draw oxDNA configurations natively. It's so lightweight that can quickly render configurations with thousands of nucleotides, unlike other visualisation tools we've tried in the past.

Making a movie

The only pre-requisite for this tutorial is cogli1, a very simple visualization program that can be downloaded here. Note that some of the options used in the tutorial require the bleeding-edge version, which can be checked out with the following command:

svn checkout svn:// cogli1-code

To make a movie, we are going to load a trajectory in cogli1, choose a view, and then export each frame in the simulation as a trajectory file. Then we'll render them in povray and convert them in a movie with mencoder. Make sure these programs are installed before running this tutorial.

  1. Open the trajectory with cogli1:
    cogli1 --always-centre -I -v -m -t trajectory.dat
    The only necessary option is -t (though you will want to use -m if you're using the DNA2 interaction), which specifies the topology file. The others are nice for this particular system and for many others, but you might or might not want to use them for the system you're working on. Their use is as follows:
    • -I
      rotate the system to attempt fix its orientation in space. Simulated objects often float freely in solution and can rotate very sharply from a snapshot to the other, so that a movie would be confusing. This tries to copmensate for that. This does fail from time to time, so you might want to double check that everything works fine before rendering the final, hi-quality version of your images.
      Start the simulation with a centered configuration.
      Move the system around to keep its center of mass to the centre of the simulation box throughout all the frames
      Show oxDNA2 nucleotides, which present major-minor groove asimmetry (just like real DNA), unlike oxDNA1.
    Several other options can be used. You can see the documentation for cogli1 by launching it without any other arguments.
  2. Find a view that you like by moving the configuration around, zooming in, zooming out, etc. See the cogli1 documentation for details on how to do this. Verify that the view is good for every frame in your trajectory by skimming through it, holding the + button. When you are satisfied that you've found a good view, just press
    to save the simulation snapshot to a .cpy file. An example of a good view for this system is saved in sample_view.cpy .
  3. Now let's export each trajectory frames to .pov.
    cogli1 -o -l view.cpy -t trajectory.dat
    where view.cpy is the name of your view. Note the -o option, which tells cogli1 to generate pov files without showing any GUI window. This will produce as many .pov files as there are simulation snapshots, and each of them will generate a movie frame. You can edit any .pov file if you so wish, replace it with one taken from another view, or with something copletely different if you feel like playing Tyler Durden :)
  4. Rendering the images in povray can take a long, time, so first we're going to do that with a low resolution to quickly create a preview. Use the command:
    for d in $(ls -v1 *.pov); do povray -D +H600 +W860 $d; done
    in order to generate the png images at a low resolution, with 600 pixels of height and 860 of width (i.e. with a format of 16:10). Further decrease the resolution if you want it to run even faster, or change the aspect ration as you please.
  5. Encode the povray images in a .avi movie with the commands
    ls -1v output_*.png > storyboard.txt
    mencoder mf://@storyboard.txt -mf w=860:h=600:fps=6:type=png -ovc xvid -xvidencopts bitrate=200 -o preview.avi
    Notice that the width and height of the movie should be the same as the one used in the povray command above.
    w and h
    respectively the width and hegiht of the movie (in pixels, here 3200 and 2400). This should be the same as the output_*.png files if you don't want the screen to view to be distorted.
    the number of frames per second (here 6).
    the type of image (here png)
    the codec used (here xvid)
    xvidencopts bitrate
    the bitrate, which is an option of the xvid codec (here set to 200)
    the name of the movie file (here output.avi).
  6. Visualise the movie with the editor of choice and verify that everything is fine. Go back to the previous points if you see that something isn't quite right.
  7. If you're happy with the view of the movie, you might want to actually use a better resolution. Notice that povray might take a few hours or days to render the scenes depending on your GPU. Notice that if you are going to make a video with a running plot in the top/bottom part of the screen and the simulation movie in the other (i.e. using the append below) you'll need to accomodate the resolution for that, so that if e.g. you want to keep the aspect ratio of the video at 16:10 and your plot takes e.g. 20% of the screen you'll want to use a resolution of 1920x1000, so that the plot can be created with a resolution of 1920x200 and the composite frames have a resolution of 1920x1200.
    for d in $(ls -v1 *.pov); do povray -D +A +H1200 +W1920 $d; done
    ls -1v output_*.png > storyboard.txt
    mencoder mf://@storyboard.txt -mf w=1920:h=1200:fps=6:type=png -ovc xvid -xvidencopts bitrate=600 -o output.avi
  8. Your video is ready! Enjoy :-D See below how to add a running plot superimposed to the video or above it.

Adding a running plot

Sometimes a system undergoes a structural change that can be seen in the variation of some physical quantity. It's then nice to show the plot updating as the system's configuration evolves. To do that: , but

  1. create the .png frames as we do in the section above (again, you probably want to do this in 2 steps if your system is demanding to render on your machine).
  2. Create the plots of the quantity you want to show. Here we'll use gnuplot. This can be done with e.g. the following gnuplot commands, which should work in gnuplot >=4.6. If you're stuck with an old version of gnuplot that doesn't support for loops, you can use another program to generate a text file that writes the instructions to be performed at every iteration of the loop, and then run the resulting script in gnuplot. In this example we'll create plots for the energy of the system, which is not a terribly informative quantity for this system and so I might change this example to something else in the future.
    set terminal png linewidth 4 font 'arial,28pt' size 3200,600
    set xrange [25e3:25e3*40]
    set yrange [-1.42; -1.24]
    set xlabel 'time [SU]'
    set ylabel 'Energy/# particles [SU]'
    set grid
    unset key
    do for [i = 0:39]{ set out 'plot_'.i.'.png'; plot 'energy.dat' every 1000::1000::1000*(i+1) w lp }

notice that several things should be changed here according to your needs, including:

    • the terminal type (to change the font, resolution, linewidth, etc). The resolution should be compatible with the one of the simulation snapshots (see last point above).
    • the xrange, to make sure that it fits all the points, just barely).
    • the yrange, to make sure that everything fits as needed).
    • the plot command, to change the appearence of the plot, the line-type, how many frames do we have, whether to print the observable only when we have a frame or for all the frames up until a point, (type help every in gnuplot to learn more about that) etc.
    • possibly adding some modifiers before plotting, including title, etc.
    • any other edit you might wish to make
  1. After both points above are finished, you can compose the images by either pasting the plots on the simulation snapshots, or appending the images together. This can easily be done with `convert` on linux and cygwin, with either of the following 2 lines:
    for i in {0..39}; do convert trajectory.dat_${i}_time_*.png plot_${i}.png -composite output_${i}.png; done
    for i in {0..39}; do convert trajectory.dat_${i}_time_*.png plot_${i}.png -append output_${i}.png; done
    swap the order of the plots and the simulations snapshots above to have the plot display below the simulation system instead.
  2. Convert the png files into a movie, with mencoder or ffmpeg, as usual, with:
ls -1v output_*.png > storyboard.txt
mencoder mf://@storyboard.txt -mf w=3200:h=2400:fps=6:type=png -ovc xvid -xvidencopts bitrate=200 -o output.avi

An example of this thing is the movie that I'm attaching here sample movie