A FEniCS Tutorial - FEniCS Project

A FEniCS Tutorial - FEniCS Project A FEniCS Tutorial - FEniCS Project

fenicsproject.org
from fenicsproject.org More from this publisher
19.06.2015 Views

dev = numpy.abs(w_e.vector().array() - w.vector().array()).max() print ’sigma=%g: max deviation=%e’ % (sigma, dev) # Should be at the end interactive() Choosing a small width σ (say 0.01) and a location (x 0 ,y 0 ) toward the circular boundary (say (0.6Rcosθ,0.6Rsinθ) for any θ ∈ [0,2π]), may produce an exciting visual comparison of w and f that demonstrates the very smoothed elastic response to a peak force (or mathematically, the smoothing properties of the inverse of the Laplace operator). One needs to experiment with the mesh resolution to get a smooth visual representation of f. You are strongly encouraged to play around with the plots and different mesh resolutions. 1.8 Quick Visualization with VTK As we go along with examples it is fun to play around with plot commands and visualize what is computed. This section explains some useful visualization features. The plot(u) command launches a FEniCS component called Viper, which applies the VTK package to visualize finite element functions. Viper is not a full-fledged, easy-to-use front-end to VTK (like Mayavi2, ParaView or, VisIt), but rather a thin layer on top of VTK’s Python interface, allowing us to quickly visualize a DOLFIN function or mesh, or data in plain Numerical Python arrays, within a Python program. Viper is ideal for debugging, teaching, and initial scientific investigations. The visualization can be interactive, or you can steer and automate it through program statements. More advanced and professional visualizations are usually better done with advanced tools like Mayavi2, ParaView, or VisIt. We have made a program membrane1v.py for the membrane deflection problem in Section 1.7 and added various demonstrations of Viper capabilities. You are encouraged to play around with membrane1v.py and modify the code as you read about various features. The plot function can take additional arguments, such as a title of the plot, or a specification of a wireframe plot (elevated mesh) instead of a colored surface plot: plot(mesh, title=’Finite element mesh’) plot(w, wireframe=True, title=’solution’) The three mouse buttons can be used to rotate, translate, and zoom the surface. Pressing h in the plot window makes a printout of several key bindings that are available in such windows. For example, pressing m in the mesh plot window dumps the plot of the mesh to an Encapsulated PostScript (.eps) file, while pressing i saves the plot in PNG format. All plotfile names are automatically generated as simulationX.eps, where X is a counter 0000, 0001, 0002, etc., being increased every time a new plot file in that format is generated (the 24

Figure 3: Plot of the deflection of a membrane. extension of PNG files is .png instead of .eps). Pressing o adds a red outline of a bounding box around the domain. One can alternatively control the visualization from the program code directly. This is done through a Viper object returned from the plot command. Let us grab this object and use it to 1) tilt the camera −65 degrees in the latitude direction, 2) add x and y axes, 3) change the default name of the plot files, 4) change the color scale, and 5) write the plot to a PNG and an EPS file. Here is the code: viz_w = plot(w, wireframe=False, title=’Scaled membrane deflection’, rescale=False, axes=True, # include axes basename=’deflection’, # default plotfile name ) viz_w.elevate(-65) # tilt camera -65 degrees (latitude dir) viz_w.set_min_max(0, 0.5*max_w) # color scale viz_w.update(w) # bring settings above into action viz_w.write_png(’deflection.png’) viz_w.write_ps(’deflection’, format=’eps’) The format argument in the latter line can also take the values ’ps’ for a standard PostScript file and ’pdf’ for a PDF file. Note the necessity of the viz_w.update(w) call – without it we will not see the effects of tilting the camera and changing the color scale. Figure 3 shows the resulting scalar surface. 25

Figure 3: Plot of the deflection of a membrane.<br />

extension of PNG files is .png instead of .eps). Pressing o adds a red outline<br />

of a bounding box around the domain.<br />

One can alternatively control the visualization from the program code directly.<br />

This is done through a Viper object returned from the plot command.<br />

Let us grab this object and use it to 1) tilt the camera −65 degrees in the latitude<br />

direction, 2) add x and y axes, 3) change the default name of the plot files,<br />

4) change the color scale, and 5) write the plot to a PNG and an EPS file. Here<br />

is the code:<br />

viz_w = plot(w,<br />

wireframe=False,<br />

title=’Scaled membrane deflection’,<br />

rescale=False,<br />

axes=True, # include axes<br />

basename=’deflection’, # default plotfile name<br />

)<br />

viz_w.elevate(-65) # tilt camera -65 degrees (latitude dir)<br />

viz_w.set_min_max(0, 0.5*max_w) # color scale<br />

viz_w.update(w) # bring settings above into action<br />

viz_w.write_png(’deflection.png’)<br />

viz_w.write_ps(’deflection’, format=’eps’)<br />

The format argument in the latter line can also take the values ’ps’ for a<br />

standard PostScript file and ’pdf’ for a PDF file. Note the necessity of the<br />

viz_w.update(w) call – without it we will not see the effects of tilting the<br />

camera and changing the color scale. Figure 3 shows the resulting scalar surface.<br />

25

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

Saved successfully!

Ooh no, something went wrong!