This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

Integral and boundary condition

+1 vote

Hello all,

I try to implement the solution of the boundary integral of u^2.
So integral_(Neumann boundary) u^2 ds
This is equivalent to || u^2 || ^2_(Neumann boundary)

I want to solve it with the following norm
dolfin.fem.norms.norm(v, norm_type='L2', mesh=None)

So i started

n = 50
mesh = UnitSquareMesh(n,n)
V = FunctionSpace(mesh, "Lagrange, 1")

p = Expression('5.0')
u = Function(V)
u.interpolate(p)

nrm = norm(p, 'L2', mesh)

but this will solve it on the whole mesh.
Is it possible to solve in only on the boundary.

Thanks

asked Dec 22, 2016 by MatheMagie FEniCS Novice (250 points)

wrong comment.

1 Answer

+1 vote

You can code this norm yourself, instead of using the dolfin.norm function. Staying close to your example, this can be done as follows:

from dolfin import *

n = 50
mesh = UnitSquareMesh(n,n)
V = FunctionSpace(mesh, "Lagrange", 1)

p = Expression('5.0')
u = Function(V)
u.interpolate(p)
# Compute L2 norm of u over boundary
bnrm = sqrt(assemble(u*u*ds))
print bnrm
answered Dec 23, 2016 by jmmal FEniCS User (5,890 points)

Thank you.
I want to have 3 out of 4 boundaries to be Dirichlet and the 4th one Neumann.
Is it possible to do it like this ?

from dolfin import *

n = 50
mesh = UnitSquareMesh(n,n)
V = FunctionSpace(mesh, "Lagrange", 1)

p = Expression('5.0')
u = Function(V)
u.interpolate(p)

# Dirichlet boundary
def boundary(x):
    return x[0] < DOLFIN_EPS or x[1] < DOLFIN_EPS or x[1] > 1 - DOLFIN_EPS
u0  = Constant(0.0)
bc  = DirichletBC(V, u0, boundary)

# Compute L2 norm of u over boundary
temp = assemble(u*u*ds)
bc.apply(temp)
bnrm = sqrt(temp)
print bnrm

You want to integrate over the Neumann boundary, right? Then I would use the following approach:

from dolfin import *

class Right(SubDomain):
    def inside(self,x,on_boundary):
        return on_boundary and near(x[0],1) 

n = 50
mesh = UnitSquareMesh(n,n)
V = FunctionSpace(mesh, "Lagrange", 1)

p = Expression('5.0')
u = Function(V)
u.interpolate(p)

boundaries = FacetFunction("size_t", mesh) 
boundaries.set_all(0)

# Now mark your Neumann boundary
rightbound = Right()
rightbound.mark(boundaries, 1)
ds=Measure('ds')[boundaries]

# And integrate over Neumann boundary
bnrm = sqrt(assemble(u*u*ds(1)))
print bnrm

Hope this solves your issue?

double reply sorry

That's right. I want to integrate over the Neumann boundary.
But this is just a short extract of the whole programm in which I already used
the Dirichlet boundary

# Dirichlet boundary
def boundary(x):
    return x[0] < DOLFIN_EPS or x[1] < DOLFIN_EPS or x[1] > 1 - DOLFIN_EPS
u0  = Constant(0.0)
bc  = DirichletBC(V, u0, boundary)

I may try your approach. But is it also possible to solve the problem as I mentioned?

from dolfin import *

n = 50
mesh = UnitSquareMesh(n,n)
V = FunctionSpace(mesh, "Lagrange", 1)

p = Expression('5.0')
u = Function(V)
u.interpolate(p)

# Dirichlet boundary
def boundary(x):
    return x[0] < DOLFIN_EPS or x[1] < DOLFIN_EPS or x[1] > 1 - DOLFIN_EPS
u0  = Constant(0.0)
bc  = DirichletBC(V, u0, boundary)

# Compute L2 norm of u over boundary
temp = assemble(u*u*ds)
bc.apply(temp)
bnrm = sqrt(temp)
print bnrm

Otherwise I need to change the whole programm.
Does the bc.apply() command solve the problem?
(I want to have 3 Dirichlet and 1 Neumann boundary.)

I tried it like this, but i received an error do to the bc.apply(temp)..
Can someone help me with this?

Hi,

I tried to use your solution, but I received the following error error.
"Notation dx[meshfunction] is deprecated. Please use dx(subdomain_data=meshfunction) instead."
Sorry but perhaps it is a stupid question: How do I fix this?
I did it like this:

ds=Measure('ds', domain=mesh, subdomain_data=boundaries)

from dolfin import *

class Right(SubDomain):
    def inside(self,x,on_boundary):
        return on_boundary and near(x[0],1) 

n = 50
mesh = UnitSquareMesh(n,n)
V = FunctionSpace(mesh, "Lagrange", 1)

p = Expression('5.0')
u = Function(V)
u.interpolate(p)

boundaries = FacetFunction("size_t", mesh) 
boundaries.set_all(0)

# Now mark your Neumann boundary
rightbound = Right()
rightbound.mark(boundaries, 1)
ds=Measure('ds', domain=mesh, subdomain_data=boundaries)

# And integrate over Neumann boundary
bnrm = sqrt(assemble(u*u*ds(1)))
print bnrm

Am I right?

...