Setting neumann boundary condition for mixed function spaces

Hi, I am trying to solve the wave equation by decomposing the 2nd order wave equation into 2 first-order wave equations and setting up an absorbing boundary condition on 2 of the ends of the domain.

My current version of fenics is 2019.1.0 and I installed it using docker.

from dolfin import *
import numpy as np

mesh = UnitSquareMesh(10, 10)
E1 = FiniteElement("CG", mesh.ufl_cell(), 1)
E2 = FiniteElement("CG", mesh.ufl_cell(), 1)
E = MixedElement([E1, E2])
V = FunctionSpace(mesh, E)

mesh_factor = 1.0
c = 343
dt = mesh_factor * mesh.hmin()
t = dt
T = 1
n_t = int(T/dt)

class initialCondition(UserExpression):
    def eval(self, value, x):
        value[0] = 0.0
        value[1] = 0.0
        
    def value_shape(self):
        return (2,)
    
x0 = interpolate(initialCondition(), V)
u_1, w_1 = x0.split(deepcopy = True)
    
n = int(np.sqrt(np.array(u_1.vector()).size))

class NeumannBoundary(SubDomain):
    def inside(self, x, on_boundary):
        tol = DOLFIN_EPS
        return on_boundary and (abs(x[0]) < tol or abs(1 - x[0]) < tol)

nb = NeumannBoundary()
boundaries = MeshFunction('size_t', mesh, mesh.topology().dim()-1, 0)
nb.mark(boundaries, 0)

(u, w) = TrialFunctions(V)
(v1, v2) = TestFunctions(V)

lhs1 = v1*u*dx - dt*v1*w*dx 
rhs1 = v1*u_1*dx  

lhs2 = v2*w*dx + c*dt*v2*u*ds(subdomain_data = nb) - c*dt*v2*u_1*ds(subdomain_data = nb) + c*c*dt*inner(grad(v2), grad(u))*dx
rhs2 = v2*w_1*dx 

w = Function(V)

while t <= T:
    A = assemble(lhs1 + lhs2)
    b = assemble(rhs1 + rhs2)
    solve(A, w.vector(), b)
    t1, t2 = w.split(deepcopy = True)
    u_1.assign(t1)
    w_1.assign(t2)
    t+=dt

This is the implementation but it is giving the error AttributeError: ‘NeumannBoundary’ object has no attribute ‘ufl_id’. I have referred to several sources but can’t find any solution to this problem. Any help on this matter would be greatly appreciated.

This should be
dt*v2*u*ds(subdomain_data = boundaries, subdomain_id=0)

Please note that using zero for marking is not recommended, i would suggest using a positive non-zero number

Thanks for the reply, I tried that but now it is showing a new error stating
ArityMismatch: Adding expressions with non-matching form arguments (‘v_0’, ‘v_1’) vs (‘v_0’,)

I think this is some dimensional mismatch due to using a mixed-function space

from dolfin import *
import numpy as np

mesh = UnitSquareMesh(10, 10)
E1 = FiniteElement("CG", mesh.ufl_cell(), 2)
E2 = FiniteElement("CG", mesh.ufl_cell(), 2)
E = MixedElement([E1, E2])
V = FunctionSpace(mesh, E)

mesh_factor = 1.0
c = 343
dt = mesh_factor * mesh.hmin()
t = dt
T = 1
n_t = int(T/dt)

class initialCondition(UserExpression):
    def eval(self, value, x):
        value[0] = 0.0
        value[1] = 0.0
        
    def value_shape(self):
        return (2,)
    
x0 = interpolate(initialCondition(), V)
u_1, w_1 = x0.split(deepcopy = True)
    
n = int(np.sqrt(np.array(u_1.vector()).size))

class NeumannBoundary(SubDomain):
    def inside(self, x, on_boundary):
        tol = DOLFIN_EPS
        return on_boundary and (abs(x[0]) < tol or abs(1 - x[0]) < tol)

nb = NeumannBoundary()
boundaries = MeshFunction('size_t', mesh, mesh.topology().dim()-1, 0)
nb.mark(boundaries, 0)

(u, w) = TrialFunctions(V)
(v1, v2) = TestFunctions(V)

lhs1 = v1*u*dx - dt*v1*w*dx 
rhs1 = v1*u_1*dx  

lhs2 = v2*w*dx + c*dt*v2*u*ds(subdomain_data = boundaries, subdomain_id=1) - c*dt*v2*u_1*ds(subdomain_data = boundaries, subdomain_id=1) + c*c*dt*inner(grad(v2), grad(u))*dx
rhs2 = v2*w_1*dx 

w = Function(V)

while t <= T:
    A = assemble(lhs1 + lhs2)
    b = assemble(rhs1 + rhs2)
    solve(A, w.vector(), b)
    t1, t2 = w.split(deepcopy = True)
    u_1.assign(t1)
    w_1.assign(t2)
    t+=dt

This term should be on the RHS

Thanks a lot for the solution ! The code is now working