Actual source code: ex25.c
slepc-3.17.0 2022-03-31
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[] = "Spectrum slicing on generalized symmetric eigenproblem.\n\n"
12: "The problem is similar to ex13.c.\n\n"
13: "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";
17: #include <slepceps.h>
19: int main(int argc,char **argv)
20: {
21: Mat A,B; /* matrices */
22: EPS eps; /* eigenproblem solver context */
23: ST st; /* spectral transformation context */
24: KSP ksp;
25: PC pc;
26: EPSType type;
27: PetscInt N,n=10,m,Istart,Iend,II,nev,i,j,*inertias,ns;
28: PetscReal inta,intb,*shifts;
29: PetscBool flag,show=PETSC_FALSE,terse;
30: #if defined(PETSC_HAVE_MUMPS) && !defined(PETSC_USE_COMPLEX)
31: Mat F;
32: #endif
34: SlepcInitialize(&argc,&argv,(char*)0,help);
36: PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
37: PetscOptionsGetInt(NULL,NULL,"-m",&m,&flag);
38: PetscOptionsGetBool(NULL,NULL,"-show_inertias",&show,NULL);
39: if (!flag) m=n;
40: N = n*m;
41: PetscPrintf(PETSC_COMM_WORLD,"\nSpectrum slicing on GHEP, N=%" PetscInt_FMT " (%" PetscInt_FMT "x%" PetscInt_FMT " grid)\n\n",N,n,m);
43: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
44: Compute the matrices that define the eigensystem, Ax=kBx
45: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
47: MatCreate(PETSC_COMM_WORLD,&A);
48: MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,N,N);
49: MatSetFromOptions(A);
50: MatSetUp(A);
52: MatCreate(PETSC_COMM_WORLD,&B);
53: MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,N,N);
54: MatSetFromOptions(B);
55: MatSetUp(B);
57: MatGetOwnershipRange(A,&Istart,&Iend);
58: for (II=Istart;II<Iend;II++) {
59: i = II/n; j = II-i*n;
60: if (i>0) MatSetValue(A,II,II-n,-1.0,INSERT_VALUES);
61: if (i<m-1) MatSetValue(A,II,II+n,-1.0,INSERT_VALUES);
62: if (j>0) MatSetValue(A,II,II-1,-1.0,INSERT_VALUES);
63: if (j<n-1) MatSetValue(A,II,II+1,-1.0,INSERT_VALUES);
64: MatSetValue(A,II,II,4.0,INSERT_VALUES);
65: MatSetValue(B,II,II,4.0,INSERT_VALUES);
66: }
68: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
69: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
70: MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
71: MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
73: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
74: Create the eigensolver and set various options
75: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
77: /*
78: Create eigensolver context
79: */
80: EPSCreate(PETSC_COMM_WORLD,&eps);
82: /*
83: Set operators and set problem type
84: */
85: EPSSetOperators(eps,A,B);
86: EPSSetProblemType(eps,EPS_GHEP);
88: /*
89: Set interval for spectrum slicing
90: */
91: inta = 0.1;
92: intb = 0.2;
93: EPSSetInterval(eps,inta,intb);
94: EPSSetWhichEigenpairs(eps,EPS_ALL);
96: /*
97: Spectrum slicing requires Krylov-Schur
98: */
99: EPSSetType(eps,EPSKRYLOVSCHUR);
101: /*
102: Set shift-and-invert with Cholesky; select MUMPS if available
103: */
105: EPSGetST(eps,&st);
106: STSetType(st,STSINVERT);
107: EPSKrylovSchurGetKSP(eps,&ksp);
108: KSPSetType(ksp,KSPPREONLY);
109: KSPGetPC(ksp,&pc);
110: PCSetType(pc,PCCHOLESKY);
112: /*
113: Use MUMPS if available.
114: Note that in complex scalars we cannot use MUMPS for spectrum slicing,
115: because MatGetInertia() is not available in that case.
116: */
117: #if defined(PETSC_HAVE_MUMPS) && !defined(PETSC_USE_COMPLEX)
118: EPSKrylovSchurSetDetectZeros(eps,PETSC_TRUE); /* enforce zero detection */
119: PCFactorSetMatSolverType(pc,MATSOLVERMUMPS);
120: PCFactorSetUpMatSolverType(pc);
121: /*
122: Set several MUMPS options, the corresponding command-line options are:
123: '-st_mat_mumps_icntl_13 1': turn off ScaLAPACK for matrix inertia
124: '-st_mat_mumps_icntl_24 1': detect null pivots in factorization (for the case that a shift is equal to an eigenvalue)
125: '-st_mat_mumps_cntl_3 <tol>': a tolerance used for null pivot detection (must be larger than machine epsilon)
127: Note: depending on the interval, it may be necessary also to increase the workspace:
128: '-st_mat_mumps_icntl_14 <percentage>': increase workspace with a percentage (50, 100 or more)
129: */
130: PCFactorGetMatrix(pc,&F);
131: MatMumpsSetIcntl(F,13,1);
132: MatMumpsSetIcntl(F,24,1);
133: MatMumpsSetCntl(F,3,1e-12);
134: #endif
136: /*
137: Set solver parameters at runtime
138: */
139: EPSSetFromOptions(eps);
141: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
142: Solve the eigensystem
143: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
144: EPSSetUp(eps);
145: if (show) {
146: EPSKrylovSchurGetInertias(eps,&ns,&shifts,&inertias);
147: PetscPrintf(PETSC_COMM_WORLD,"Subintervals (after setup):\n");
148: for (i=0;i<ns;i++) PetscPrintf(PETSC_COMM_WORLD,"Shift %g Inertia %" PetscInt_FMT " \n",(double)shifts[i],inertias[i]);
149: PetscPrintf(PETSC_COMM_WORLD,"\n");
150: PetscFree(shifts);
151: PetscFree(inertias);
152: }
153: EPSSolve(eps);
154: if (show) {
155: EPSKrylovSchurGetInertias(eps,&ns,&shifts,&inertias);
156: PetscPrintf(PETSC_COMM_WORLD,"All shifts (after solve):\n");
157: for (i=0;i<ns;i++) PetscPrintf(PETSC_COMM_WORLD,"Shift %g Inertia %" PetscInt_FMT " \n",(double)shifts[i],inertias[i]);
158: PetscPrintf(PETSC_COMM_WORLD,"\n");
159: PetscFree(shifts);
160: PetscFree(inertias);
161: }
163: /*
164: Show eigenvalues in interval and print solution
165: */
166: EPSGetType(eps,&type);
167: PetscPrintf(PETSC_COMM_WORLD," Solution method: %s\n\n",type);
168: EPSGetDimensions(eps,&nev,NULL,NULL);
169: EPSGetInterval(eps,&inta,&intb);
170: PetscPrintf(PETSC_COMM_WORLD," %" PetscInt_FMT " eigenvalues found in [%g, %g]\n",nev,(double)inta,(double)intb);
172: /*
173: Show detailed info unless -terse option is given by user
174: */
175: PetscOptionsHasName(NULL,NULL,"-terse",&terse);
176: if (terse) EPSErrorView(eps,EPS_ERROR_RELATIVE,NULL);
177: else {
178: PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL);
179: EPSConvergedReasonView(eps,PETSC_VIEWER_STDOUT_WORLD);
180: EPSErrorView(eps,EPS_ERROR_RELATIVE,PETSC_VIEWER_STDOUT_WORLD);
181: PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);
182: }
184: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
185: Clean up
186: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
187: EPSDestroy(&eps);
188: MatDestroy(&A);
189: MatDestroy(&B);
190: SlepcFinalize();
191: return 0;
192: }
194: /*TEST
196: testset:
197: args: -terse
198: test:
199: requires: !mumps
200: test:
201: requires: mumps !complex
203: TEST*/