Dear @French_fries. Please create a reproducible example. Your code is far from reproducible.
For your problem, I would probably use tools such as MeshView
(Abstractions and Automated Algorithms for Mixed Domain Finite Element Methods | ACM Transactions on Mathematical Software, cdaversin (Cécile Daversin-Catty) · GitHub, GitHub - cdaversin/mixed-dimensional-examples: Code to reproduce numerical examples presented in "Abstractions and automated algorithms for mixed-dimensional finite element methods" (2019)), or multiphenics (GitHub - multiphenics/multiphenics: multiphenics - easy prototyping of multiphysics problems in FEniCS).
If you want to do this in another way, you need to assemble the matrices prior to solving, using ident_zeros as you are solving on a subset of the cells (and degrees of freedom), see for instance: Obtain velocity normal to boundary - #2 by dokken
or the minimal reproducible example below:
import dolfin
mesh = dolfin.UnitSquareMesh(10, 10)
V = dolfin.FunctionSpace(mesh, "Lagrange", 1)
uh = dolfin.Function(V)
uh.rename("uh", "")
class OneSide(dolfin.SubDomain):
def inside(self, x, on_boundary):
return x[0] <= 0.5+1e2*dolfin.DOLFIN_EPS
cf = dolfin.MeshFunction("size_t", mesh, mesh.topology().dim(), 0)
OneSide().mark(cf, 1)
u = dolfin.TrialFunction(V)
v = dolfin.TestFunction(V)
dx1 = dolfin.dx(domain=mesh,
subdomain_data=cf, subdomain_id=1)
a = dolfin.inner(u, v)*dx1
x = dolfin.SpatialCoordinate(mesh)
L = dolfin.inner(x[0], v)*dx1
A = dolfin.assemble(a, keep_diagonal=True)
b = dolfin.assemble(L)
A.ident_zeros()
dolfin.solve(A, uh.vector(), b)
class InterFace(dolfin.SubDomain):
def inside(self, x, on_boundary):
return dolfin.near(x[0], 0.5)
mf = dolfin.MeshFunction("size_t", mesh, mesh.topology().dim()-1, 0)
InterFace().mark(mf, 1)
v = dolfin.Function(V)
v.rename("v", "")
bc = dolfin.DirichletBC(V, uh, mf, 1)
bc.apply(v.vector())
with dolfin.XDMFFile(mesh.mpi_comm(), "functions.xdmf") as xdmf:
xdmf.write(uh, 0.0)
xdmf.write(v, 0.0)