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.

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

if on_boundary:<br />

if x[0] == 0 or x[0] == 1:<br />

return True<br />

else:<br />

return False<br />

else:<br />

return False<br />

A more compact implementation reads<br />

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

return on_boundary and (x[0] == 0 or x[0] == 1)<br />

As pointed out already in Section 1.3, testing for an exact match of real numbers<br />

is not good programming practice so we introduce a tolerance in the test:<br />

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

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

return on_boundary and \<br />

(abs(x[0]) < tol or abs(x[0] - 1) < tol)<br />

The second adjustment of our program concerns the definition of L, where<br />

we have to add a boundary integral and a definition of the g function to be<br />

integrated:<br />

g = Expression(’-4*x[1]’)<br />

L = f*v*dx - g*v*ds<br />

The ds variable implies a boundary integral, while dx implies an integral over<br />

the domain Ω. No more modifications are necessary.<br />

The file dn1_p2D.py in the stationary/poisson directory implements this<br />

problem. Running the program verifies the implementation: u equals the exact<br />

solution at all the nodes, regardless of how many elements we use.<br />

1.14 Multiple Dirichlet Conditions<br />

The PDE problem from the previous section applies a function u 0 (x,y) for setting<br />

Dirichlet conditions at two parts of the boundary. Having a single function<br />

to set multiple Dirichlet conditions is seldom possible. The more general case<br />

is to have m functions for setting Dirichlet conditions on m parts of the boundary.<br />

The purpose of this section is to explain how such multiple conditions are<br />

treated in <strong>FEniCS</strong> programs.<br />

Let us return to the case from Section 1.13 and define two separate functions<br />

for the two Dirichlet conditions:<br />

−∇ 2 u = −6 in Ω,<br />

u = u L on Γ 0 ,<br />

u = u R on Γ 1 ,<br />

− ∂u<br />

∂n = g on Γ N .<br />

42

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

Saved successfully!

Ooh no, something went wrong!