~ubuntu-branches/ubuntu/natty/dolfin/natty

« back to all changes in this revision

Viewing changes to doc/manual/chapters/linearalgebra.tex

  • Committer: Bazaar Package Importer
  • Author(s): Johannes Ring
  • Date: 2011-02-24 10:34:44 UTC
  • mfrom: (1.1.7 upstream) (14.1.4 sid)
  • Revision ID: james.westby@ubuntu.com-20110224103444-n3fwnmh32lfoske0
Tags: 0.9.10-1
* New upstream release. This release fixes bug "FTBFS: error:
  'SCOTCH_Dgraph' was not declared in this scope" (closes: #612602).
* debian/control:
  - Add libslepc3.1-dev and libboost-thread-dev to Build-Depends and
    Depends field in binary package libdolfin0-dev.
  - Bump build dependency on python-ufc to >= 2.0.0.
  - Remove Build-Depends-Indep field as upstream no longer ships the
    user manual.
  - Remove old fields Conflicts, Provides, and Replaces from
    libdolfin0-dev, libdolfin0, libdolfin0-dbg, and python-dolfin.
* Remove all patches as they are now incorporated upstream.
* Add dolfin-plot and dolfin-version to debian/dolfin-bin.install.
* Remove .doc-base file since the user manual is removed by upstream.
* Remove targets clean and install/dolfin-doc from debian/rules since
  they are no longer needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
\chapter{Linear algebra}
2
 
 
3
 
\devnote{Since this chapter was written, the \dolfin{} linear algebra
4
 
  interface has undergone some changes that are not documented here.
5
 
  As a result, some of the information in this chapter may be
6
 
  outdated, incomplete or simply wrong.}
7
 
 
8
 
\dolfin{} provides a high-performance linear algebra library,
9
 
including matrices and vectors, a set of linear solvers,
10
 
preconditioners, and an eigenvalue solver. The core part of the
11
 
functionality is provided through wrappers that provide a common
12
 
interface to the functionality of the linear algebra libraries
13
 
uBLAS~\cite{www:ublas}, PETSc~\cite{www:petsc} and 
14
 
Trilinos~\cite{www.trilonos}.
15
 
 
16
 
%------------------------------------------------------------------------------
17
 
\section{Matrices and vectors}
18
 
\index{\texttt{Matrix}}
19
 
\index{\texttt{Vector}}
20
 
 
21
 
The two basic linear algebra data structures are the classes
22
 
\texttt{Matrix} and \texttt{Vector}, representing a sparse $M\times
23
 
N$ matrix and a vector of length $N$ respectively.
24
 
 
25
 
The following code demonstrates how to create a matrix and a vector:
26
 
\begin{code}
27
 
Matrix A(M, N);
28
 
Vector x(N);
29
 
\end{code}
30
 
Alternatively, the matrix and the vector may be created by
31
 
\begin{code}
32
 
Matrix A;
33
 
Vector x;
34
 
 
35
 
A.init(M, N);
36
 
x.init(N);
37
 
\end{code}
38
 
 
39
 
The following code demonstrates how to access the size and the
40
 
elements of a matrix and a vector:
41
 
\begin{code}
42
 
A(5, 5) = 1.0;
43
 
double a = A(4, 3);
44
 
 
45
 
x(3) = 2.0;
46
 
double b = x(5);
47
 
 
48
 
unsigned int M = A.size(0);
49
 
unsigned int N = A.size(1);
50
 
 
51
 
N = x.size();
52
 
\end{code}
53
 
 
54
 
In addition, \dolfin{} provides optimized functions for setting the
55
 
values of a set of entries in a matrix or vector:
56
 
\begin{code}
57
 
double block[] = {2, 4, 6};
58
 
int rows[] = {0, 1, 2};
59
 
int cols[] = {0, 1, 2};
60
 
  
61
 
A.set(block, rows, cols, 3);
62
 
\end{code}
63
 
Alternatively, the set of values given by the array \texttt{block} can
64
 
be added to the entries given by the arrays \texttt{rows} and
65
 
\texttt{cols}:
66
 
\begin{code}
67
 
double block[] = {2, 4, 6};
68
 
int rows[] = {0, 1, 2};
69
 
int cols[] = {0, 1, 2};
70
 
  
71
 
A.add(block, rows, cols, 3);
72
 
\end{code}
73
 
These functions are particularly useful for efficient assembly of a (sparse)
74
 
