Debian Codegraph @

demo/documented/eigenvalue/cpp/main.cpp.rst

raw
1 A simple eigenvalue solver (C++)
2 ================================
3
4 We recommend that you are familiar with the demo for the Poisson
5 equation before looking at this demo.
6
7
8 Implementation
9 --------------
10
11 Running this demo requires the files: :download:`main.cpp`,
12 :download:`StiffnessMatrix.ufl` and :download:`CMakeLists.txt`.
13
14
15 Under construction
16
17 .. code-block:: cpp
18
19
20 #include <dolfin.h>
21 #include "StiffnessMatrix.h"
22
23 using namespace dolfin;
24
25 int main()
26 {
27 #ifdef HAS_SLEPC
28
29 // Create mesh
30 auto mesh = std::make_shared<Mesh>("../box_with_dent.xml.gz");
31
32 // Build stiffness matrix
33 auto A = std::make_shared<PETScMatrix>();
34 auto V = std::make_shared<StiffnessMatrix::FunctionSpace>(mesh);
35 StiffnessMatrix::BilinearForm a(V, V);
36 assemble(*A, a);
37
38 // Create eigensolver
39 SLEPcEigenSolver esolver(A);
40
41 // Compute all eigenvalues of A x = \lambda x
42 esolver.solve();
43
44 // Extract largest (first, n =0) eigenpair
45 double r, c;
46 PETScVector rx, cx;
47 esolver.get_eigenpair(r, c, rx, cx, 0);
48
49 cout << "Largest eigenvalue: " << r << endl;
50
51 // Initialize function with eigenvector
52 Function u(V);
53 *u.vector() = rx;
54
55 // Output eigenfunction to file
56 XDMFFile("eigenfunction.xdmf").write(u);
57
58 #else
59
60 dolfin::cout << "SLEPc must be installed to run this demo." << dolfin::endl;
61
62 #endif
63
64 return 0;
65 }