Actual source code: ex2.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[] = "Standard symmetric eigenproblem corresponding to the Laplacian operator in 2 dimensions.\n\n"
 12:   "The command line options are:\n"
 13:   "  -n <n>, where <n> = number of grid subdivisions in x dimension.\n"
 14:   "  -m <m>, where <m> = number of grid subdivisions in y dimension.\n\n";

 16: #include <slepceps.h>

 18: int main(int argc,char **argv)
 19: {
 20:   Mat            A;               /* operator matrix */
 21:   EPS            eps;             /* eigenproblem solver context */
 22:   EPSType        type;
 23:   PetscInt       N,n=10,m,Istart,Iend,II,nev,i,j;
 24:   PetscBool      flag,terse;

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

 29:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
 30:   PetscOptionsGetInt(NULL,NULL,"-m",&m,&flag);
 31:   if (!flag) m=n;
 32:   N = n*m;
 33:   PetscPrintf(PETSC_COMM_WORLD,"\n2-D Laplacian Eigenproblem, N=%D (%Dx%D grid)\n\n",N,n,m);

 35:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 36:      Compute the operator matrix that defines the eigensystem, Ax=kx
 37:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 39:   MatCreate(PETSC_COMM_WORLD,&A);
 40:   MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,N,N);
 41:   MatSetFromOptions(A);
 42:   MatSetUp(A);

 44:   MatGetOwnershipRange(A,&Istart,&Iend);
 45:   for (II=Istart;II<Iend;II++) {
 46:     i = II/n; j = II-i*n;
 47:     if (i>0) { MatSetValue(A,II,II-n,-1.0,INSERT_VALUES); }
 48:     if (i<m-1) { MatSetValue(A,II,II+n,-1.0,INSERT_VALUES); }
 49:     if (j>0) { MatSetValue(A,II,II-1,-1.0,INSERT_VALUES); }
 50:     if (j<n-1) { MatSetValue(A,II,II+1,-1.0,INSERT_VALUES); }
 51:     MatSetValue(A,II,II,4.0,INSERT_VALUES);
 52:   }

 54:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
 55:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);

 57:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 58:                 Create the eigensolver and set various options
 59:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 61:   /*
 62:      Create eigensolver context
 63:   */
 64:   EPSCreate(PETSC_COMM_WORLD,&eps);

 66:   /*
 67:      Set operators. In this case, it is a standard eigenvalue problem
 68:   */
 69:   EPSSetOperators(eps,A,NULL);
 70:   EPSSetProblemType(eps,EPS_HEP);

 72:   /*
 73:      Set solver parameters at runtime
 74:   */
 75:   EPSSetFromOptions(eps);

 77:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 78:                       Solve the eigensystem
 79:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 81:   EPSSolve(eps);

 83:   /*
 84:      Optional: Get some information from the solver and display it
 85:   */
 86:   EPSGetType(eps,&type);
 87:   PetscPrintf(PETSC_COMM_WORLD," Solution method: %s\n\n",type);
 88:   EPSGetDimensions(eps,&nev,NULL,NULL);
 89:   PetscPrintf(PETSC_COMM_WORLD," Number of requested eigenvalues: %D\n",nev);

 91:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 92:                     Display solution and clean up
 93:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 95:   /* show detailed info unless -terse option is given by user */
 96:   PetscOptionsHasName(NULL,NULL,"-terse",&terse);
 97:   if (terse) {
 98:     EPSErrorView(eps,EPS_ERROR_RELATIVE,NULL);
 99:   } else {
100:     PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL);
101:     EPSReasonView(eps,PETSC_VIEWER_STDOUT_WORLD);
102:     EPSErrorView(eps,EPS_ERROR_RELATIVE,PETSC_VIEWER_STDOUT_WORLD);
103:     PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);
104:   }
105:   EPSDestroy(&eps);
106:   MatDestroy(&A);
107:   SlepcFinalize();
108:   return ierr;
109: }

111: /*TEST

113:    testset:
114:       args: -n 72 -eps_nev 4 -eps_ncv 20 -terse
115:       requires: !single
116:       output_file: output/ex2_1.out
117:       test:
118:          suffix: 1
119:       test:
120:          suffix: 2
121:          args: -dynamic_library_preload

123:    testset:
124:       args: -n 30 -eps_type ciss -terse
125:       requires: double
126:       output_file: output/ex2_ciss.out
127:       test:
128:          suffix: ciss_1
129:          nsize: 1
130:          args: -rg_type interval -rg_interval_endpoints 1.1,1.25
131:       test:
132:          suffix: ciss_2
133:          nsize: 2
134:          args: -rg_type ellipse -rg_ellipse_center 1.175 -rg_ellipse_radius 0.075 -eps_ciss_partitions 2

136: TEST*/