Actual source code: ex48.c
slepc-main 2024-11-15
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 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). Instead of"
15: " a file it is possible to specify one of 'identity', 'bidiagonal' or 'tridiagonal'"
16: " -p <p>, in case B is not taken from a file.\n\n";
18: #include <slepcsvd.h>
20: int main(int argc,char **argv)
21: {
22: Mat A,B; /* matrices */
23: SVD svd; /* singular value problem solver context */
24: PetscInt i,m,n,p,Istart,Iend,col[3];
25: PetscScalar vals[3];
26: char filename[PETSC_MAX_PATH_LEN];
27: PetscViewer viewer;
28: PetscBool flg,terse;
30: PetscFunctionBeginUser;
31: PetscCall(SlepcInitialize(&argc,&argv,NULL,help));
33: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
34: Load matrices that define the generalized singular value problem
35: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
37: PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\nGeneralized singular value problem stored in file.\n\n"));
38: PetscCall(PetscOptionsGetString(NULL,NULL,"-f1",filename,sizeof(filename),&flg));
39: PetscCheck(flg,PETSC_COMM_WORLD,PETSC_ERR_USER_INPUT,"Must indicate a file name for matrix A with the -f1 option");
41: #if defined(PETSC_USE_COMPLEX)
42: PetscCall(PetscPrintf(PETSC_COMM_WORLD," Reading COMPLEX matrix from a binary file...\n"));
43: #else
44: PetscCall(PetscPrintf(PETSC_COMM_WORLD," Reading REAL matrix from a binary file...\n"));
45: #endif
46: PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,FILE_MODE_READ,&viewer));
47: PetscCall(MatCreate(PETSC_COMM_WORLD,&A));
48: PetscCall(MatSetFromOptions(A));
49: PetscCall(MatLoad(A,viewer));
50: PetscCall(PetscViewerDestroy(&viewer));
52: PetscCall(MatGetSize(A,&m,&n));
54: PetscCall(PetscOptionsGetString(NULL,NULL,"-f2",filename,sizeof(filename),&flg));
55: PetscCheck(flg,PETSC_COMM_WORLD,PETSC_ERR_USER_INPUT,"Must indicate a file name for matrix B with the -f2 option, or alternatively the strings 'identity', 'bidiagonal', or 'tridiagonal'");
56: PetscCall(PetscStrcmp(filename,"identity",&flg));
57: if (flg) {
58: p = n;
59: PetscCall(PetscOptionsGetInt(NULL,NULL,"-p",&p,&flg));
60: PetscCall(PetscPrintf(PETSC_COMM_WORLD," Using B=I with p=%" PetscInt_FMT "\n\n",p));
61: PetscCall(MatCreate(PETSC_COMM_WORLD,&B));
62: PetscCall(MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,p,n));
63: PetscCall(MatSetFromOptions(B));
64: PetscCall(MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY));
65: PetscCall(MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY));
66: PetscCall(MatShift(B,1.0));
67: } else {
68: PetscCall(PetscStrcmp(filename,"bidiagonal",&flg));
69: if (flg) {
70: p = n+1;
71: PetscCall(PetscOptionsGetInt(NULL,NULL,"-p",&p,&flg));
72: vals[0]=-1; vals[1]=1;
73: PetscCall(PetscPrintf(PETSC_COMM_WORLD," Using B=bidiag(1,-1) with p=%" PetscInt_FMT "\n\n",p));
74: PetscCall(MatCreate(PETSC_COMM_WORLD,&B));
75: PetscCall(MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,p,n));
76: PetscCall(MatSetFromOptions(B));
77: PetscCall(MatGetOwnershipRange(B,&Istart,&Iend));
78: for (i=Istart;i<Iend;i++) {
79: col[0]=i-1; col[1]=i;
80: if (i==0) PetscCall(MatSetValue(B,i,col[1],vals[1],INSERT_VALUES));
81: else if (i<n) PetscCall(MatSetValues(B,1,&i,2,col,vals,INSERT_VALUES));
82: else if (i==n) PetscCall(MatSetValue(B,i,col[0],vals[0],INSERT_VALUES));
83: }
84: PetscCall(MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY));
85: PetscCall(MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY));
86: } else {
87: PetscCall(PetscStrcmp(filename,"tridiagonal",&flg));
88: if (flg) {
89: p = n-2;
90: PetscCall(PetscOptionsGetInt(NULL,NULL,"-p",&p,&flg));
91: vals[0]=-1; vals[1]=2; vals[2]=-1;
92: PetscCall(PetscPrintf(PETSC_COMM_WORLD," Using B=tridiag(-1,2,-1) with p=%" PetscInt_FMT "\n\n",p));
93: PetscCall(MatCreate(PETSC_COMM_WORLD,&B));
94: PetscCall(MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,p,n));
95: PetscCall(MatSetFromOptions(B));
96: PetscCall(MatGetOwnershipRange(B,&Istart,&Iend));
97: for (i=Istart;i<Iend;i++) {
98: col[0]=i; col[1]=i+1; col[2]=i+2;
99: PetscCall(MatSetValues(B,1,&i,3,col,vals,INSERT_VALUES));
100: }
101: PetscCall(MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY));
102: PetscCall(MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY));
103: } else { /* load file */
104: PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,FILE_MODE_READ,&viewer));
105: PetscCall(MatCreate(PETSC_COMM_WORLD,&B));
106: PetscCall(MatSetFromOptions(B));
107: PetscCall(MatLoad(B,viewer));
108: PetscCall(PetscViewerDestroy(&viewer));
109: }
110: }
111: }
113: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
114: Create the singular value solver and set various options
115: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
117: /*
118: Create singular value solver context
119: */
120: PetscCall(SVDCreate(PETSC_COMM_WORLD,&svd));
122: /*
123: Set operators of GSVD problem
124: */
125: PetscCall(SVDSetOperators(svd,A,B));
126: PetscCall(SVDSetProblemType(svd,SVD_GENERALIZED));
128: /*
129: Set solver parameters at runtime
130: */
131: PetscCall(SVDSetFromOptions(svd));
133: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
134: Solve the problem and print solution
135: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
137: PetscCall(SVDSolve(svd));
139: /* show detailed info unless -terse option is given by user */
140: PetscCall(PetscOptionsHasName(NULL,NULL,"-terse",&terse));
141: if (terse) PetscCall(SVDErrorView(svd,SVD_ERROR_NORM,NULL));
142: else {
143: PetscCall(PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL));
144: PetscCall(SVDConvergedReasonView(svd,PETSC_VIEWER_STDOUT_WORLD));
145: PetscCall(SVDErrorView(svd,SVD_ERROR_NORM,PETSC_VIEWER_STDOUT_WORLD));
146: PetscCall(PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD));
147: }
148: PetscCall(SVDDestroy(&svd));
149: PetscCall(MatDestroy(&A));
150: PetscCall(MatDestroy(&B));
151: PetscCall(SlepcFinalize());
152: return 0;
153: }
154: /*TEST
156: testset:
157: requires: double !complex !defined(PETSC_USE_64BIT_INDICES)
158: args: -f1 ${SLEPC_DIR}/share/slepc/datafiles/matrices/bfw62a.petsc -f2 ${SLEPC_DIR}/share/slepc/datafiles/matrices/bfw62b.petsc -svd_nsv 3 -terse
159: output_file: output/ex48_1.out
160: test:
161: suffix: 1
162: args: -svd_type trlanczos -svd_trlanczos_explicitmatrix {{0 1}} -svd_trlanczos_scale 1e5 -svd_trlanczos_ksp_rtol 1e-15
163: test:
164: suffix: 1_spqr
165: args: -svd_type trlanczos -svd_trlanczos_explicitmatrix -svd_trlanczos_pc_type qr -svd_trlanczos_scale 1e5 -svd_trlanczos_oneside {{0 1}}
166: requires: suitesparse
167: test:
168: suffix: 1_autoscale
169: args: -svd_type trlanczos -svd_trlanczos_gbidiag {{lower upper}} -svd_trlanczos_scale -5 -svd_trlanczos_ksp_rtol 1e-16 -svd_trlanczos_oneside {{0 1}}
170: test:
171: suffix: 1_cross
172: args: -svd_type cross -svd_cross_explicitmatrix
173: test:
174: suffix: 1_cyclic
175: args: -svd_type cyclic -svd_cyclic_explicitmatrix
177: testset:
178: requires: double complex datafilespath !defined(PETSC_USE_64BIT_INDICES)
179: args: -f1 ${DATAFILESPATH}/matrices/complex/qc324.petsc -f2 bidiagonal -svd_nsv 3 -terse
180: output_file: output/ex48_2.out
181: filter: sed -e "s/30749/30748/"
182: timeoutfactor: 2
183: test:
184: suffix: 2
185: args: -svd_type trlanczos -svd_trlanczos_explicitmatrix {{0 1}} -svd_trlanczos_ksp_rtol 1e-10 -svd_trlanczos_scale 100
186: requires: !defined(PETSCTEST_VALGRIND)
187: test:
188: suffix: 2_spqr
189: args: -svd_type trlanczos -svd_trlanczos_explicitmatrix -svd_trlanczos_pc_type qr -svd_trlanczos_ksp_rtol 1e-10
190: requires: suitesparse
191: test:
192: suffix: 2_cross
193: args: -svd_type cross -svd_cross_explicitmatrix
194: test:
195: suffix: 2_cyclic
196: args: -svd_type cyclic -svd_cyclic_explicitmatrix
198: test:
199: requires: double complex datafilespath !defined(PETSC_USE_64BIT_INDICES) !defined(PETSCTEST_VALGRIND)
200: args: -f1 ${DATAFILESPATH}/matrices/complex/qc324.petsc -f2 bidiagonal -p 320 -svd_nsv 3 -svd_type trlanczos -svd_trlanczos_ksp_rtol 1e-14 -svd_trlanczos_scale 100 -terse
201: timeoutfactor: 2
202: suffix: 3
204: testset:
205: requires: double !complex !defined(PETSC_USE_64BIT_INDICES)
206: args: -f1 ${SLEPC_DIR}/share/slepc/datafiles/matrices/rdb200.petsc -f2 identity -svd_nsv 3 -svd_ncv 24 -svd_smallest -terse
207: output_file: output/ex48_4.out
208: test:
209: suffix: 4
210: args: -svd_type trlanczos
211: test:
212: suffix: 4_spqr
213: args: -svd_type trlanczos -svd_trlanczos_explicitmatrix -svd_trlanczos_pc_type qr
214: requires: suitesparse
215: test:
216: suffix: 4_cross
217: args: -svd_type cross -svd_cross_explicitmatrix
218: test:
219: suffix: 4_cross_implicit
220: args: -svd_type cross -svd_cross_eps_type lobpcg -svd_cross_st_ksp_type cg -svd_cross_st_pc_type jacobi -svd_max_it 1000
221: test:
222: suffix: 4_cyclic
223: args: -svd_type cyclic -svd_cyclic_explicitmatrix
224: test:
225: suffix: 4_hpddm
226: nsize: 4
227: args: -svd_type trlanczos -svd_trlanczos_explicitmatrix -svd_trlanczos_pc_type hpddm
228: args: -prefix_push svd_trlanczos_pc_hpddm_ -levels_1_st_share_sub_ksp -levels_1_eps_nev 10 -levels_1_eps_threshold 0.005 -levels_1_pc_asm_type basic -define_subdomains -levels_1_pc_asm_sub_mat_type sbaij -levels_1_sub_pc_type cholesky -prefix_pop
229: requires: hpddm
231: TEST*/