Eigenvalue Problem not satisfying BCs

I’m new to FeniCS and still experimenting. As a toy problem, I am trying to solve a simple eigenvalue problem, with nonzero Dirichlet BCs. The program finds solutions, but they all have zero BCs (and I’m trying to find them with BC = 1). I am for sure doing something wrong but I can’t find what. Thank you!

Here’s my code:

#define mesh and function space
mesh = IntervalMesh(100, -5, 5)
V = FunctionSpace(mesh, ‘Lagrange’, 3)

#define boundary
def boundary(x, on_boundary):
return on_boundary

#apply essential boundary conditions
bc = DirichletBC(V, Constant(1.0), boundary)

#define functions
u = TrialFunction(V)
v = TestFunction(V)

x2 = Expression(‘x[0]*x[0]’, element = V.ufl_element())

#define problem
a = (inner(grad(u), grad(v)) + x2uv)*dx
m = u * v * dx
L_dummy = Constant(0)vdx

A = PETScMatrix()
M = PETScMatrix()

assemble_system(a, L_dummy, bc, A_tensor=A)
assemble_system(m, L_dummy, bc, A_tensor=M)

#create eigensolver
eigensolver = SLEPcEigenSolver(A,M)
eigensolver.parameters[‘spectrum’] = ‘smallest magnitude’
eigensolver.parameters[‘solver’] = ‘lapack’
eigensolver.parameters[‘tolerance’] = 1.e-10

#solve for eigenvalues

first it does not make any sense to compute eigenmodes with non-zero DirichletBC. Second, considering row number i of the eigenvalue system with i corresponding to a DirichletBC dof, you are replacing
A_{ij}u_j = \lambda M_{ij}u_j with u_i=0
since bc.apply replaces row i with a row of zeros and a 1 on the diagonal and bc.zero replaces row i with a full row of zeros. It is then normal to find a solution with zero DirichletBC.