Dolfinx discontinous expression

You could for instance do the following for geometrical search:

import dolfinx
import dolfinx.io

mesh = dolfinx.UnitSquareMesh(dolfinx.MPI.comm_world, 9, 9)
V = dolfinx.FunctionSpace(mesh, ("DG", 0))
v = dolfinx.Function(V)
x = V.tabulate_dof_coordinates()
for i in range(x.shape[0]):
    midpoint = x[i,:]
    if midpoint[0]> 0.5 and midpoint[1]>0.25:
        v.vector.setValueLocal(i, 2)
    else:
        v.vector.setValueLocal(i, 1)

dolfinx.io.XDMFFile(dolfinx.MPI.comm_world, "DG.xdmf").write_checkpoint(v, "v", 0)

and this if the data comes from a MeshFunction

v2 = dolfinx.Function(V)
mf = dolfinx.MeshFunction("size_t", mesh, mesh.topology.dim, 0)
mf.mark(lambda x: numpy.logical_and(x[0] < 0.5, x[1]>0.2), 2)
for i in range(x.shape[0]):
    if mf.values[i] == 2:
        v.vector.setValueLocal(i, 2)
    else:
        v.vector.setValueLocal(i, 1)

dolfinx.io.XDMFFile(dolfinx.MPI.comm_world, "DG_mf.xdmf").write_checkpoint(v, "v", 0)
3 Likes