1
$\begingroup$

I am going to develop an unsteady diffusion calculation in Mathematica. Because it is unsteady, I would like to visualize the solution each time step (or in some time steps). My MATLAB skills are much better than Mathematica, so I am going to give the example from there. I looked at some of the posts about that, but they were for small number of time steps like 10, or saving each plot and creating a movie. But in MATLAB I can do

surf(x,z,U_sol) F=getframe; 

x and z are the position vectors and U_sol is the solution matrix. GetFrame simply preserves frame but not the plot, so it looks like a continuous plotting, like a movie.

In Matlab, you can do something like this to save the movie directly. This is before time steps

fid=figure; pos=get(fid,'position'); writerObj=VideoWriter('sol.avi'); writerObj.FrameRate=10; open(writerObj); 

and inside the time loop

 PLOT... frame=getframe(gcf); writeVideo(writerObj,frame) 

and at the end of the time loop.

 close (writerObj) 

and you will have the movie file without dealing with each frame. There should be some equivalent version in Mathematica.

$\endgroup$
5
  • $\begingroup$ I guess you want ListPlot3D[] (or Plot3D[] if you're using NDSolve[] for the PDE), Animate[], and/or Export[]... $\endgroup$ Commented Dec 27, 2016 at 13:16
  • $\begingroup$ mathematica can not Export on a frame by frame basis, you need to compile all frames as a list and Export all at once. $\endgroup$ Commented Dec 27, 2016 at 13:48
  • $\begingroup$ I am not using NDSolve. I use my own code and every time step I create a solution which I want continuously plot without recreating the frame and ideally save them while plotting them. But it seems I need to save each frame but that is really inefficient if you have 10000 or more steps. $\endgroup$ Commented Dec 27, 2016 at 17:13
  • $\begingroup$ if you want to watch the frames as the solution proceeds have a look at Monitor $\endgroup$ Commented Dec 27, 2016 at 20:25
  • $\begingroup$ If you are on Windows you can use my MathMF package to write frames one by one to a video stream. $\endgroup$ Commented Dec 27, 2016 at 22:24

1 Answer 1

6
$\begingroup$

Mathematica can't build a writer object (in fact it doesn't have proper objects at all, excluding the front-end) but you can always build your snapshots in a temporary directory. Give this a go:

movieDir = CreateDirectory[]; getFrame[plot_, tag_] := Export[FileNameJoin@{movieDir, ToString[tag] <> ".png"}, plot]; getImages[] := Import /@ SortBy[FileNames["*.png", movieDir], ToExpression@FileBaseName@# &]; createMovie[file_] := With[{images = getImages[]}, With[{dims = ImageDimensions@First@images}, Export[file, ImageResize[ColorConvert[#, "RGB"], dims] & /@ images ] ] ]; 

Note the manipulations done to the images in createMovie. That's to get around the issues mentioned here.

Then we can build our plots and images:

In[458]:= (Do[ getFrame[Plot[Sin[s*x], {x, 0, 2 \[Pi]}, PlotRange -> {-1, 1}], IntegerPart[10*s]], {s, 0, 2, .1} ]; createMovie["~/Desktop/sol.gif"]) // AbsoluteTiming Out[458]= {3.2397, "~/Desktop/sol.gif"} 

And what we get is this (note that .avi works too, I just used .gif so I could embed it here):

visualization gif

$\endgroup$
1
  • $\begingroup$ Thank you ..this is what I was trying to do. $\endgroup$ Commented Dec 28, 2016 at 17:08

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.