Actual source code: test12.c

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

  6:    This file is part of SLEPc.
  7:    SLEPc is distributed under a 2-clause BSD license (see LICENSE).
  8:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  9: */

 11: static char help[] = "Diagonal eigenproblem. Illustrates use of shell preconditioner.\n\n"
 12:   "The command line options are:\n"
 13:   "  -n <n>, where <n> = number of grid subdivisions = matrix dimension.\n"
 14:   "  -seed <s>, where <s> = seed for random number generation.\n\n";

 16: #include <slepceps.h>

 18: PetscErrorCode PCApply_User(PC pc,Vec x,Vec y)
 19: {

 23:   VecCopy(x,y);
 24:   return(0);
 25: }

 27: int main(int argc,char **argv)
 28: {
 29:   Mat            A;           /* problem matrix */
 30:   EPS            eps;         /* eigenproblem solver context */
 31:   Vec            v0;          /* initial vector */
 32:   PetscRandom    rand;
 33:   PetscReal      tol=1000*PETSC_MACHINE_EPSILON;
 34:   PetscInt       n=30,i,Istart,Iend,seed=0x12345678;
 36:   ST             st;
 37:   KSP            ksp;
 38:   PC             pc;

 40:   SlepcInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;

 42:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
 43:   PetscPrintf(PETSC_COMM_WORLD,"\nDiagonal Eigenproblem, n=%D\n\n",n);

 45:   MatCreate(PETSC_COMM_WORLD,&A);
 46:   MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,n,n);
 47:   MatSetFromOptions(A);
 48:   MatSetUp(A);
 49:   MatGetOwnershipRange(A,&Istart,&Iend);
 50:   for (i=Istart;i<Iend;i++) {
 51:     MatSetValue(A,i,i,i+1,INSERT_VALUES);
 52:   }
 53:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
 54:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);

 56:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 57:                       Solve the eigensystem
 58:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 59:   EPSCreate(PETSC_COMM_WORLD,&eps);
 60:   EPSSetOperators(eps,A,NULL);
 61:   EPSSetProblemType(eps,EPS_HEP);
 62:   EPSSetTolerances(eps,tol,PETSC_DEFAULT);
 63:   EPSSetFromOptions(eps);
 64:   EPSGetST(eps,&st);
 65:   STGetKSP(st,&ksp);
 66:   KSPGetPC(ksp,&pc);
 67:   PCSetType(pc,PCSHELL);
 68:   PCShellSetApply(pc,PCApply_User);
 69:   STPrecondSetMatForPC(st,A);

 71:   /* set random initial vector */
 72:   MatCreateVecs(A,&v0,NULL);
 73:   PetscRandomCreate(PETSC_COMM_WORLD,&rand);
 74:   PetscRandomSetFromOptions(rand);
 75:   PetscOptionsGetInt(NULL,NULL,"-seed",&seed,NULL);
 76:   PetscRandomSetSeed(rand,seed);
 77:   PetscRandomSeed(rand);
 78:   VecSetRandom(v0,rand);
 79:   EPSSetInitialSpace(eps,1,&v0);
 80:   /* call the solver */
 81:   EPSSolve(eps);

 83:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 84:                     Display solution and clean up
 85:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 86:   EPSErrorView(eps,EPS_ERROR_RELATIVE,NULL);
 87:   EPSDestroy(&eps);
 88:   MatDestroy(&A);
 89:   VecDestroy(&v0);
 90:   PetscRandomDestroy(&rand);
 91:   SlepcFinalize();
 92:   return ierr;
 93: }

 95: /*TEST

 97:    testset:
 98:       requires: !single
 99:       output_file: output/test12_1.out
100:       test:
101:          suffix: 1
102:          args: -eps_type {{krylovschur subspace arnoldi gd jd}} -eps_nev 4
103:       test:
104:          suffix: 1_power
105:          args: -eps_type power -eps_max_it 10000 -eps_nev 4
106:       test:
107:          suffix: 1_gd2
108:          args: -eps_type gd -eps_gd_double_expansion -eps_nev 4

110: TEST*/