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.

UnitSquare to make the mesh object. The result u_box is a BoxField object<br />

that supports ”finite difference” indexing and an underlying grid suitable for<br />

numpy operations on 2D data. Also 1D and 3D meshes (with linear elements)<br />

can be turned into BoxField objects.<br />

The ability to access a finite element field in the way one can access a finite<br />

difference-type of field is handy in many occasions, including visualization and<br />

data analysis. Here is an example of writing out the coordinates and the field<br />

valueatagridpointwithindicesiandj(goingfrom0tonxandny,respectively,<br />

from lower left to upper right corner):<br />

X = 0; Y = 1; Z = 0 # convenient indices<br />

i = nx; j = ny # upper right corner<br />

print ’u(%g,%g)=%g’ % (u_box.grid.coor[X][i],<br />

u_box.grid.coor[Y][j],<br />

u_box.values[i,j])<br />

For instance, the x coordinates are reached by u_box.grid.coor[X]. The grid<br />

attribute is an instance of class UniformBoxGrid.<br />

Many plotting programs can be used to visualize the data in u_box. Matplotlib<br />

is now a very popular plotting program in the Python world and could<br />

be used to make contour plots of u_box. However, other programs like Gnuplot,<br />

VTK, and MATLAB have better support for surface plots at the time of this<br />

writing. OurchoiceinthistutorialistousethePythonpackagescitools.easyviz,<br />

which offers a uniform MATLAB-like syntax as interface to various plotting<br />

packages such as Gnuplot, Matplotlib, VTK, OpenDX, MATLAB, and others.<br />

With scitools.easyviz we write one set of statements, close to what one<br />

would do in MATLAB or Octave, and then it is easy to switch between different<br />

plotting programs, at a later stage, through a command-line option, a line in a<br />

configuration file, or an import statement in the program.<br />

A contour plot is made by the following scitools.easyviz command:<br />

import scitools.easyviz as ev<br />

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

5, clabels=’on’)<br />

evtitle(’Contour plot of u’)<br />

ev.savefig(’u_contours.eps’)<br />

# or more compact syntax:<br />

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

5, clabels=’on’,<br />

savefig=’u_contours.eps’, title=’Contour plot of u’)<br />

The resulting plot can be viewed in Figure 1.12a. The contour function needs<br />

arrays with the x and y coordinates expanded to 2D arrays (in the same way as<br />

demanded when making vectorized numpy calculations of arithmetic expressions<br />

over all grid points). The correctly expanded arrays are stored in grid.coorv.<br />

The above call to contour creates 5 equally spaced contour lines, and with<br />

clabels=’on’ the contour values can be seen in the plot.<br />

36

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

Saved successfully!

Ooh no, something went wrong!