A FEniCS Tutorial - FEniCS Project
A FEniCS Tutorial - FEniCS Project
A FEniCS Tutorial - FEniCS Project
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
The grad_u_x and grad_u_y variables behave as Function objects. In particular,<br />
we can extract the underlying arrays of nodal values by<br />
grad_u_x_array = grad_u_x.vector().array()<br />
grad_u_y_array = grad_u_y.vector().array()<br />
The degrees of freedom of the grad_u vector field can also be reached by<br />
grad_u_array = grad_u.vector().array()<br />
but this is a flat numpy array where the degrees of freedom for the x component<br />
of the gradient is stored in the first part, then the degrees of freedom of the y<br />
component, and so on.<br />
The program d5_p2D.py extends the code d5_p2D.py from Section 1.6 with<br />
computationsandvisualizationsofthegradient. Examiningthearraysgrad_u_x_array<br />
andgrad_u_y_array, orlookingattheplotsofgrad_u_xandgrad_u_y, quickly<br />
revealsthatthecomputedgrad_ufielddoesnotequaltheexactgradient(2x,4y)<br />
in this particular test problem where u = 1+x 2 +2y 2 . There are inaccuracies at<br />
the boundaries, arising from the approximation problem for w. Increasing the<br />
mesh resolution shows, however, that the components of the gradient vary linearly<br />
as 2x and 4y in the interior of the mesh (i.e., as soon as we are one element<br />
away from the boundary). See Section 1.8 for illustrations of this phenomenon.<br />
<strong>Project</strong>ing some function onto some space is a very common operation in<br />
finite element programs. The manual steps in this process have therefore been<br />
collected in a utility function project(q, W), which returns the projection of<br />
some Function or Expression object named q onto the FunctionSpace or<br />
VectorFunctionSpace named W. Specifically, the previous code for projecting<br />
each component of grad(u) onto the same space that we use for u, can now be<br />
done by a one-line call<br />
grad_u = project(grad(u), VectorFunctionSpace(mesh, ’Lagrange’, 1))<br />
The applications of projection are many, including turning discontinuous gradient<br />
fields into continuous ones, comparing higher- and lower-order function<br />
approximations, and transforming a higher-order finite element solution down<br />
to a piecewise linear field, which is required by many visualization packages.<br />
1.10 A Variable-Coefficient Poisson Problem<br />
Suppose we have a variable coefficient p(x,y) in the Laplace operator, as in the<br />
boundary-value problem<br />
−∇·[p(x,y)∇u(x,y)] = f(x,y) in Ω,<br />
u(x,y) = u 0 (x,y) on ∂Ω.<br />
(21)<br />
We shall quickly demonstrate that this simple extension of our model problem<br />
only requires an equally simple extension of the <strong>FEniCS</strong> program.<br />
28