Making a simulation movie with a running plot: Difference between revisions

From OxDNA
Jump to navigation Jump to search
(Making the article more clear. Still a work in progress, but hopefully less so :))
No edit summary
 
(9 intermediate revisions by 2 users not shown)
Line 1: Line 1:
'''Page currently under construction'''
[[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 <tt>cogli1</tt>, 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.  
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 <tt>cogli1</tt>, 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.  
Line 5: Line 5:
== Making a movie ==
== Making a movie ==


To make a movie, where's going to load a trajectory in <tt>cogli1</tt>, choose a view, and then export each frame in the simulation as a trajectory file. Then we'll render them in <tt>povray</tt> and convert them in a movie with <tt>mencoder</tt>. Make sure these programs are installed before running this tutorial.
The only pre-requisite for this tutorial is cogli1, a very simple visualization program that can be downloaded [https://sourceforge.net/projects/cogli1/ 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:
 
<pre>
svn checkout svn://svn.code.sf.net/p/cogli1/code/ cogli1-code
</pre>
 
To make a movie, we are going to load a trajectory in <tt>cogli1</tt>, choose a view, and then export each frame in the simulation as a trajectory file. Then we'll render them in <tt>povray</tt> and convert them in a movie with <tt>mencoder</tt>. Make sure these programs are installed before running this tutorial.


# Open the trajectory with <tt>cogli1</tt>:
# Open the trajectory with <tt>cogli1</tt>:
Line 18: Line 24:
#*;<tt>-m</tt>
#*;<tt>-m</tt>
#*:Show oxDNA2 nucleotides, which present major-minor groove asimmetry (just like real DNA), unlike oxDNA1.
#*: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 <tt>cogli1</tt> by launching it without any other arguments.
#:Several other options can be used. You can see the documentation for <tt>cogli1</tt> by launching it without any other arguments.
# Find a view that you like by moving the configuration around, zooming in, zooming out, etc. See the <tt>cogli1</tt> 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  
# Find a view that you like by moving the configuration around, zooming in, zooming out, etc. See the <tt>cogli1</tt> 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  
#*:<pre>
#:<pre>
#*::u</pre>
#::u</pre>
#: to save the simulation snapshot to a <tt>.cpy</tt> file. An example of a good view for this system is saved in <tt> sample_view.cpy </tt>.
#: to save the simulation snapshot to a <tt>.cpy</tt> file. An example of a good view for this system is saved in <tt> sample_view.cpy </tt>.
#* Now let's export each trajectory frames to <tt>.pov</tt>.
# Now let's export each trajectory frames to <tt>.pov</tt>.
#:<pre>
#:<pre>
#::cogli1 -l view.cpy -t prova.top trajectory.dat</pre>
#::cogli1 -o -l view.cpy -t prova.top trajectory.dat</pre>
#: where <tt> view.cpy</tt> is the name of your view. This will produce as many <tt>.pov</tt> files as there are simulation snapshots, and each of them will generate a movie frame. You can edit any <tt>.pov</tt> 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 :)
#: where <tt> view.cpy</tt> 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 <tt>.pov</tt> files as there are simulation snapshots, and each of them will generate a movie frame. You can edit any <tt>.pov</tt> file if you so wish, replace it with one taken from another view, or with something completely different if you feel like playing Tyler Durden :)
#* 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:
# 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:
#:<pre>
#:<pre>
#:: povray -D +H600 +W860 *.pov</prev>
#:: for d in $(ls -v1 *.pov); do povray -D +H600 +W860 $d; done</pre>
#: 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.
#: 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.
# Encode the povray images in a <tt>.avi</tt> movie with the commands
# Encode the povray images in a <tt>.avi</tt> movie with the commands (use only one of the two mencoder commands)
#:<pre>
#:<pre>
#::ls -1v output_*.png > storyboard.txt
#::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</pre>
#::mencoder mf://@storyboard.txt -mf w=860:h=600:fps=6:type=png -ovc xvid -xvidencopts bitrate=200 -o preview.avi
#::mencoder mf://@storyboard.txt -mf w=860:h=600:fps=6:type=png -ovc lavc -lavcopts vcodec=mpeg4:mbd=2:trell -oac copy -o preview.avi # this usually results in a higher-quality movie
</pre>
#:Notice that the width and height of the movie should be the same as the one used in the povray command above.
#:Notice that the width and height of the movie should be the same as the one used in the povray command above.
#* 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.
#:;w and h
# 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.  
#::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.
#: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 <tt>append</tt> 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.
#:;fps
#::the number of frames per second (here 6).
#:;png
#::the type of image (here png)
#:;ovc
#::the codec used (here  <tt>xvid</tt>)
#:;xvidencopts bitrate
#:: the bitrate, which is an option of the xvid codec (here set to 200)
#:;o
#::the name of the movie file (here <tt>output.avi</tt>).
# 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.
# 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 <tt>append</tt> 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. The encoded video is higher quality for the same amount of disk space if you allow <tt>mencoder</tt> to compress it in time as well as in space. This is done by using the two-pass mode as described below:
#:<pre>
#:<pre>
#:: povray -D +A +H1200 +W1920 *.pov</prev>
#::for d in $(ls -v1 *.pov); do povray -D +A +H1200 +W1920 $d; done
#:and then make the video with
#::ls -1v output_*.png > storyboard.txt
#:<pre>ls -1v output_*.png > storyboard.txt
#::mencoder mf://@storyboard.txt -mf w=1920:h=1200:fps=6:type=png -ovc xvid -xvidencopts pass=1 -o /dev/null
#::mencoder mf://@storyboard.txt -mf w=1920:h=1200:fps=6:type=png -ovc xvid -xvidencopts bitrate=600 -o output.avi</pre>
#::mencoder mf://@storyboard.txt -mf w=1920:h=1200:fps=6:type=png -ovc xvid -xvidencopts pass=2:bitrate=3000 -o output.avi</pre>
# You're video is ready! Enjoy :-D See below how to add a running plot superimposed to the video or above it.
# 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 ==
== 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, we still need to create the <tt>.png</tt> frames as we do in the section above, but
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
#
# create the <tt>.png</tt> 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).
# Create the simulation snapshots with <tt>cogli1</tt> and <tt>povray</tt>. This might take a few minutes or a few days depending on the length and resolution of your movie, so you want to start doing this before anything else. Also before producing the final high-quality simulation snapshots you might want to produce a low-quality-quickly-generated preview, by lowering the resolution.
# Create the plots of the quantity you want to show. Here we'll use gnuplot. This can be done with e.g. the following <tt>gnuplot</tt> commands, which should work in <tt>gnuplot</tt> >=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.
# Create the plots of the quantity with gnuplot. This can be done with e.g. the following <tt>gnuplot</tt> commands ,which should work in <tt>gnuplot</tt> >=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.
#:<pre>
#:<pre>
#::set term png linewidth 4 font 'arial,28pt' size 3200,600
#::set terminal png linewidth 4 font 'arial,28pt' size 3200,600
#::set xrange [25e3:25e3*40]
#::set xrange [25e3:25e3*40]
#::set yrange [-1.42; -1.24]
#::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 }
#::do for [i = 0:39]{ set out 'plot_'.i.'.png';  plot 'energy.dat' every 1000::1000::1000*(i+1) w lp }
</pre>  
</pre>  
notice that several things should be changed here according to your needs, including:  
notice that several things should be changed here according to your needs, including:  
#*the term type (to change the font, resolution, linewidth, etc). The resolution should be compatible with the one of the simulation snapshots
#*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 xrange, to make sure that it fits all the points, just barely).
#* the yrange (to make sure that everything fits as needed)
#*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, etc.).
#*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 <tt> help every</tt> in gnuplot to learn more about that) etc.
#* possibly adding some modifiers before plotting, including title, xlabel, ylabel, etc, a grid, etc.
#*possibly adding some modifiers before plotting, including title, etc.
#* any other edit you might wish to make
#*any other edit you might wish to make
# 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: TO SUPERIMPOSE THE PLOT TO THE SIMULATION SNAPSHOT``
#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:
<pre>
#:<pre>
for i in {0..39}; do convert trajectory.dat_${i}_time_*.png plot_${i}.png -composite output_${i}.png; done
#::TO SUPERIMPOSE THE PLOT TO THE SIMULATION SNAPSHOT``
</pre>
#::for i in {0..39}; do convert trajectory.dat_${i}_time_*.png plot_${i}.png -composite output_${i}.png; done
OR TO APPEND IT ABOVE IT
#::OR TO APPEND IT ABOVE IT
<pre>
#::for i in {0..39}; do convert trajectory.dat_${i}_time_*.png plot_${i}.png -append output_${i}.png; done</pre>
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.
</pre>
swap the order of the plots and the simulations snapshots above to have the plot display below the simulation system instead.
# Convert the png files into a movie, with <tt>mencoder</tt> or <tt>ffmpeg</tt>, as usual, with:
# Convert the png files into a movie, with <tt>mencoder</tt> or <tt>ffmpeg</tt>, as usual, with:
<pre>
<pre>
ls -1v output_*.png > storyboard.txt
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` where the keys mean:
mencoder mf://@storyboard.txt -mf w=3200:h=2400:fps=6:type=png -ovc xvid -xvidencopts bitrate=200 -o output.avi
</pre>
</pre>
#*;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.
#*;fps
#*:the number of frames per second (here 6).
#*;png
#*:the type of image (here png)
#*;ovc
#*:the codec used (here  <tt>xvid</tt>)
#*;xvidencopts bitrate
#*: the bitrate, which is an option of the xvid codec (here set to 200)
#*;o
#*:the name of the movie file (here <tt>output.avi</tt>).


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

Latest revision as of 15:50, 26 November 2016


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://svn.code.sf.net/p/cogli1/code/ 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 prova.top 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.
      -v
      Start the simulation with a centered configuration.
      --always-centre
      Move the system around to keep its center of mass to the centre of the simulation box throughout all the frames
      -m
      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
    u
    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 prova.top 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 completely 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 (use only one of the two mencoder 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
    mencoder mf://@storyboard.txt -mf w=860:h=600:fps=6:type=png -ovc lavc -lavcopts vcodec=mpeg4:mbd=2:trell -oac copy -o preview.avi # this usually results in a higher-quality movie
  1. 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.
    fps
    the number of frames per second (here 6).
    png
    the type of image (here png)
    ovc
    the codec used (here xvid)
    xvidencopts bitrate
    the bitrate, which is an option of the xvid codec (here set to 200)
    o
    the name of the movie file (here output.avi).
  2. 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.
  3. 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. The encoded video is higher quality for the same amount of disk space if you allow mencoder to compress it in time as well as in space. This is done by using the two-pass mode as described below:
    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 pass=1 -o /dev/null
    mencoder mf://@storyboard.txt -mf w=1920:h=1200:fps=6:type=png -ovc xvid -xvidencopts pass=2:bitrate=3000 -o output.avi
  4. 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:
    TO SUPERIMPOSE THE PLOT TO THE SIMULATION SNAPSHOT``
    for i in {0..39}; do convert trajectory.dat_${i}_time_*.png plot_${i}.png -composite output_${i}.png; done
    OR TO APPEND IT ABOVE IT
    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