Actual source code: test1.c

slepc-3.6.3 2016-03-29
Report Typos and Errors
  1: /*
  2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
  4:    Copyright (c) 2002-2015, 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:   MatGetOwnershipRange(M,&Istart,&Iend);
 99:   for (i=Istart;i<Iend;i++) {
100:     MatSetValue(M,i,i,1.0,INSERT_VALUES);
101:   }
102:   MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);
103:   MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);

105:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
106:                 Create the eigensolver and set various options
107:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

109:   /*
110:      Create eigensolver context
111:   */
112:   PEPCreate(PETSC_COMM_WORLD,&pep);

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

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

152:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
153:                       Solve the eigensystem
154:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

156:   PEPSolve(pep);

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

168:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
169:                     Display solution and clean up
170:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

172:   PEPErrorView(pep,PEP_ERROR_BACKWARD,NULL);
173:   PEPDestroy(&pep);
174:   MatDestroy(&M);
175:   MatDestroy(&C);
176:   MatDestroy(&K);
177:   SlepcFinalize();
178:   return 0;
179: }