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.

the numerical solution u. The approximation now consists of solving w = −p∇u<br />

by a finite element method: find w ∈ V (g) such that<br />

where<br />

a(w,v) = L(v) ∀v ∈ ˆ V (g) , (24)<br />

∫<br />

a(w,v) = w·vdx, (25)<br />

Ω<br />

∫<br />

L(v) = (−p∇u)·vdx. (26)<br />

Ω<br />

This problem is identical to the one in Section 1.9, except that p enters the<br />

integral in L.<br />

The relevant Python statements for computing the flux field take the form<br />

V_g = VectorFunctionSpace(mesh, ’Lagrange’, 1)<br />

w = TrialFunction(V_g)<br />

v = TestFunction(V_g)<br />

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

L = inner(-p*grad(u), v)*dx<br />

flux = Function(V_g)<br />

solve(a == L, flux)<br />

The following call to project is equivalent to the above statements:<br />

flux = project(-p*grad(u),<br />

VectorFunctionSpace(mesh, ’Lagrange’, 1))<br />

Plotting the flux vector field is naturally as easy as plotting the gradient (see<br />

Section 1.9):<br />

plot(flux, title=’flux field’)<br />

flux_x, flux_y = flux.split(deepcopy=True) # extract components<br />

plot(flux_x, title=’x-component of flux (-p*grad(u))’)<br />

plot(flux_y, title=’y-component of flux (-p*grad(u))’)<br />

For data analysis of the nodal values of the flux field we can grab the underlying<br />

numpy arrays:<br />

flux_x_array = flux_x.vector().array()<br />

flux_y_array = flux_y.vector().array()<br />

The program vcp2D.py contains in addition some plots, including a curve<br />

plot comparing flux_x and the exact counterpart along the line y = 1/2. The<br />

associated programming details related to this visualization are explained in<br />

Section 1.12.<br />

30

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

Saved successfully!

Ooh no, something went wrong!