19.06.2015 Views

A FEniCS Tutorial - FEniCS Project

A FEniCS Tutorial - FEniCS Project

A FEniCS Tutorial - FEniCS Project

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

7.5 Using a Backend-Specific Solver<br />

The linear algebra backend determines the specific data structures that are<br />

used in the Matrix and Vector classes. For example, with the PETSc backend,<br />

Matrix encapsulates a PETSc matrix storage structure, and Vector encapsulates<br />

a PETSc vector storage structure. Sometimes one wants to perform operations<br />

directly on (say) the underlying PETSc objects. These can be fetched<br />

by<br />

A_PETSc =<br />

down_cast(A).mat() b_PETSc = down_cast(b).vec() U_PETSc =<br />

down_cast(u.vector()).vec()<br />

Here, u is a Function, A is a Matrix, and b is a Vector. The same syntax<br />

applies if we want to fetch the underlying Epetra, uBLAS, or MTL4 matrices<br />

and vectors.<br />

Sometimes one wants to implement tailored solution algorithms, using special<br />

features of the underlying numerical packages. Here is an example where we<br />

create an ML preconditioned Conjugate Gradient solver by programming with<br />

Trilinos-specific objects directly. Given a linear system AU = b, represented<br />

by a Matrix object A, and two Vector objects U and b in a Python program,<br />

the purpose is to set up a solver using the Aztec Conjugate Gradient method<br />

fromTrilinos’Azteclibraryandcombinethatsolverwiththealgebraicmultigrid<br />

preconditioner ML from the ML library in Trilinos. Since the various parts of<br />

Trilinos are mirrored in Python through the PyTrilinos package, we can operate<br />

directly on Trilinos-specific objects.<br />

try:<br />

from PyTrilinos import Epetra, AztecOO, TriUtils, ML<br />

except:<br />

print ’’’You Need to have PyTrilinos with’<br />

Epetra, AztecOO, TriUtils and ML installed<br />

for this demo to run’’’<br />

exit()<br />

from dolfin import *<br />

if not has_la_backend(’Epetra’):<br />

print ’Warning: Dolfin is not compiled with Trilinos’<br />

exit()<br />

parameters[’linear_algebra_backend’] = ’Epetra’<br />

# create matrix A and vector b in the usual way<br />

# u is a Function<br />

# Fetch underlying Epetra objects<br />

A_epetra = down_cast(A).mat()<br />

b_epetra = down_cast(b).vec()<br />

U_epetra = down_cast(u.vector()).vec()<br />

# Sets up the parameters for ML using a python dictionary<br />

ML_param = {"max levels" : 3,<br />

"output" : 10,<br />

86

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

Saved successfully!

Ooh no, something went wrong!