Actual source code: ex25.c

slepc-3.11.2 2019-07-30
Report Typos and Errors
  1: /*
  2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
  4:    Copyright (c) 2002-2019, 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;

 32:   SlepcInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;

 34:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
 35:   PetscOptionsGetInt(NULL,NULL,"-m",&m,&flag);
 36:   PetscOptionsGetBool(NULL,NULL,"-show_inertias",&show,NULL);
 37:   if (!flag) m=n;
 38:   N = n*m;
 39:   PetscPrintf(PETSC_COMM_WORLD,"\nSpectrum slicing on GHEP, N=%D (%Dx%D grid)\n\n",N,n,m);

 41:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 42:      Compute the matrices that define the eigensystem, Ax=kBx
 43:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 45:   MatCreate(PETSC_COMM_WORLD,&A);
 46:   MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,N,N);
 47:   MatSetFromOptions(A);
 48:   MatSetUp(A);

 50:   MatCreate(PETSC_COMM_WORLD,&B);
 51:   MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,N,N);
 52:   MatSetFromOptions(B);
 53:   MatSetUp(B);

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

 66:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
 67:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
 68:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
 69:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);

 71:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 72:                 Create the eigensolver and set various options
 73:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 75:   /*
 76:      Create eigensolver context
 77:   */
 78:   EPSCreate(PETSC_COMM_WORLD,&eps);

 80:   /*
 81:      Set operators and set problem type
 82:   */
 83:   EPSSetOperators(eps,A,B);
 84:   EPSSetProblemType(eps,EPS_GHEP);

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

 94:   /*
 95:      Spectrum slicing requires Krylov-Schur
 96:   */
 97:   EPSSetType(eps,EPSKRYLOVSCHUR);

 99:   /*
100:      Set shift-and-invert with Cholesky; select MUMPS if available
101:   */

103:   EPSGetST(eps,&st);
104:   STSetType(st,STSINVERT);

106:   STGetKSP(st,&ksp);
107:   KSPSetType(ksp,KSPPREONLY);
108:   KSPGetPC(ksp,&pc);
109:   PCSetType(pc,PCCHOLESKY);

111: #if defined(PETSC_HAVE_MUMPS)
112: #if defined(PETSC_USE_COMPLEX)
113:   SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP,"Spectrum slicing with MUMPS is not available for complex scalars");
114: #endif
115:   EPSKrylovSchurSetDetectZeros(eps,PETSC_TRUE);  /* enforce zero detection */
116:   PCFactorSetMatSolverType(pc,MATSOLVERMUMPS);
117:   /*
118:      Add several MUMPS options (currently there is no better way of setting this in program):
119:      '-mat_mumps_icntl_13 1': turn off ScaLAPACK for matrix inertia
120:      '-mat_mumps_icntl_24 1': detect null pivots in factorization (for the case that a shift is equal to an eigenvalue)
121:      '-mat_mumps_cntl_3 <tol>': a tolerance used for null pivot detection (must be larger than machine epsilon)

123:      Note: depending on the interval, it may be necessary also to increase the workspace:
124:      '-mat_mumps_icntl_14 <percentage>': increase workspace with a percentage (50, 100 or more)
125:   */
126:   PetscOptionsInsertString(NULL,"-mat_mumps_icntl_13 1 -mat_mumps_icntl_24 1 -mat_mumps_cntl_3 1e-12");
127: #endif

129:   /*
130:      Set solver parameters at runtime
131:   */
132:   EPSSetFromOptions(eps);

134:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
135:                       Solve the eigensystem
136:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
137:   EPSSetUp(eps);
138:   if (show) {
139:     EPSKrylovSchurGetInertias(eps,&ns,&shifts,&inertias);
140:     PetscPrintf(PETSC_COMM_WORLD,"Subintervals (after setup):\n");
141:     for (i=0;i<ns;i++) { PetscPrintf(PETSC_COMM_WORLD,"Shift %g  Inertia %D \n",(double)shifts[i],inertias[i]); }
142:     PetscPrintf(PETSC_COMM_WORLD,"\n");
143:     PetscFree(shifts);
144:     PetscFree(inertias);
145:   }
146:   EPSSolve(eps);
147:   if (show) {
148:     EPSKrylovSchurGetInertias(eps,&ns,&shifts,&inertias);
149:     PetscPrintf(PETSC_COMM_WORLD,"All shifts (after solve):\n");
150:     for (i=0;i<ns;i++) { PetscPrintf(PETSC_COMM_WORLD,"Shift %g  Inertia %D \n",(double)shifts[i],inertias[i]); }
151:     PetscPrintf(PETSC_COMM_WORLD,"\n");
152:     PetscFree(shifts);
153:     PetscFree(inertias);
154:   }

156:   /*
157:      Show eigenvalues in interval and print solution
158:   */
159:   EPSGetType(eps,&type);
160:   PetscPrintf(PETSC_COMM_WORLD," Solution method: %s\n\n",type);
161:   EPSGetDimensions(eps,&nev,NULL,NULL);
162:   EPSGetInterval(eps,&inta,&intb);
163:   PetscPrintf(PETSC_COMM_WORLD," %D eigenvalues found in [%g, %g]\n",nev,(double)inta,(double)intb);

165:   /*
166:      Show detailed info unless -terse option is given by user
167:    */
168:   PetscOptionsHasName(NULL,NULL,"-terse",&terse);
169:   if (terse) {
170:     EPSErrorView(eps,EPS_ERROR_RELATIVE,NULL);
171:   } else {
172:     PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL);
173:     EPSReasonView(eps,PETSC_VIEWER_STDOUT_WORLD);
174:     EPSErrorView(eps,EPS_ERROR_RELATIVE,PETSC_VIEWER_STDOUT_WORLD);
175:     PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);
176:   }

178:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
179:                     Clean up
180:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
181:   EPSDestroy(&eps);
182:   MatDestroy(&A);
183:   MatDestroy(&B);
184:   SlepcFinalize();
185:   return ierr;
186: }

188: /*TEST

190:    testset:
191:       args: -terse
192:       requires: !complex
193:       output_file: output/ex25_1.out
194:       test:
195:          suffix: 1
196:          requires: !single
197:       test:
198:          suffix: 1_single
199:          args: -eps_tol 1e-5
200:          requires: single

202: TEST*/