Actual source code: ex2.c

  1: /*
  2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
  4:    Copyright (c) 2002-2009, Universidad 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[] = "Standard symmetric eigenproblem corresponding to the Laplacian operator in 2 dimensions.\n\n"
 23:   "The command line options are:\n"
 24:   "  -n <n>, where <n> = number of grid subdivisions in x dimension.\n"
 25:   "  -m <m>, where <m> = number of grid subdivisions in y dimension.\n\n";

 27:  #include slepceps.h

 31: int main( int argc, char **argv )
 32: {
 33:   Mat                  A;                  /* operator matrix */
 34:   EPS                  eps;                  /* eigenproblem solver context */
 35:   const EPSType  type;
 36:   PetscReal            error, tol, re, im;
 37:   PetscScalar          kr, ki;
 39:   PetscInt             N, n=10, m, Istart, Iend, II, J, nev, maxit, i, j, its, nconv;
 40:   PetscScalar          v;
 41:   PetscTruth           flag;

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

 45:   PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);
 46:   PetscOptionsGetInt(PETSC_NULL,"-m",&m,&flag);
 47:   if( flag==PETSC_FALSE ) m=n;
 48:   N = n*m;
 49:   PetscPrintf(PETSC_COMM_WORLD,"\n2-D Laplacian Eigenproblem, N=%d (%dx%d grid)\n\n",N,n,m);

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

 55:   MatCreate(PETSC_COMM_WORLD,&A);
 56:   MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,N,N);
 57:   MatSetFromOptions(A);
 58: 
 59:   MatGetOwnershipRange(A,&Istart,&Iend);
 60:   for( II=Istart; II<Iend; II++ ) {
 61:     v = -1.0; i = II/n; j = II-i*n;
 62:     if(i>0) { J=II-n; MatSetValues(A,1,&II,1,&J,&v,INSERT_VALUES); }
 63:     if(i<m-1) { J=II+n; MatSetValues(A,1,&II,1,&J,&v,INSERT_VALUES); }
 64:     if(j>0) { J=II-1; MatSetValues(A,1,&II,1,&J,&v,INSERT_VALUES); }
 65:     if(j<n-1) { J=II+1; MatSetValues(A,1,&II,1,&J,&v,INSERT_VALUES); }
 66:     v=4.0; MatSetValues(A,1,&II,1,&II,&v,INSERT_VALUES);
 67:   }

 69:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
 70:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);

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

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

 81:   /* 
 82:      Set operators. In this case, it is a standard eigenvalue problem
 83:   */
 84:   EPSSetOperators(eps,A,PETSC_NULL);
 85:   EPSSetProblemType(eps,EPS_HEP);

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

 92:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 93:                       Solve the eigensystem
 94:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 96:   EPSSolve(eps);
 97:   EPSGetIterationNumber(eps, &its);
 98:   PetscPrintf(PETSC_COMM_WORLD," Number of iterations of the method: %d\n",its);

100:   /*
101:      Optional: Get some information from the solver and display it
102:   */
103:   EPSGetType(eps,&type);
104:   PetscPrintf(PETSC_COMM_WORLD," Solution method: %s\n\n",type);
105:   EPSGetDimensions(eps,&nev,PETSC_NULL,PETSC_NULL);
106:   PetscPrintf(PETSC_COMM_WORLD," Number of requested eigenvalues: %d\n",nev);
107:   EPSGetTolerances(eps,&tol,&maxit);
108:   PetscPrintf(PETSC_COMM_WORLD," Stopping condition: tol=%.4g, maxit=%d\n",tol,maxit);

110:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
111:                     Display solution and clean up
112:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

114:   /* 
115:      Get number of converged approximate eigenpairs
116:   */
117:   EPSGetConverged(eps,&nconv);
118:   PetscPrintf(PETSC_COMM_WORLD," Number of converged approximate eigenpairs: %d\n\n",nconv);
119: 

121:   if (nconv>0) {
122:     /*
123:        Display eigenvalues and relative errors
124:     */
125:     PetscPrintf(PETSC_COMM_WORLD,
126:          "           k          ||Ax-kx||/||kx||\n"
127:          "   ----------------- ------------------\n" );

129:     for( i=0; i<nconv; i++ ) {
130:       /* 
131:         Get converged eigenpairs: i-th eigenvalue is stored in kr (real part) and
132:         ki (imaginary part)
133:       */
134:       EPSGetEigenpair(eps,i,&kr,&ki,PETSC_NULL,PETSC_NULL);
135:       /*
136:          Compute the relative error associated to each eigenpair
137:       */
138:       EPSComputeRelativeError(eps,i,&error);

140: #ifdef PETSC_USE_COMPLEX
141:       re = PetscRealPart(kr);
142:       im = PetscImaginaryPart(kr);
143: #else
144:       re = kr;
145:       im = ki;
146: #endif 
147:       if (im!=0.0) {
148:         PetscPrintf(PETSC_COMM_WORLD," %9f%+9f j %12g\n",re,im,error);
149:       } else {
150:         PetscPrintf(PETSC_COMM_WORLD,"   %12f       %12g\n",re,error);
151:       }
152:     }
153:     PetscPrintf(PETSC_COMM_WORLD,"\n" );
154:   }
155: 
156:   /* 
157:      Free work space
158:   */
159:   EPSDestroy(eps);
160:   MatDestroy(A);
161:   SlepcFinalize();
162:   return 0;
163: }