Actual source code: test1.c
slepc-3.11.2 2019-07-30
1: /*
2: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3: SLEPc - Scalable Library for Eigenvalue Problem Computations
4: Copyright (c) 2002-2019, 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,*wr,*wi;
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,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: /* Eigenvectors */
106: j = 2;
107: DSVectors(ds,DS_MAT_X,&j,&rnorm); /* third eigenvector */
108: PetscPrintf(PETSC_COMM_WORLD,"Value of rnorm for 3rd vector = %.3f\n",(double)rnorm);
109: DSVectors(ds,DS_MAT_X,NULL,NULL); /* all eigenvectors */
110: j = 0;
111: rnorm = 0.0;
112: DSGetArray(ds,DS_MAT_X,&X);
113: for (i=0;i<n;i++) {
114: #if defined(PETSC_USE_COMPLEX)
115: aux = PetscAbsScalar(X[i+j*ld]);
116: #else
117: if (PetscAbs(wi[j])==0.0) aux = PetscAbsScalar(X[i+j*ld]);
118: else aux = SlepcAbsEigenvalue(X[i+j*ld],X[i+(j+1)*ld]);
119: #endif
120: rnorm += aux*aux;
121: }
122: DSRestoreArray(ds,DS_MAT_X,&X);
123: rnorm = PetscSqrtReal(rnorm);
124: PetscPrintf(PETSC_COMM_WORLD,"Norm of 1st vector = %.3f\n",(double)rnorm);
125: if (verbose) {
126: PetscPrintf(PETSC_COMM_WORLD,"After vectors - - - - - - - - -\n");
127: DSView(ds,viewer);
128: }
130: PetscFree2(wr,wi);
131: DSDestroy(&ds);
132: SlepcFinalize();
133: return ierr;
134: }
136: /*TEST
138: test:
139: suffix: 1
140: requires: !complex !single
142: TEST*/