Actual source code: ex16.c

slepc-main 2024-11-15
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[] = "Simple quadratic eigenvalue problem.\n\n"
 12:   "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,nev,i,j,nconv;
 23:   PetscBool      flag,terse;
 24:   PetscReal      error,re,im;
 25:   PetscScalar    kr,ki;
 26:   Vec            xr,xi;
 27:   BV             V;
 28:   PetscRandom    rand;

 30:   PetscFunctionBeginUser;
 31:   PetscCall(SlepcInitialize(&argc,&argv,NULL,help));

 33:   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
 34:   PetscCall(PetscOptionsGetInt(NULL,NULL,"-m",&m,&flag));
 35:   if (!flag) m=n;
 36:   N = n*m;
 37:   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\nQuadratic Eigenproblem, N=%" PetscInt_FMT " (%" PetscInt_FMT "x%" PetscInt_FMT " grid)\n\n",N,n,m));

 39:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 40:      Compute the matrices that define the eigensystem, (k^2*M+k*C+K)x=0
 41:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

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

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

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

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

 86:   /*
 87:      Create eigensolver context
 88:   */
 89:   PetscCall(PEPCreate(PETSC_COMM_WORLD,&pep));

 91:   /*
 92:      Set matrices and problem type
 93:   */
 94:   A[0] = K; A[1] = C; A[2] = M;
 95:   PetscCall(PEPSetOperators(pep,3,A));
 96:   PetscCall(PEPSetProblemType(pep,PEP_HERMITIAN));

 98:   /*
 99:      In complex scalars, use a real initial vector since in this example
100:      the matrices are all real, then all vectors generated by the solver
101:      will have a zero imaginary part. This is not really necessary.
102:   */
103:   PetscCall(PEPGetBV(pep,&V));
104:   PetscCall(BVGetRandomContext(V,&rand));
105:   PetscCall(PetscRandomSetInterval(rand,-1,1));

107:   /*
108:      Set solver parameters at runtime
109:   */
110:   PetscCall(PEPSetFromOptions(pep));

112:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
113:                       Solve the eigensystem
114:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

116:   PetscCall(PEPSolve(pep));

118:   /*
119:      Optional: Get some information from the solver and display it
120:   */
121:   PetscCall(PEPGetDimensions(pep,&nev,NULL,NULL));
122:   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Number of requested eigenvalues: %" PetscInt_FMT "\n",nev));

124:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
125:                     Display solution and clean up
126:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

128:   /* show detailed info unless -terse option is given by user */
129:   PetscCall(PetscOptionsHasName(NULL,NULL,"-terse",&terse));
130:   if (terse) PetscCall(PEPErrorView(pep,PEP_ERROR_BACKWARD,NULL));
131:   else {
132:     PetscCall(PEPGetConverged(pep,&nconv));
133:     if (nconv>0) {
134:       PetscCall(MatCreateVecs(M,&xr,&xi));
135:       /* display eigenvalues and relative errors */
136:       PetscCall(PetscPrintf(PETSC_COMM_WORLD,
137:            "\n           k          ||P(k)x||/||kx||\n"
138:            "   ----------------- ------------------\n"));
139:       for (i=0;i<nconv;i++) {
140:         /* get converged eigenpairs */
141:         PetscCall(PEPGetEigenpair(pep,i,&kr,&ki,xr,xi));
142:         /* compute the relative error associated to each eigenpair */
143:         PetscCall(PEPComputeError(pep,i,PEP_ERROR_BACKWARD,&error));
144: #if defined(PETSC_USE_COMPLEX)
145:         re = PetscRealPart(kr);
146:         im = PetscImaginaryPart(kr);
147: #else
148:         re = kr;
149:         im = ki;
150: #endif
151:         if (im!=0.0) PetscCall(PetscPrintf(PETSC_COMM_WORLD," %9f%+9fi   %12g\n",(double)re,(double)im,(double)error));
152:         else PetscCall(PetscPrintf(PETSC_COMM_WORLD,"   %12f       %12g\n",(double)re,(double)error));
153:       }
154:       PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\n"));
155:       PetscCall(VecDestroy(&xr));
156:       PetscCall(VecDestroy(&xi));
157:     }
158:   }
159:   PetscCall(PEPDestroy(&pep));
160:   PetscCall(MatDestroy(&M));
161:   PetscCall(MatDestroy(&C));
162:   PetscCall(MatDestroy(&K));
163:   PetscCall(SlepcFinalize());
164:   return 0;
165: }

167: /*TEST

169:    testset:
170:       args: -pep_nev 4 -pep_ncv 21 -n 12 -terse
171:       output_file: output/ex16_1.out
172:       test:
173:          suffix: 1
174:          args: -pep_type {{toar qarnoldi}}
175:       test:
176:          suffix: 1_linear
177:          args: -pep_type linear -pep_linear_explicitmatrix
178:          requires: !single
179:       test:
180:          suffix: 1_linear_symm
181:          args: -pep_type linear -pep_linear_explicitmatrix -pep_linear_eps_gen_indefinite -pep_scale scalar -pep_linear_bv_definite_tol 1e-12
182:          requires: !single
183:       test:
184:          suffix: 1_stoar
185:          args: -pep_type stoar -pep_scale scalar
186:          requires: double
187:       test:
188:          suffix: 1_stoar_t
189:          args: -pep_type stoar -pep_scale scalar -st_transform
190:          requires: double

192: TEST*/