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

Explain FEniCS's boundary conditions

+2 votes

I'm a grad student with some Python experience looking to learn FEniCS for waveguide simulation. I've gone through the tutorial, looked through some examples, and searched around on the internet for help with my problem. I have a good understanding of boundary conditions in the context of electromagnetic waves, but I'm struggling to figure out what FEniCS is doing.

Here's an example I found online:

import sys
from dolfin import *

## Problem data
E0 = Constant(0.0)
n = Constant(1.0)
k = Constant(50.0)

## Formulation
mesh = UnitSquareMesh(500,500)
V = FunctionSpace(mesh, "Lagrange", 1)

E = TrialFunction(V)
v = TestFunction(V)

# Boundary conditions
point = Point(0.5, 0.5)
f = PointSource(V, point)

def E0_boundary(x, on_boundary):
    return on_boundary

bc = DirichletBC(V, E0, E0_boundary)

# Equation
a = ((k**2 / n**2) * inner(E, v) - inner(nabla_grad(E), nabla_grad(v))) * dx
L = Constant(0.0) * v * dx

# Assemble system
A, rhs = assemble_system(a, L, bc)
f.apply(rhs)

# Solve system
E = Function(V)
E_vec = E.vector()
solve(A, E_vec, rhs)

# Plot and export solution
plot(E)
interactive()

I'm used to normal Python, where things like on_boundary that aren't defined have no meaning and throw up errors. If I hop into iPython and enter the import statements and check to see if it was defined from those, Python tells me on_boundary still isn't defined. I looked through some C++ files to see if something was referenced in some way that made sense, but I didn't come away from it with any useful conclusions. Could someone explain what's going on here?

Cheers,

asked May 29, 2015 by OpticsPhysics FEniCS Novice (180 points)

1 Answer

+2 votes
 
Best answer

E0_boundary(x, on_boundary) here is a callback function that dolfin calls to test if a point is part of a BC or not. x and on_boundary are input (by dolfin) and you have to return True or False if it is part of your boundary. Try a print x, on_boundary statement before return

answered May 29, 2015 by chris_richardson FEniCS Expert (31,740 points)
selected May 30, 2015 by OpticsPhysics

Chris,

Thanks for the help. I'm still not seeing what tells on_boundary whether to be True or False. Does dolfin compare x to the mesh behind the scenes and determine whether or not it's on a boundary? Does it look at DirichletBC, which has V, which has mesh, and determine it from there? I'm also confused that E0_boundary in the bc assignment isn't given any arguments.

Cheers,

Dolfin knows which points or facets are on the exterior, and flags them with on_boundary. E0_boundary is here a reference to a python function, not a call to it.

...