Actual source code: ex14.c
slepc-3.15.0 2021-03-31
1: /*
2: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3: SLEPc - Scalable Library for Eigenvalue Problem Computations
4: Copyright (c) 2002-2021, 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 singular value problem 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 <slepcsvd.h>
18: int main(int argc,char **argv)
19: {
20: Mat A; /* operator matrix */
21: SVD svd; /* singular value problem solver context */
22: SVDType type;
23: PetscReal tol;
24: PetscInt nsv,maxit,its;
25: char filename[PETSC_MAX_PATH_LEN];
26: PetscViewer viewer;
27: PetscBool flg,terse;
30: SlepcInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
32: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
33: Load the operator matrix that defines the singular value problem
34: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
36: PetscPrintf(PETSC_COMM_WORLD,"\nSingular value problem stored in file.\n\n");
37: PetscOptionsGetString(NULL,NULL,"-file",filename,sizeof(filename),&flg);
38: if (!flg) SETERRQ(PETSC_COMM_WORLD,1,"Must indicate a file name with the -file option");
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 singular value solver and set various options
53: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
55: /*
56: Create singular value solver context
57: */
58: SVDCreate(PETSC_COMM_WORLD,&svd);
60: /*
61: Set operator
62: */
63: SVDSetOperators(svd,A,NULL);
65: /*
66: Set solver parameters at runtime
67: */
68: SVDSetFromOptions(svd);
70: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
71: Solve the singular value system
72: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
74: SVDSolve(svd);
75: SVDGetIterationNumber(svd,&its);
76: PetscPrintf(PETSC_COMM_WORLD," Number of iterations of the method: %D\n",its);
78: /*
79: Optional: Get some information from the solver and display it
80: */
81: SVDGetType(svd,&type);
82: PetscPrintf(PETSC_COMM_WORLD," Solution method: %s\n\n",type);
83: SVDGetDimensions(svd,&nsv,NULL,NULL);
84: PetscPrintf(PETSC_COMM_WORLD," Number of requested singular values: %D\n",nsv);
85: SVDGetTolerances(svd,&tol,&maxit);
86: PetscPrintf(PETSC_COMM_WORLD," Stopping condition: tol=%.4g, maxit=%D\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) {
95: SVDErrorView(svd,SVD_ERROR_RELATIVE,NULL);
96: } else {
97: PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL);
98: SVDConvergedReasonView(svd,PETSC_VIEWER_STDOUT_WORLD);
99: SVDErrorView(svd,SVD_ERROR_RELATIVE,PETSC_VIEWER_STDOUT_WORLD);
100: PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);
101: }
102: SVDDestroy(&svd);
103: MatDestroy(&A);
104: SlepcFinalize();
105: return ierr;
106: }
107: /*TEST
109: testset:
110: requires: double !complex !define(PETSC_USE_64BIT_INDICES)
111: args: -file ${SLEPC_DIR}/share/slepc/datafiles/matrices/rdb200.petsc -terse
112: test:
113: suffix: 1
114: args: -svd_nsv 4 -svd_standard -svd_ncv 12 -svd_type {{trlanczos lanczos randomized cross}}
115: filter: grep -v method
116: test:
117: suffix: 1_scalapack
118: nsize: {{1 2 3}}
119: args: -svd_nsv 4 -svd_type scalapack
120: requires: scalapack
121: test:
122: suffix: 1_elemental
123: nsize: {{1 2 3}}
124: args: -svd_nsv 4 -svd_type elemental
125: requires: elemental
126: test:
127: suffix: 2
128: args: -svd_nsv 2 -svd_type cyclic -svd_cyclic_explicitmatrix -svd_cyclic_st_type sinvert -svd_cyclic_eps_target 12.0 -svd_cyclic_st_ksp_type preonly -svd_cyclic_st_pc_type lu -svd_view
129: filter: grep -v tolerance
131: testset:
132: requires: double complex datafilespath !define(PETSC_USE_64BIT_INDICES)
133: args: -file ${DATAFILESPATH}/matrices/complex/qc324.petsc -terse
134: test:
135: suffix: 1_complex
136: args: -svd_nsv 4
137: test:
138: suffix: 1_complex_scalapack
139: nsize: {{1 2 3}}
140: args: -svd_nsv 4 -svd_type scalapack
141: requires: scalapack
142: test:
143: suffix: 1_complex_elemental
144: nsize: {{1 2 3}}
145: args: -svd_nsv 4 -svd_type elemental
146: requires: elemental
147: test:
148: suffix: 2_complex
149: args: -svd_nsv 2 -svd_type cyclic -svd_cyclic_explicitmatrix -svd_cyclic_st_type sinvert -svd_cyclic_eps_target 12.0 -svd_cyclic_st_ksp_type preonly -svd_cyclic_st_pc_type lu -svd_view
150: filter: grep -v tolerance
152: testset:
153: args: -svd_nsv 5 -svd_type randomized -svd_max_it 1 -svd_conv_maxit
154: test:
155: suffix: 3
156: args: -file ${SLEPC_DIR}/share/slepc/datafiles/matrices/rdb200.petsc
157: requires: double !complex !define(PETSC_USE_64BIT_INDICES)
158: test:
159: suffix: 3_complex
160: args: -file ${DATAFILESPATH}/matrices/complex/qc324.petsc
161: requires: double complex datafilespath !define(PETSC_USE_64BIT_INDICES)
162: filter: sed -e 's/[0-9][0-9]$//'
164: TEST*/