19.06.2015 Views

A FEniCS Tutorial - FEniCS Project

A FEniCS Tutorial - FEniCS Project

A FEniCS Tutorial - FEniCS Project

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

from the weak form. We then only need to take care of Dirichlet conditions at<br />

two sides:<br />

tol = 1E-14 # tolerance for coordinate comparisons<br />

def Dirichlet_boundary0(x, on_boundary):<br />

return on_boundary and abs(x[0]) < tol<br />

def Dirichlet_boundary1(x, on_boundary):<br />

return on_boundary and abs(x[0] - 1) < tol<br />

bc0 = DirichletBC(V, Constant(0), Dirichlet_boundary0)<br />

bc1 = DirichletBC(V, Constant(1), Dirichlet_boundary1)<br />

bcs = [bc0, bc1]<br />

Note that this code is independent of the number of space dimensions. So are<br />

the statements defining and solving the variational problem:<br />

u = TrialFunction(V)<br />

v = TestFunction(V)<br />

f = Constant(-2)<br />

a = inner(nabla_grad(u), nabla_grad(v))*dx<br />

L = f*v*dx<br />

u = Function(V)<br />

solve(a == L, u, bcs)<br />

The complete code is found in the file paD.py (Poisson problem in ”anyD”).<br />

If we want to parameterize the direction in which u varies, say by the space<br />

direction number e, we only need to replace x[0] in the code by x[e]. The<br />

parameter e could be given as a second command-line argument. The reader is<br />

encouraged to perform this modification.<br />

2 Nonlinear Problems<br />

Now we shall address how to solve nonlinear PDEs in <strong>FEniCS</strong>. Our sample PDE<br />

for implementation is taken as a nonlinear Poisson equation:<br />

−∇·(q(u)∇u) = f . (38)<br />

The coefficient q(u) makes the equation nonlinear (unless q(u) is constant in u).<br />

To be able to easily verify our implementation, we choose the domain, q(u),<br />

f, and the boundary conditions such that we have a simple, exact solution u.<br />

Let Ω be the unit hypercube [0,1] d in d dimensions, q(u) = (1 + u) m , f = 0,<br />

u = 0 for x 0 = 0, u = 1 for x 0 = 1, and ∂u/∂n = 0 at all other boundaries<br />

x i = 0 and x i = 1, i = 1,...,d−1. The coordinates are now represented by the<br />

symbols x 0 ,...,x d−1 . The exact solution is then<br />

u(x 0 ,...,x d ) = ( (2 m+1 −1)x 0 +1 ) 1/(m+1)<br />

−1. (39)<br />

We refer to Section 1.16 for details on formulating a PDE problem in d space<br />

dimensions.<br />

48

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!