Actual source code: ex4.c
slepc-3.7.2 2016-07-19
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[] = "Solves a standard eigensystem Ax=kx with the matrix loaded from a file.\n"
23: "This example works for both real and complex numbers.\n\n"
24: "The command line options are:\n"
25: " -file <filename>, where <filename> = matrix file in PETSc binary form.\n\n";
27: #include <slepceps.h>
31: int main(int argc,char **argv)
32: {
33: Mat A; /* operator matrix */
34: EPS eps; /* eigenproblem solver context */
35: EPSType type;
36: PetscReal tol;
37: PetscInt nev,maxit,its;
38: char filename[PETSC_MAX_PATH_LEN];
39: PetscViewer viewer;
40: PetscBool flg,terse;
43: SlepcInitialize(&argc,&argv,(char*)0,help);
45: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
46: Load the operator matrix that defines the eigensystem, Ax=kx
47: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
49: PetscPrintf(PETSC_COMM_WORLD,"\nEigenproblem stored in file.\n\n");
50: PetscOptionsGetString(NULL,NULL,"-file",filename,PETSC_MAX_PATH_LEN,&flg);
51: if (!flg) SETERRQ(PETSC_COMM_WORLD,1,"Must indicate a file name with the -file option");
53: #if defined(PETSC_USE_COMPLEX)
54: PetscPrintf(PETSC_COMM_WORLD," Reading COMPLEX matrix from a binary file...\n");
55: #else
56: PetscPrintf(PETSC_COMM_WORLD," Reading REAL matrix from a binary file...\n");
57: #endif
58: PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,FILE_MODE_READ,&viewer);
59: MatCreate(PETSC_COMM_WORLD,&A);
60: MatSetFromOptions(A);
61: MatLoad(A,viewer);
62: PetscViewerDestroy(&viewer);
64: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
65: Create the eigensolver and set various options
66: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
68: /*
69: Create eigensolver context
70: */
71: EPSCreate(PETSC_COMM_WORLD,&eps);
73: /*
74: Set operators. In this case, it is a standard eigenvalue problem
75: */
76: EPSSetOperators(eps,A,NULL);
78: /*
79: Set solver parameters at runtime
80: */
81: EPSSetFromOptions(eps);
83: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
84: Solve the eigensystem
85: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
87: EPSSolve(eps);
88: EPSGetIterationNumber(eps,&its);
89: PetscPrintf(PETSC_COMM_WORLD," Number of iterations of the method: %D\n",its);
91: /*
92: Optional: Get some information from the solver and display it
93: */
94: EPSGetType(eps,&type);
95: PetscPrintf(PETSC_COMM_WORLD," Solution method: %s\n\n",type);
96: EPSGetDimensions(eps,&nev,NULL,NULL);
97: PetscPrintf(PETSC_COMM_WORLD," Number of requested eigenvalues: %D\n",nev);
98: EPSGetTolerances(eps,&tol,&maxit);
99: PetscPrintf(PETSC_COMM_WORLD," Stopping condition: tol=%.4g, maxit=%D\n",(double)tol,maxit);
101: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
102: Display solution and clean up
103: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
105: /* show detailed info unless -terse option is given by user */
106: PetscOptionsHasName(NULL,NULL,"-terse",&terse);
107: if (terse) {
108: EPSErrorView(eps,EPS_ERROR_RELATIVE,NULL);
109: } else {
110: PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL);
111: EPSReasonView(eps,PETSC_VIEWER_STDOUT_WORLD);
112: EPSErrorView(eps,EPS_ERROR_RELATIVE,PETSC_VIEWER_STDOUT_WORLD);
113: PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);
114: }
115: EPSDestroy(&eps);
116: MatDestroy(&A);
117: SlepcFinalize();
118: return ierr;
119: }