matrix from a bilinear form.
75
 
 
76
 
\subsection{Sparse matrices}
77
 
\index{sparse matrix}
78
 
 
79
 
The default \dolfin{} class \texttt{Matrix} is a sparse matrix, which
80
 
efficiently represents the discretization of a partial differential
81
 
equation where most entries in the matrix are zero. Alternatively,
82
 
the class \texttt{SparseMatrix} may be used which is identical with
83
 
the class \texttt{Matrix}\footnote{The class \texttt{Matrix} is a
84
 
\texttt{typedef} for the class \texttt{SparseMatrix}.}.
85
 
 
86
 
If \dolfin{} has been compiled with support for PETSc, then the sparse
87
 
matrix is represented as a sparse PETSc matrix\footnote{By default, the
88
 
sparse matrix is represented as a PETSc \texttt{MATSEQAIJ} matrix, but
89
 
other PETSc representations are also available.}. Alternatively, the
90
 
class \texttt{PETScMatrix} may be used, together with the corresponding
91
 
class \texttt{PETScVector}.
92
 
 
93
 
If \dolfin{} has been compiled without support for PETSc, then the
94
 
sparse matrix is represented as a uBLAS sparse matrix. Alternatively,
95
 
the class \texttt{uBLASSparseMatrix} may be used, together with the
96
 
corresponding class \texttt{uBLASVector}.
97
 
 
98
 
\subsection{Dense matrices}
99
 
\index{dense matrix}
100
 
 
101
 
\dolfin{} provides the class \texttt{DenseMatrix} for representation
102
 
of dense matrices. A dense matrix representation is often preferable
103
 
when computing with matrices of small to moderate size. In particular,
104
 
accessing individual elements 
105
 
is more efficient with a dense matrix representation.
106
 
 
107
 
A \texttt{DenseMatrix} is represented as uBLAS dense matrix and
108
 
alternatively the class \texttt{uBLASDenseMatrix} may be used,
109
 
together with the corresponding class \texttt{uBLASVector}.
110
 
 
111
 
\subsection{The common interface}
112
 
\index{\texttt{GenericMatrix}}
113
 
\index{\texttt{GenericVector}}
114
 
 
115
 
Although \dolfin{} differentiates between sparse and dense data
116
 
structures, the two classes \texttt{GenericMatrix} and
117
 
\texttt{GenericVector} define a common interface to all matrices and
118
 
