Actual source code: ex11.c
slepc-3.19.2 2023-09-05
1: /*
2: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3: SLEPc - Scalable Library for Eigenvalue Problem Computations
4: Copyright (c) 2002-, Universitat Politecnica de Valencia, Spain
6: This file is part of SLEPc.
7: SLEPc is distributed under a 2-clause BSD license (see LICENSE).
8: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
9: */
11: static char help[] = "Computes the smallest nonzero eigenvalue of the Laplacian of a graph.\n\n"
12: "This example illustrates EPSSetDeflationSpace(). The example graph corresponds to a "
13: "2-D regular mesh. The command line options are:\n"
14: " -n <n>, where <n> = number of grid subdivisions in x dimension.\n"
15: " -m <m>, where <m> = number of grid subdivisions in y dimension.\n\n";
17: #include <slepceps.h>
19: int main (int argc,char **argv)
20: {
21: EPS eps; /* eigenproblem solver context */
22: Mat A; /* operator matrix */
23: Vec x;
24: EPSType type;
25: PetscInt N,n=10,m,i,j,II,Istart,Iend,nev;
26: PetscScalar w;
27: PetscBool flag,terse;
29: PetscFunctionBeginUser;
30: PetscCall(SlepcInitialize(&argc,&argv,(char*)0,help));
32: PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
33: PetscCall(PetscOptionsGetInt(NULL,NULL,"-m",&m,&flag));
34: if (!flag) m=n;
35: N = n*m;
36: PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\nFiedler vector of a 2-D regular mesh, N=%" PetscInt_FMT " (%" PetscInt_FMT "x%" PetscInt_FMT " grid)\n\n",N,n,m));
38: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
39: Compute the operator matrix that defines the eigensystem, Ax=kx
40: In this example, A = L(G), where L is the Laplacian of graph G, i.e.
41: Lii = degree of node i, Lij = -1 if edge (i,j) exists in G
42: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
44: PetscCall(MatCreate(PETSC_COMM_WORLD,&A));
45: PetscCall(MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,N,N));
46: PetscCall(MatSetFromOptions(A));
47: PetscCall(MatSetUp(A));
49: PetscCall(MatGetOwnershipRange(A,&Istart,&Iend));
50: for (II=Istart;II<Iend;II++) {
51: i = II/n; j = II-i*n;
52: w = 0.0;
53: if (i>0) { PetscCall(MatSetValue(A,II,II-n,-1.0,INSERT_VALUES)); w=w+1.0; }
54: if (i<m-1) { PetscCall(MatSetValue(A,II,II+n,-1.0,INSERT_VALUES)); w=w+1.0; }
55: if (j>0) { PetscCall(MatSetValue(A,II,II-1,-1.0,INSERT_VALUES)); w=w+1.0; }
56: if (j<n-1) { PetscCall(MatSetValue(A,II,II+1,-1.0,INSERT_VALUES)); w=w+1.0; }
57: PetscCall(MatSetValue(A,II,II,w,INSERT_VALUES));
58: }
60: PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY));
61: PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY));
63: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
64: Create the eigensolver and set various options
65: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
67: /*
68: Create eigensolver context
69: */
70: PetscCall(EPSCreate(PETSC_COMM_WORLD,&eps));
72: /*
73: Set operators. In this case, it is a standard eigenvalue problem
74: */
75: PetscCall(EPSSetOperators(eps,A,NULL));
76: PetscCall(EPSSetProblemType(eps,EPS_HEP));
78: /*
79: Select portion of spectrum
80: */
81: PetscCall(EPSSetWhichEigenpairs(eps,EPS_SMALLEST_REAL));
83: /*
84: Set solver parameters at runtime
85: */
86: PetscCall(EPSSetFromOptions(eps));
88: /*
89: Attach deflation space: in this case, the matrix has a constant
90: nullspace, [1 1 ... 1]^T is the eigenvector of the zero eigenvalue
91: */
92: PetscCall(MatCreateVecs(A,&x,NULL));
93: PetscCall(VecSet(x,1.0));
94: PetscCall(EPSSetDeflationSpace(eps,1,&x));
95: PetscCall(VecDestroy(&x));
97: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
98: Solve the eigensystem
99: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
101: PetscCall(EPSSolve(eps));
103: /*
104: Optional: Get some information from the solver and display it
105: */
106: PetscCall(EPSGetType(eps,&type));
107: PetscCall(PetscPrintf(PETSC_COMM_WORLD," Solution method: %s\n\n",type));
108: PetscCall(EPSGetDimensions(eps,&nev,NULL,NULL));
109: PetscCall(PetscPrintf(PETSC_COMM_WORLD," Number of requested eigenvalues: %" PetscInt_FMT "\n",nev));
111: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
112: Display solution and clean up
113: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
115: /* show detailed info unless -terse option is given by user */
116: PetscCall(PetscOptionsHasName(NULL,NULL,"-terse",&terse));
117: if (terse) PetscCall(EPSErrorView(eps,EPS_ERROR_RELATIVE,NULL));
118: else {
119: PetscCall(PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL));
120: PetscCall(EPSConvergedReasonView(eps,PETSC_VIEWER_STDOUT_WORLD));
121: PetscCall(EPSErrorView(eps,EPS_ERROR_RELATIVE,PETSC_VIEWER_STDOUT_WORLD));
122: PetscCall(PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD));
123: }
124: PetscCall(EPSDestroy(&eps));
125: PetscCall(MatDestroy(&A));
126: PetscCall(SlepcFinalize());
127: return 0;
128: }
130: /*TEST
132: testset:
133: args: -eps_nev 4 -terse
134: output_file: output/ex11_1.out
135: test:
136: suffix: 1
137: args: -eps_krylovschur_restart .2
138: test:
139: suffix: 2
140: args: -eps_ncv 20 -eps_target 0 -st_type sinvert -st_ksp_type cg -st_pc_type jacobi
142: TEST*/