Actual source code: test2.c
slepc-3.20.1 2023-11-27
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[] = "Tests multiple calls to EPSSolve with the same matrix.\n\n";
13: #include <slepceps.h>
15: int main(int argc,char **argv)
16: {
17: Mat A; /* problem matrix */
18: EPS eps; /* eigenproblem solver context */
19: ST st;
20: PetscReal tol=PetscMax(1000*PETSC_MACHINE_EPSILON,1e-9);
21: PetscInt n=30,i,Istart,Iend;
22: PetscBool flg;
23: EPSLanczosReorthogType reorth;
25: PetscFunctionBeginUser;
26: PetscCall(SlepcInitialize(&argc,&argv,(char*)0,help));
28: PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
29: PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\n1-D Laplacian Eigenproblem, n=%" PetscInt_FMT "\n\n",n));
31: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
32: Compute the operator matrix that defines the eigensystem, Ax=kx
33: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
35: PetscCall(MatCreate(PETSC_COMM_WORLD,&A));
36: PetscCall(MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,n,n));
37: PetscCall(MatSetFromOptions(A));
38: PetscCall(MatSetUp(A));
40: PetscCall(MatGetOwnershipRange(A,&Istart,&Iend));
41: for (i=Istart;i<Iend;i++) {
42: if (i>0) PetscCall(MatSetValue(A,i,i-1,-1.0,INSERT_VALUES));
43: if (i<n-1) PetscCall(MatSetValue(A,i,i+1,-1.0,INSERT_VALUES));
44: PetscCall(MatSetValue(A,i,i,2.0,INSERT_VALUES));
45: }
46: PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY));
47: PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY));
49: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
50: Create the eigensolver
51: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
52: PetscCall(EPSCreate(PETSC_COMM_WORLD,&eps));
53: PetscCall(EPSSetOperators(eps,A,NULL));
54: PetscCall(EPSSetProblemType(eps,EPS_HEP));
55: PetscCall(EPSSetTolerances(eps,tol,PETSC_DEFAULT));
56: PetscCall(EPSSetFromOptions(eps));
58: /* illustrate how to extract parameters from specific solver types */
59: PetscCall(PetscObjectTypeCompare((PetscObject)eps,EPSLANCZOS,&flg));
60: if (flg) {
61: PetscCall(EPSLanczosGetReorthog(eps,&reorth));
62: PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Reorthogonalization type used in Lanczos: %s\n",EPSLanczosReorthogTypes[reorth]));
63: }
65: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
66: Solve for largest eigenvalues
67: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
68: PetscCall(EPSSetWhichEigenpairs(eps,EPS_LARGEST_REAL));
69: PetscCall(EPSSolve(eps));
70: PetscCall(PetscPrintf(PETSC_COMM_WORLD," - - - Largest eigenvalues - - -\n"));
71: PetscCall(EPSErrorView(eps,EPS_ERROR_RELATIVE,NULL));
73: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
74: Solve for smallest eigenvalues
75: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
76: PetscCall(EPSSetWhichEigenpairs(eps,EPS_SMALLEST_REAL));
77: PetscCall(EPSSolve(eps));
78: PetscCall(PetscPrintf(PETSC_COMM_WORLD," - - - Smallest eigenvalues - - -\n"));
79: PetscCall(EPSErrorView(eps,EPS_ERROR_RELATIVE,NULL));
81: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
82: Solve for interior eigenvalues (target=2.1)
83: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
84: PetscCall(EPSSetWhichEigenpairs(eps,EPS_TARGET_MAGNITUDE));
85: PetscCall(EPSSetTarget(eps,2.1));
86: PetscCall(PetscObjectTypeCompare((PetscObject)eps,EPSLANCZOS,&flg));
87: if (flg) {
88: PetscCall(EPSGetST(eps,&st));
89: PetscCall(STSetType(st,STSINVERT));
90: } else {
91: PetscCall(PetscObjectTypeCompare((PetscObject)eps,EPSKRYLOVSCHUR,&flg));
92: if (!flg) PetscCall(PetscObjectTypeCompare((PetscObject)eps,EPSARNOLDI,&flg));
93: if (flg) PetscCall(EPSSetExtraction(eps,EPS_HARMONIC));
94: }
95: PetscCall(EPSSolve(eps));
96: PetscCall(PetscPrintf(PETSC_COMM_WORLD," - - - Interior eigenvalues - - -\n"));
97: PetscCall(EPSErrorView(eps,EPS_ERROR_RELATIVE,NULL));
99: PetscCall(EPSDestroy(&eps));
100: PetscCall(MatDestroy(&A));
101: PetscCall(SlepcFinalize());
102: return 0;
103: }
105: /*TEST
107: testset:
108: args: -eps_nev 4
109: requires: !single
110: output_file: output/test2_1.out
111: test:
112: suffix: 1
113: args: -eps_type {{arnoldi gd jd lapack}}
114: test:
115: suffix: 1_gd2
116: args: -eps_type gd -eps_gd_double_expansion
117: timeoutfactor: 2
118: test:
119: suffix: 1_krylovschur
120: args: -eps_type krylovschur -eps_krylovschur_locking {{0 1}}
121: test:
122: suffix: 1_scalapack
123: requires: scalapack
124: args: -eps_type scalapack
125: test:
126: suffix: 1_elpa
127: requires: elpa
128: args: -eps_type elpa
129: test:
130: suffix: 1_elemental
131: requires: elemental
132: args: -eps_type elemental
134: testset:
135: args: -eps_type lanczos -eps_nev 4
136: requires: !single
137: filter: grep -v "Lanczos"
138: output_file: output/test2_1.out
139: test:
140: suffix: 2
141: args: -eps_lanczos_reorthog {{local full periodic partial}}
142: test:
143: suffix: 2_selective
144: args: -eps_lanczos_reorthog selective
145: requires: !defined(PETSCTEST_VALGRIND)
147: testset:
148: args: -n 32 -eps_nev 4
149: requires: !single
150: output_file: output/test2_3.out
151: test:
152: nsize: 2
153: suffix: 3
154: args: -eps_type {{krylovschur lapack}}
155: test:
156: nsize: 2
157: suffix: 3_gd
158: args: -eps_type gd -eps_gd_krylov_start
159: timeoutfactor: 2
160: test:
161: suffix: 3_jd
162: args: -eps_type jd -eps_jd_krylov_start -eps_ncv 18
164: testset:
165: args: -eps_nev 4 -mat_type aijcusparse
166: requires: cuda !single
167: output_file: output/test2_1.out
168: test:
169: suffix: 4_cuda
170: args: -eps_type {{krylovschur arnoldi gd jd}}
172: TEST*/