Interpolation issue with N1curl elements in dolfinx

Hi all,

I’m trying to understand the differences in terms of syntax and workflow between dolfin and dolfinx, since I’d like to use the latter for working with complex numbers.

Now I was trying to implement the dolfinx equivalent of Expression, e.g.:

E = Expression(("0.0", "0.0", "cos(x[0])"), degree=2)

By looking at dolfinx demos (e.g. helmholtz demo) and at other posts in the group, I’ve developed this .py file.

#minimum example

from dolfinx import FunctionSpace, Function, UnitCubeMesh
from ufl import FiniteElement
from mpi4py import MPI 
from numpy import stack, zeros, cos, sin 

mesh = UnitCubeMesh(MPI.COMM_WORLD, 8, 8, 8)

curl_el = FiniteElement("N1curl", mesh.ufl_cell(), 2)
V = FunctionSpace(mesh, curl_el)

def incident_field(x):

    E = stack((zeros(x.shape[1]), zeros(x.shape[1]),

    return E

E = Function(V)

When I run the script, I get the following error:

Traceback (most recent call last):
  File "/usr/local/dolfinx-complex/lib/python3.8/dist-packages/dolfinx/fem/", line 262, in _interpolate
AttributeError: 'function' object has no attribute '_cpp_object'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "", line 19, in <module>
  File "/usr/local/dolfinx-complex/lib/python3.8/dist-packages/dolfinx/fem/", line 270, in interpolate
  File "/usr/lib/python3.8/", line 875, in wrapper
    return dispatch(args[0].__class__)(*args, **kw)
  File "/usr/local/dolfinx-complex/lib/python3.8/dist-packages/dolfinx/fem/", line 264, in _interpolate
RuntimeError: Point dim does not match element dim.

Substituting the command curl_el = FiniteElement("N1curl", mesh.ufl_cell(), 2) with curl_el = FiniteElement("N1curl", mesh.ufl_cell(), 1) does not give me any error, but anyway I’m not understanding why I should do that. Maybe I’m getting confused with the variables involved, but, In general, I think that my main issue is related to the fact that I do not get how to handle the interpolate function when dealing with vectors in dolfinx, since I’ve not found an example as clear as the scalar one.

Could you give me some hints on how to solve the error? Thank you so much.

I’m running the latest version of dolfinx-complex on Docker.

It appears to work ok for me. Some changes to this were merged into master a few days ago. Are you definitely on the latest version, and not one from a few days ago?

You were totally right! I was not using the last version, sorry for my mistake, now it works.

By the way, is there an alternative way to define an expression for a vector in dolfinx? My version (quoted below) seems a bit messy.

I’m relieved that this wasn’t due to the a bug in those recent changes.

You could use the following, although it’s only slightly different to what you already had (I’m using the trick of writing 0 * x[0] to get an array of zeros with the same size as x[0])

    def incident_field(x):

        return (0 * x[0], 0 * x[0], cos(x[0])+1j*sin(x[0]))

    E = Function(V)

Well, this is much nicer than mine, thank you so much! I will definitely use your version!