Interpolate discontinuous function on DG1

Hi all! I would like to interpolate a discontinuous onto a DG1 space:

import dolfinx
import mpi4py.MPI as MPI
import numpy as np

mesh = dolfinx.mesh.create_unit_square(MPI.COMM_WORLD, 10, 10)

Q1 = dolfinx.fem.functionspace(mesh, ("DG", 1))


u = dolfinx.fem.Function(Q1)
u.interpolate(lambda x: np.where(x[1] > 0.5 - 1e-10, 1, 2))

dolfinx.io.VTXWriter(MPI.COMM_WORLD, "u.bp", u, "BP5").write(0.0)

However this is what I obtain:

Is there a way to interpolate/project a discontinuous function like this?

If you instead interpolate onto a subset of cells (use locate_entities with x[1] > 0.5 - 1e-10
you can do as shown in
http://jsdokken.com/FEniCS-workshop/src/approximations.html#interpolate-expressions

Thanks for the reply!

Now I’m trying to do the same on DG4 but VTX doesn’t support this, correct?

VTX supports DG-4:) what issue are you getting?

Nevermind, it was an issue on my side. For some reason when creating a DG space and then calling family_name it returns "P" and not "DG". Which is why it was interpolating on a continuous space…

import dolfinx
import mpi4py.MPI as MPI

mesh = dolfinx.mesh.create_unit_square(MPI.COMM_WORLD, 10, 10)
Q1 = dolfinx.fem.functionspace(mesh, ("DG", 1))
print(Q1.ufl_element().family_name)  # returns P