Actual source code: test4.c

  1: /*
  2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
  4:    Copyright (c) 2002-2011, Universitat Politecnica de Valencia, Spain

  6:    This file is part of SLEPc.
  7:       
  8:    SLEPc is free software: you can redistribute it and/or modify it under  the
  9:    terms of version 3 of the GNU Lesser General Public License as published by
 10:    the Free Software Foundation.

 12:    SLEPc  is  distributed in the hope that it will be useful, but WITHOUT  ANY 
 13:    WARRANTY;  without even the implied warranty of MERCHANTABILITY or  FITNESS 
 14:    FOR  A  PARTICULAR PURPOSE. See the GNU Lesser General Public  License  for 
 15:    more details.

 17:    You  should have received a copy of the GNU Lesser General  Public  License
 18:    along with SLEPc. If not, see <http://www.gnu.org/licenses/>.
 19:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 20: */

 22: static char help[] = "Test the solution of a HEP without calling EPSSetFromOptions (based on ex1.c).\n\n"
 23:   "The command line options are:\n"
 24:   "  -n <n>, where <n> = number of grid subdivisions = matrix dimension.\n"
 25:   "  -type <eps_type> = eps type to test.\n\n";

 27: #include <slepceps.h>

 31: int main(int argc,char **argv)
 32: {
 33:   Mat            A;           /* problem matrix */
 34:   EPS            eps;         /* eigenproblem solver context */
 35:   const EPSType  type;
 36:   PetscReal      tol=1000*PETSC_MACHINE_EPSILON;
 37:   PetscScalar    value[3];
 38:   PetscInt       n=30,i,Istart,Iend,col[3],nev;
 39:   PetscBool      FirstBlock=PETSC_FALSE,LastBlock=PETSC_FALSE;
 40:   char           epstype[30] = "krylovschur";

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

 45:   PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);
 46:   PetscOptionsGetString(PETSC_NULL,"-type",epstype,30,PETSC_NULL);
 47:   PetscPrintf(PETSC_COMM_WORLD,"\n1-D Laplacian Eigenproblem, n=%D",n);
 48:   PetscPrintf(PETSC_COMM_WORLD,"\nEPS type: %s\n\n",epstype);

 50:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 51:      Compute the operator matrix that defines the eigensystem, Ax=kx
 52:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 54:   MatCreate(PETSC_COMM_WORLD,&A);
 55:   MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,n,n);
 56:   MatSetFromOptions(A);
 57: 
 58:   MatGetOwnershipRange(A,&Istart,&Iend);
 59:   if (Istart==0) FirstBlock=PETSC_TRUE;
 60:   if (Iend==n) LastBlock=PETSC_TRUE;
 61:   value[0]=-1.0; value[1]=2.0; value[2]=-1.0;
 62:   for (i=(FirstBlock? Istart+1: Istart); i<(LastBlock? Iend-1: Iend); i++) {
 63:     col[0]=i-1; col[1]=i; col[2]=i+1;
 64:     MatSetValues(A,1,&i,3,col,value,INSERT_VALUES);
 65:   }
 66:   if (LastBlock) {
 67:     i=n-1; col[0]=n-2; col[1]=n-1;
 68:     MatSetValues(A,1,&i,2,col,value,INSERT_VALUES);
 69:   }
 70:   if (FirstBlock) {
 71:     i=0; col[0]=0; col[1]=1; value[0]=2.0; value[1]=-1.0;
 72:     MatSetValues(A,1,&i,2,col,value,INSERT_VALUES);
 73:   }

 75:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
 76:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);

 78:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 79:                 Create the eigensolver and set various options
 80:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 81:   /* 
 82:      Create eigensolver context
 83:   */
 84:   EPSCreate(PETSC_COMM_WORLD,&eps);

 86:   /* 
 87:      Set operators. In this case, it is a standard eigenvalue problem
 88:   */
 89:   EPSSetOperators(eps,A,PETSC_NULL);
 90:   EPSSetProblemType(eps,EPS_HEP);
 91:   EPSSetDimensions(eps,4,PETSC_DEFAULT,PETSC_DEFAULT);
 92:   EPSSetTolerances(eps,tol,PETSC_DEFAULT);

 94:   /*
 95:      Set solver parameters at runtime
 96:   */
 97:   EPSSetType(eps,epstype);

 99:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
100:                       Solve the eigensystem
101:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

103:   EPSSolve(eps);
104:   /*
105:      Optional: Get some information from the solver and display it
106:   */
107:   EPSGetType(eps,&type);
108:   PetscPrintf(PETSC_COMM_WORLD," Solution method: %s\n\n",type);
109:   EPSGetDimensions(eps,&nev,PETSC_NULL,PETSC_NULL);
110:   PetscPrintf(PETSC_COMM_WORLD," Number of requested eigenvalues: %D\n",nev);

112:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
113:                     Display solution and clean up
114:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

116:   EPSPrintSolution(eps,PETSC_NULL);
117:   EPSDestroy(&eps);
118:   MatDestroy(&A);
119:   SlepcFinalize();
120:   return 0;
121: }