Actual source code: test4.c
slepc-3.7.0 2016-05-16
1: /*
2: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3: SLEPc - Scalable Library for Eigenvalue Problem Computations
4: Copyright (c) 2002-2016, Universitat Politecnica de Valencia, Spain
6: This file is part of SLEPc.
8: SLEPc is free software: you can redistribute it and/or modify it under the
9: terms of version 3 of the GNU Lesser General Public License as published by
10: the Free Software Foundation.
12: SLEPc is distributed in the hope that it will be useful, but WITHOUT ANY
13: WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14: FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
15: more details.
17: You should have received a copy of the GNU Lesser General Public License
18: along with SLEPc. If not, see <http://www.gnu.org/licenses/>.
19: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
20: */
22: static char help[] = "Test DSGNHEP.\n\n";
24: #include <slepcds.h>
28: int main(int argc,char **argv)
29: {
31: DS ds;
32: SlepcSC sc;
33: PetscScalar *A,*B,*X,*wr,*wi;
34: PetscReal re,im,rnorm,aux;
35: PetscInt i,j,n=10,ld;
36: PetscViewer viewer;
37: PetscBool verbose;
39: SlepcInitialize(&argc,&argv,(char*)0,help);
40: PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
41: PetscPrintf(PETSC_COMM_WORLD,"Solve a Dense System of type GNHEP - dimension %D.\n",n);
42: PetscOptionsHasName(NULL,NULL,"-verbose",&verbose);
44: /* Create DS object */
45: DSCreate(PETSC_COMM_WORLD,&ds);
46: DSSetType(ds,DSGNHEP);
47: DSSetFromOptions(ds);
48: ld = n+2; /* test leading dimension larger than n */
49: DSAllocate(ds,ld);
50: DSSetDimensions(ds,n,0,0,0);
52: /* Set up viewer */
53: PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&viewer);
54: PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_INFO_DETAIL);
55: DSView(ds,viewer);
56: PetscViewerPopFormat(viewer);
57: if (verbose) {
58: PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_MATLAB);
59: }
61: /* Fill A with Grcar matrix */
62: DSGetArray(ds,DS_MAT_A,&A);
63: PetscMemzero(A,sizeof(PetscScalar)*ld*n);
64: for (i=1;i<n;i++) A[i+(i-1)*ld]=-1.0;
65: for (j=0;j<4;j++) {
66: for (i=0;i<n-j;i++) A[i+(i+j)*ld]=1.0;
67: }
68: DSRestoreArray(ds,DS_MAT_A,&A);
69: /* Fill B with an identity matrix */
70: DSGetArray(ds,DS_MAT_B,&B);
71: PetscMemzero(B,sizeof(PetscScalar)*ld*n);
72: for (i=0;i<n;i++) B[i+i*ld]=1.0;
73: DSRestoreArray(ds,DS_MAT_B,&B);
75: if (verbose) {
76: PetscPrintf(PETSC_COMM_WORLD,"Initial - - - - - - - - -\n");
77: DSView(ds,viewer);
78: }
80: /* Solve */
81: PetscMalloc2(n,&wr,n,&wi);
82: DSGetSlepcSC(ds,&sc);
83: sc->comparison = SlepcCompareLargestMagnitude;
84: sc->comparisonctx = NULL;
85: sc->map = NULL;
86: sc->mapobj = NULL;
87: DSSolve(ds,wr,wi);
88: DSSort(ds,wr,wi,NULL,NULL,NULL);
89: if (verbose) {
90: PetscPrintf(PETSC_COMM_WORLD,"After solve - - - - - - - - -\n");
91: DSView(ds,viewer);
92: }
94: /* Print eigenvalues */
95: PetscPrintf(PETSC_COMM_WORLD,"Computed eigenvalues =\n");
96: for (i=0;i<n;i++) {
97: #if defined(PETSC_USE_COMPLEX)
98: re = PetscRealPart(wr[i]);
99: im = PetscImaginaryPart(wr[i]);
100: #else
101: re = wr[i];
102: im = wi[i];
103: #endif
104: if (PetscAbs(im)<1e-10) {
105: PetscViewerASCIIPrintf(viewer," %.5f\n",(double)re);
106: } else {
107: PetscViewerASCIIPrintf(viewer," %.5f%+.5fi\n",(double)re,(double)im);
108: }
109: }
111: /* Eigenvectors */
112: j = 2;
113: DSVectors(ds,DS_MAT_X,&j,&rnorm); /* third eigenvector */
114: PetscPrintf(PETSC_COMM_WORLD,"Value of rnorm for 3rd vector = %.3f\n",(double)rnorm);
115: DSVectors(ds,DS_MAT_X,NULL,NULL); /* all eigenvectors */
116: j = 0;
117: rnorm = 0.0;
118: DSGetArray(ds,DS_MAT_X,&X);
119: for (i=0;i<n;i++) {
120: #if defined(PETSC_USE_COMPLEX)
121: aux = PetscAbsScalar(X[i+j*ld]);
122: #else
123: if (PetscAbs(wi[j])==0.0) aux = PetscAbsScalar(X[i+j*ld]);
124: else aux = SlepcAbsEigenvalue(X[i+j*ld],X[i+(j+1)*ld]);
125: #endif
126: rnorm += aux*aux;
127: }
128: DSRestoreArray(ds,DS_MAT_X,&X);
129: rnorm = PetscSqrtReal(rnorm);
130: PetscPrintf(PETSC_COMM_WORLD,"Norm of 1st vector = %.3f\n",(double)rnorm);
131: if (verbose) {
132: PetscPrintf(PETSC_COMM_WORLD,"After vectors - - - - - - - - -\n");
133: DSView(ds,viewer);
134: }
136: PetscFree2(wr,wi);
137: DSDestroy(&ds);
138: SlepcFinalize();
139: return ierr;
140: }