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 boundary(x, on_boundary):<br />

return on_boundary<br />

bc = DirichletBC(V, Constant(0.0), boundary)<br />

omega = 1.0<br />

u_e = Expression(’sin(omega*pi*x[0])*sin(omega*pi*x[1])’,<br />

omega=omega)<br />

f = 2*pi**2*omega**2*u_e<br />

The computation of (∫ Ω (u e −u) 2 dx ) 1/2<br />

can be done by<br />

error = (u - u_e)**2*dx<br />

E = sqrt(assemble(error))<br />

Here, u_e will be interpolated onto the function space V. This implies that the<br />

exact solution used in the integral will vary linearly over the cells, and not as a<br />

sine function, if V corresponds to linear Lagrange elements. This situation may<br />

yield a smaller error u - u_e than what is actually true.<br />

More accurate representation of the exact solution is easily achieved by interpolating<br />

the formula onto a space defined by higher-order elements, say of<br />

third degree:<br />

Ve = FunctionSpace(mesh, ’Lagrange’, degree=3)<br />

u_e_Ve = interpolate(u_e, Ve)<br />

error = (u - u_e_Ve)**2*dx<br />

E = sqrt(assemble(error))<br />

To achieve complete mathematical control of which function space the computations<br />

are carried out in, we can explicitly interpolate u to the same space:<br />

u_Ve = interpolate(u, Ve)<br />

error = (u_Ve - u_e_Ve)**2*dx<br />

The square in the expression for error will be expanded and lead to a<br />

lot of terms that almost cancel when the error is small, with the potential of<br />

introducing significant round-off errors. The function errornorm is available<br />

for avoiding this effect by first interpolating u and u_e to a space with higherorder<br />

elements, then subtracting the degrees of freedom, and then performing<br />

the integration of the error field. The usage is simple:<br />

E = errornorm(u_e, u, normtype=’L2’, degree=3)<br />

It is illustrative to look at the short implementation of errornorm:<br />

def errornorm(u_e, u, Ve):<br />

u_Ve = interpolate(u, Ve)<br />

u_e_Ve = interpolate(u_e, Ve)<br />

e_Ve = Function(Ve)<br />

# Subtract degrees of freedom for the error field<br />

32

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

Saved successfully!

Ooh no, something went wrong!