Actual source code: test4.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[] = "Test the solution of a HEP without calling EPSSetFromOptions (based on ex1.c).\n\n"
12: "The command line options are:\n"
13: " -n <n>, where <n> = number of grid subdivisions = matrix dimension.\n"
14: " -type <eps_type> = eps type to test.\n\n";
16: #include <slepceps.h>
18: int main(int argc,char **argv)
19: {
20: Mat A; /* problem matrix */
21: EPS eps; /* eigenproblem solver context */
22: PetscReal tol=1000*PETSC_MACHINE_EPSILON;
23: PetscInt n=30,i,Istart,Iend,nev;
24: PetscBool flg,gd2;
25: char epstype[30] = "krylovschur";
27: SlepcInitialize(&argc,&argv,(char*)0,help);
29: PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
30: PetscOptionsGetString(NULL,NULL,"-type",epstype,sizeof(epstype),NULL);
31: PetscPrintf(PETSC_COMM_WORLD,"\n1-D Laplacian Eigenproblem, n=%" PetscInt_FMT "\n\n",n);
33: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
34: Compute the operator matrix that defines the eigensystem, Ax=kx
35: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
37: MatCreate(PETSC_COMM_WORLD,&A);
38: MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,n,n);
39: MatSetFromOptions(A);
40: MatSetUp(A);
42: MatGetOwnershipRange(A,&Istart,&Iend);
43: for (i=Istart;i<Iend;i++) {
44: if (i>0) MatSetValue(A,i,i-1,-1.0,INSERT_VALUES);
45: if (i<n-1) MatSetValue(A,i,i+1,-1.0,INSERT_VALUES);
46: MatSetValue(A,i,i,2.0,INSERT_VALUES);
47: }
48: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
49: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
51: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
52: Create the eigensolver and set various options
53: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
54: /*
55: Create eigensolver context
56: */
57: EPSCreate(PETSC_COMM_WORLD,&eps);
59: /*
60: Set operators. In this case, it is a standard eigenvalue problem
61: */
62: EPSSetOperators(eps,A,NULL);
63: EPSSetProblemType(eps,EPS_HEP);
64: EPSSetDimensions(eps,4,PETSC_DEFAULT,PETSC_DEFAULT);
65: EPSSetTolerances(eps,tol,PETSC_DEFAULT);
67: /*
68: Set solver parameters at runtime
69: */
70: PetscStrcmp(epstype,"gd2",&flg);
71: if (flg) {
72: EPSSetType(eps,EPSGD);
73: EPSGDSetDoubleExpansion(eps,PETSC_TRUE);
74: EPSGDGetDoubleExpansion(eps,&gd2); /* not used */
75: } else EPSSetType(eps,epstype);
76: PetscStrcmp(epstype,"jd",&flg);
77: if (flg) {
78: EPSSetWhichEigenpairs(eps,EPS_TARGET_MAGNITUDE);
79: EPSSetTarget(eps,4.0);
80: }
81: PetscStrcmp(epstype,"lanczos",&flg);
82: if (flg) EPSLanczosSetReorthog(eps,EPS_LANCZOS_REORTHOG_LOCAL);
83: PetscObjectTypeCompareAny((PetscObject)eps,&flg,EPSRQCG,EPSLOBPCG,"");
84: if (flg) EPSSetWhichEigenpairs(eps,EPS_SMALLEST_REAL);
86: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
87: Solve the eigensystem
88: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
90: EPSSolve(eps);
91: EPSGetDimensions(eps,&nev,NULL,NULL);
92: PetscPrintf(PETSC_COMM_WORLD," Number of requested eigenvalues: %" PetscInt_FMT "\n",nev);
94: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
95: Display solution and clean up
96: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
98: EPSErrorView(eps,EPS_ERROR_RELATIVE,NULL);
99: EPSDestroy(&eps);
100: MatDestroy(&A);
101: SlepcFinalize();
102: return 0;
103: }
105: /*TEST
107: testset:
108: output_file: output/test4_1.out
109: test:
110: suffix: 1
111: args: -type {{krylovschur subspace arnoldi lanczos gd jd gd2 lapack}}
112: test:
113: suffix: 1_arpack
114: args: -type arpack
115: requires: arpack
116: test:
117: suffix: 1_primme
118: args: -type primme
119: requires: primme
120: test:
121: suffix: 1_trlan
122: args: -type trlan
123: requires: trlan
125: test:
126: suffix: 2
127: args: -type {{rqcg lobpcg}}
129: TEST*/