Actual source code: test12.c
slepc-3.22.1 2024-10-28
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,NULL,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(MatGetOwnershipRange(A,&Istart,&Iend));
47: for (i=Istart;i<Iend;i++) PetscCall(MatSetValue(A,i,i,i+1,INSERT_VALUES));
48: PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY));
49: PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY));
51: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
52: Solve the eigensystem
53: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
54: PetscCall(EPSCreate(PETSC_COMM_WORLD,&eps));
55: PetscCall(EPSSetOperators(eps,A,NULL));
56: PetscCall(EPSSetProblemType(eps,EPS_HEP));
57: PetscCall(EPSSetTolerances(eps,tol,PETSC_CURRENT));
58: PetscCall(EPSSetFromOptions(eps));
59: PetscCall(EPSGetST(eps,&st));
60: PetscCall(STGetKSP(st,&ksp));
61: PetscCall(KSPGetPC(ksp,&pc));
62: PetscCall(PCSetType(pc,PCSHELL));
63: PetscCall(PCShellSetApply(pc,PCApply_User));
65: /* set random initial vector */
66: PetscCall(MatCreateVecs(A,&v0,NULL));
67: PetscCall(PetscRandomCreate(PETSC_COMM_WORLD,&rand));
68: PetscCall(PetscRandomSetFromOptions(rand));
69: PetscCall(PetscOptionsGetInt(NULL,NULL,"-seed",&seed,NULL));
70: PetscCall(PetscRandomSetSeed(rand,seed));
71: PetscCall(PetscRandomSeed(rand));
72: PetscCall(VecSetRandom(v0,rand));
73: PetscCall(EPSSetInitialSpace(eps,1,&v0));
74: /* call the solver */
75: PetscCall(EPSSolve(eps));
77: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
78: Display solution and clean up
79: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
80: PetscCall(EPSErrorView(eps,EPS_ERROR_RELATIVE,NULL));
81: PetscCall(EPSDestroy(&eps));
82: PetscCall(MatDestroy(&A));
83: PetscCall(VecDestroy(&v0));
84: PetscCall(PetscRandomDestroy(&rand));
85: PetscCall(SlepcFinalize());
86: return 0;
87: }
89: /*TEST
91: testset:
92: requires: !single
93: output_file: output/test12_1.out
94: test:
95: suffix: 1
96: args: -eps_type {{krylovschur subspace arnoldi gd jd}} -eps_nev 4
97: test:
98: suffix: 1_power
99: args: -eps_type power -eps_max_it 10000 -eps_nev 4
100: test:
101: suffix: 1_gd2
102: args: -eps_type gd -eps_gd_double_expansion -eps_nev 4
104: TEST*/