Actual source code: test8.c

slepc-3.17.2 2022-08-09
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 interface functions of polynomial JD.\n\n"
 12:   "This is based on ex16.c. The command line options are:\n"
 13:   "  -n <n>, where <n> = number of grid subdivisions in x dimension.\n"
 14:   "  -m <m>, where <m> = number of grid subdivisions in y dimension.\n\n";

 16: #include <slepcpep.h>

 18: int main(int argc,char **argv)
 19: {
 20:   Mat             M,C,K,A[3];      /* problem matrices */
 21:   PEP             pep;             /* polynomial eigenproblem solver context */
 22:   PetscInt        N,n=10,m,Istart,Iend,II,i,j,midx;
 23:   PetscReal       restart,fix;
 24:   PetscBool       flag,reuse;
 25:   PEPJDProjection proj;

 27:   SlepcInitialize(&argc,&argv,(char*)0,help);

 29:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
 30:   PetscOptionsGetInt(NULL,NULL,"-m",&m,&flag);
 31:   if (!flag) m=n;
 32:   N = n*m;
 33:   PetscPrintf(PETSC_COMM_WORLD,"\nQuadratic Eigenproblem, N=%" PetscInt_FMT " (%" PetscInt_FMT "x%" PetscInt_FMT " grid)\n\n",N,n,m);

 35:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 36:      Compute the matrices that define the eigensystem, (k^2*M+k*C+K)x=0
 37:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 39:   /* K is the 2-D Laplacian */
 40:   MatCreate(PETSC_COMM_WORLD,&K);
 41:   MatSetSizes(K,PETSC_DECIDE,PETSC_DECIDE,N,N);
 42:   MatSetFromOptions(K);
 43:   MatSetUp(K);
 44:   MatGetOwnershipRange(K,&Istart,&Iend);
 45:   for (II=Istart;II<Iend;II++) {
 46:     i = II/n; j = II-i*n;
 47:     if (i>0) MatSetValue(K,II,II-n,-1.0,INSERT_VALUES);
 48:     if (i<m-1) MatSetValue(K,II,II+n,-1.0,INSERT_VALUES);
 49:     if (j>0) MatSetValue(K,II,II-1,-1.0,INSERT_VALUES);
 50:     if (j<n-1) MatSetValue(K,II,II+1,-1.0,INSERT_VALUES);
 51:     MatSetValue(K,II,II,4.0,INSERT_VALUES);
 52:   }
 53:   MatAssemblyBegin(K,MAT_FINAL_ASSEMBLY);
 54:   MatAssemblyEnd(K,MAT_FINAL_ASSEMBLY);

 56:   /* C is the 1-D Laplacian on horizontal lines */
 57:   MatCreate(PETSC_COMM_WORLD,&C);
 58:   MatSetSizes(C,PETSC_DECIDE,PETSC_DECIDE,N,N);
 59:   MatSetFromOptions(C);
 60:   MatSetUp(C);
 61:   MatGetOwnershipRange(C,&Istart,&Iend);
 62:   for (II=Istart;II<Iend;II++) {
 63:     i = II/n; j = II-i*n;
 64:     if (j>0) MatSetValue(C,II,II-1,-1.0,INSERT_VALUES);
 65:     if (j<n-1) MatSetValue(C,II,II+1,-1.0,INSERT_VALUES);
 66:     MatSetValue(C,II,II,2.0,INSERT_VALUES);
 67:   }
 68:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
 69:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);

 71:   /* M is a diagonal matrix */
 72:   MatCreate(PETSC_COMM_WORLD,&M);
 73:   MatSetSizes(M,PETSC_DECIDE,PETSC_DECIDE,N,N);
 74:   MatSetFromOptions(M);
 75:   MatSetUp(M);
 76:   MatGetOwnershipRange(M,&Istart,&Iend);
 77:   for (II=Istart;II<Iend;II++) MatSetValue(M,II,II,(PetscReal)(II+1),INSERT_VALUES);
 78:   MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);
 79:   MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);

 81:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 82:                 Create the eigensolver and set various options
 83:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 85:   PEPCreate(PETSC_COMM_WORLD,&pep);
 86:   A[0] = K; A[1] = C; A[2] = M;
 87:   PEPSetOperators(pep,3,A);
 88:   PEPSetType(pep,PEPJD);

 90:   /*
 91:      Test interface functions of JD solver
 92:   */
 93:   PEPJDGetRestart(pep,&restart);
 94:   PetscPrintf(PETSC_COMM_WORLD," Restart parameter before changing = %g",(double)restart);
 95:   PEPJDSetRestart(pep,0.3);
 96:   PEPJDGetRestart(pep,&restart);
 97:   PetscPrintf(PETSC_COMM_WORLD," ... changed to %g\n",(double)restart);

 99:   PEPJDGetFix(pep,&fix);
100:   PetscPrintf(PETSC_COMM_WORLD," Fix parameter before changing = %g",(double)fix);
101:   PEPJDSetFix(pep,0.001);
102:   PEPJDGetFix(pep,&fix);
103:   PetscPrintf(PETSC_COMM_WORLD," ... changed to %g\n",(double)fix);

105:   PEPJDGetReusePreconditioner(pep,&reuse);
106:   PetscPrintf(PETSC_COMM_WORLD," Reuse preconditioner flag before changing = %d",(int)reuse);
107:   PEPJDSetReusePreconditioner(pep,PETSC_TRUE);
108:   PEPJDGetReusePreconditioner(pep,&reuse);
109:   PetscPrintf(PETSC_COMM_WORLD," ... changed to %d\n",(int)reuse);

111:   PEPJDGetProjection(pep,&proj);
112:   PetscPrintf(PETSC_COMM_WORLD," Projection type before changing = %d",(int)proj);
113:   PEPJDSetProjection(pep,PEP_JD_PROJECTION_ORTHOGONAL);
114:   PEPJDGetProjection(pep,&proj);
115:   PetscPrintf(PETSC_COMM_WORLD," ... changed to %d\n",(int)proj);

117:   PEPJDGetMinimalityIndex(pep,&midx);
118:   PetscPrintf(PETSC_COMM_WORLD," Minimality index before changing = %" PetscInt_FMT,midx);
119:   PEPJDSetMinimalityIndex(pep,2);
120:   PEPJDGetMinimalityIndex(pep,&midx);
121:   PetscPrintf(PETSC_COMM_WORLD," ... changed to %" PetscInt_FMT "\n",midx);

123:   PEPSetFromOptions(pep);

125:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
126:                       Solve the eigensystem
127:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

129:   PEPSolve(pep);
130:   PEPErrorView(pep,PEP_ERROR_BACKWARD,NULL);
131:   PEPDestroy(&pep);
132:   MatDestroy(&M);
133:   MatDestroy(&C);
134:   MatDestroy(&K);
135:   SlepcFinalize();
136:   return 0;
137: }

139: /*TEST

141:    test:
142:       args: -n 12 -pep_nev 2 -pep_ncv 21 -pep_conv_abs

144: TEST*/