This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

How to make a video of a sequence of FEM functions?

0 votes

Starting with

from dolfin import *
mesh = UnitSquareMesh(nx, ny)
V = FunctionSpace(mesh, "CG", 1)

I do some processing and obtain a list u of length 20 (where each member of this list is an object of class Function(V) and has been obtained as the solution to a certain boundary-value problem on mesh).

I would like to make an animation which:

  1. plots (a 2-dimensional view of) u[0]
  2. holds for 0.5 seconds
  3. hides u[0] and plots (a 2-dimensional view of) u[1]
  4. holds for 0.5 seconds
  5. etc.

I have tried using the plot command inside a loop, but then I have to indivually close each window by hand and each new plot pops up at a different location on the screen.

Instead, I would like to produce an animation in one window only; also, I would like to store this animation for later use. Could someone propose a way of accomplishing these tasks? (I would prefer a solution that does not require any external software, but any suggested solutions would be welcome!)

asked Jun 11, 2015 by kdma-at-dtu FEniCS Novice (590 points)

Hi!

Concerning the multiple windows issue, you can use the plot command 'key' argument like this:

plot(u[0], key='u0')

If you put it inside a loop you'll see the succession of plots of u[0] in a single window. Then to create the animation I'm afraid I can't help you but if you can time it correctly you may be able to record it thanks to some software recording the display from the gpu of the computer... Still, there must be some way of doing it with a Python code but I don't know how.

1 Answer

0 votes

Hi,

You can export your solution in Paraview

fid = File("solution.pvd")
for i in range(0, ntimesteps):
     t = i *dt
     # compute u at time t by solving the PDE
     u.rename("u", "u") #see the QA reported below.
     fid << u, t

Then creating the video in paraview is very easy.

https://www.youtube.com/watch?v=NFnw1AGoh_s

Also, look at this QA for some issues regarding exporting in paraview:
http://fenicsproject.org/qa/7334/bug-in-how-vtk-files-are-written-in-fenics-1-5-0?show=7334#q7334

answered Jun 11, 2015 by umberto FEniCS User (6,440 points)

@umberto: Thanks for the suggestion.

Because I store my data in a list u of length 20, I tried out the following modification of your code:

fid = File("solution.pvd")

for i in range(0, 20):
    u[i].rename("u[i]", "u[i]")
    fid << u[i], i

Then, upon opening solution.pvd in Paraview, I clicked "Apply" and then "Play" which produced a movie as desired.

However, since https://www.youtube.com/watch?v=NFnw1AGoh_s makes an animation of a 3D object and since my goal was to make a frame-by-frame animation of a 2D object, I modified your code rather blindly...

In particular, even though I got the desired result, I don't quite understand what purpose the renaming serves and how it should be best done - could you kindly advise me about that?

Because you may be creating a new Function object on each timestep, they will be given different names by dolfin, and saved as different time series. For the data to appear in the same time series, they must have the same 'name'.

@chris_richardson: Thanks for your comment.

I also came across http://fenicsproject.org/qa/1382/how-to-name-the-variables-when-writing-to-vtk-file and it appears that, if I change the code from my post on June 12 by replacing

u[i].rename("u[i]", "u[i]")

with

u[i].rename("something", "label")

then, upon opening fid in ParaView, "something" is the name for my time series displayed by ParaView.

May I ask what the purpose of "label" is? (I couldn't quite gather that from http://fenicsproject.org/qa/1382/how-to-name-the-variables-when-writing-to-vtk-file.)

...