Debian Codegraph @

python/demo/documented/eigenvalue/demo_eigenvalue.py.rst

raw
1
2 .. _demo_eigenvalue:
3
4 A simple eigenvalue solver
5 ==========================
6
7 We recommend that you are familiar with the demo for the Poisson equation
8 before looking at this demo.
9
10 If you want a more complex problem, we suggest that you look at the
11 other eigenvalue demo.
12
13 Implementation
14 --------------
15
16 This demo is implemented in a single Python file,
17 :download:`demo_eigenvalue.py` and reads in an external mesh
18 :download:`box_with_dent.xml.gz`.
19
20 The eigensolver functionality in DOLFIN relies on the library SLEPc
21 which in turn relies on the linear algebra library PETSc. Therefore,
22 both PETSc and SLEPc are required for this demo. We can test whether
23 PETSc and SLEPc are available, and exit if not, as follows ::
24
25 import matplotlib.pyplot as plt
26 from dolfin import *
27 # Test for PETSc and SLEPc
28
29 if not has_linear_algebra_backend("PETSc"):
30 print("DOLFIN has not been configured with PETSc. Exiting.")
31 exit()
32
33 if not has_slepc():
34 print("DOLFIN has not been configured with SLEPc. Exiting.")
35 exit()
36
37 First, we need to construct the matrix :math:`A`. This will be done in
38 three steps: defining the mesh and the function space associated with
39 it; constructing the variational form defining the matrix; and then
40 assembling this form. The code is shown below ::
41
42 # Define mesh, function space
43 mesh = Mesh("box_with_dent.xml.gz")
44 V = FunctionSpace(mesh, "Lagrange", 1)
45
46 # Define basis and bilinear form
47 u = TrialFunction(V)
48 v = TestFunction(V)
49 a = dot(grad(u), grad(v))*dx
50
51 # Assemble stiffness form
52 A = PETScMatrix()
53 assemble(a, tensor=A)
54
55 Note that we (in this example) first define the matrix ``A`` as a
56 :py:class:`PETScMatrix <dolfin.cpp.PETScMatrix>` and then assemble the
57 form into it. This is an easy way to ensure that the matrix has the
58 right type.
59
60 In order to solve the eigenproblem, we need to define an
61 eigensolver. To solve a standard eigenvalue problem, the eigensolver
62 is initialized with a single argument, namely the matrix ``A``. ::
63
64 # Create eigensolver
65 eigensolver = SLEPcEigenSolver(A)
66
67 Now, we ready solve the eigenproblem by calling the :py:func:`solve
68 <dolfin.cpp.SLEPcEigenSolver.solve>` method of the eigensolver. Note
69 that eigenvalue problems tend to be computationally intensive and may
70 hence take a while. ::
71
72 # Compute all eigenvalues of A x = \lambda x
73 print("Computing eigenvalues. This can take a minute.")
74 eigensolver.solve()
75
76 The result is kept by the eigensolver, but can fortunately be
77 extracted. Here, we have computed all eigenvalues, and they will be
78 sorted by largest magnitude. We can extract the real and complex part
79 (``r`` and ``c``) of the largest eigenvalue and the real and complex
80 part of the corresponding eigenvector (``ru`` and ``cu``) by asking
81 for the first eigenpair as follows ::
82
83 # Extract largest (first) eigenpair
84 r, c, rx, cx = eigensolver.get_eigenpair(0)
85
86 Finally, we want to examine the results. The eigenvalue can easily be
87 printed. But, the real part of eigenvector is probably most easily
88 visualized by constructing the corresponding eigenfunction. This can
89 be done by creating a :py:class:`Function
90 <dolfin.functions.function.Function>` in the function space ``V``
91 and the associating eigenvector ``rx`` with the Function. Then the
92 eigenfunction can be manipulated as any other :py:class:`Function
93 <dolfin.functions.function.Function>`, and in particular plotted ::
94
95 print("Largest eigenvalue: ", r)
96
97 # Initialize function and assign eigenvector
98 u = Function(V)
99 u.vector()[:] = rx
100
101 # Plot eigenfunction
102 plot(u)
103 plt.show()