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

Bug with multiple boundary conditions and mark

+2 votes

Hi all,

I have encountered a problem when solving a pde due to marking of the boundaries which seems like a bug, you can judge by yourself: the pde is similar to http://fenicsproject.org/documentation/tutorial/materials.html
but with Dirichlet and Neumann boundary conditions.

The pde is of the type:
$$\nabla\cdot(\sigma(x,y)\nabla u(x,y))=0$$
with $\sigma(x,y) = 1$ in $\Omega_0$ and $\sigma(x,y)$ = 10 in $\Omega_1$, and the union of $\Omega_0$ and $\Omega_1$ is the unit square. $\Omega_1$ is defined as a ball of center (0.3,0.5) and radius 0.2 .

The boundary is split in 5 parts: top, right, bottom, top left and bottom left. I mark the boundaries in the following way:
top is marked as 3
right is marked as 4
bottom is marked as 5
top left is marked as 6
bottom left is marked as 7

Then I put Dirichlet conditions on the boundary "top left" and nonhomogeneous Neumann conditions on all other boundaries.

If I use the code corresponding to what I just described, then it works fine. If on the other hand I replace the mark 7 for bottom left by the mark 2 then I get a solution which is equal to zero everywhere! So it seems I cannot mark a boundary with the number 2.

If you want to try it yourself you can find the code below. The one I am posting is working well. Then replace 7 by 2 in

leftbot.mark(  boundaries, 7)

and in

  g*v*ds(7)

at the end of the code and you should get a solution equal to zero everywhere which is not correct (equal to zero).

from dolfin import *
import numpy as np
import pdb
#----------
# Some data
#----------
N      = 31
sigma0 = 1 
sigma1 = 10
#----------------------
# Define mesh for Omega
#----------------------
mesh = RectangleMesh(0, 0, 1, 1, N, N)
V    = FunctionSpace(mesh, "Lagrange", 1)
phi  = Function(V)
#------------------------
# Omega = Omega1 + Omega0
#------------------------
psi = Expression('(x[0]-0.3)*(x[0]-0.3) + (x[1]-0.5)*(x[1]-0.5) - 0.2*0.2')
phi = project(psi,V)

class Omega1(SubDomain):
    def inside(self, x, on_boundary):
        return True if x[0]>=0 and x[0]<=1 and x[1]>=0 and x[1]<=1 and phi(x) < 0 else False

class Top(SubDomain):
    def inside(self, x, on_boundary):
        tol = DOLFIN_EPS  
        return on_boundary and  abs(x[1]-1.0) < tol                          
class Right(SubDomain):
    def inside(self, x, on_boundary):
        tol = DOLFIN_EPS   
        return on_boundary and  abs(x[0]-1.0)< tol
class Bottom(SubDomain):
    def inside(self, x, on_boundary):
        tol = DOLFIN_EPS   
        return on_boundary and  abs(x[1]) < tol
class LeftTop(SubDomain):
    def inside(self, x, on_boundary):
        tol = DOLFIN_EPS   
        return on_boundary and x[1]> 0.5 and abs(x[0]) < tol            
class LeftBot(SubDomain):
    def inside(self, x, on_boundary):
        tol = DOLFIN_EPS   
        return on_boundary and x[1]<= 0.5  and abs(x[0]) < tol
#--------------------------------
# Initialize sub-domain instances
#--------------------------------
lefttop   = LeftTop()
leftbot   = LeftBot()
top       = Top()
right     = Right()
bottom    = Bottom()
omega1    = Omega1()
#----------------------------------------------
# Initialize mesh function for boundary domains
#----------------------------------------------
boundaries = FacetFunction("uint", mesh)
domains    = CellFunction("uint", mesh)
domains.set_all(0)
omega1.mark(domains, 1)

top.mark(      boundaries, 3)
right.mark(    boundaries, 4)
bottom.mark(   boundaries, 5)
lefttop.mark(  boundaries, 6)
leftbot.mark(  boundaries, 7)

dx = Measure("dx")[domains]
ds = Measure("ds")[boundaries]
#--------------------
# Boundary conditions
#--------------------
f0  = Expression("0")
f1  = Expression("0")
g  = Expression("1")
bcs = [DirichletBC(V, 0.0, boundaries, 6) ]
#----------
# Solve pde
#----------
y  = TrialFunction(V)
v  = TestFunction(V)
a  = sigma0*inner(grad(y), grad(v))*dx(0) + sigma1*inner(grad(y), grad(v))*dx(1)
L  = f0*v*dx(0) + f1*v*dx(1) + g*v*ds(3) + g*v*ds(4) + g*v*ds(5) + g*v*ds(7) 
y  = Function(V)
solve(a == L, y, bcs)
#--------------
# Plot solution
#--------------
plot(y)
interactive()
asked Jul 10, 2013 by Antoine FEniCS Novice (810 points)
edited Jul 10, 2013 by Antoine

Nobody can try this code without messing up with indentation and stars. You need to use code highlighting to enable us to copy and paste verbatim code. Please, also use $\LaTeX$ to write math.

Ok sorry for that, now you can copy paste the code.

I can't reproduce it with DOLFIN 1.1.x which I guess you use from the syntax. I get same non-trivial solution for both cases.

But if you're using DOLFIN 1.0.x you must initialize boundaries by say

boundaries.set_all(0)

because in 1.0.x non-initialized MeshFunctions takes random values. Then on some interior facet it can have value 6 and Dirichlet condition is applied there.

Try also

plot(boundaries, interactive=True)

to see that one exterior facet is not set.

Actually I just moved from Dolfin 1.0.0 to Dolfin 1.2.0 and my codes that were working fine now don't. I've added

boundaries.set_all(0)

but it still does not work. How do you see that one exterior facet is not initialized?

One facet in the middle of the left boundary has value 0.

boundaries.set_all(0) is not necessary since 1.2.0.

You need to change 'uint' to 'size_t' since 1.2.0.

I can't reproduce your problem with 1.2.0 either.

1 Answer

0 votes
 
Best answer

I can't reproduce it in 1.1.0 or 1.2.0 either.

I once had a problem with the order in which I marked the boundaries in the program, which I resolved by reordering the lines. I wouldn't think that the values for the marks would affect things...

answered Jul 10, 2013 by mwelland FEniCS User (8,410 points)
selected Jul 19, 2013 by Jan Blechta

That's strange cause I still have the same problem, I get a trivial solution when I mark with the number two. So it seems the problem is only on my computer, maybe I have a problem with the installation of Fenics?

Strange. If you want, post the code with the numbers switched (that gives the trivial solution) and I'll run it verbatim...

...