Date: 2026-05-01T10:02:32+00:00

Actual source code: test12.c

      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: typedef struct {
     19:   PetscScalar target;
     20: } PCCtx;

     22: PetscErrorCode PCApply_User(PC pc,Vec x,Vec y)
     23: {
     24:   PetscInt    i,rstart,rend;
     25:   PetscScalar *yarray;
     26:   PCCtx       *ctx;

     28:   PetscFunctionBeginUser;
     29:   PetscCall(PCShellGetContext(pc,&ctx));
     30:   PetscCall(VecCopy(x,y));
     31:   PetscCall(VecGetOwnershipRange(y,&rstart,&rend));
     32:   PetscCall(VecGetArray(y,&yarray));
     33:   for (i=0;i<rend-rstart;i++) yarray[i] /= (i-rstart+1-ctx->target);
     34:   PetscCall(VecRestoreArray(y,&yarray));
     35:   PetscFunctionReturn(PETSC_SUCCESS);
     36: }

     38: int main(int argc,char **argv)
     39: {
     40:   Mat            A;           /* problem matrix */
     41:   EPS            eps;         /* eigenproblem solver context */
     42:   Vec            v0;          /* initial vector */
     43:   PetscRandom    rand;
     44:   PetscReal      tol=PETSC_SMALL;
     45:   PetscInt       n=30,i,Istart,Iend,seed=0x12345678;
     46:   ST             st;
     47:   KSP            ksp;
     48:   PC             pc;
     49:   PCCtx          ctx;

     51:   PetscFunctionBeginUser;
     52:   PetscCall(SlepcInitialize(&argc,&argv,NULL,help));

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

     57:   PetscCall(MatCreate(PETSC_COMM_WORLD,&A));
     58:   PetscCall(MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,n,n));
     59:   PetscCall(MatSetFromOptions(A));
     60:   PetscCall(MatGetOwnershipRange(A,&Istart,&Iend));
     61:   for (i=Istart;i<Iend;i++) PetscCall(MatSetValue(A,i,i,i+1,INSERT_VALUES));
     62:   PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY));
     63:   PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY));

     65:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     66:                       Solve the eigensystem
     67:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     68:   PetscCall(EPSCreate(PETSC_COMM_WORLD,&eps));
     69:   PetscCall(EPSSetOperators(eps,A,NULL));
     70:   PetscCall(EPSSetProblemType(eps,EPS_HEP));
     71:   PetscCall(EPSSetTolerances(eps,tol,PETSC_CURRENT));
     72:   PetscCall(EPSSetFromOptions(eps));
     73:   PetscCall(EPSGetST(eps,&st));
     74:   PetscCall(STGetKSP(st,&ksp));
     75:   PetscCall(KSPGetPC(ksp,&pc));
     76:   PetscCall(PCSetType(pc,PCSHELL));
     77:   PetscCall(PCShellSetApply(pc,PCApply_User));
     78:   PetscCall(EPSGetTarget(eps,&ctx.target));
     79:   PetscCall(PCShellSetContext(pc,&ctx));

     81:   /* set random initial vector */
     82:   PetscCall(MatCreateVecs(A,&v0,NULL));
     83:   PetscCall(PetscRandomCreate(PETSC_COMM_WORLD,&rand));
     84:   PetscCall(PetscRandomSetFromOptions(rand));
     85:   PetscCall(PetscOptionsGetInt(NULL,NULL,"-seed",&seed,NULL));
     86:   PetscCall(PetscRandomSetSeed(rand,seed));
     87:   PetscCall(PetscRandomSeed(rand));
     88:   PetscCall(VecSetRandom(v0,rand));
     89:   PetscCall(EPSSetInitialSpace(eps,1,&v0));
     90:   /* call the solver */
     91:   PetscCall(EPSSolve(eps));

     93:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     94:                     Display solution and clean up
     95:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     96:   PetscCall(EPSErrorView(eps,EPS_ERROR_RELATIVE,NULL));
     97:   PetscCall(EPSDestroy(&eps));
     98:   PetscCall(MatDestroy(&A));
     99:   PetscCall(VecDestroy(&v0));
    100:   PetscCall(PetscRandomDestroy(&rand));
    101:   PetscCall(SlepcFinalize());
    102:   return 0;
    103: }

    105: /*TEST

    107:    testset:
    108:       args: -eps_nev 4 -eps_target 31
    109:       requires: !single
    110:       output_file: output/test12_1.out
    111:       test:
    112:          suffix: 1
    113:          args: -eps_type {{krylovschur subspace arnoldi power}} -st_type sinvert
    114:       test:
    115:          suffix: 1_gd
    116:          args: -eps_type {{gd jd}}
    117:       test:
    118:          suffix: 1_gd2
    119:          args: -eps_type gd -eps_gd_double_expansion

    121: TEST*/