Actual source code: test3.c

slepc-3.19.2 2023-09-05
Report Typos and Errors
  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 PEP interface functions.\n\n";

 13: #include <slepcpep.h>

 15: int main(int argc,char **argv)
 16: {
 17:   Mat                A[3],B;      /* problem matrices */
 18:   PEP                pep;         /* eigenproblem solver context */
 19:   ST                 st;
 20:   KSP                ksp;
 21:   DS                 ds;
 22:   PetscReal          tol,alpha;
 23:   PetscScalar        target;
 24:   PetscInt           n=20,i,its,nev,ncv,mpd,Istart,Iend,nmat;
 25:   PEPWhich           which;
 26:   PEPConvergedReason reason;
 27:   PEPType            type;
 28:   PEPExtract         extr;
 29:   PEPBasis           basis;
 30:   PEPScale           scale;
 31:   PEPRefine          refine;
 32:   PEPRefineScheme    rscheme;
 33:   PEPConv            conv;
 34:   PEPStop            stop;
 35:   PEPProblemType     ptype;
 36:   PetscViewerAndFormat *vf;

 38:   PetscFunctionBeginUser;
 39:   PetscCall(SlepcInitialize(&argc,&argv,(char*)0,help));
 40:   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\nDiagonal Quadratic Eigenproblem, n=%" PetscInt_FMT "\n\n",n));

 42:   PetscCall(MatCreate(PETSC_COMM_WORLD,&A[0]));
 43:   PetscCall(MatSetSizes(A[0],PETSC_DECIDE,PETSC_DECIDE,n,n));
 44:   PetscCall(MatSetFromOptions(A[0]));
 45:   PetscCall(MatSetUp(A[0]));
 46:   PetscCall(MatGetOwnershipRange(A[0],&Istart,&Iend));
 47:   for (i=Istart;i<Iend;i++) PetscCall(MatSetValue(A[0],i,i,i+1,INSERT_VALUES));
 48:   PetscCall(MatAssemblyBegin(A[0],MAT_FINAL_ASSEMBLY));
 49:   PetscCall(MatAssemblyEnd(A[0],MAT_FINAL_ASSEMBLY));

 51:   PetscCall(MatCreate(PETSC_COMM_WORLD,&A[1]));
 52:   PetscCall(MatSetSizes(A[1],PETSC_DECIDE,PETSC_DECIDE,n,n));
 53:   PetscCall(MatSetFromOptions(A[1]));
 54:   PetscCall(MatSetUp(A[1]));
 55:   PetscCall(MatGetOwnershipRange(A[1],&Istart,&Iend));
 56:   for (i=Istart;i<Iend;i++) PetscCall(MatSetValue(A[1],i,i,1.0,INSERT_VALUES));
 57:   PetscCall(MatAssemblyBegin(A[1],MAT_FINAL_ASSEMBLY));
 58:   PetscCall(MatAssemblyEnd(A[1],MAT_FINAL_ASSEMBLY));

 60:   PetscCall(MatCreate(PETSC_COMM_WORLD,&A[2]));
 61:   PetscCall(MatSetSizes(A[2],PETSC_DECIDE,PETSC_DECIDE,n,n));
 62:   PetscCall(MatSetFromOptions(A[2]));
 63:   PetscCall(MatSetUp(A[2]));
 64:   PetscCall(MatGetOwnershipRange(A[1],&Istart,&Iend));
 65:   for (i=Istart;i<Iend;i++) PetscCall(MatSetValue(A[2],i,i,n/(PetscReal)(i+1),INSERT_VALUES));
 66:   PetscCall(MatAssemblyBegin(A[2],MAT_FINAL_ASSEMBLY));
 67:   PetscCall(MatAssemblyEnd(A[2],MAT_FINAL_ASSEMBLY));

 69:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 70:              Create eigensolver and test interface functions
 71:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 72:   PetscCall(PEPCreate(PETSC_COMM_WORLD,&pep));
 73:   PetscCall(PEPSetOperators(pep,3,A));
 74:   PetscCall(PEPGetNumMatrices(pep,&nmat));
 75:   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Polynomial of degree %" PetscInt_FMT "\n",nmat-1));
 76:   PetscCall(PEPGetOperators(pep,0,&B));
 77:   PetscCall(MatView(B,NULL));

 79:   PetscCall(PEPSetType(pep,PEPTOAR));
 80:   PetscCall(PEPGetType(pep,&type));
 81:   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Type set to %s\n",type));

 83:   PetscCall(PEPGetProblemType(pep,&ptype));
 84:   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Problem type before changing = %d",(int)ptype));
 85:   PetscCall(PEPSetProblemType(pep,PEP_HERMITIAN));
 86:   PetscCall(PEPGetProblemType(pep,&ptype));
 87:   PetscCall(PetscPrintf(PETSC_COMM_WORLD," ... changed to %d.\n",(int)ptype));

 89:   PetscCall(PEPGetExtract(pep,&extr));
 90:   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Extraction before changing = %d",(int)extr));
 91:   PetscCall(PEPSetExtract(pep,PEP_EXTRACT_STRUCTURED));
 92:   PetscCall(PEPGetExtract(pep,&extr));
 93:   PetscCall(PetscPrintf(PETSC_COMM_WORLD," ... changed to %d\n",(int)extr));

 95:   PetscCall(PEPSetScale(pep,PEP_SCALE_SCALAR,.1,NULL,NULL,5,1.0));
 96:   PetscCall(PEPGetScale(pep,&scale,&alpha,NULL,NULL,&its,NULL));
 97:   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Scaling: %s, alpha=%g, its=%" PetscInt_FMT "\n",PEPScaleTypes[scale],(double)alpha,its));

 99:   PetscCall(PEPSetBasis(pep,PEP_BASIS_CHEBYSHEV1));
100:   PetscCall(PEPGetBasis(pep,&basis));
101:   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Polynomial basis: %s\n",PEPBasisTypes[basis]));

103:   PetscCall(PEPSetRefine(pep,PEP_REFINE_SIMPLE,1,1e-9,2,PEP_REFINE_SCHEME_SCHUR));
104:   PetscCall(PEPGetRefine(pep,&refine,NULL,&tol,&its,&rscheme));
105:   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Refinement: %s, tol=%g, its=%" PetscInt_FMT ", scheme=%s\n",PEPRefineTypes[refine],(double)tol,its,PEPRefineSchemes[rscheme]));

107:   PetscCall(PEPSetTarget(pep,4.8));
108:   PetscCall(PEPGetTarget(pep,&target));
109:   PetscCall(PEPSetWhichEigenpairs(pep,PEP_TARGET_MAGNITUDE));
110:   PetscCall(PEPGetWhichEigenpairs(pep,&which));
111:   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Which = %d, target = %g\n",(int)which,(double)PetscRealPart(target)));

113:   PetscCall(PEPSetDimensions(pep,4,PETSC_DEFAULT,PETSC_DEFAULT));
114:   PetscCall(PEPGetDimensions(pep,&nev,&ncv,&mpd));
115:   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Dimensions: nev=%" PetscInt_FMT ", ncv=%" PetscInt_FMT ", mpd=%" PetscInt_FMT "\n",nev,ncv,mpd));

117:   PetscCall(PEPSetTolerances(pep,2.2e-4,200));
118:   PetscCall(PEPGetTolerances(pep,&tol,&its));
119:   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Tolerance = %.5f, max_its = %" PetscInt_FMT "\n",(double)tol,its));

121:   PetscCall(PEPSetConvergenceTest(pep,PEP_CONV_ABS));
122:   PetscCall(PEPGetConvergenceTest(pep,&conv));
123:   PetscCall(PEPSetStoppingTest(pep,PEP_STOP_BASIC));
124:   PetscCall(PEPGetStoppingTest(pep,&stop));
125:   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Convergence test = %d, stopping test = %d\n",(int)conv,(int)stop));

127:   PetscCall(PetscViewerAndFormatCreate(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_DEFAULT,&vf));
128:   PetscCall(PEPMonitorSet(pep,(PetscErrorCode (*)(PEP,PetscInt,PetscInt,PetscScalar*,PetscScalar*,PetscReal*,PetscInt,void*))PEPMonitorFirst,vf,(PetscErrorCode (*)(void**))PetscViewerAndFormatDestroy));
129:   PetscCall(PEPMonitorCancel(pep));

131:   PetscCall(PEPGetST(pep,&st));
132:   PetscCall(STGetKSP(st,&ksp));
133:   PetscCall(KSPSetTolerances(ksp,1e-8,1e-35,PETSC_DEFAULT,PETSC_DEFAULT));
134:   PetscCall(STView(st,NULL));
135:   PetscCall(PEPGetDS(pep,&ds));
136:   PetscCall(DSView(ds,NULL));

138:   PetscCall(PEPSetFromOptions(pep));
139:   PetscCall(PEPSolve(pep));
140:   PetscCall(PEPGetConvergedReason(pep,&reason));
141:   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Finished - converged reason = %d\n",(int)reason));

143:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
144:                     Display solution and clean up
145:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
146:   PetscCall(PEPErrorView(pep,PEP_ERROR_RELATIVE,NULL));
147:   PetscCall(PEPDestroy(&pep));
148:   PetscCall(MatDestroy(&A[0]));
149:   PetscCall(MatDestroy(&A[1]));
150:   PetscCall(MatDestroy(&A[2]));
151:   PetscCall(SlepcFinalize());
152:   return 0;
153: }

155: /*TEST

157:    test:
158:       suffix: 1
159:       args: -pep_tol 1e-6 -pep_ncv 22
160:       filter: sed -e "s/[+-]0\.0*i//g"

162: TEST*/