Line data Source code
1 : /*
2 : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3 : SLEPc - Scalable Library for Eigenvalue Problem Computations
4 : Copyright (c) 2002-, Universitat Politecnica de Valencia, Spain
5 :
6 : This file is part of SLEPc.
7 : SLEPc is distributed under a 2-clause BSD license (see LICENSE).
8 : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
9 : */
10 :
11 : static char help[] = "Solves a polynomial eigenproblem P(l)x = 0 with matrices loaded from a file.\n\n"
12 : "The command line options are:\n"
13 : "-A <filename1,filename2, ...> , where <filename1,.. > = matrices A0 ... files in PETSc binary form.\n\n";
14 :
15 : #include <slepcpep.h>
16 :
17 : #define MAX_MATRICES 40
18 :
19 3 : int main(int argc,char **argv)
20 : {
21 3 : Mat A[MAX_MATRICES]; /* problem matrices */
22 3 : PEP pep; /* polynomial eigenproblem solver context */
23 3 : PetscReal tol;
24 3 : PetscInt nev,maxit,its,nmat=MAX_MATRICES,i;
25 3 : char* filenames[MAX_MATRICES];
26 3 : PetscViewer viewer;
27 3 : PetscBool flg,terse;
28 :
29 3 : PetscFunctionBeginUser;
30 3 : PetscCall(SlepcInitialize(&argc,&argv,NULL,help));
31 :
32 : /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
33 : Load the matrices that define the polynomial eigenproblem
34 : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
35 :
36 3 : PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\nPolynomial eigenproblem stored in file.\n\n"));
37 : #if defined(PETSC_USE_COMPLEX)
38 : PetscCall(PetscPrintf(PETSC_COMM_WORLD," Reading COMPLEX matrices from binary files...\n"));
39 : #else
40 3 : PetscCall(PetscPrintf(PETSC_COMM_WORLD," Reading REAL matrices from binary files...\n"));
41 : #endif
42 3 : PetscCall(PetscOptionsGetStringArray(NULL,NULL,"-A",filenames,&nmat,&flg));
43 3 : PetscCheck(flg,PETSC_COMM_WORLD,PETSC_ERR_USER_INPUT,"Must indicate a comma-separated list of file names with the -A option");
44 12 : for (i=0;i<nmat;i++) {
45 9 : PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD,filenames[i],FILE_MODE_READ,&viewer));
46 9 : PetscCall(MatCreate(PETSC_COMM_WORLD,&A[i]));
47 9 : PetscCall(MatSetFromOptions(A[i]));
48 9 : PetscCall(MatLoad(A[i],viewer));
49 9 : PetscCall(PetscViewerDestroy(&viewer));
50 : }
51 : /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
52 : Create the eigensolver and set various options
53 : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
54 :
55 : /*
56 : Create eigensolver context
57 : */
58 3 : PetscCall(PEPCreate(PETSC_COMM_WORLD,&pep));
59 :
60 : /*
61 : Set matrices
62 : */
63 3 : PetscCall(PEPSetOperators(pep,nmat,A));
64 : /*
65 : Set solver parameters at runtime
66 : */
67 3 : PetscCall(PEPSetFromOptions(pep));
68 :
69 : /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
70 : Solve the eigensystem
71 : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
72 :
73 3 : PetscCall(PEPSolve(pep));
74 3 : PetscCall(PEPGetIterationNumber(pep,&its));
75 3 : PetscCall(PetscPrintf(PETSC_COMM_WORLD," Number of iterations of the method: %" PetscInt_FMT "\n",its));
76 :
77 : /*
78 : Optional: Get some information from the solver and display it
79 : */
80 3 : PetscCall(PEPGetDimensions(pep,&nev,NULL,NULL));
81 3 : PetscCall(PetscPrintf(PETSC_COMM_WORLD," Number of requested eigenvalues: %" PetscInt_FMT "\n",nev));
82 3 : PetscCall(PEPGetTolerances(pep,&tol,&maxit));
83 3 : PetscCall(PetscPrintf(PETSC_COMM_WORLD," Stopping condition: tol=%.4g, maxit=%" PetscInt_FMT "\n",(double)tol,maxit));
84 :
85 : /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
86 : Display solution and clean up
87 : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
88 :
89 : /* show detailed info unless -terse option is given by user */
90 3 : PetscCall(PetscOptionsHasName(NULL,NULL,"-terse",&terse));
91 3 : if (terse) PetscCall(PEPErrorView(pep,PEP_ERROR_BACKWARD,NULL));
92 : else {
93 0 : PetscCall(PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL));
94 0 : PetscCall(PEPConvergedReasonView(pep,PETSC_VIEWER_STDOUT_WORLD));
95 0 : PetscCall(PEPErrorView(pep,PEP_ERROR_BACKWARD,PETSC_VIEWER_STDOUT_WORLD));
96 0 : PetscCall(PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD));
97 : }
98 3 : PetscCall(PEPDestroy(&pep));
99 12 : for (i=0;i<nmat;i++) {
100 9 : PetscCall(MatDestroy(&A[i]));
101 9 : PetscCall(PetscFree(filenames[i]));
102 : }
103 3 : PetscCall(SlepcFinalize());
104 : return 0;
105 : }
106 :
107 : /*TEST
108 :
109 : test:
110 : suffix: 1
111 : args: -A ${SLEPC_DIR}/share/slepc/datafiles/matrices/speaker107k.petsc,${SLEPC_DIR}/share/slepc/datafiles/matrices/speaker107c.petsc,${SLEPC_DIR}/share/slepc/datafiles/matrices/speaker107m.petsc -pep_type {{toar qarnoldi linear}} -pep_nev 4 -pep_ncv 20 -pep_scale scalar -terse
112 : requires: double !complex !defined(PETSC_USE_64BIT_INDICES)
113 :
114 : TEST*/
|