Actual source code: ex4.c
slepc-3.18.3 2023-03-24
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[] = "Solves a standard eigensystem Ax=kx with the matrix loaded from a file.\n"
12: "This example works for both real and complex numbers.\n\n"
13: "The command line options are:\n"
14: " -file <filename>, where <filename> = matrix file in PETSc binary form.\n\n";
16: #include <slepceps.h>
18: int main(int argc,char **argv)
19: {
20: Mat A; /* operator matrix */
21: EPS eps; /* eigenproblem solver context */
22: EPSType type;
23: PetscReal tol;
24: PetscInt nev,maxit,its;
25: char filename[PETSC_MAX_PATH_LEN];
26: PetscViewer viewer;
27: PetscBool flg,terse;
30: SlepcInitialize(&argc,&argv,(char*)0,help);
32: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
33: Load the operator matrix that defines the eigensystem, Ax=kx
34: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
36: PetscPrintf(PETSC_COMM_WORLD,"\nEigenproblem stored in file.\n\n");
37: PetscOptionsGetString(NULL,NULL,"-file",filename,sizeof(filename),&flg);
40: #if defined(PETSC_USE_COMPLEX)
41: PetscPrintf(PETSC_COMM_WORLD," Reading COMPLEX matrix from a binary file...\n");
42: #else
43: PetscPrintf(PETSC_COMM_WORLD," Reading REAL matrix from a binary file...\n");
44: #endif
45: PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,FILE_MODE_READ,&viewer);
46: MatCreate(PETSC_COMM_WORLD,&A);
47: MatSetFromOptions(A);
48: MatLoad(A,viewer);
49: PetscViewerDestroy(&viewer);
51: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
52: Create the eigensolver and set various options
53: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
55: /*
56: Create eigensolver context
57: */
58: EPSCreate(PETSC_COMM_WORLD,&eps);
60: /*
61: Set operators. In this case, it is a standard eigenvalue problem
62: */
63: EPSSetOperators(eps,A,NULL);
65: /*
66: Set solver parameters at runtime
67: */
68: EPSSetFromOptions(eps);
70: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
71: Solve the eigensystem
72: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
74: EPSSolve(eps);
75: EPSGetIterationNumber(eps,&its);
76: PetscPrintf(PETSC_COMM_WORLD," Number of iterations of the method: %" PetscInt_FMT "\n",its);
78: /*
79: Optional: Get some information from the solver and display it
80: */
81: EPSGetType(eps,&type);
82: PetscPrintf(PETSC_COMM_WORLD," Solution method: %s\n\n",type);
83: EPSGetDimensions(eps,&nev,NULL,NULL);
84: PetscPrintf(PETSC_COMM_WORLD," Number of requested eigenvalues: %" PetscInt_FMT "\n",nev);
85: EPSGetTolerances(eps,&tol,&maxit);
86: PetscPrintf(PETSC_COMM_WORLD," Stopping condition: tol=%.4g, maxit=%" PetscInt_FMT "\n",(double)tol,maxit);
88: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
89: Display solution and clean up
90: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
92: /* show detailed info unless -terse option is given by user */
93: PetscOptionsHasName(NULL,NULL,"-terse",&terse);
94: if (terse) EPSErrorView(eps,EPS_ERROR_RELATIVE,NULL);
95: else {
96: PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL);
97: EPSConvergedReasonView(eps,PETSC_VIEWER_STDOUT_WORLD);
98: EPSErrorView(eps,EPS_ERROR_RELATIVE,PETSC_VIEWER_STDOUT_WORLD);
99: PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);
100: }
101: EPSDestroy(&eps);
102: MatDestroy(&A);
103: SlepcFinalize();
104: return 0;
105: }
107: /*TEST
109: test:
110: suffix: 1
111: args: -file ${SLEPC_DIR}/share/slepc/datafiles/matrices/rdb200.petsc -eps_nev 4 -terse
112: requires: double !complex !defined(PETSC_USE_64BIT_INDICES)
114: testset:
115: args: -file ${DATAFILESPATH}/matrices/complex/qc324.petsc -eps_type ciss -terse
116: requires: double complex datafilespath !defined(PETSC_USE_64BIT_INDICES)
117: test:
118: suffix: ciss_1
119: args: -rg_type ellipse -rg_ellipse_center -.012-.08i -rg_ellipse_radius .05
120: test:
121: suffix: ciss_2
122: args: -rg_type interval -rg_interval_endpoints -0.062,.038,-.13,-.03 -eps_max_it 1
124: TEST*/