Actual source code: ex25.c

slepc-3.18.0 2022-10-01
Report Typos and Errors
  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

 35:   SlepcInitialize(&argc,&argv,(char*)0,help);

 37:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
 38:   PetscOptionsGetInt(NULL,NULL,"-m",&m,&flag);
 39:   PetscOptionsGetBool(NULL,NULL,"-show_inertias",&show,NULL);
 40:   if (!flag) m=n;
 41:   N = n*m;
 42:   PetscPrintf(PETSC_COMM_WORLD,"\nSpectrum slicing on GHEP, N=%" PetscInt_FMT " (%" PetscInt_FMT "x%" PetscInt_FMT " grid)\n\n",N,n,m);

 44:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 45:      Compute the matrices that define the eigensystem, Ax=kBx
 46:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 48:   MatCreate(PETSC_COMM_WORLD,&A);
 49:   MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,N,N);
 50:   MatSetFromOptions(A);
 51:   MatSetUp(A);

 53:   MatCreate(PETSC_COMM_WORLD,&B);
 54:   MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,N,N);
 55:   MatSetFromOptions(B);
 56:   MatSetUp(B);

 58:   MatGetOwnershipRange(A,&Istart,&Iend);
 59:   for (II=Istart;II<Iend;II++) {
 60:     i = II/n; j = II-i*n;
 61:     if (i>0) MatSetValue(A,II,II-n,-1.0,INSERT_VALUES);
 62:     if (i<m-1) MatSetValue(A,II,II+n,-1.0,INSERT_VALUES);
 63:     if (j>0) MatSetValue(A,II,II-1,-1.0,INSERT_VALUES);
 64:     if (j<n-1) MatSetValue(A,II,II+1,-1.0,INSERT_VALUES);
 65:     MatSetValue(A,II,II,4.0,INSERT_VALUES);
 66:     MatSetValue(B,II,II,4.0,INSERT_VALUES);
 67:   }

 69:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
 70:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
 71:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
 72:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);

 74:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 75:                 Create the eigensolver and set various options
 76:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 78:   /*
 79:      Create eigensolver context
 80:   */
 81:   EPSCreate(PETSC_COMM_WORLD,&eps);

 83:   /*
 84:      Set operators and set problem type
 85:   */
 86:   EPSSetOperators(eps,A,B);
 87:   EPSSetProblemType(eps,EPS_GHEP);

 89:   /*
 90:      Set interval for spectrum slicing
 91:   */
 92:   inta = 0.1;
 93:   intb = 0.2;
 94:   EPSSetInterval(eps,inta,intb);
 95:   EPSSetWhichEigenpairs(eps,EPS_ALL);

 97:   /*
 98:      Spectrum slicing requires Krylov-Schur
 99:   */
100:   EPSSetType(eps,EPSKRYLOVSCHUR);

102:   /*
103:      Set shift-and-invert with Cholesky; select MUMPS if available
104:   */

106:   EPSGetST(eps,&st);
107:   STSetType(st,STSINVERT);
108:   EPSKrylovSchurGetKSP(eps,&ksp);
109:   KSPSetType(ksp,KSPPREONLY);
110:   KSPGetPC(ksp,&pc);
111:   PCSetType(pc,PCCHOLESKY);

113:   /*
114:      Use MUMPS if available.
115:      Note that in complex scalars we cannot use MUMPS for spectrum slicing,
116:      because MatGetInertia() is not available in that case.
117:   */
118: #if defined(PETSC_HAVE_MUMPS) && !defined(PETSC_USE_COMPLEX)
119:   EPSKrylovSchurSetDetectZeros(eps,PETSC_TRUE);  /* enforce zero detection */
120:   PCFactorSetMatSolverType(pc,MATSOLVERMUMPS);
121:   PCFactorSetUpMatSolverType(pc);
122:   /*
123:      Set several MUMPS options, the corresponding command-line options are:
124:      '-st_mat_mumps_icntl_13 1': turn off ScaLAPACK for matrix inertia
125:      '-st_mat_mumps_icntl_24 1': detect null pivots in factorization (for the case that a shift is equal to an eigenvalue)
126:      '-st_mat_mumps_cntl_3 <tol>': a tolerance used for null pivot detection (must be larger than machine epsilon)

128:      Note: depending on the interval, it may be necessary also to increase the workspace:
129:      '-st_mat_mumps_icntl_14 <percentage>': increase workspace with a percentage (50, 100 or more)
130:   */
131:   PCFactorGetMatrix(pc,&F);
132:   MatMumpsSetIcntl(F,13,1);
133:   MatMumpsSetIcntl(F,24,1);
134:   MatMumpsSetCntl(F,3,1e-12);
135: #endif

137:   /*
138:      Set solver parameters at runtime
139:   */
140:   EPSSetFromOptions(eps);

142:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
143:                       Solve the eigensystem
144:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
145:   EPSSetUp(eps);
146:   if (show) {
147:     EPSKrylovSchurGetInertias(eps,&ns,&shifts,&inertias);
148:     PetscPrintf(PETSC_COMM_WORLD,"Subintervals (after setup):\n");
149:     for (i=0;i<ns;i++) PetscPrintf(PETSC_COMM_WORLD,"Shift %g  Inertia %" PetscInt_FMT " \n",(double)shifts[i],inertias[i]);
150:     PetscPrintf(PETSC_COMM_WORLD,"\n");
151:     PetscFree(shifts);
152:     PetscFree(inertias);
153:   }
154:   EPSSolve(eps);
155:   if (show) {
156:     EPSKrylovSchurGetInertias(eps,&ns,&shifts,&inertias);
157:     PetscPrintf(PETSC_COMM_WORLD,"All shifts (after solve):\n");
158:     for (i=0;i<ns;i++) PetscPrintf(PETSC_COMM_WORLD,"Shift %g  Inertia %" PetscInt_FMT " \n",(double)shifts[i],inertias[i]);
159:     PetscPrintf(PETSC_COMM_WORLD,"\n");
160:     PetscFree(shifts);
161:     PetscFree(inertias);
162:   }

164:   /*
165:      Show eigenvalues in interval and print solution
166:   */
167:   EPSGetType(eps,&type);
168:   PetscPrintf(PETSC_COMM_WORLD," Solution method: %s\n\n",type);
169:   EPSGetDimensions(eps,&nev,NULL,NULL);
170:   EPSGetInterval(eps,&inta,&intb);
171:   PetscPrintf(PETSC_COMM_WORLD," %" PetscInt_FMT " eigenvalues found in [%g, %g]\n",nev,(double)inta,(double)intb);

173:   /*
174:      Show detailed info unless -terse option is given by user
175:    */
176:   PetscOptionsHasName(NULL,NULL,"-terse",&terse);
177:   if (terse) EPSErrorView(eps,EPS_ERROR_RELATIVE,NULL);
178:   else {
179:     PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL);
180:     EPSConvergedReasonView(eps,PETSC_VIEWER_STDOUT_WORLD);
181:     EPSErrorView(eps,EPS_ERROR_RELATIVE,PETSC_VIEWER_STDOUT_WORLD);
182:     PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);
183:   }

185:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
186:                     Clean up
187:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
188:   EPSDestroy(&eps);
189:   MatDestroy(&A);
190:   MatDestroy(&B);
191:   SlepcFinalize();
192:   return 0;
193: }

195: /*TEST

197:    testset:
198:       args: -terse
199:       test:
200:          requires: !mumps
201:       test:
202:          requires: mumps !complex

204: TEST*/