Actual source code: test12.c

slepc-3.18.3 2023-03-24
Report Typos and Errors
  1: /*
  2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
  4:    Copyright (c) 2002-, 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: {
 21:   VecCopy(x,y);
 22:   return 0;
 23: }

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

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

 40:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
 41:   PetscPrintf(PETSC_COMM_WORLD,"\nDiagonal Eigenproblem, n=%" PetscInt_FMT "\n\n",n);

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

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

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

 78:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 79:                     Display solution and clean up
 80:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 81:   EPSErrorView(eps,EPS_ERROR_RELATIVE,NULL);
 82:   EPSDestroy(&eps);
 83:   MatDestroy(&A);
 84:   VecDestroy(&v0);
 85:   PetscRandomDestroy(&rand);
 86:   SlepcFinalize();
 87:   return 0;
 88: }

 90: /*TEST

 92:    testset:
 93:       requires: !single
 94:       output_file: output/test12_1.out
 95:       test:
 96:          suffix: 1
 97:          args: -eps_type {{krylovschur subspace arnoldi gd jd}} -eps_nev 4
 98:       test:
 99:          suffix: 1_power
100:          args: -eps_type power -eps_max_it 10000 -eps_nev 4
101:       test:
102:          suffix: 1_gd2
103:          args: -eps_type gd -eps_gd_double_expansion -eps_nev 4

105: TEST*/