Actual source code: ex11.c
1: /*
2: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3: SLEPc - Scalable Library for Eigenvalue Problem Computations
4: Copyright (c) 2002-2011, Universitat Politecnica de Valencia, Spain
6: This file is part of SLEPc.
7:
8: SLEPc is free software: you can redistribute it and/or modify it under the
9: terms of version 3 of the GNU Lesser General Public License as published by
10: the Free Software Foundation.
12: SLEPc is distributed in the hope that it will be useful, but WITHOUT ANY
13: WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14: FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
15: more details.
17: You should have received a copy of the GNU Lesser General Public License
18: along with SLEPc. If not, see <http://www.gnu.org/licenses/>.
19: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
20: */
22: static char help[] = "Computes the smallest nonzero eigenvalue of the Laplacian of a graph.\n\n"
23: "This example illustrates EPSSetDeflationSpace(). The example graph corresponds to a "
24: "2-D regular mesh. The command line options are:\n"
25: " -n <n>, where <n> = number of grid subdivisions in x dimension.\n"
26: " -m <m>, where <m> = number of grid subdivisions in y dimension.\n\n";
28: #include <slepceps.h>
32: int main (int argc,char **argv)
33: {
34: EPS eps; /* eigenproblem solver context */
35: Mat A; /* operator matrix */
36: Vec x;
37: const EPSType type;
38: PetscReal tol;
39: PetscInt N,n=10,m,i,j,II,Istart,Iend,nev,maxit,its;
40: PetscScalar w;
41: PetscBool flag;
44: SlepcInitialize(&argc,&argv,(char*)0,help);
46: PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);
47: PetscOptionsGetInt(PETSC_NULL,"-m",&m,&flag);
48: if(!flag) m=n;
49: N = n*m;
50: PetscPrintf(PETSC_COMM_WORLD,"\nFiedler vector of a 2-D regular mesh, N=%D (%Dx%D grid)\n\n",N,n,m);
52: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
53: Compute the operator matrix that defines the eigensystem, Ax=kx
54: In this example, A = L(G), where L is the Laplacian of graph G, i.e.
55: Lii = degree of node i, Lij = -1 if edge (i,j) exists in G
56: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
58: MatCreate(PETSC_COMM_WORLD,&A);
59: MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,N,N);
60: MatSetFromOptions(A);
61:
62: MatGetOwnershipRange(A,&Istart,&Iend);
63: for (II=Istart;II<Iend;II++) {
64: i = II/n; j = II-i*n;
65: w = 0.0;
66: if(i>0) { MatSetValue(A,II,II-n,-1.0,INSERT_VALUES); w=w+1.0; }
67: if(i<m-1) { MatSetValue(A,II,II+n,-1.0,INSERT_VALUES); w=w+1.0; }
68: if(j>0) { MatSetValue(A,II,II-1,-1.0,INSERT_VALUES); w=w+1.0; }
69: if(j<n-1) { MatSetValue(A,II,II+1,-1.0,INSERT_VALUES); w=w+1.0; }
70: MatSetValue(A,II,II,w,INSERT_VALUES);
71: }
73: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
74: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
76: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
77: Create the eigensolver and set various options
78: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
80: /*
81: Create eigensolver context
82: */
83: EPSCreate(PETSC_COMM_WORLD,&eps);
85: /*
86: Set operators. In this case, it is a standard eigenvalue problem
87: */
88: EPSSetOperators(eps,A,PETSC_NULL);
89: EPSSetProblemType(eps,EPS_HEP);
90:
91: /*
92: Select portion of spectrum
93: */
94: EPSSetWhichEigenpairs(eps,EPS_SMALLEST_REAL);
96: /*
97: Set solver parameters at runtime
98: */
99: EPSSetFromOptions(eps);
101: /*
102: Attach deflation space: in this case, the matrix has a constant
103: nullspace, [1 1 ... 1]^T is the eigenvector of the zero eigenvalue
104: */
105: MatGetVecs(A,&x,PETSC_NULL);
106: VecSet(x,1.0);
107: EPSSetDeflationSpace(eps,1,&x);
108: VecDestroy(&x);
110: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
111: Solve the eigensystem
112: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
114: EPSSolve(eps);
115: EPSGetIterationNumber(eps,&its);
116: PetscPrintf(PETSC_COMM_WORLD," Number of iterations of the method: %D\n",its);
118: /*
119: Optional: Get some information from the solver and display it
120: */
121: EPSGetType(eps,&type);
122: PetscPrintf(PETSC_COMM_WORLD," Solution method: %s\n\n",type);
123: EPSGetDimensions(eps,&nev,PETSC_NULL,PETSC_NULL);
124: PetscPrintf(PETSC_COMM_WORLD," Number of requested eigenvalues: %D\n",nev);
125: EPSGetTolerances(eps,&tol,&maxit);
126: PetscPrintf(PETSC_COMM_WORLD," Stopping condition: tol=%.4G, maxit=%D\n",tol,maxit);
128: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
129: Display solution and clean up
130: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
132: EPSPrintSolution(eps,PETSC_NULL);
133: EPSDestroy(&eps);
134: MatDestroy(&A);
135: SlepcFinalize();
136: return 0;
137: }