Dolfinx and pygmsh : error when interpolating

Dear all,

I am experiencing issues with gmsh and dolfinx. With pygmsh I construct a square mesh with a hole in the middle, which one I use to construct a function space in dolfinx. However, I get an error when interpolating a function to that space.

Here is a minimal example:

import pygmsh
from mpi4py import MPI
import ufl
import dolfinx
from dolfinx.io import ufl_mesh_from_gmsh
import numpy as np

N, r = 10, 0.1

geom = pygmsh.built_in.Geometry()
circle = geom.add_circle(x0=[0.5, 0.5, 0.0], radius=r, lcar=1./N, num_sections=4, make_surface=False)
geom.add_rectangle(0.0, 1.0, 0.0, 1.0, 0.0, lcar=1./N, holes=[circle.line_loop])
pygmsh_mesh = pygmsh.generate_mesh(geom, geo_filename="h.geo")
cells, x = pygmsh_mesh.cells[-1].data, pygmsh_mesh.points
pygmsh_cell = pygmsh_mesh.cells[-1].type
mesh = dolfinx.mesh.create_mesh(MPI.COMM_SELF, cells, x, ufl_mesh_from_gmsh(pygmsh_cell, x.shape[1]))

P1 = ufl.VectorElement("Lagrange", mesh.ufl_cell(), 1)
V = dolfinx.FunctionSpace(mesh, P1)

def expression(x):
    return np.stack((np.ones(x.shape[1]), np.zeros(x.shape[1])))

u = dolfinx.Function(V)
u.interpolate(expression)

And here is the error that I got when running this code in dolfinx Docker container:

Traceback (most recent call last):
  File "/usr/local/dolfinx-real/lib/python3.8/dist-packages/dolfinx/function.py", line 136, in _interpolate
    self._cpp_object.interpolate(u._cpp_object)
AttributeError: 'function' object has no attribute '_cpp_object'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "minimal_pygmsh.py", line 27, in <module>
    u.interpolate(expression)
  File "/usr/local/dolfinx-real/lib/python3.8/dist-packages/dolfinx/function.py", line 144, in interpolate
    _interpolate(u)
  File "/usr/lib/python3.8/functools.py", line 874, in wrapper
    return dispatch(args[0].__class__)(*args, **kw)
  File "/usr/local/dolfinx-real/lib/python3.8/dist-packages/dolfinx/function.py", line 138, in _interpolate
    self._cpp_object.interpolate(u)
RuntimeError: Values shape is incorrect. (2)

Any help or comments are welcome.

Cheers,
Fabien

The error message Values shape is incorrect is due to the fact that the function create_mesh return a three-dimensional mesh, since pygmsh constructs 3d vertices by default. Replacing this line by:

mesh = dolfinx.mesh.create_mesh(MPI.COMM_SELF, cells, x[:,:2], ufl_mesh_from_gmsh(pygmsh_cell, x.shape[1]))

does the trick.

1 Like