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.

The object A is of type Matrix, while b and u.vector() are of type Vector.<br />

We may convert the matrix and vector data to numpy arrays by calling the<br />

array() method as shown before. If you wonder how essential boundary conditions<br />

are incorporated in the linear system, you can print out A and b before<br />

and after the bc.apply(A, b) call:<br />

A = assemble(a)<br />

b = assemble(L)<br />

if mesh.num_cells() < 16: # print for small meshes only<br />

print A.array()<br />

print b.array()<br />

bc.apply(A, b)<br />

if mesh.num_cells() < 16:<br />

print A.array()<br />

print b.array()<br />

WithaccesstotheelementsinAthroughanumpyarraywecaneasilyperform<br />

computations on this matrix, such as computing the eigenvalues (using the<br />

eig function in numpy.linalg). We can alternatively dump A and b to file in<br />

MATLAB format and invoke MATLAB or Octave to analyze the linear system.<br />

Dumping the arrays A and b to MATLAB format is done by<br />

import scipy.io<br />

scipy.io.savemat(’Ab.mat’, {’A’: A, ’b’: b})<br />

Writing load Ab.mat in MATLAB or Octave will then make the variables A<br />

and b available for computations.<br />

Matrix processing in Python or MATLAB/Octave is only feasible for small<br />

PDE problems since the numpy arrays or matrices in MATLAB file format are<br />

densematrices. DOLFINalsohasaninterfacetotheeigensolverpackageSLEPc,<br />

which is a preferred tool for computing the eigenvalues of large, sparse matrices<br />

of the type encountered in PDE problems (see demo/la/eigenvalue in the<br />

DOLFIN source code tree for a demo).<br />

A complete code where the linear system AU = b is explicitly assembled and<br />

solved is found in the file dn3_p2D.py in the directory stationary/poisson.<br />

This code solves the same problem as in dn2_p2D.py (Section 1.14). For small<br />

linear systems, the program writes out A and b before and after incorporation of<br />

essential boundary conditions and illustrates the difference between assemble<br />

and assemble_system. The reader is encouraged to run the code for a 2 × 1<br />

mesh (UnitSquare(2, 1) and study the output of A.<br />

By default, solve(A, U, b) applies sparse LU decomposition as solver.<br />

Specification of an iterative solver and preconditioner is done through two optional<br />

arguments:<br />

solve(A, U, b, ’cg’, ’ilu’)<br />

Appropriate names of solvers and preconditioners are found in Section 7.4.<br />

To control tolerances in the stopping criterion and the maximum number of<br />

iterations, one can explicitly form a KrylovSolver object and set items in its<br />

parameters attribute (see also Section 1.5):<br />

45

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

Saved successfully!

Ooh no, something went wrong!