Line data Source code
1 : /*
2 : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3 : SLEPc - Scalable Library for Eigenvalue Problem Computations
4 : Copyright (c) 2002-, Universitat Politecnica de Valencia, Spain
5 :
6 : This file is part of SLEPc.
7 : SLEPc is distributed under a 2-clause BSD license (see LICENSE).
8 : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
9 : */
10 :
11 : static char help[] = "Test DSSVD with compact storage.\n\n";
12 :
13 : #include <slepcds.h>
14 :
15 6 : int main(int argc,char **argv)
16 : {
17 6 : DS ds;
18 6 : SlepcSC sc;
19 6 : PetscReal *T,sigma;
20 6 : PetscScalar *U,*w,d;
21 6 : PetscInt i,n=10,m,l=2,k=5,ld;
22 6 : PetscViewer viewer;
23 6 : PetscBool verbose,extrarow;
24 :
25 6 : PetscFunctionBeginUser;
26 6 : PetscCall(SlepcInitialize(&argc,&argv,NULL,help));
27 6 : PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
28 6 : m = n;
29 6 : PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Solve a Dense System of type SVD with compact storage - dimension %" PetscInt_FMT "x%" PetscInt_FMT ".\n",n,m));
30 6 : PetscCall(PetscOptionsGetInt(NULL,NULL,"-l",&l,NULL));
31 6 : PetscCall(PetscOptionsGetInt(NULL,NULL,"-k",&k,NULL));
32 6 : PetscCheck(l<=n && k<=n && l<=k,PETSC_COMM_WORLD,PETSC_ERR_USER_INPUT,"Wrong value of dimensions");
33 6 : PetscCall(PetscOptionsHasName(NULL,NULL,"-verbose",&verbose));
34 6 : PetscCall(PetscOptionsHasName(NULL,NULL,"-extrarow",&extrarow));
35 :
36 : /* Create DS object */
37 6 : PetscCall(DSCreate(PETSC_COMM_WORLD,&ds));
38 6 : PetscCall(DSSetType(ds,DSSVD));
39 6 : PetscCall(DSSetFromOptions(ds));
40 6 : ld = n+2; /* test leading dimension larger than n */
41 6 : PetscCall(DSAllocate(ds,ld));
42 6 : PetscCall(DSSetDimensions(ds,n,l,k));
43 6 : PetscCall(DSSVDSetDimensions(ds,m));
44 6 : PetscCall(DSSetCompact(ds,PETSC_TRUE));
45 6 : PetscCall(DSSetExtraRow(ds,extrarow));
46 :
47 : /* Set up viewer */
48 6 : PetscCall(PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&viewer));
49 6 : PetscCall(PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_INFO_DETAIL));
50 6 : PetscCall(DSView(ds,viewer));
51 6 : PetscCall(PetscViewerPopFormat(viewer));
52 6 : PetscCall(PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_MATLAB));
53 :
54 : /* Fill upper arrow-bidiagonal matrix */
55 6 : PetscCall(DSGetArrayReal(ds,DS_MAT_T,&T));
56 66 : for (i=0;i<n;i++) T[i] = (PetscReal)(i+1);
57 52 : for (i=l;i<n-1;i++) T[i+ld] = 1.0;
58 6 : if (extrarow) T[n-1+ld] = 1.0;
59 6 : PetscCall(DSRestoreArrayReal(ds,DS_MAT_T,&T));
60 6 : if (l==0 && k==0) PetscCall(DSSetState(ds,DS_STATE_INTERMEDIATE));
61 4 : else PetscCall(DSSetState(ds,DS_STATE_RAW));
62 6 : PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Initial - - - - - - - - -\n"));
63 6 : PetscCall(DSView(ds,viewer));
64 :
65 : /* Solve */
66 6 : PetscCall(PetscMalloc1(n,&w));
67 6 : PetscCall(DSGetSlepcSC(ds,&sc));
68 6 : sc->comparison = SlepcCompareLargestReal;
69 6 : sc->comparisonctx = NULL;
70 6 : sc->map = NULL;
71 6 : sc->mapobj = NULL;
72 6 : PetscCall(DSSolve(ds,w,NULL));
73 6 : PetscCall(DSSort(ds,w,NULL,NULL,NULL,NULL));
74 6 : if (extrarow) PetscCall(DSUpdateExtraRow(ds));
75 6 : if (verbose) {
76 0 : PetscCall(PetscPrintf(PETSC_COMM_WORLD,"After solve - - - - - - - - -\n"));
77 0 : PetscCall(DSView(ds,viewer));
78 : }
79 :
80 : /* Print singular values */
81 6 : PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Computed singular values =\n"));
82 66 : for (i=0;i<n;i++) {
83 60 : sigma = PetscRealPart(w[i]);
84 60 : PetscCall(PetscViewerASCIIPrintf(viewer," %.5f\n",(double)sigma));
85 : }
86 :
87 6 : if (extrarow) {
88 : /* Check that extra row is correct */
89 2 : PetscCall(DSGetArrayReal(ds,DS_MAT_T,&T));
90 2 : PetscCall(DSGetArray(ds,DS_MAT_U,&U));
91 2 : d = 0.0;
92 18 : for (i=l;i<n;i++) d += T[i+ld]-U[n-1+i*ld];
93 2 : if (PetscAbsScalar(d)>10*PETSC_MACHINE_EPSILON) PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Warning: there is a mismatch in the extra row of %g\n",(double)PetscAbsScalar(d)));
94 2 : PetscCall(DSRestoreArrayReal(ds,DS_MAT_T,&T));
95 2 : PetscCall(DSRestoreArray(ds,DS_MAT_U,&U));
96 : }
97 6 : PetscCall(PetscFree(w));
98 6 : PetscCall(DSDestroy(&ds));
99 6 : PetscCall(SlepcFinalize());
100 : return 0;
101 : }
102 :
103 : /*TEST
104 :
105 : test:
106 : args: -ds_method {{0 1}}
107 : suffix: 1
108 : filter: grep -v "solving the problem"
109 : requires: !single
110 :
111 : test:
112 : args: -l 0 -k 0 -ds_method {{0 1}}
113 : suffix: 2
114 : filter: grep -v "solving the problem"
115 : requires: !single
116 :
117 : test:
118 : args: -extrarow -ds_method {{0 1}}
119 : suffix: 3
120 : filter: grep -v "solving the problem"
121 : requires: !single
122 :
123 : TEST*/
|