Actual source code: test1.c

slepc-3.5.2 2014-10-10
Report Typos and Errors
  1: /*
  2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
  4:    Copyright (c) 2002-2014, Universitat Politecnica de Valencia, Spain

  6:    This file is part of SLEPc.

  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 PEP without calling PEPSetFromOptions (based on ex16.c).\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"
 26:   "  -type <pep_type> = pep type to test.\n"
 27:   "  -epstype <eps_type> = eps type to test (for linear).\n\n";

 29: #include <slepcpep.h>

 33: int main(int argc,char **argv)
 34: {
 35:   Mat            M,C,K,A[3];      /* problem matrices */
 36:   PEP            pep;             /* polynomial eigenproblem solver context */
 37:   PEPType        type;
 38:   PetscInt       N,n=10,m,Istart,Iend,II,nev,maxit,i,j;
 39:   PetscBool      flag,isgd2,epsgiven;
 40:   char           peptype[30] = "linear",epstype[30] = "";
 41:   EPS            eps;
 42:   ST             st;
 43:   KSP            ksp;
 44:   PC             pc;

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

 49:   PetscOptionsGetInt(NULL,"-n",&n,NULL);
 50:   PetscOptionsGetInt(NULL,"-m",&m,&flag);
 51:   if (!flag) m=n;
 52:   N = n*m;
 53:   PetscOptionsGetString(NULL,"-type",peptype,30,NULL);
 54:   PetscOptionsGetString(NULL,"-epstype",epstype,30,&epsgiven);
 55:   PetscPrintf(PETSC_COMM_WORLD,"\nQuadratic Eigenproblem, N=%D (%Dx%D grid)",N,n,m);
 56:   PetscPrintf(PETSC_COMM_WORLD,"\nPEP type: %s",peptype);
 57:   if (epsgiven) {
 58:     PetscPrintf(PETSC_COMM_WORLD,"\nEPS type: %s",epstype);
 59:   }
 60:   PetscPrintf(PETSC_COMM_WORLD,"\n\n");

 62:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 63:      Compute the matrices that define the eigensystem, (k^2*M+k*C+K)x=0
 64:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 66:   /* K is the 2-D Laplacian */
 67:   MatCreate(PETSC_COMM_WORLD,&K);
 68:   MatSetSizes(K,PETSC_DECIDE,PETSC_DECIDE,N,N);
 69:   MatSetFromOptions(K);
 70:   MatSetUp(K);

 72:   MatGetOwnershipRange(K,&Istart,&Iend);
 73:   for (II=Istart;II<Iend;II++) {
 74:     i = II/n; j = II-i*n;
 75:     if (i>0) { MatSetValue(K,II,II-n,-1.0,INSERT_VALUES); }
 76:     if (i<m-1) { MatSetValue(K,II,II+n,-1.0,INSERT_VALUES); }
 77:     if (j>0) { MatSetValue(K,II,II-1,-1.0,INSERT_VALUES); }
 78:     if (j<n-1) { MatSetValue(K,II,II+1,-1.0,INSERT_VALUES); }
 79:     MatSetValue(K,II,II,4.0,INSERT_VALUES);
 80:   }

 82:   MatAssemblyBegin(K,MAT_FINAL_ASSEMBLY);
 83:   MatAssemblyEnd(K,MAT_FINAL_ASSEMBLY);

 85:   /* C is the zero matrix */
 86:   MatCreate(PETSC_COMM_WORLD,&C);
 87:   MatSetSizes(C,PETSC_DECIDE,PETSC_DECIDE,N,N);
 88:   MatSetFromOptions(C);
 89:   MatSetUp(C);
 90:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
 91:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);

 93:   /* M is the identity matrix */
 94:   MatCreate(PETSC_COMM_WORLD,&M);
 95:   MatSetSizes(M,PETSC_DECIDE,PETSC_DECIDE,N,N);
 96:   MatSetFromOptions(M);
 97:   MatSetUp(M);
 98:   MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);
 99:   MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);
100:   MatShift(M,1.0);

102:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
103:                 Create the eigensolver and set various options
104:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

106:   /*
107:      Create eigensolver context
108:   */
109:   PEPCreate(PETSC_COMM_WORLD,&pep);

111:   /*
112:      Set matrices and problem type
113:   */
114:   A[0] = K; A[1] = C; A[2] = M;
115:   PEPSetOperators(pep,3,A);
116:   PEPSetProblemType(pep,PEP_GENERAL);
117:   PEPSetDimensions(pep,4,20,PETSC_DEFAULT);
118:   PEPSetTolerances(pep,PETSC_SMALL,PETSC_DEFAULT);

120:   /*
121:      Set solver type at runtime
122:   */
123:   PEPSetType(pep,peptype);
124:   if (epsgiven) {
125:     PetscObjectTypeCompare((PetscObject)pep,PEPLINEAR,&flag);
126:     if (flag) {
127:       PEPLinearGetEPS(pep,&eps);
128:       PetscStrcmp(epstype,"gd2",&isgd2);
129:       if (isgd2) {
130:         EPSSetType(eps,EPSGD);
131:         EPSGDSetDoubleExpansion(eps,PETSC_TRUE);
132:       } else {
133:         EPSSetType(eps,epstype);
134:       }
135:       EPSGetST(eps,&st);
136:       STGetKSP(st,&ksp);
137:       KSPGetPC(ksp,&pc);
138:       PCSetType(pc,PCJACOBI);
139:       PetscObjectTypeCompare((PetscObject)eps,EPSGD,&flag);
140:     }
141:   }
142:   PetscObjectTypeCompare((PetscObject)pep,PEPQARNOLDI,&flag);
143:   if (flag) {
144:     PEPGetST(pep,&st);
145:     STSetTransform(st,PETSC_TRUE);
146:   }

148:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
149:                       Solve the eigensystem
150:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

152:   PEPSolve(pep);

154:   /*
155:      Optional: Get some information from the solver and display it
156:   */
157:   PEPGetType(pep,&type);
158:   PetscPrintf(PETSC_COMM_WORLD," Solution method: %s\n\n",type);
159:   PEPGetDimensions(pep,&nev,NULL,NULL);
160:   PetscPrintf(PETSC_COMM_WORLD," Number of requested eigenvalues: %D\n",nev);
161:   PEPGetTolerances(pep,NULL,&maxit);
162:   PetscPrintf(PETSC_COMM_WORLD," Stopping condition: maxit=%D\n",maxit);

164:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
165:                     Display solution and clean up
166:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

168:   PEPPrintSolution(pep,NULL);
169:   PEPDestroy(&pep);
170:   MatDestroy(&M);
171:   MatDestroy(&C);
172:   MatDestroy(&K);
173:   SlepcFinalize();
174:   return 0;
175: }