Actual source code: test12.c
slepc-3.17.1 2022-04-11
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: PetscFunctionReturn(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;
37: SlepcInitialize(&argc,&argv,(char*)0,help);
39: PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
40: PetscPrintf(PETSC_COMM_WORLD,"\nDiagonal Eigenproblem, n=%" PetscInt_FMT "\n\n",n);
42: MatCreate(PETSC_COMM_WORLD,&A);
43: MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,n,n);
44: MatSetFromOptions(A);
45: MatSetUp(A);
46: MatGetOwnershipRange(A,&Istart,&Iend);
47: for (i=Istart;i<Iend;i++) MatSetValue(A,i,i,i+1,INSERT_VALUES);
48: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
49: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
51: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
52: Solve the eigensystem
53: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
54: EPSCreate(PETSC_COMM_WORLD,&eps);
55: EPSSetOperators(eps,A,NULL);
56: EPSSetProblemType(eps,EPS_HEP);
57: EPSSetTolerances(eps,tol,PETSC_DEFAULT);
58: EPSSetFromOptions(eps);
59: EPSGetST(eps,&st);
60: STGetKSP(st,&ksp);
61: KSPGetPC(ksp,&pc);
62: PCSetType(pc,PCSHELL);
63: PCShellSetApply(pc,PCApply_User);
65: /* set random initial vector */
66: MatCreateVecs(A,&v0,NULL);
67: PetscRandomCreate(PETSC_COMM_WORLD,&rand);
68: PetscRandomSetFromOptions(rand);
69: PetscOptionsGetInt(NULL,NULL,"-seed",&seed,NULL);
70: PetscRandomSetSeed(rand,seed);
71: PetscRandomSeed(rand);
72: VecSetRandom(v0,rand);
73: EPSSetInitialSpace(eps,1,&v0);
74: /* call the solver */
75: EPSSolve(eps);
77: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
78: Display solution and clean up
79: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
80: EPSErrorView(eps,EPS_ERROR_RELATIVE,NULL);
81: EPSDestroy(&eps);
82: MatDestroy(&A);
83: VecDestroy(&v0);
84: PetscRandomDestroy(&rand);
85: 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*/