Line data Source code
1 : /*
2 : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3 : SLEPc - Scalable Library for Eigenvalue Problem Computations
4 : Copyright (c) 2002-, Universitat Politecnica de Valencia, Spain
5 :
6 : This file is part of SLEPc.
7 : SLEPc is distributed under a 2-clause BSD license (see LICENSE).
8 : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
9 : */
10 :
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";
15 :
16 : #include <slepceps.h>
17 :
18 0 : PetscErrorCode PCApply_User(PC pc,Vec x,Vec y)
19 : {
20 0 : PetscFunctionBeginUser;
21 0 : PetscCall(VecCopy(x,y));
22 0 : PetscFunctionReturn(PETSC_SUCCESS);
23 : }
24 :
25 7 : int main(int argc,char **argv)
26 : {
27 7 : Mat A; /* problem matrix */
28 7 : EPS eps; /* eigenproblem solver context */
29 7 : Vec v0; /* initial vector */
30 7 : PetscRandom rand;
31 7 : PetscReal tol=PETSC_SMALL;
32 7 : PetscInt n=30,i,Istart,Iend,seed=0x12345678;
33 7 : ST st;
34 7 : KSP ksp;
35 7 : PC pc;
36 :
37 7 : PetscFunctionBeginUser;
38 7 : PetscCall(SlepcInitialize(&argc,&argv,NULL,help));
39 :
40 7 : PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
41 7 : PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\nDiagonal Eigenproblem, n=%" PetscInt_FMT "\n\n",n));
42 :
43 7 : PetscCall(MatCreate(PETSC_COMM_WORLD,&A));
44 7 : PetscCall(MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,n,n));
45 7 : PetscCall(MatSetFromOptions(A));
46 7 : PetscCall(MatGetOwnershipRange(A,&Istart,&Iend));
47 217 : for (i=Istart;i<Iend;i++) PetscCall(MatSetValue(A,i,i,i+1,INSERT_VALUES));
48 7 : PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY));
49 7 : PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY));
50 :
51 : /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
52 : Solve the eigensystem
53 : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
54 7 : PetscCall(EPSCreate(PETSC_COMM_WORLD,&eps));
55 7 : PetscCall(EPSSetOperators(eps,A,NULL));
56 7 : PetscCall(EPSSetProblemType(eps,EPS_HEP));
57 7 : PetscCall(EPSSetTolerances(eps,tol,PETSC_CURRENT));
58 7 : PetscCall(EPSSetFromOptions(eps));
59 7 : PetscCall(EPSGetST(eps,&st));
60 7 : PetscCall(STGetKSP(st,&ksp));
61 7 : PetscCall(KSPGetPC(ksp,&pc));
62 7 : PetscCall(PCSetType(pc,PCSHELL));
63 7 : PetscCall(PCShellSetApply(pc,PCApply_User));
64 :
65 : /* set random initial vector */
66 7 : PetscCall(MatCreateVecs(A,&v0,NULL));
67 7 : PetscCall(PetscRandomCreate(PETSC_COMM_WORLD,&rand));
68 7 : PetscCall(PetscRandomSetFromOptions(rand));
69 7 : PetscCall(PetscOptionsGetInt(NULL,NULL,"-seed",&seed,NULL));
70 7 : PetscCall(PetscRandomSetSeed(rand,seed));
71 7 : PetscCall(PetscRandomSeed(rand));
72 7 : PetscCall(VecSetRandom(v0,rand));
73 7 : PetscCall(EPSSetInitialSpace(eps,1,&v0));
74 : /* call the solver */
75 7 : PetscCall(EPSSolve(eps));
76 :
77 : /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
78 : Display solution and clean up
79 : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
80 7 : PetscCall(EPSErrorView(eps,EPS_ERROR_RELATIVE,NULL));
81 7 : PetscCall(EPSDestroy(&eps));
82 7 : PetscCall(MatDestroy(&A));
83 7 : PetscCall(VecDestroy(&v0));
84 7 : PetscCall(PetscRandomDestroy(&rand));
85 7 : PetscCall(SlepcFinalize());
86 : return 0;
87 : }
88 :
89 : /*TEST
90 :
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
103 :
104 : TEST*/
|