Actual source code: test12.c

slepc-3.19.2 2023-09-05
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: {
 20:   PetscFunctionBeginUser;
 21:   PetscCall(VecCopy(x,y));
 22:   PetscFunctionReturn(PETSC_SUCCESS);
 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;

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

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

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

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

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

 78:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 79:                     Display solution and clean up
 80:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 81:   PetscCall(EPSErrorView(eps,EPS_ERROR_RELATIVE,NULL));
 82:   PetscCall(EPSDestroy(&eps));
 83:   PetscCall(MatDestroy(&A));
 84:   PetscCall(VecDestroy(&v0));
 85:   PetscCall(PetscRandomDestroy(&rand));
 86:   PetscCall(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*/