Actual source code: ex14.c
slepc-3.17.0 2022-03-31
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 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;
29: SlepcInitialize(&argc,&argv,(char*)0,help);
31: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
32: Load the operator matrix that defines the singular value problem
33: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
35: PetscPrintf(PETSC_COMM_WORLD,"\nSingular value problem stored in file.\n\n");
36: PetscOptionsGetString(NULL,NULL,"-file",filename,sizeof(filename),&flg);
39: #if defined(PETSC_USE_COMPLEX)
40: PetscPrintf(PETSC_COMM_WORLD," Reading COMPLEX matrix from a binary file...\n");
41: #else
42: PetscPrintf(PETSC_COMM_WORLD," Reading REAL matrix from a binary file...\n");
43: #endif
44: PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,FILE_MODE_READ,&viewer);
45: MatCreate(PETSC_COMM_WORLD,&A);
46: MatSetFromOptions(A);
47: MatLoad(A,viewer);
48: PetscViewerDestroy(&viewer);
50: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
51: Create the singular value solver and set various options
52: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
54: /*
55: Create singular value solver context
56: */
57: SVDCreate(PETSC_COMM_WORLD,&svd);
59: /*
60: Set operator
61: */
62: SVDSetOperators(svd,A,NULL);
64: /*
65: Set solver parameters at runtime
66: */
67: SVDSetFromOptions(svd);
69: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
70: Solve the singular value system
71: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
73: SVDSolve(svd);
74: SVDGetIterationNumber(svd,&its);
75: PetscPrintf(PETSC_COMM_WORLD," Number of iterations of the method: %" PetscInt_FMT "\n",its);
77: /*
78: Optional: Get some information from the solver and display it
79: */
80: SVDGetType(svd,&type);
81: PetscPrintf(PETSC_COMM_WORLD," Solution method: %s\n\n",type);
82: SVDGetDimensions(svd,&nsv,NULL,NULL);
83: PetscPrintf(PETSC_COMM_WORLD," Number of requested singular values: %" PetscInt_FMT "\n",nsv);
84: SVDGetTolerances(svd,&tol,&maxit);
85: PetscPrintf(PETSC_COMM_WORLD," Stopping condition: tol=%.4g, maxit=%" PetscInt_FMT "\n",(double)tol,maxit);
87: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
88: Display solution and clean up
89: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
91: /* show detailed info unless -terse option is given by user */
92: PetscOptionsHasName(NULL,NULL,"-terse",&terse);
93: if (terse) SVDErrorView(svd,SVD_ERROR_RELATIVE,NULL);
94: else {
95: PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL);
96: SVDConvergedReasonView(svd,PETSC_VIEWER_STDOUT_WORLD);
97: SVDErrorView(svd,SVD_ERROR_RELATIVE,PETSC_VIEWER_STDOUT_WORLD);
98: PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);
99: }
100: SVDDestroy(&svd);
101: MatDestroy(&A);
102: SlepcFinalize();
103: return 0;
104: }
105: /*TEST
107: testset:
108: requires: double !complex !defined(PETSC_USE_64BIT_INDICES)
109: args: -file ${SLEPC_DIR}/share/slepc/datafiles/matrices/rdb200.petsc -terse
110: test:
111: suffix: 1
112: args: -svd_nsv 4 -svd_standard -svd_ncv 12 -svd_type {{trlanczos lanczos randomized cross}}
113: filter: grep -v method
114: test:
115: suffix: 1_scalapack
116: nsize: {{1 2 3}}
117: args: -svd_nsv 4 -svd_type scalapack
118: requires: scalapack
119: test:
120: suffix: 1_elemental
121: nsize: {{1 2 3}}
122: args: -svd_nsv 4 -svd_type elemental
123: requires: elemental
124: test:
125: suffix: 2
126: 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
127: filter: grep -v tolerance
128: test:
129: suffix: 2_cross
130: args: -svd_nsv 2 -svd_type cross -svd_cross_explicitmatrix -svd_cross_st_type sinvert -svd_cross_eps_target 100.0
131: filter: grep -v tolerance
133: testset:
134: requires: double complex datafilespath !defined(PETSC_USE_64BIT_INDICES)
135: args: -file ${DATAFILESPATH}/matrices/complex/qc324.petsc -terse
136: test:
137: suffix: 1_complex
138: args: -svd_nsv 4
139: test:
140: suffix: 1_complex_scalapack
141: nsize: {{1 2 3}}
142: args: -svd_nsv 4 -svd_type scalapack
143: requires: scalapack
144: test:
145: suffix: 1_complex_elemental
146: nsize: {{1 2 3}}
147: args: -svd_nsv 4 -svd_type elemental
148: requires: elemental
149: test:
150: suffix: 2_complex
151: 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
152: filter: grep -v tolerance
154: testset:
155: args: -svd_nsv 5 -svd_type randomized -svd_max_it 1 -svd_conv_maxit
156: test:
157: suffix: 3
158: args: -file ${SLEPC_DIR}/share/slepc/datafiles/matrices/rdb200.petsc
159: requires: double !complex !defined(PETSC_USE_64BIT_INDICES)
160: test:
161: suffix: 3_complex
162: args: -file ${DATAFILESPATH}/matrices/complex/qc324.petsc
163: requires: double complex datafilespath !defined(PETSC_USE_64BIT_INDICES)
164: filter: sed -e 's/[0-9][0-9]$//'
166: TEST*/