Actual source code: ex13.c

slepc-3.11.1 2019-04-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[] = "Generalized Symmetric eigenproblem.\n\n"
 12:   "The problem is Ax = lambda Bx, with:\n"
 13:   "   A = Laplacian operator in 2-D\n"
 14:   "   B = diagonal matrix with all values equal to 4 except nulldim zeros\n\n"
 15:   "The command line options are:\n"
 16:   "  -n <n>, where <n> = number of grid subdivisions in x dimension.\n"
 17:   "  -m <m>, where <m> = number of grid subdivisions in y dimension.\n"
 18:   "  -nulldim <k>, where <k> = dimension of the nullspace of B.\n\n";

 20: #include <slepceps.h>

 22: int main(int argc,char **argv)
 23: {
 24:   Mat            A,B;         /* matrices */
 25:   EPS            eps;         /* eigenproblem solver context */
 26:   ST             st;          /* spectral transformation context */
 27:   EPSType        type;
 28:   PetscInt       N,n=10,m,Istart,Iend,II,nev,i,j,nulldim=0;
 29:   PetscBool      flag,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:   if (!flag) m=n;
 37:   N = n*m;
 38:   PetscOptionsGetInt(NULL,NULL,"-nulldim",&nulldim,NULL);
 39:   PetscPrintf(PETSC_COMM_WORLD,"\nGeneralized Symmetric Eigenproblem, N=%D (%Dx%D grid), null(B)=%D\n\n",N,n,m,nulldim);

 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:     if (II>=nulldim) { 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. In this case, it is a generalized eigenvalue problem
 82:   */
 83:   EPSSetOperators(eps,A,B);
 84:   EPSSetProblemType(eps,EPS_GHEP);

 86:   /*
 87:      Set solver parameters at runtime
 88:   */
 89:   EPSSetFromOptions(eps);

 91:   PetscObjectTypeCompareAny((PetscObject)eps,&flag,EPSBLOPEX,EPSLOBPCG,EPSRQCG,"");
 92:   if (flag) {
 93:     EPSSetWhichEigenpairs(eps,EPS_SMALLEST_REAL);
 94:   } else {
 95:     /*
 96:        Select portion of spectrum
 97:     */
 98:     EPSSetTarget(eps,0.0);
 99:     EPSSetWhichEigenpairs(eps,EPS_TARGET_MAGNITUDE);
100:     /*
101:        Use shift-and-invert to avoid solving linear systems with a singular B
102:        in case nulldim>0
103:     */
104:     PetscObjectTypeCompareAny((PetscObject)eps,&flag,EPSGD,EPSJD,"");
105:     if (!flag) {
106:       EPSGetST(eps,&st);
107:       STSetType(st,STSINVERT);
108:     }
109:   }

111:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
112:                       Solve the eigensystem
113:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

115:   EPSSolve(eps);

117:   /*
118:      Optional: Get some information from the solver and display it
119:   */
120:   EPSGetType(eps,&type);
121:   PetscPrintf(PETSC_COMM_WORLD," Solution method: %s\n\n",type);
122:   EPSGetDimensions(eps,&nev,NULL,NULL);
123:   PetscPrintf(PETSC_COMM_WORLD," Number of requested eigenvalues: %D\n",nev);

125:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
126:                     Display solution and clean up
127:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

129:   /* show detailed info unless -terse option is given by user */
130:   PetscOptionsHasName(NULL,NULL,"-terse",&terse);
131:   if (terse) {
132:     EPSErrorView(eps,EPS_ERROR_RELATIVE,NULL);
133:   } else {
134:     PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL);
135:     EPSReasonView(eps,PETSC_VIEWER_STDOUT_WORLD);
136:     EPSErrorView(eps,EPS_ERROR_RELATIVE,PETSC_VIEWER_STDOUT_WORLD);
137:     PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);
138:   }
139:   EPSDestroy(&eps);
140:   MatDestroy(&A);
141:   MatDestroy(&B);
142:   SlepcFinalize();
143:   return ierr;
144: }

146: /*TEST

148:    test:
149:       suffix: 1
150:       args: -eps_nev 4 -eps_ncv 22 -eps_tol 1e-5 -terse

152:    test:
153:       suffix: 2
154:       args: -n 110 -nulldim 6 -eps_nev 4 -eps_ncv 18 -eps_tol 1e-5 -eps_purify 1 -st_type sinvert -terse
155:       requires: !single

157: TEST*/