Unable to properly locate dofs

Hi,
I just started using dolfinx very recently so there might be some stuff that I don’t understand yet.
I am defining a circular 1D mesh embedded in 2D space through Gmsh as follows:

import dolfinx
import gmsh
import numpy as np
from mpi4py import MPI
from dolfinx.io import extract_gmsh_geometry, extract_gmsh_topology_and_markers, \
                       ufl_mesh_from_gmsh
from dolfinx.mesh import create_mesh

def gmsh_to_dolfin(model, gdim, tdim):
    # Get mesh geometry
    geometry_data = extract_gmsh_geometry(model)
    # Get mesh topology foreach element
    topology_data = extract_gmsh_topology_and_markers(model)

    gmsh_cell_type = list(topology_data.keys())[0]    
    properties = gmsh.model.mesh.getElementProperties(gmsh_cell_type)
    name, dim, order, num_nodes, local_coords, _ = properties
    cells = topology_data[gmsh_cell_type]["topology"]
    cell_id, num_nodes = MPI.COMM_WORLD.bcast([gmsh_cell_type, num_nodes], root=0)

    ufl_domain = ufl_mesh_from_gmsh(cell_id, 2)
    # Create distributed mesh
    return create_mesh(MPI.COMM_WORLD, cells, geometry_data[:, :gdim], ufl_domain)

gmsh.initialize()
circle = gmsh.model.occ.addCircle(0, 0, 0, 1)
gmsh.model.occ.synchronize()
gdim = 2
tdim = 1
status = gmsh.model.addPhysicalGroup(tdim, [circle], 1)
h = 0.5
gmsh.option.setNumber("Mesh.CharacteristicLengthMin", h)
gmsh.option.setNumber("Mesh.CharacteristicLengthMax", h)
gmsh.model.mesh.generate(gdim)

mesh = gmsh_to_dolfin(gmsh.model, gdim, tdim)

gmsh.finalize()

import matplotlib.pyplot as plt
coor = mesh.geometry.x
plt.plot(coor[:, 0], coor[:, 1], "o")
plt.show()

This works OK, I have a total of 13 nodes and 13 cells.
Now, I would like to define a VectorFunctionSpace of dim=2 and apply DirichletBC to all the dofs corresponding to V.sub(0). When I do the following, I don’t get all the dofs that I expect:

V = dolfinx.VectorFunctionSpace(mesh, ("CG", 1), dim=2)
V0 = V.sub(0).collapse()

dofs = dolfinx.fem.locate_dofs_geometrical((V.sub(0), V0), lambda x: True)
assert len(dofs[0])==13, "Wrong number of dofs"
1 Like

Note that if I change the lambda function to lambda x: abs(x[0]) >= 0 it works although the condition should always return True… Is there something I am missing ?

Hi Jeremy,

Note that the input to the lambda function is an array, and therefore expects and array output:

dofs = dolfinx.fem.locate_dofs_geometrical((V.sub(0), V0), lambda x: np.full(x.shape[1], True))
print(dofs, len(dofs))
1 Like

Thanks Jorgen. Indeed, it didn’t see that it required an array output.