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.

Other functions for visualizing 2D scalar fields are surf and mesh as known<br />

from MATLAB:<br />

import scitools.easyviz as ev<br />

ev.figure()<br />

ev.surf(u_box.grid.coorv[X], u_box.grid.coorv[Y], u_box.values,<br />

shading=’interp’, colorbar=’on’,<br />

title=’surf plot of u’, savefig=’u_surf.eps’)<br />

ev.figure()<br />

ev.mesh(u_box.grid.coorv[X], u_box.grid.coorv[Y], u_box.values,<br />

title=’mesh plot of u’, savefig=’u_mesh.eps’)<br />

Figure 1.12 exemplifies the surfaces arising from the two plotting commands<br />

above. You can type pydoc scitools.easyviz in a terminal window to get<br />

a full tutorial. Note that scitools.easyviz offers function names like plot<br />

and mesh, which clash with plot from dolfin and the mesh variable in our<br />

programs. Therefore, we recommend the ev prefix.<br />

A handy feature of BoxField is the ability to give a start point in the grid<br />

and a direction, and then extract the field and corresponding coordinates along<br />

the nearest grid line. In 3D fields one can also extract data in a plane. Say we<br />

want to plot u along the line y = 1/2 in the grid. The grid points, x, and the u<br />

values along this line, uval, are extracted by<br />

start = (0, 0.5)<br />

x, uval, y_fixed, snapped = u_box.gridline(start, direction=X)<br />

The variable snapped is true if the line had to be snapped onto a gridline and<br />

in that case y_fixed holds the snapped (altered) y value. Plotting u versus the<br />

x coordinate along this line, using scitools.easyviz, is now a matter of<br />

ev.figure() # new plot window<br />

ev.plot(x, uval, ’r-’) # ’r--: red solid line<br />

ev.title(’Solution’)<br />

ev.legend(’finite element solution’)<br />

# or more compactly:<br />

ev.plot(x, uval, ’r-’, title=’Solution’,<br />

legend=’finite element solution’)<br />

A more exciting plot compares the projected numerical flux in x direction<br />

along the line y = 1/2 with the exact flux:<br />

ev.figure()<br />

flux2_x = flux_x if flux_x.ufl_element().degree() == 1 else \<br />

interpolate(flux_x, FunctionSpace(mesh, ’Lagrange’, 1))<br />

flux_x_box = scitools.BoxField.dolfin_function2BoxField(<br />

flux2_x, mesh, (nx,ny), uniform_mesh=True)<br />

x, fluxval, y_fixed, snapped = \<br />

flux_x_box.gridline(start, direction=X)<br />

y = y_fixed<br />

flux_x_exact = -(x + y)*2*x<br />

ev.plot(x, fluxval, ’r-’,<br />

37

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

Saved successfully!

Ooh no, something went wrong!