Actual source code: test2.c
slepc-3.17.2 2022-08-09
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[] = "Test DSHEP.\n\n";
13: #include <slepcds.h>
15: int main(int argc,char **argv)
16: {
17: DS ds;
18: SlepcSC sc;
19: PetscScalar *A,*X,*Q,*eig,d;
20: PetscReal rnorm,aux;
21: PetscInt i,j,n=10,ld;
22: PetscViewer viewer;
23: PetscBool verbose,extrarow;
25: SlepcInitialize(&argc,&argv,(char*)0,help);
26: PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
27: PetscPrintf(PETSC_COMM_WORLD,"Solve a Dense System of type HEP - dimension %" PetscInt_FMT ".\n",n);
28: PetscOptionsHasName(NULL,NULL,"-verbose",&verbose);
29: PetscOptionsHasName(NULL,NULL,"-extrarow",&extrarow);
31: /* Create DS object */
32: DSCreate(PETSC_COMM_WORLD,&ds);
33: DSSetType(ds,DSHEP);
34: DSSetFromOptions(ds);
35: ld = n+2; /* test leading dimension larger than n */
36: DSAllocate(ds,ld);
37: DSSetDimensions(ds,n,0,0);
38: DSSetExtraRow(ds,extrarow);
40: /* Set up viewer */
41: PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&viewer);
42: PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_INFO_DETAIL);
43: DSView(ds,viewer);
44: PetscViewerPopFormat(viewer);
45: if (verbose) PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_MATLAB);
47: /* Fill with a symmetric Toeplitz matrix */
48: DSGetArray(ds,DS_MAT_A,&A);
49: for (i=0;i<n;i++) A[i+i*ld]=2.0;
50: for (j=1;j<3;j++) {
51: for (i=0;i<n-j;i++) { A[i+(i+j)*ld]=1.0; A[(i+j)+i*ld]=1.0; }
52: }
53: if (extrarow) { A[n+(n-2)*ld]=1.0; A[n+(n-1)*ld]=1.0; }
54: DSRestoreArray(ds,DS_MAT_A,&A);
55: DSSetState(ds,DS_STATE_RAW);
56: if (verbose) {
57: PetscPrintf(PETSC_COMM_WORLD,"Initial - - - - - - - - -\n");
58: DSView(ds,viewer);
59: }
61: /* Solve */
62: PetscMalloc1(n,&eig);
63: DSGetSlepcSC(ds,&sc);
64: sc->comparison = SlepcCompareLargestMagnitude;
65: sc->comparisonctx = NULL;
66: sc->map = NULL;
67: sc->mapobj = NULL;
68: DSSolve(ds,eig,NULL);
69: DSSort(ds,eig,NULL,NULL,NULL,NULL);
70: if (extrarow) DSUpdateExtraRow(ds);
71: if (verbose) {
72: PetscPrintf(PETSC_COMM_WORLD,"After solve - - - - - - - - -\n");
73: DSView(ds,viewer);
74: }
76: /* Print eigenvalues */
77: PetscPrintf(PETSC_COMM_WORLD,"Computed eigenvalues =\n");
78: for (i=0;i<n;i++) PetscViewerASCIIPrintf(viewer," %.5f\n",(double)PetscRealPart(eig[i]));
80: if (extrarow) {
81: /* Check that extra row is correct */
82: DSGetArray(ds,DS_MAT_A,&A);
83: DSGetArray(ds,DS_MAT_Q,&Q);
84: d = 0.0;
85: for (i=0;i<n;i++) d += A[n+i*ld]-Q[n-2+i*ld]-Q[n-1+i*ld];
86: if (PetscAbsScalar(d)>10*PETSC_MACHINE_EPSILON) PetscPrintf(PETSC_COMM_WORLD,"Warning: there is a mismatch in the extra row of %g\n",(double)PetscAbsScalar(d));
87: DSRestoreArray(ds,DS_MAT_A,&A);
88: DSRestoreArray(ds,DS_MAT_Q,&Q);
89: }
91: /* Eigenvectors */
92: j = 2;
93: DSVectors(ds,DS_MAT_X,&j,&rnorm); /* third eigenvector */
94: PetscPrintf(PETSC_COMM_WORLD,"Value of rnorm for 3rd vector = %.3f\n",(double)rnorm);
95: DSVectors(ds,DS_MAT_X,NULL,NULL); /* all eigenvectors */
96: j = 0;
97: rnorm = 0.0;
98: DSGetArray(ds,DS_MAT_X,&X);
99: for (i=0;i<n;i++) {
100: aux = PetscAbsScalar(X[i+j*ld]);
101: rnorm += aux*aux;
102: }
103: DSRestoreArray(ds,DS_MAT_X,&X);
104: rnorm = PetscSqrtReal(rnorm);
105: PetscPrintf(PETSC_COMM_WORLD,"Norm of 1st vector = %.3f\n",(double)rnorm);
106: if (verbose) {
107: PetscPrintf(PETSC_COMM_WORLD,"After vectors - - - - - - - - -\n");
108: DSView(ds,viewer);
109: }
111: PetscFree(eig);
112: DSDestroy(&ds);
113: SlepcFinalize();
114: return 0;
115: }
117: /*TEST
119: testset:
120: args: -n 12 -ds_method {{0 1 2}}
121: filter: grep -v "solving the problem" | sed -e "s/extrarow//"
122: output_file: output/test2_1.out
123: requires: !single
124: test:
125: suffix: 1
126: test:
127: suffix: 2
128: args: -extrarow
130: TEST*/