Actual source code: test15.c
slepc-3.18.0 2022-10-01
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 DSPEP.\n\n";
13: #include <slepcds.h>
15: int main(int argc,char **argv)
16: {
17: DS ds;
18: SlepcSC sc;
19: Mat X;
20: Vec x0;
21: PetscScalar *K,*C,*M,*wr,*wi,z=1.0;
22: PetscReal re,im,nrm,*pbc;
23: PetscInt i,j,n=10,d=2,ld;
24: PetscViewer viewer;
25: PetscBool verbose;
28: SlepcInitialize(&argc,&argv,(char*)0,help);
29: PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
30: PetscPrintf(PETSC_COMM_WORLD,"Solve a Dense System of type PEP - n=%" PetscInt_FMT ".\n",n);
31: PetscOptionsHasName(NULL,NULL,"-verbose",&verbose);
33: /* Create DS object */
34: DSCreate(PETSC_COMM_WORLD,&ds);
35: DSSetType(ds,DSPEP);
36: DSSetFromOptions(ds);
37: DSPEPSetDegree(ds,d);
39: /* Set dimensions */
40: ld = n+2; /* test leading dimension larger than n */
41: DSAllocate(ds,ld);
42: DSSetDimensions(ds,n,0,0);
44: /* Set up viewer */
45: PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&viewer);
46: PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_INFO_DETAIL);
47: DSView(ds,viewer);
48: PetscViewerPopFormat(viewer);
49: if (verbose) PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_MATLAB);
51: /* Fill matrices */
52: DSGetArray(ds,DS_MAT_E0,&K);
53: for (i=0;i<n-1;i++) K[i+i*ld] = 2.0*n;
54: K[n-1+(n-1)*ld] = 1.0*n;
55: for (i=1;i<n;i++) {
56: K[i+(i-1)*ld] = -1.0*n;
57: K[(i-1)+i*ld] = -1.0*n;
58: }
59: DSRestoreArray(ds,DS_MAT_E0,&K);
60: DSGetArray(ds,DS_MAT_E1,&C);
61: C[n-1+(n-1)*ld] = 2.0*PETSC_PI/z;
62: DSRestoreArray(ds,DS_MAT_E1,&C);
63: DSGetArray(ds,DS_MAT_E2,&M);
64: for (i=0;i<n-1;i++) M[i+i*ld] = -4.0*PETSC_PI*PETSC_PI/n;
65: M[i+i*ld] = -2.0*PETSC_PI*PETSC_PI/n;
66: DSRestoreArray(ds,DS_MAT_E2,&M);
68: if (verbose) {
69: PetscPrintf(PETSC_COMM_WORLD,"Initial - - - - - - - - -\n");
70: DSView(ds,viewer);
71: }
73: /* Solve */
74: PetscMalloc2(d*n,&wr,d*n,&wi);
75: DSGetSlepcSC(ds,&sc);
76: sc->comparison = SlepcCompareLargestReal;
77: sc->comparisonctx = NULL;
78: sc->map = NULL;
79: sc->mapobj = NULL;
80: DSSolve(ds,wr,wi);
81: DSSort(ds,wr,wi,NULL,NULL,NULL);
82: if (verbose) {
83: PetscPrintf(PETSC_COMM_WORLD,"After solve - - - - - - - - -\n");
84: DSView(ds,viewer);
85: }
87: /* Print polynomial coefficients */
88: PetscPrintf(PETSC_COMM_WORLD,"Polynomial coefficients (alpha,beta,gamma) =\n");
89: DSPEPGetCoefficients(ds,&pbc);
90: for (j=0;j<3;j++) {
91: for (i=0;i<d+1;i++) PetscViewerASCIIPrintf(viewer," %.5f",(double)pbc[j+3*i]);
92: PetscViewerASCIIPrintf(viewer,"\n");
93: }
94: PetscFree(pbc);
96: /* Print eigenvalues */
97: PetscPrintf(PETSC_COMM_WORLD,"Computed eigenvalues =\n");
98: for (i=0;i<d*n;i++) {
99: #if defined(PETSC_USE_COMPLEX)
100: re = PetscRealPart(wr[i]);
101: im = PetscImaginaryPart(wr[i]);
102: #else
103: re = wr[i];
104: im = wi[i];
105: #endif
106: if (PetscAbs(im)<1e-10) PetscViewerASCIIPrintf(viewer," %.5f\n",(double)re);
107: else PetscViewerASCIIPrintf(viewer," %.5f%+.5fi\n",(double)re,(double)im);
108: }
110: /* Eigenvectors */
111: DSVectors(ds,DS_MAT_X,NULL,NULL); /* all eigenvectors */
112: DSGetMat(ds,DS_MAT_X,&X);
113: MatCreateVecs(X,NULL,&x0);
114: MatGetColumnVector(X,x0,0);
115: VecNorm(x0,NORM_2,&nrm);
116: DSRestoreMat(ds,DS_MAT_X,&X);
117: VecDestroy(&x0);
118: PetscPrintf(PETSC_COMM_WORLD,"Norm of 1st vector = %.3f\n",(double)nrm);
119: if (verbose) {
120: PetscPrintf(PETSC_COMM_WORLD,"After vectors - - - - - - - - -\n");
121: DSView(ds,viewer);
122: }
124: PetscFree2(wr,wi);
125: DSDestroy(&ds);
126: SlepcFinalize();
127: return 0;
128: }
130: /*TEST
132: test:
133: suffix: 1
134: requires: !single
136: TEST*/