Date: 2026-03-09T09:12:25+00:00

Actual source code: test28.c

      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[] = "Tests multiple calls to EPSSolve with different matrix of different size.\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 <slepceps.h>

     18: int main(int argc,char **argv)
     19: {
     20:   Mat            A,B;
     21:   EPS            eps;
     22:   PetscInt       N,n=10,m=11,Istart,Iend,II,nev=3,i,j;
     23:   PetscBool      flag,terse;

     25:   PetscFunctionBeginUser;
     26:   PetscCall(SlepcInitialize(&argc,&argv,NULL,help));
     27:   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
     28:   PetscCall(PetscOptionsGetInt(NULL,NULL,"-m",&m,&flag));
     29:   N = n*m;
     30:   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\n2-D Laplacian Eigenproblem, N=%" PetscInt_FMT " (%" PetscInt_FMT "x%" PetscInt_FMT " grid)\n\n",N,n,m));

     32:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     33:                 Create the 2-D Laplacian with coarse mesh
     34:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

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

     51:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     52:         Create the eigensolver, set options and solve the eigensystem
     53:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     55:   PetscCall(EPSCreate(PETSC_COMM_WORLD,&eps));
     56:   PetscCall(EPSSetOperators(eps,A,NULL));
     57:   PetscCall(EPSSetProblemType(eps,EPS_HEP));
     58:   PetscCall(EPSSetWhichEigenpairs(eps,EPS_SMALLEST_REAL));
     59:   PetscCall(EPSSetDimensions(eps,nev,PETSC_DETERMINE,PETSC_DETERMINE));
     60:   PetscCall(EPSSetFromOptions(eps));

     62:   PetscCall(EPSSolve(eps));

     64:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     65:                     Display solution of first solve
     66:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     68:   PetscCall(PetscOptionsHasName(NULL,NULL,"-terse",&terse));
     69:   if (terse) PetscCall(EPSErrorView(eps,EPS_ERROR_RELATIVE,NULL));
     70:   else {
     71:     PetscCall(PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL));
     72:     PetscCall(EPSConvergedReasonView(eps,PETSC_VIEWER_STDOUT_WORLD));
     73:     PetscCall(EPSErrorView(eps,EPS_ERROR_RELATIVE,PETSC_VIEWER_STDOUT_WORLD));
     74:     PetscCall(PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD));
     75:   }

     77:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     78:                Create the 2-D Laplacian with finer mesh
     79:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     81:   n *= 2;
     82:   m *= 2;
     83:   N = n*m;
     84:   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\n2-D Laplacian Eigenproblem, N=%" PetscInt_FMT " (%" PetscInt_FMT "x%" PetscInt_FMT " grid)\n\n",N,n,m));

     86:   PetscCall(MatCreate(PETSC_COMM_WORLD,&B));
     87:   PetscCall(MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,N,N));
     88:   PetscCall(MatSetFromOptions(B));
     89:   PetscCall(MatGetOwnershipRange(B,&Istart,&Iend));
     90:   for (II=Istart;II<Iend;II++) {
     91:     i = II/n; j = II-i*n;
     92:     if (i>0) PetscCall(MatSetValue(B,II,II-n,-1.0,INSERT_VALUES));
     93:     if (i<m-1) PetscCall(MatSetValue(B,II,II+n,-1.0,INSERT_VALUES));
     94:     if (j>0) PetscCall(MatSetValue(B,II,II-1,-1.0,INSERT_VALUES));
     95:     if (j<n-1) PetscCall(MatSetValue(B,II,II+1,-1.0,INSERT_VALUES));
     96:     PetscCall(MatSetValue(B,II,II,4.0,INSERT_VALUES));
     97:   }
     98:   PetscCall(MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY));
     99:   PetscCall(MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY));

    101:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    102:        Solve again, calling EPSReset() since matrix size has changed
    103:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

    105:   /* PetscCall(EPSReset(eps)); */  /* not required, will be called in EPSSetOperators() */
    106:   PetscCall(EPSSetOperators(eps,B,NULL));
    107:   PetscCall(EPSSolve(eps));

    109:   if (terse) PetscCall(EPSErrorView(eps,EPS_ERROR_RELATIVE,NULL));
    110:   else {
    111:     PetscCall(PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL));
    112:     PetscCall(EPSConvergedReasonView(eps,PETSC_VIEWER_STDOUT_WORLD));
    113:     PetscCall(EPSErrorView(eps,EPS_ERROR_RELATIVE,PETSC_VIEWER_STDOUT_WORLD));
    114:     PetscCall(PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD));
    115:   }

    117:   PetscCall(EPSDestroy(&eps));
    118:   PetscCall(MatDestroy(&A));
    119:   PetscCall(MatDestroy(&B));
    120:   PetscCall(SlepcFinalize());
    121:   return 0;
    122: }

    124: /*TEST

    126:    testset:
    127:       requires: !single
    128:       output_file: output/test28_1.out
    129:       test:
    130:          suffix: 1
    131:          args: -eps_type {{krylovschur arnoldi gd rqcg lobpcg lapack}} -terse
    132:       test:
    133:          suffix: 1_jd
    134:          args: -eps_type jd -vec_mdot_use_gemv 0 -terse
    135:       test:
    136:          suffix: 1_lanczos
    137:          args: -eps_type lanczos -eps_lanczos_reorthog local -terse

    139:    test:
    140:       suffix: 2
    141:       args: -eps_type {{power subspace}} -eps_target 8 -st_type sinvert -terse

    143:    testset:
    144:       args: -eps_interval 0.5,0.67 -terse
    145:       output_file: output/test28_3.out
    146:       test:
    147:          suffix: 3
    148:          args: -st_type sinvert -st_pc_type cholesky
    149:          requires: !single
    150:       test:
    151:          suffix: 3_evsl
    152:          nsize: {{1 2}}
    153:          args: -eps_type evsl -eps_evsl_dos_method lanczos
    154:          requires: evsl

    156: TEST*/