Exporting a Video of a Simulation of Pendulum Motion - Mathematica Stack Exchange most recent 30 from mathematica.stackexchange.com 2019-10-16T20:31:35Z https://mathematica.stackexchange.com/feeds/question/21384 https://creativecommons.org/licenses/by-sa/4.0/rdf https://mathematica.stackexchange.com/q/21384 5 Exporting a Video of a Simulation of Pendulum Motion Vincent Tjeng https://mathematica.stackexchange.com/users/5603 2013-03-15T07:59:00Z 2013-04-02T10:41:22Z <p>In a series of previous questions, I asked how to <a href="https://mathematica.stackexchange.com/questions/21035/running-a-numerical-simulation-on-a-system-of-differential-equations-with-unique">solve a series of differential equations</a> describing a series of coupled pendulums, and then how to <a href="https://mathematica.stackexchange.com/questions/21130/coloring-points-in-a-plot-based-on-their-position-in-the-list">plot this data by coloring the different pendulums</a>. Using the excellent answers from these two questions, I was able to output a 3-dimensional view of how the pendulums swing.</p> <p><img src="https://i.stack.imgur.com/TuKVd.png" alt="Simulation of Pendulums"></p> <p>My system's graphics card is pretty outdated, so using Szabolcs' <a href="https://mathematica.stackexchange.com/questions/198/antialiasing-in-3d/200#200">poor man's antialiasing</a>, I attempted to output a movie of the pendulums' motion. </p> <p>To begin with, my code to generate the individual 3-dimensional views at a given point of time is shown below.</p> <pre><code>funcposition = Table[{Sin[# + i], 1}, {i, 0, n, 1}] &amp;; (*the position function of each pendulum, as a function of time. note - this is a dummy variable; you can see the explanation below of why I used a dummy variable. The variable # is the time*) connectionpoints = Transpose[{Table[i, {i, 0, n}], Table[0, {i, 0, n}], Table[0, {i, 0, n}]}]; (*the points where the balls are connected to the supporting stick*) supportstick = {Black, Thick, Line[{{0, 0, 0}, {n, 0, 0}}]}; (*the thick black supporting stick*) toPolar = {#2 Cos[# - \[Pi]/2], #2 Sin[# - \[Pi]/2]}\[Transpose] &amp; @@ (#\[Transpose]) &amp;; (*Mr Wizard's method for coloring the points and producing the output here*) colors = ColorData["Rainbow"] /@ Rescale@Range@Length@funcposition; (*A three-dimensional plot of the data, including the antialiasing*) antialiasedthreedpendulumviewer = ImageResize[ Rasterize[ Graphics3D[{PointSize[Large], Point[Transpose[ Prepend[Transpose[toPolar@funcposition[#1]], Range[n + 1] - 1]], VertexColors -&gt; colors]}~Join~ supportstick~Join~{Black, Thin}~ Join~(Line /@ Partition[ Riffle[Transpose[ Prepend[Transpose[toPolar@funcposition[#1]], Range[n + 1] - 1]], connectionpoints], 2]), PlotRange -&gt; {{-1, n + 1}, {-1, 1}, {-1, 1}}, Axes -&gt; True, ViewPoint -&gt; #2, ImageSize -&gt; Large], "Image", ImageResolution -&gt; 8*72], Scaled[1/4]] &amp;; </code></pre> <p>To export the images as a video, I then apply the following:</p> <pre><code>starttime=0; endtime=0.5; timestep=0.005 perspective={-0.87, 0.25, 0} tableofimages = Table[antialiasedthreedpendulumviewer[tdummy, perspective], {tdummy, starttime, endtime, timestep}]; Export["animation.flv", tableofimages] </code></pre> <p>However, this process is very slow, and producing even <a href="http://youtu.be/G9heHCWyvc4" rel="nofollow noreferrer">ten frames</a> took me three minutes.</p> <p>My <strong>question</strong> is - how do we improve the performance of the exporting process? As a side question, how would we reduce the file size of the file exported?</p> <p><strong>Note:</strong> The actual motion of the coupled pendulums is replaced with a toy function in this running example above, as I don't think that the evaluation of the solution is the key rate-determining step for the exporting of the video in the simulation. The actual code that I am running involves solving the ODE first and also replace the <code>funcposition</code> with a substitution of the functions' solution.</p> <pre><code>m = 0.1; l = 0.2; b = 1; mu = 1; k = 10; eta = 0.2; g = 0.2; a = 1; g = 9.81; n = 20; tmax = 20; funch = (1 - eta/(2 (1 - Cos[#1 - #2]) + (g/a)^2)^0.5) &amp;; funcf = # + Abs[#] &amp;; tau = Sin[#1 - #2]*k*l^2*funcf[funch[#1, #2]] &amp;; node = b*m*l^2* D[theta[#][t], {t, 2}] == -g*m*mu*l* Sin[theta[#][t]] + tau[theta[# - 1][t], theta[#][t]]* HeavisidePi[(#*(1 + n*\$MachineEpsilon) - 1)/n - 0.5] + tau[ theta[# + 1][t], theta[#][t]]* HeavisidePi[(#*(1 - n*\$MachineEpsilon) + 1)/n - 0.5] &amp;; (*machine epsilon bit is to make side pendula only affected by \ themselves*) initialposition = theta[#] == 0 &amp;; initialvelocity = theta[#]' == 0 &amp;; initialconditions = {theta == 0, theta' == 50, theta' == 45, theta' == 40, theta' == 35, theta' == 30, theta' == 25}~Join~ Table[initialposition[i], {i, 1, n}]~Join~ Table[initialvelocity[i], {i, 6, n}]; (*initial conditions defined as above*) equations = Table[node[i], {i, 0, n}]; system = equations~Join~initialconditions; functions = Table[theta[i][t], {i, 0, n}]; solution = NDSolve[system, functions, {t, 0, tmax}, MaxSteps -&gt; 10000*n*tmax]; funcposition = Table[{(Evaluate[theta[i][t] /.solution] /.t -&gt; #)[], 1}, {i, 0, n, 1}] &amp;; </code></pre> https://mathematica.stackexchange.com/questions/21384/-/22497#22497 5 Answer by Albert Retey for Exporting a Video of a Simulation of Pendulum Motion Albert Retey https://mathematica.stackexchange.com/users/169 2013-04-02T10:35:29Z 2013-04-02T10:41:22Z <p>As some simple comparisons confirm, the main time in creating the images is spent in the <code>Rasterize</code> function and that time has -- not to much surprise -- a quadratical dependence on the image resolution. </p> <p>I have not tested it, but I guess that the <code>Export</code> behaves similar, and of course also the final file size and intermediate memory usage depends on the resolution used. </p> <p>It turns out that the OP didn't really need the very high resolution originally defined with <code>ImageResolution-&gt;8*72</code>. With a lower resolution of e.g. <code>2*72</code> the plots already look very nice and the creation and export of the rasterized images is done in reasonable time. </p> <p>If one really needs very high resolutions one needs to pay a price, and it probably is a good idea to initially use low resolutions to get everything running correctly and only in a last step increase the resolution to the desired value to produce the final result once.</p> <p>Mathematicas export functions for movies are probably not optimized for such very high resolution cases so that it could well be that an export won't work due to extensive memory allocations. In such cases one could try to export just single frames and use external programs to convert these single frames into a high resolution video, some possibilities are described in the answers to these questions: <a href="https://mathematica.stackexchange.com/a/4903/169">variable frame rate</a>, <a href="https://mathematica.stackexchange.com/q/4232/169">save as mp4</a>. There are some other tricks that are good to know when trying to export videos in the answers to these questions: <a href="https://mathematica.stackexchange.com/q/1428/169">prerender animation</a>, <a href="https://mathematica.stackexchange.com/q/6914/169">export to mov</a> .</p>