Actual source code: test4.c
slepc-3.7.3 2016-09-29
1: /*
2: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3: SLEPc - Scalable Library for Eigenvalue Problem Computations
4: Copyright (c) 2002-2016, Universitat Politecnica de Valencia, Spain
6: This file is part of SLEPc.
8: SLEPc is free software: you can redistribute it and/or modify it under the
9: terms of version 3 of the GNU Lesser General Public License as published by
10: the Free Software Foundation.
12: SLEPc is distributed in the hope that it will be useful, but WITHOUT ANY
13: WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14: FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
15: more details.
17: You should have received a copy of the GNU Lesser General Public License
18: along with SLEPc. If not, see <http://www.gnu.org/licenses/>.
19: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
20: */
22: static char help[] = "Test the solution of a HEP without calling EPSSetFromOptions (based on ex1.c).\n\n"
23: "The command line options are:\n"
24: " -n <n>, where <n> = number of grid subdivisions = matrix dimension.\n"
25: " -type <eps_type> = eps type to test.\n\n";
27: #include <slepceps.h>
31: int main(int argc,char **argv)
32: {
33: Mat A; /* problem matrix */
34: EPS eps; /* eigenproblem solver context */
35: EPSType type;
36: PetscReal tol=1000*PETSC_MACHINE_EPSILON;
37: PetscInt n=30,i,Istart,Iend,nev;
38: PetscBool isgd2;
39: char epstype[30] = "krylovschur";
42: SlepcInitialize(&argc,&argv,(char*)0,help);
44: PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
45: PetscOptionsGetString(NULL,NULL,"-type",epstype,30,NULL);
46: PetscPrintf(PETSC_COMM_WORLD,"\n1-D Laplacian Eigenproblem, n=%D",n);
47: PetscPrintf(PETSC_COMM_WORLD,"\nEPS type: %s\n\n",epstype);
49: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
50: Compute the operator matrix that defines the eigensystem, Ax=kx
51: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
53: MatCreate(PETSC_COMM_WORLD,&A);
54: MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,n,n);
55: MatSetFromOptions(A);
56: MatSetUp(A);
58: MatGetOwnershipRange(A,&Istart,&Iend);
59: for (i=Istart;i<Iend;i++) {
60: if (i>0) { MatSetValue(A,i,i-1,-1.0,INSERT_VALUES); }
61: if (i<n-1) { MatSetValue(A,i,i+1,-1.0,INSERT_VALUES); }
62: MatSetValue(A,i,i,2.0,INSERT_VALUES);
63: }
64: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
65: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
67: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
68: Create the eigensolver and set various options
69: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
70: /*
71: Create eigensolver context
72: */
73: EPSCreate(PETSC_COMM_WORLD,&eps);
75: /*
76: Set operators. In this case, it is a standard eigenvalue problem
77: */
78: EPSSetOperators(eps,A,NULL);
79: EPSSetProblemType(eps,EPS_HEP);
80: EPSSetDimensions(eps,4,PETSC_DEFAULT,PETSC_DEFAULT);
81: EPSSetTolerances(eps,tol,PETSC_DEFAULT);
83: /*
84: Set solver parameters at runtime
85: */
86: PetscStrcmp(epstype,"gd2",&isgd2);
87: if (isgd2) {
88: EPSSetType(eps,EPSGD);
89: EPSGDSetDoubleExpansion(eps,PETSC_TRUE);
90: } else {
91: EPSSetType(eps,epstype);
92: }
94: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
95: Solve the eigensystem
96: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
98: EPSSolve(eps);
99: /*
100: Optional: Get some information from the solver and display it
101: */
102: EPSGetType(eps,&type);
103: PetscPrintf(PETSC_COMM_WORLD," Solution method: %s\n\n",type);
104: EPSGetDimensions(eps,&nev,NULL,NULL);
105: PetscPrintf(PETSC_COMM_WORLD," Number of requested eigenvalues: %D\n",nev);
107: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
108: Display solution and clean up
109: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
111: EPSErrorView(eps,EPS_ERROR_RELATIVE,NULL);
112: EPSDestroy(&eps);
113: MatDestroy(&A);
114: SlepcFinalize();
115: return ierr;
116: }