Actual source code: test1.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[] = "Test DSNHEP.\n\n";
13: #include <slepcds.h>
15: int main(int argc,char **argv)
16: {
18: DS ds;
19: SlepcSC sc;
20: DSType type;
21: DSStateType state;
22: PetscScalar *A,*X,*Q,*wr,*wi,d;
23: PetscReal re,im,rnorm,aux;
24: PetscInt i,j,n=10,ld,method;
25: PetscViewer viewer;
26: PetscBool verbose,extrarow;
28: SlepcInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
29: PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
30: PetscPrintf(PETSC_COMM_WORLD,"Solve a Dense System of type NHEP - dimension %D.\n",n);
31: PetscOptionsHasName(NULL,NULL,"-verbose",&verbose);
32: PetscOptionsHasName(NULL,NULL,"-extrarow",&extrarow);
34: /* Create DS object */
35: DSCreate(PETSC_COMM_WORLD,&ds);
36: DSSetType(ds,DSNHEP);
37: DSSetFromOptions(ds);
38: ld = n+2; /* test leading dimension larger than n */
39: DSAllocate(ds,ld);
40: DSSetDimensions(ds,n,0,0);
41: DSSetExtraRow(ds,extrarow);
43: /* Set up viewer */
44: PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&viewer);
45: PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_INFO_DETAIL);
46: DSView(ds,viewer);
47: PetscViewerPopFormat(viewer);
48: if (verbose) {
49: PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_MATLAB);
50: }
52: /* Fill with Grcar matrix */
53: DSGetArray(ds,DS_MAT_A,&A);
54: for (i=1;i<n;i++) A[i+(i-1)*ld]=-1.0;
55: for (j=0;j<4;j++) {
56: for (i=0;i<n-j;i++) A[i+(i+j)*ld]=1.0;
57: }
58: if (extrarow) A[n+(n-1)*ld]=-1.0;
59: DSRestoreArray(ds,DS_MAT_A,&A);
60: DSSetState(ds,DS_STATE_INTERMEDIATE);
61: if (verbose) {
62: PetscPrintf(PETSC_COMM_WORLD,"Initial - - - - - - - - -\n");
63: DSView(ds,viewer);
64: }
66: /* Solve */
67: PetscMalloc2(n,&wr,n,&wi);
68: DSGetSlepcSC(ds,&sc);
69: sc->comparison = SlepcCompareLargestMagnitude;
70: sc->comparisonctx = NULL;
71: sc->map = NULL;
72: sc->mapobj = NULL;
73: DSSolve(ds,wr,wi);
74: DSSort(ds,wr,wi,NULL,NULL,NULL);
75: if (extrarow) { DSUpdateExtraRow(ds); }
77: DSGetType(ds,&type);
78: DSGetMethod(ds,&method);
79: PetscPrintf(PETSC_COMM_WORLD,"DS of type %s, method used=%d\n",type,method);
80: DSGetState(ds,&state);
81: PetscPrintf(PETSC_COMM_WORLD,"State after solve: %s\n",DSStateTypes[state]);
83: if (verbose) {
84: PetscPrintf(PETSC_COMM_WORLD,"After solve - - - - - - - - -\n");
85: DSView(ds,viewer);
86: }
88: /* Print eigenvalues */
89: PetscPrintf(PETSC_COMM_WORLD,"Computed eigenvalues =\n");
90: for (i=0;i<n;i++) {
91: #if defined(PETSC_USE_COMPLEX)
92: re = PetscRealPart(wr[i]);
93: im = PetscImaginaryPart(wr[i]);
94: #else
95: re = wr[i];
96: im = wi[i];
97: #endif
98: if (PetscAbs(im)<1e-10) {
99: PetscViewerASCIIPrintf(viewer," %.5f\n",(double)re);
100: } else {
101: PetscViewerASCIIPrintf(viewer," %.5f%+.5fi\n",(double)re,(double)im);
102: }
103: }
105: if (extrarow) {
106: /* Check that extra row is correct */
107: DSGetArray(ds,DS_MAT_A,&A);
108: DSGetArray(ds,DS_MAT_Q,&Q);
109: d = 0.0;
110: for (i=0;i<n;i++) d += A[n+i*ld]+Q[n-1+i*ld];
111: if (PetscAbsScalar(d)>10*PETSC_MACHINE_EPSILON) {
112: PetscPrintf(PETSC_COMM_WORLD,"Warning: there is a mismatch in the extra row of %g\n",(double)PetscAbsScalar(d));
113: }
114: DSRestoreArray(ds,DS_MAT_A,&A);
115: DSRestoreArray(ds,DS_MAT_Q,&Q);
116: }
118: /* Eigenvectors */
119: j = 2;
120: DSVectors(ds,DS_MAT_X,&j,&rnorm); /* third eigenvector */
121: PetscPrintf(PETSC_COMM_WORLD,"Value of rnorm for 3rd vector = %.3f\n",(double)rnorm);
122: DSVectors(ds,DS_MAT_X,NULL,NULL); /* all eigenvectors */
123: j = 0;
124: rnorm = 0.0;
125: DSGetArray(ds,DS_MAT_X,&X);
126: for (i=0;i<n;i++) {
127: #if defined(PETSC_USE_COMPLEX)
128: aux = PetscAbsScalar(X[i+j*ld]);
129: #else
130: if (PetscAbs(wi[j])==0.0) aux = PetscAbsScalar(X[i+j*ld]);
131: else aux = SlepcAbsEigenvalue(X[i+j*ld],X[i+(j+1)*ld]);
132: #endif
133: rnorm += aux*aux;
134: }
135: DSRestoreArray(ds,DS_MAT_X,&X);
136: rnorm = PetscSqrtReal(rnorm);
137: PetscPrintf(PETSC_COMM_WORLD,"Norm of 1st vector = %.3f\n",(double)rnorm);
138: if (verbose) {
139: PetscPrintf(PETSC_COMM_WORLD,"After vectors - - - - - - - - -\n");
140: DSView(ds,viewer);
141: }
143: PetscFree2(wr,wi);
144: DSDestroy(&ds);
145: SlepcFinalize();
146: return ierr;
147: }
149: /*TEST
151: testset:
152: filter: sed -e "s/[+-]\([0-9]\.[0-9]*i\)/+-\\1/" | sed -e "s/extrarow//"
153: output_file: output/test1_1.out
154: requires: !single
155: test:
156: suffix: 1
157: test:
158: suffix: 2
159: args: -extrarow
161: TEST*/