Actual source code: ex48.c
slepc-3.16.1 2021-11-17
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 GSVD problem with matrices loaded from a file.\n"
12: "The command line options are:\n"
13: " -f1 <filename>, PETSc binary file containing matrix A.\n"
14: " -f2 <filename>, PETSc binary file containing matrix B (optional).\n\n";
16: #include <slepcsvd.h>
18: int main(int argc,char **argv)
19: {
20: Mat A,B; /* matrices */
21: SVD svd; /* singular value problem solver context */
22: PetscInt i,m,n,Istart,Iend,col[2];
23: PetscScalar vals[] = { -1, 1 };
24: char filename[PETSC_MAX_PATH_LEN];
25: PetscViewer viewer;
26: PetscBool flg,terse;
29: SlepcInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
31: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
32: Load matrices that define the generalized singular value problem
33: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
35: PetscPrintf(PETSC_COMM_WORLD,"\nGeneralized singular value problem stored in file.\n\n");
36: PetscOptionsGetString(NULL,NULL,"-f1",filename,sizeof(filename),&flg);
37: if (!flg) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_USER_INPUT,"Must indicate a file name for matrix A with the -f1 option");
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: MatGetSize(A,&m,&n);
52: PetscOptionsGetString(NULL,NULL,"-f2",filename,sizeof(filename),&flg);
53: if (flg) {
54: PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,FILE_MODE_READ,&viewer);
55: MatCreate(PETSC_COMM_WORLD,&B);
56: MatSetFromOptions(B);
57: MatLoad(B,viewer);
58: PetscViewerDestroy(&viewer);
59: } else {
60: PetscPrintf(PETSC_COMM_WORLD," Matrix B was not provided, setting B=bidiag(1,-1)\n\n");
61: MatCreate(PETSC_COMM_WORLD,&B);
62: MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,n+1,n);
63: MatSetFromOptions(B);
64: MatSetUp(B);
65: MatGetOwnershipRange(B,&Istart,&Iend);
66: for (i=Istart;i<Iend;i++) {
67: col[0]=i-1; col[1]=i;
68: if (i==0) {
69: MatSetValue(B,i,col[1],vals[1],INSERT_VALUES);
70: } else if (i<n) {
71: MatSetValues(B,1,&i,2,col,vals,INSERT_VALUES);
72: } else if (i==n) {
73: MatSetValue(B,i,col[0],vals[0],INSERT_VALUES);
74: }
75: }
76: MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
77: MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
78: }
80: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
81: Create the singular value solver and set various options
82: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
84: /*
85: Create singular value solver context
86: */
87: SVDCreate(PETSC_COMM_WORLD,&svd);
89: /*
90: Set operators of GSVD problem
91: */
92: SVDSetOperators(svd,A,B);
93: SVDSetProblemType(svd,SVD_GENERALIZED);
95: /*
96: Set solver parameters at runtime
97: */
98: SVDSetFromOptions(svd);
100: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
101: Solve the problem and print solution
102: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
104: SVDSolve(svd);
106: /* show detailed info unless -terse option is given by user */
107: PetscOptionsHasName(NULL,NULL,"-terse",&terse);
108: if (terse) {
109: SVDErrorView(svd,SVD_ERROR_RELATIVE,NULL);
110: } else {
111: PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL);
112: SVDConvergedReasonView(svd,PETSC_VIEWER_STDOUT_WORLD);
113: SVDErrorView(svd,SVD_ERROR_RELATIVE,PETSC_VIEWER_STDOUT_WORLD);
114: PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);
115: }
116: SVDDestroy(&svd);
117: MatDestroy(&A);
118: MatDestroy(&B);
119: SlepcFinalize();
120: return ierr;
121: }
122: /*TEST
124: testset:
125: requires: double !complex !defined(PETSC_USE_64BIT_INDICES)
126: args: -f1 ${SLEPC_DIR}/share/slepc/datafiles/matrices/bfw62a.petsc -f2 ${SLEPC_DIR}/share/slepc/datafiles/matrices/bfw62b.petsc -svd_nsv 3 -terse
127: output_file: output/ex48_1.out
128: test:
129: suffix: 1
130: args: -svd_type trlanczos -svd_trlanczos_explicitmatrix {{0 1}}
131: TODO: does not work for largest singular values
132: test:
133: suffix: 1_spqr
134: args: -svd_type trlanczos -svd_trlanczos_explicitmatrix -svd_trlanczos_pc_type qr
135: requires: suitesparse
136: TODO: does not work for largest singular values
137: test:
138: suffix: 1_cross
139: args: -svd_type cross -svd_cross_explicitmatrix
140: test:
141: suffix: 1_cyclic
142: args: -svd_type cyclic -svd_cyclic_explicitmatrix
144: testset:
145: requires: double complex datafilespath !defined(PETSC_USE_64BIT_INDICES)
146: args: -f1 ${DATAFILESPATH}/matrices/complex/qc324.petsc -svd_nsv 3 -terse
147: output_file: output/ex48_2.out
148: test:
149: suffix: 2
150: args: -svd_type trlanczos -svd_tol 1e-10 -svd_trlanczos_explicitmatrix {{0 1}} -svd_trlanczos_ksp_rtol 1e-11
151: test:
152: suffix: 2_spqr
153: args: -svd_type trlanczos -svd_tol 1e-10 -svd_trlanczos_explicitmatrix -svd_trlanczos_pc_type qr -svd_trlanczos_ksp_rtol 1e-11
154: requires: suitesparse
155: test:
156: suffix: 2_cross
157: args: -svd_type cross -svd_cross_explicitmatrix
158: test:
159: suffix: 2_cyclic
160: args: -svd_type cyclic -svd_cyclic_explicitmatrix
162: TEST*/