Actual source code: test38.c

slepc-3.17.1 2022-04-11
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 EPSLYAPII interface functions.\n\n"
 12:   "Based on ex2.\n"
 13:   "The command line options are:\n"
 14:   "  -n <n>, where <n> = number of grid subdivisions in x dimension.\n"
 15:   "  -m <m>, where <m> = number of grid subdivisions in y dimension.\n"
 16:   "  -shift <sigma>, where <sigma> = shift of origin.\n\n";

 18: #include <slepceps.h>

 20: int main(int argc,char **argv)
 21: {
 22:   Mat            A;
 23:   EPS            eps;
 24:   PetscInt       N,n=10,m,Istart,Iend,II,i,j,rkl,rkc;
 25:   PetscBool      flag,terse;
 26:   PetscReal      sigma=8.0;

 28:   SlepcInitialize(&argc,&argv,(char*)0,help);
 29:   PetscOptionsGetReal(NULL,NULL,"-shift",&sigma,NULL);
 30:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
 31:   PetscOptionsGetInt(NULL,NULL,"-m",&m,&flag);
 32:   if (!flag) m=n;
 33:   N = n*m;
 34:   PetscPrintf(PETSC_COMM_WORLD,"\nShifted 2-D Laplacian Eigenproblem, N=%" PetscInt_FMT " (%" PetscInt_FMT "x%" PetscInt_FMT " grid) sigma=%.1f\n\n",N,n,m,(double)sigma);

 36:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 37:                     Create the 2-D Laplacian
 38:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

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

 56:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 57:                 Create the eigensolver and set various options
 58:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 60:   EPSCreate(PETSC_COMM_WORLD,&eps);
 61:   EPSSetOperators(eps,A,NULL);
 62:   EPSSetProblemType(eps,EPS_HEP);
 63:   EPSSetWhichEigenpairs(eps,EPS_LARGEST_REAL);
 64:   EPSSetType(eps,EPSLYAPII);
 65:   EPSSetFromOptions(eps);

 67:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 68:                 Solve the problem and display the solution
 69:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 71:   EPSSolve(eps);

 73:   /* print solver information */
 74:   PetscObjectTypeCompare((PetscObject)eps,EPSLYAPII,&flag);
 75:   if (flag) {
 76:     EPSLyapIIGetRanks(eps,&rkc,&rkl);
 77:     PetscPrintf(PETSC_COMM_WORLD," EPSLYAPII ranks: for Lyapunov solver=%" PetscInt_FMT ", after compression=%" PetscInt_FMT "\n\n",rkl,rkc);
 78:   }

 80:   PetscOptionsHasName(NULL,NULL,"-terse",&terse);
 81:   if (terse) EPSErrorView(eps,EPS_ERROR_RELATIVE,NULL);
 82:   else {
 83:     PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL);
 84:     EPSConvergedReasonView(eps,PETSC_VIEWER_STDOUT_WORLD);
 85:     EPSErrorView(eps,EPS_ERROR_RELATIVE,PETSC_VIEWER_STDOUT_WORLD);
 86:     PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);
 87:   }

 89:   EPSDestroy(&eps);
 90:   MatDestroy(&A);
 91:   SlepcFinalize();
 92:   return 0;
 93: }

 95: /*TEST

 97:    test:
 98:       args: -eps_view -terse
 99:       filter: grep -v tolerance | sed -e "s/symmetric/hermitian/"

101: TEST*/