Plotting 3D elastic model in paraview

Hi I am a new user to with fenics. I have a stupid question. I tried to solve a 3D elasticity problem, and my code runs okay. It’s a cantilever beam problem with force applied. I wanted to plot its bending figure. But when I tried to plot it in my spyder on anaconda, it failed. Also, I tried to save it and plotted it with paraview. It failed too. The displacement in the function is “u”. The code is show below:

``````from __future__ import print_function
from dolfin import *
from fenics import *
import matplotlib.pyplot as plt
import math
import ufl
from mpl_toolkits import mplot3d
import numpy as np
from mpl_toolkits.mplot3d import Axes3D

L = 25.
H = 1.
T = 1.
Nx = 25
Ny = 5
Nz = 5
mesh = BoxMesh(Point(0., 0., 0.), Point(L, H, T), Nx, Ny, Nz)

def eps(v):

E = Constant(1e5)
nu = Constant(0.3)

lmbda = E*nu/(1+nu)/(1-2*nu)
mu = E/2/(1+nu)
lmbda = 2*mu*lmbda/(lmbda+2*mu)

def sigma(v):
return lmbda*tr(eps(v))*Identity(3) + 2.0*mu*eps(v)

rho_g = 1e-3
f = Constant((0, -rho_g, 0))
V = VectorFunctionSpace(mesh, "CG", degree = 1)

du = TrialFunction(V)
u_ = TestFunction(V)
a = inner(sigma(du), eps(u_))*dx
l = inner(f, u_)*dx

def left(x, on_boundary):
return near(x[0], 0.)

bc = DirichletBC(V, Constant((0., 0., 0.)), left)

u = Function(V, name="Displacement")
solve(a == l, u, bc)

plot(u)
``````

The saving codes are:

``````file = XDMFFile("output.xdmf")
file.write(u, 0)
``````

I also tried .pvd, but it failed too. I hope someone could help me out with this hard problem. Thanks a lot.

Could you post the error you get while plotting, and which version of `dolfin` are you using? Your code runs fine and simply doing

``````XDMFFile("test.xdmf").write(u)
``````

works just fine in paraview !

2 Likes

Also, for simple 3D problems you could also use vtkplotter, namely, for your example

``````from dolfin import *
from vtkplotter.dolfin import plot as vplot
# code here to write solution in the function `u`
vplot(u)
``````

There is an extensive set of examples in `vtkplotter` and specifically in `vtkplotter.dolfin`. But for larger, more complicated problems `paraview` would be better.

1 Like

Thanks a lot for your help. Sorry for the late reply. My dolfin version is 2019.1.0. And my paraview is 5.8.0. Is that okay to do the plot?

Hi I just made it out! the vtkplot works for me! Thank you so much!

1 Like

There is a bug in Paraview 5.8.0, see: Error in opening 3D plots in fenics and .pvd files in Paraview 5.8.0 in Ubuntu 19.04

1 Like

Yes this should work. I have paraview 5.6 on my windows machine but I expect this to work on `5.8` too unless it didn’t install properly.
Also just for completeness:

``````from dolfin import *
``````

is the same as

``````from fenics import *
``````

and you may need to explicitly import `ufl` only in very selected cases. For the most part importing `dolfin` with wildcards should work. Also, `dolfin's` plot would work fine for `2D` functions as is but not `3D`.

1 Like

I see. Thank you for your help!