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

How do I retrieve the mesh from a FunctionSpace

0 votes

Given a FunctionSpace, is there a way to retrieve the mesh it was built on? FunctionSpace has a member function ufl_domain that returns a Mesh, but this seems to be just a stub that can't actually be used for anything. for instance, I do the following in a jupyter notebook (python 3):

import fenics as fe
import ufl
import matplotlib.pyplot as plt
%matplotlib inline

mesh = fe.UnitIntervalMesh(8)
fe.plot(mesh)

This produces, as expected, a plot of the mesh. Now:

S = fe.FunctionSpace(mesh, 'DG', 2)
mesh_out = S.ufl_domain()
print(str(mesh_out))
fe.plot(mesh_out)

This prints <Mesh #0>, and the attempt to plot throws an exception 'Mesh' object has no attribute 'ufl_domain'. Furthermore, if I poke around inside mesh_out, I find that it contains almost no actual information. This puzzles me, since the FunctionSpace obviously has to know things about the mesh it was built on.

Thanks for any hints.

asked Jun 18, 2017 by lavery FEniCS Novice (350 points)

Have you tried with S.mesh()?

Yup. It throws an exception.

OK, I figured it out. In a jupyter notebook, 'S.mesh()' in a cell by itself throws an exception because FEniCS attempts to produce an HTML representation of the mesh. The method for that is apparently broken. However, if I just do

mesh_out = S.mesh()
fe.plot(mesh_out)

it now works.

Thanks.

...