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

Plotting with respect to time

0 votes

Hi,
I have prepared a code which is about a time-dependent problem. You can see this below:


____________________________
from dolfin import *
mesh = RectangleMesh(Point(0,0), Point(4,2),20,10,'crossed')
V=FunctionSpace(mesh, "Lagrange", 1)
u=Function(V)
v=TestFunction(V)
# Time variables
dt = Constant(0.3); t = float(dt); T = 1.8
g_expr = "1 + x[0]*x[0] + alpha*x[1]*x[1] + beta*t*t"
g = Expression(g_expr, alpha=3.0, beta=1.2, t=0, degree=2)
# Previous and current solution
u0 = interpolate(g, V); u1 = Function(V)
# Variational problem at each time
u = TrialFunction(V)
v = TestFunction(V)
f = Constant(2*1.2 - 2. - 2*3.0)
a = 2*u*v*dx + dt*dt*inner(grad(u), grad(v))*dx
L = (2*1.2+u0)*v*dx - dt*dt*f*v*dx
bc = DirichletBC(V, g, "on_boundary")
while t <= T:
 g.t = t
 solve(a == L, u1, bc)
 u0.assign(u1)
 t += float(dt)
plot(u1, interactive=True)

I want to know how I can get the result (U1 which is displacement) with respect to time. In other word, I am looking for a animation when I run this code to show me the displacements with respect to time. Thanks in advance for your help.

asked Oct 29, 2015 by jafar FEniCS Novice (670 points)
edited Oct 30, 2015 by chris_richardson

4 Answers

+2 votes

I'd recommend saving your results to file (e.g. xdmf)
and using a proper visualusation package, e.g. ParaView

answered Oct 30, 2015 by chris_richardson FEniCS Expert (31,740 points)

Thanks for your response. I installed ParaView on my system. But still do not know how to save and use my results. Here is what I did to create a XDMF file and store my result in this file. I added two lines (indicated bold):


bc = DirichletBC(V, g, "on_boundary")
f = File("results.xdmf")
while t <= T:
g.t = t
solve(a == L, u1, bc)
u0.assign(u1)
plot(u1, interactive=True,rescale=False)
f<<u1
t += float(dt)


I do not know where this XDMF file is saved!

0 votes
  1. Install Paraview
  2. In your code above, in the while loop save the solution for each time step in a vtu or pvd file.
  3. Open that vtu file in paraview and click 'play' on the animation panel for 2D visualisation.
  4. Click 'warp by scalar' , then apply, then 'play' for 3D visualisation.
answered Oct 30, 2015 by Chaitanya_Raj_Goyal FEniCS User (4,150 points)

Thanks for your response. I added two lines (indicated as bold) to my code as below:


bc = DirichletBC(V, g, "on_boundary")
f = File("results.xdmf")
while t <= T:
g.t = t
solve(a == L, u1, bc)
u0.assign(u1)
plot(u1, interactive=True,rescale=False)
f<<u1
t += float(dt)


But I do not know where I can find the "results.xdmf" file. I installed ParaView on my system but still have problem about saving and finding the saved file!

0 votes

Put the plot command within your loop and set rescale=False. You can also define range_max and range_min to fit your data within a scale.
I would plot u instead of u1 because you would solve for u in the loop. Then you can calculate u1 INSIDE the loop and also plot u1 INSIDE the loop.

Bonus: you can also see which time point you are at by creating the title containing t variable as follow:

while t <= T:
 g.t = t
 solve(a == L, u, bc)
 u0.assign(u)
 plot(u, interactive=True, rescale=False, title = 't = ' +str(t))
 t += float(dt)
answered Oct 30, 2015 by lxd FEniCS Novice (290 points)
0 votes

Thank you guys. Finally I could save my code to a pvd file and use it on ParaView. But there is only one question left. Isn't it any other way to get the same animation in FEniCS? Because in FEniCS we have more control over the plot. For example we can see our elements or nodes (by pressing "W") but we are not able to do any of these actions in ParaView. Does anybody know any way to get the animation (displacement with respect to time) in FEniCS?
Thanks!
Jafar

answered Oct 31, 2015 by jafar FEniCS Novice (670 points)

ParaView is indeed able to display all this (and I do not think the FEniCS standard viewer has any feature which ParaView does not have): Make sure you have the Representation Toolbar displayed, this is the one which has "Surface" as its standard setting, and select Surface with edges or Wireframe there.

...