Actual source code: ex1.c

  2: static char help[] = "Solves a standard symmetric eigenproblem corresponding to the Laplacian operator in 1 dimension.\n\n"
  3:   "The command line options are:\n\n"
  4:   "  -n <n>, where <n> = number of grid subdivisions = matrix dimension.\n\n";

 6:  #include slepceps.h

 10: int main( int argc, char **argv )
 11: {
 12:   Mat         A;               /* operator matrix */
 13:   EPS         eps;             /* eigenproblem solver context */
 14:   EPSType     type;
 15:   PetscReal   error, tol,re, im;
 16:   PetscScalar kr, ki;
 17:   int         n=30, nev, ierr, maxit, i, its, nconv,
 18:               col[3], Istart, Iend, FirstBlock=0, LastBlock=0;
 19:   PetscScalar value[3];

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

 23:   PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);
 24:   PetscPrintf(PETSC_COMM_WORLD,"\n1-D Laplacian Eigenproblem, n=%d\n\n",n);
 25: 

 27:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 28:      Compute the operator matrix that defines the eigensystem, Ax=kx
 29:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 31:   MatCreate(PETSC_COMM_WORLD,PETSC_DECIDE,PETSC_DECIDE,n,n,&A);
 32:   MatSetFromOptions(A);
 33: 
 34:   MatGetOwnershipRange(A,&Istart,&Iend);
 35:   if (Istart==0) FirstBlock=PETSC_TRUE;
 36:   if (Iend==n) LastBlock=PETSC_TRUE;
 37:   value[0]=-1.0; value[1]=2.0; value[2]=-1.0;
 38:   for( i=(FirstBlock? Istart+1: Istart); i<(LastBlock? Iend-1: Iend); i++ ) {
 39:     col[0]=i-1; col[1]=i; col[2]=i+1;
 40:     MatSetValues(A,1,&i,3,col,value,INSERT_VALUES);
 41:   }
 42:   if (LastBlock) {
 43:     i=n-1; col[0]=n-2; col[1]=n-1;
 44:     MatSetValues(A,1,&i,2,col,value,INSERT_VALUES);
 45:   }
 46:   if (FirstBlock) {
 47:     i=0; col[0]=0; col[1]=1; value[0]=2.0; value[1]=-1.0;
 48:     MatSetValues(A,1,&i,2,col,value,INSERT_VALUES);
 49:   }

 51:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
 52:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);

 54:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 55:                 Create the eigensolver and set various options
 56:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 58:   /* 
 59:      Create eigensolver context
 60:   */
 61:   EPSCreate(PETSC_COMM_WORLD,&eps);

 63:   /* 
 64:      Set operators. In this case, it is a standard eigenvalue problem
 65:   */
 66:   EPSSetOperators(eps,A,PETSC_NULL);
 67:   EPSSetProblemType(eps,EPS_HEP);

 69:   /*
 70:      Set solver parameters at runtime
 71:   */
 72:   EPSSetFromOptions(eps);

 74:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 75:                       Solve the eigensystem
 76:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 78:   EPSSolve(eps);
 79:   EPSGetIterationNumber(eps, &its);
 80:   PetscPrintf(PETSC_COMM_WORLD," Number of iterations of the method: %d\n",its);
 81: 
 82:   /*
 83:      Optional: Get some information from the solver and display it
 84:   */
 85:   EPSGetType(eps,&type);
 86:   PetscPrintf(PETSC_COMM_WORLD," Solution method: %s\n\n",type);
 87:   EPSGetDimensions(eps,&nev,PETSC_NULL);
 88:   PetscPrintf(PETSC_COMM_WORLD," Number of requested eigenvalues: %d\n",nev);
 89: 
 90:   EPSGetTolerances(eps,&tol,&maxit);
 91:   PetscPrintf(PETSC_COMM_WORLD," Stopping condition: tol=%.4g, maxit=%d\n",tol,maxit);
 92: 


 95:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 96:                     Display solution and clean up
 97:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 99:   /* 
100:      Get number of converged approximate eigenpairs
101:   */
102:   EPSGetConverged(eps,&nconv);
103:   PetscPrintf(PETSC_COMM_WORLD," Number of converged eigenpairs: %d\n\n",nconv);
104: 

106:   if (nconv>0) {
107:     /*
108:        Display eigenvalues and relative errors
109:     */
110:     PetscPrintf(PETSC_COMM_WORLD,
111:          "           k          ||Ax-kx||/||kx||\n"
112:          "   ----------------- ------------------\n" );

114:     for( i=0; i<nconv; i++ ) {
115:       /* 
116:         Get converged eigenpairs: i-th eigenvalue is stored in kr (real part) and
117:         ki (imaginary part)
118:       */
119:       EPSGetEigenpair(eps,i,&kr,&ki,PETSC_NULL,PETSC_NULL);
120:       /*
121:          Compute the relative error associated to each eigenpair
122:       */
123:       EPSComputeRelativeError(eps,i,&error);

125: #ifdef PETSC_USE_COMPLEX
126:       re = PetscRealPart(kr);
127:       im = PetscImaginaryPart(kr);
128: #else
129:       re = kr;
130:       im = ki;
131: #endif 
132:       if (im!=0.0) {
133:         PetscPrintf(PETSC_COMM_WORLD," %9f%+9f j %12f\n",re,im,error);
134:       } else {
135:         PetscPrintf(PETSC_COMM_WORLD,"   %12f       %12f\n",re,error);
136:       }
137:     }
138:     PetscPrintf(PETSC_COMM_WORLD,"\n" );
139:   }
140: 
141:   /* 
142:      Free work space
143:   */
144:   EPSDestroy(eps);
145:   MatDestroy(A);
146:   SlepcFinalize();
147:   return 0;
148: }