Actual source code: ex7.c
slepc-3.5.2 2014-10-10
1: /*
2: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3: SLEPc - Scalable Library for Eigenvalue Problem Computations
4: Copyright (c) 2002-2014, 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 generalized eigensystem Ax=kBx with matrices loaded from a file.\n"
23: "The command line options are:\n"
24: " -f1 <filename> -f2 <filename>, PETSc binary files containing A and B.\n"
25: " -evecs <filename>, output file to save computed eigenvectors.\n"
26: " -ninitial <nini>, number of user-provided initial guesses.\n"
27: " -finitial <filename>, binary file containing <nini> vectors.\n"
28: " -nconstr <ncon>, number of user-provided constraints.\n"
29: " -fconstr <filename>, binary file containing <ncon> vectors.\n\n";
31: #include <slepceps.h>
35: int main(int argc,char **argv)
36: {
37: Mat A,B; /* matrices */
38: EPS eps; /* eigenproblem solver context */
39: ST st;
40: EPSType type;
41: PetscReal tol;
42: Vec xr,xi,*Iv,*Cv;
43: PetscInt nev,maxit,i,its,lits,nconv,nini=0,ncon=0;
44: char filename[PETSC_MAX_PATH_LEN];
45: PetscViewer viewer;
46: PetscBool flg,evecs,ishermitian;
49: SlepcInitialize(&argc,&argv,(char*)0,help);
51: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
52: Load the matrices that define the eigensystem, Ax=kBx
53: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
55: PetscPrintf(PETSC_COMM_WORLD,"\nGeneralized eigenproblem stored in file.\n\n");
56: PetscOptionsGetString(NULL,"-f1",filename,PETSC_MAX_PATH_LEN,&flg);
57: if (!flg) SETERRQ(PETSC_COMM_WORLD,1,"Must indicate a file name for matrix A with the -f1 option");
59: #if defined(PETSC_USE_COMPLEX)
60: PetscPrintf(PETSC_COMM_WORLD," Reading COMPLEX matrices from binary files...\n");
61: #else
62: PetscPrintf(PETSC_COMM_WORLD," Reading REAL matrices from binary files...\n");
63: #endif
64: PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,FILE_MODE_READ,&viewer);
65: MatCreate(PETSC_COMM_WORLD,&A);
66: MatSetFromOptions(A);
67: MatLoad(A,viewer);
68: PetscViewerDestroy(&viewer);
70: PetscOptionsGetString(NULL,"-f2",filename,PETSC_MAX_PATH_LEN,&flg);
71: if (flg) {
72: PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,FILE_MODE_READ,&viewer);
73: MatCreate(PETSC_COMM_WORLD,&B);
74: MatSetFromOptions(B);
75: MatLoad(B,viewer);
76: PetscViewerDestroy(&viewer);
77: } else {
78: PetscPrintf(PETSC_COMM_WORLD," Matrix B was not provided, setting B=I\n\n");
79: B = NULL;
80: }
82: MatGetVecs(A,NULL,&xr);
83: MatGetVecs(A,NULL,&xi);
85: /*
86: Read user constraints if available
87: */
88: PetscOptionsGetInt(NULL,"-nconstr",&ncon,&flg);
89: if (flg) {
90: if (ncon<=0) SETERRQ(PETSC_COMM_WORLD,1,"The number of constraints must be >0");
91: PetscOptionsGetString(NULL,"-fconstr",filename,PETSC_MAX_PATH_LEN,&flg);
92: if (!flg) SETERRQ(PETSC_COMM_WORLD,1,"Must specify the name of the file storing the constraints");
93: PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,FILE_MODE_READ,&viewer);
94: VecDuplicateVecs(xr,ncon,&Cv);
95: for (i=0;i<ncon;i++) {
96: VecLoad(Cv[i],viewer);
97: }
98: PetscViewerDestroy(&viewer);
99: }
101: /*
102: Read initial guesses if available
103: */
104: PetscOptionsGetInt(NULL,"-ninitial",&nini,&flg);
105: if (flg) {
106: if (nini<=0) SETERRQ(PETSC_COMM_WORLD,1,"The number of initial vectors must be >0");
107: PetscOptionsGetString(NULL,"-finitial",filename,PETSC_MAX_PATH_LEN,&flg);
108: if (!flg) SETERRQ(PETSC_COMM_WORLD,1,"Must specify the name of the file containing the initial vectors");
109: PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,FILE_MODE_READ,&viewer);
110: VecDuplicateVecs(xr,nini,&Iv);
111: for (i=0;i<nini;i++) {
112: VecLoad(Iv[i],viewer);
113: }
114: PetscViewerDestroy(&viewer);
115: }
117: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
118: Create the eigensolver and set various options
119: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
121: /*
122: Create eigensolver context
123: */
124: EPSCreate(PETSC_COMM_WORLD,&eps);
126: /*
127: Set operators. In this case, it is a generalized eigenvalue problem
128: */
129: EPSSetOperators(eps,A,B);
131: /*
132: If the user provided initial guesses or constraints, pass them here
133: */
134: EPSSetInitialSpace(eps,nini,Iv);
135: EPSSetDeflationSpace(eps,ncon,Cv);
137: /*
138: Set solver parameters at runtime
139: */
140: EPSSetFromOptions(eps);
142: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
143: Solve the eigensystem
144: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
146: EPSSolve(eps);
148: /*
149: Optional: Get some information from the solver and display it
150: */
151: EPSGetIterationNumber(eps,&its);
152: PetscPrintf(PETSC_COMM_WORLD," Number of iterations of the method: %D\n",its);
153: EPSGetST(eps,&st);
154: STGetOperationCounters(st,NULL,&lits);
155: PetscPrintf(PETSC_COMM_WORLD," Number of linear iterations of the method: %D\n",lits);
156: EPSGetType(eps,&type);
157: PetscPrintf(PETSC_COMM_WORLD," Solution method: %s\n\n",type);
158: EPSGetDimensions(eps,&nev,NULL,NULL);
159: PetscPrintf(PETSC_COMM_WORLD," Number of requested eigenvalues: %D\n",nev);
160: EPSGetTolerances(eps,&tol,&maxit);
161: PetscPrintf(PETSC_COMM_WORLD," Stopping condition: tol=%.4g, maxit=%D\n",(double)tol,maxit);
163: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
164: Display solution and clean up
165: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
167: EPSPrintSolution(eps,NULL);
168: /*
169: Save eigenvectors, if requested
170: */
171: PetscOptionsGetString(NULL,"-evecs",filename,PETSC_MAX_PATH_LEN,&evecs);
172: EPSGetConverged(eps,&nconv);
173: if (nconv>0 && evecs) {
174: PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,FILE_MODE_WRITE,&viewer);
175: EPSIsHermitian(eps,&ishermitian);
176: for (i=0;i<nconv;i++) {
177: EPSGetEigenvector(eps,i,xr,xi);
178: VecView(xr,viewer);
179: #if !defined(PETSC_USE_COMPLEX)
180: if (!ishermitian) { VecView(xi,viewer); }
181: #endif
182: }
183: PetscViewerDestroy(&viewer);
184: }
186: /*
187: Free work space
188: */
189: EPSDestroy(&eps);
190: MatDestroy(&A);
191: MatDestroy(&B);
192: VecDestroy(&xr);
193: VecDestroy(&xi);
194: if (nini > 0) {
195: VecDestroyVecs(nini,&Iv);
196: }
197: if (ncon > 0) {
198: VecDestroyVecs(ncon,&Cv);
199: }
200: SlepcFinalize();
201: return 0;
202: }