19.06.2015 Views

A FEniCS Tutorial - FEniCS Project

A FEniCS Tutorial - FEniCS Project

A FEniCS Tutorial - FEniCS Project

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

All mesh objects are of type Mesh so typing the command pydoc dolfin.Mesh<br />

in a terminal window will give a list of methods (that is, functions in a class)<br />

that can be called through any Mesh object. In fact, pydoc dolfin.X shows<br />

the documentation of any DOLFIN name X.<br />

Writing out the solution on the screen can now be done by a simple loop:<br />

coor = mesh.coordinates()<br />

if mesh.num_vertices() == len(u_array):<br />

for i in range(mesh.num_vertices()):<br />

print ’u(%8g,%8g) = %g’ % (coor[i][0], coor[i][1], u_array[i])<br />

The beginning of the output looks like this:<br />

u( 0, 0) = 1<br />

u(0.166667, 0) = 1.02778<br />

u(0.333333, 0) = 1.11111<br />

u( 0.5, 0) = 1.25<br />

u(0.666667, 0) = 1.44444<br />

u(0.833333, 0) = 1.69444<br />

u( 1, 0) = 2<br />

For Lagrange elements of degree higher than one, the vertices do not correspond<br />

to all the nodal points and the ‘if‘-test fails.<br />

For verification purposes we want to compare the values of the computed u<br />

at the nodes (given by u_array) with the exact solution u0 evaluated at the<br />

nodes. The difference between the computed and exact solution should be less<br />

than a small tolerance at all the nodes. The Expression object u0 can be<br />

evaluated at any point x by calling u0(x). Specifically, u0(coor[i]) returns<br />

the value of u0 at the vertex or node with global number i.<br />

Alternatively, we can make a finite element field u_e, representing the exact<br />

solution, whose values at the nodes are given by the u0 function. With<br />

mathematics, ue = ∑ N<br />

j=1 E jφ j , where E j = u 0 (x j ,y j ), (x j ,y j ) being the coordinates<br />

of node number j. This process is known as interpolation. <strong>FEniCS</strong> has<br />

a function for performing the operation:<br />

u_e = interpolate(u0, V)<br />

The maximum error can now be computed as<br />

u_e_array = u_e.vector().array()<br />

print ’Max error:’, numpy.abs(u_e_array - u_array).max()<br />

The value of the error should be at the level of the machine precision (10 −16 ).<br />

To demonstrate the use of point evaluations of Function objects, we write<br />

out the computed u at the center point of the domain and compare it with the<br />

exact solution:<br />

center = (0.5, 0.5)<br />

print ’numerical u at the center point:’, u(center)<br />

print ’exact u at the center point:’, u0(center)<br />

Trying a 3×3 mesh, the output from the previous snippet becomes<br />

19

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

Saved successfully!

Ooh no, something went wrong!