vectors. Refer to the \emph{DOLFIN programmer's reference} for the
119
 
exact specification of these interfaces.
120
 
 
121
 
The following points should be noted:
122
 
\begin{enumerate}
123
 
\item
124
 
  If you want a specific backend like PETSc, then use
125
 
  \texttt{PETScVector}/\texttt{PETScMatrix}.
126
 
\item
127
 
  If you don't care about the backend, then use \texttt{Vector}/\texttt{Matrix}.
128
 
\item
129
 
  If you write a \emph{function} that should be able to accept vectors
130
 
  and matrices from any backend as input, then use
131
 
  \texttt{GenericVector}/\texttt{GenericMatrix}.
132
 
\end{enumerate}
133
 
 
134
 
%------------------------------------------------------------------------------
135
 
\section{Solving linear systems}
136
 
\index{linear systems}
137
 
 
138
 
\dolfin{} provides a set of efficient solvers for linear systems of
139
 
the form
140
 
\begin{equation}
141
 
  Ax = b,
142
 
\end{equation}
143
 
where $A$ is an $N\times N$ matrix and where $x$ and $b$ are vectors
144
 
of length $N$. Both iterative (Krylov subspace) solvers and direct
145
 
(LU) solvers are provided.
146
 
 
147
 
\subsection{Iterative methods}
148
 
\index{iterative methods}
149
 
\index{\texttt{GMRES}}
150
 
\index{GMRES method}
151
 
\index{\texttt{KrylovSolver}}
152
 
\index{Krylov subspace methods}
153
 
 
154
 
A linear system may be solved by the GMRES Krylov method as follows:
155
 
\begin{code}
156
 
Matrix A;
157
 
Vector x, b:
158
 
 
159
 
GMRES::solve(A, x, b);
160
 
\end{code}
161
 
Alternatively, the linear system may be solved by first creating an
162
 
object of the class \texttt{KrylovSolver}, which is more efficient for
163
 
repeated solution of linear systems and also allows the specification
164
 
of both the Krylov method and the preconditioner:
165
 
\begin{code}
166
 
KrylovSolver solver(gmres, ilu);
167
 
solver.solve(A, x, b);
168
 
\end{code}
169
 
 
170
 
For uBLAS matrices and vectors, the class \texttt{uBLASKrylovSolver}
171
 
may be used and for PETSc matrices and vectors, the class
172
 
\texttt{PETScKrylovSolver} may be used.
173
 
 
174
 
\subsubsection{Krylov methods}
175
 
\index{conjugate gradient method}
176
 
\index{BiCGStab}
177
 
 
178
 
\dolfin{} provides the following set of Krylov methods:
179
 
 
180
 
\begin{center}
181
 
\begin{tabular}{|l|l|}
182
 
\hline
183
 
\texttt{cg}              & The conjugate gradient method \\
184
 
\hline
185
 
\texttt{gmres}           & The GMRES method \\
186
 
\hline
187
 
\texttt{bicgstab}        & The stabilized biconjugate gradient squared
188
 
method \\
189
 
\hline
190
 
\texttt{default\_method} & Default choice of Krylov method \\
191
 
\hline
192
 
\end{tabular}
193
 
\end{center}
194
 
 
195
 
\subsubsection{Preconditioners}
196
 
\index{preconditioners}
197
 
\index{ILU}
198
 
\index{incomplete LU factorization}
199
 
\index{SOR}
200
 
\index{successive over-relaxation}
201
 
\index{Jacobi}
202
 
\index{AMG}
203
 
\index{algebraic multigrid}
204
 
 
205
 
\dolfin{} provides the following set of preconditioners:
206
 
 
207
 
\begin{center}
208
 
\begin{tabular}{|l|l|}
209
 
\hline
210
 
\texttt{none}        & No preconditioning \\
211
 
\hline
212
 
\texttt{jacobi}      & Simple Jacobi preconditioning \\
213
 
\hline
214
 
\texttt{sor}         & SOR, successive over-relaxation \\
215
 
\hline
216
 
\texttt{ilu}         & Incomplete LU factorization \\
217
 
\hline
218
 
\texttt{icc}         & Incomplete Cholesky factorization \\
219
 
\hline
220
 
\texttt{amg}         & Algebraic multigrid (through Hypre when available) \\
221
 
\hline
222
 
\texttt{default\_pc} & Default choice of preconditioner \\
223
 
\hline
224
 
\end{tabular}
225
 
\end{center}
226
 
 
227
 
\subsubsection{Matrix-free solvers}
228
 
\index{matrix-free solvers}
229
 
\index{virtual matrix}
230
 
 
231
 
The \dolfin{} Krylov solvers may be used without direct access to a
232
 
matrix representation. All that is needed is to provide the
233
 
size of the linear system, the right-hand side, and a method
234
 
implementing the multiplication of the matrix with any given vector.
235
 
 
236
 
Such a ``virtual matrix'' may be defined by implementing the
237
 
interface defined by either the class \texttt{uBLASKrylovMatrix} of
238
 
\texttt{PETScKrylovMatrix}. The matrix may then be used together with
239
 
either the class \texttt{uBLASKrylovSolver} or \texttt{PETScKrylovSolver}.
240
 
 
241
 
\subsection{Direct methods}
242
 
\index{\texttt{LU}}
243
 
\index{\texttt{LUSolver}}
244
 
\index{direct methods}
245
 
\index{LU factorization}
246
 
 
247
 
A linear system may be solved by a direct LU factorization as follows:
248
 
\begin{code}
249
 
Matrix A;
250
 
Vector x, b;
251
 
  
252
 
LU::solve(A, x, b);
253
 
\end{code}
254
 
Alternatively, the linear system may be solved by first creating an
255
 
object of the class \texttt{LUSolver}, which may be more efficient for
256
 
repeated solution of linear systems:
257
 
\begin{code}
258
 
LUSolver solver;
259
 
solver.solve(A, x, b);
260
 
\end{code}
261
 
 
262
 
For uBLAS matrices and vectors, the class \texttt{uBLASLUSolver} may
263
 
be used and for PETSc matrices and vectors, the class
264
 
\texttt{PETScLUSolver} may be used.
265
 
 
266
 
%------------------------------------------------------------------------------
267
 
\section{Eigenvalue problems}
268
 
\index{eigenvalue problems}
269
 
\index{SLEPc}
270
 
 
271
 
\dolfin{} also provides a solver for eigenvalue problems\index{eigenvalue solver}. 
272
 
The solver is only available when \dolfin{} has been compiled with support for 
273
 
PETSc and SLEPc~\cite{www:slepc}.
274
 
 
275
 
For the basic eigenvalue problem
276
 
%
277
 
\begin{equation}
278
 
  Ax = \lambda x,
279
 
\end{equation}
280
 
%
281
 
the following code demonstrates how to compute the first eigenpair:
282
 
%
283
 
\begin{code} 
284
 
SLEPcEigenvalueSolver esolver; 
285
 
esolver.solve(A);
286
 
 
287
 
double lr, lc;
288
 
PETScVector xr, xc;
289
 
esolver.getEigenpair(lr, lc, xr, xc, 0);
290
 
\end{code} 
291
 
%
292
 
The double and complex components of the eigenvalue are returned in \texttt{lr}
293
 
and \texttt{lc}, respectively, and the double and complex parts of the eigenvector
294
 
are returned in \texttt{xr} and \texttt{xc}, respectively.
295
 
 
296
 
For the generalized eigenvalue problem
297
 
\begin{equation}
298
 
 A x = \lambda B x,
299
 
\end{equation}
300
 
the following code demonstrates how to compute the third eigenpair:
301
 
\begin{code} 
302
 
PETScEigenvalueSolver esolver; 
303
 
esolver.solve(A, B);
304
 
 
305
 
double lr, lc;
306
 
PETScVector xr, xc;
307
 
esolver.getEigenpair(lr, lc, xr, xc, 2);
308
 
\end{code} 
309
 
%------------------------------------------------------------------------------
310
 
\section{Linear algebra backends}
311
 
\index{linear algebra backends}
312
 
 
313
 
\subsection{uBLAS}
314
 
\index{uBLAS}
315
 
 
316
 
uBLAS is a C++ template library that provides BLAS level 1, 2 and 3
317
 
functionality (and more) for dense, packed and sparse matrices.
318
 
The design and implementation unify mathematical notation via operator
319
 
overloading and efficient code generation via expression templates.
320
 
 
321
 
\dolfin{} wrappers for uBLAS linear algebra is provided through the
322
 
classes \texttt{uBLASSparseMatrix}, \texttt{uBLASDenseMatrix} and
323
 
\texttt{uBLASVector}. These classes are implemented by subclassing the
324
 
corresponding uBLAS classes, which means that all standard
325
 
\texttt{uBLAS} operations are supported for these classes. For
326
 
advanced usage not covered by the common \dolfin{} interface specified
327
 
by the classes \texttt{GenericMatrix} and \texttt{GenericVector},
328
 
refer directly to the documentation of \texttt{uBLAS}.
329
 
%------------------------------------------------------------------------------
330
 
\subsection{PETSc}
331
 
\index{PETSc}
332
 
%
333
 
PETSc is a suite of data structures and routines for the scalable
334
 
(parallel) solution of scientific applications modeled by partial
335
 
differential equations. It employs the MPI standard for all 
336
 
message-passing communication.
337
 
 
338
 
\dolfin{} wrappers for PETSc linear algebra is provided through the
339
 
classes \texttt{PETScMatrix} and \texttt{PETScVector}. Direct access
340
 
to the PETSc data structures is available through the member functions
341
 
\texttt{mat()} and \texttt{vec()}, which return the PETSc \texttt{Mat}
342
 
and \texttt{Vec} pointers respectively. For advanced usage not covered
343
 
by the common \dolfin{} interface specified by the classes
344
 
\texttt{GenericMatrix} and \texttt{GenericVector}, refer directly to
345
 
the documentation of PETSc.
346
 
 
347
 
%------------------------------------------------------------------------------
348
 
\subsection{Trilinos}
349
 
\index{Trilinos}
350
 
%
351
 
Trilinos . . . . 
352
 
%------------------------------------------------------------------------------
353
 
\subsection{Matrix Template Library (MTL4)}
354
 
\index{MTL4}
355
 
\index{Matrix Template Library}
356
 
%
357
 
MTL4 . . . . 
358
 
%------------------------------------------------------------------------------
359
 
\subsection{User provided linear algebra backends}
360
 
\index{Trilinos}
361
 
%
362
 
\devnote{This section will explain how a user-provided linear alegra backend
363
 
          can be used with \dolfin{}.}
364