Date: 2026-05-01T10:02:30+00:00

Actual source code: test3.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.\n\n";

     13: #include <slepceps.h>

     15: int main(int argc,char **argv)
     16: {
     17:   Mat            A1,A2;       /* problem matrices */
     18:   EPS            eps;         /* eigenproblem solver context */
     19:   PetscReal      tol=PETSC_SMALL,v;
     20:   Vec            d;
     21:   PetscInt       n=30,i,Istart,Iend;
     22:   PetscRandom    myrand;

     24:   PetscFunctionBeginUser;
     25:   PetscCall(SlepcInitialize(&argc,&argv,NULL,help));

     27:   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
     28:   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\nTridiagonal with random diagonal, n=%" PetscInt_FMT "\n\n",n));

     30:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     31:            Create matrix tridiag([-1 0 -1])
     32:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     33:   PetscCall(MatCreate(PETSC_COMM_WORLD,&A1));
     34:   PetscCall(MatSetSizes(A1,PETSC_DECIDE,PETSC_DECIDE,n,n));
     35:   PetscCall(MatSetFromOptions(A1));

     37:   PetscCall(MatGetOwnershipRange(A1,&Istart,&Iend));
     38:   for (i=Istart;i<Iend;i++) {
     39:     if (i>0) PetscCall(MatSetValue(A1,i,i-1,-1.0,INSERT_VALUES));
     40:     if (i<n-1) PetscCall(MatSetValue(A1,i,i+1,-1.0,INSERT_VALUES));
     41:   }
     42:   PetscCall(MatAssemblyBegin(A1,MAT_FINAL_ASSEMBLY));
     43:   PetscCall(MatAssemblyEnd(A1,MAT_FINAL_ASSEMBLY));

     45:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     46:        Create two matrices by filling the diagonal with rand values
     47:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     48:   PetscCall(MatDuplicate(A1,MAT_COPY_VALUES,&A2));
     49:   PetscCall(MatCreateVecs(A1,NULL,&d));
     50:   PetscCall(PetscRandomCreate(PETSC_COMM_WORLD,&myrand));
     51:   PetscCall(PetscRandomSetFromOptions(myrand));
     52:   PetscCall(PetscRandomSetInterval(myrand,0.0,1.0));
     53:   for (i=Istart;i<Iend;i++) {
     54:     PetscCall(PetscRandomGetValueReal(myrand,&v));
     55:     PetscCall(VecSetValue(d,i,v,INSERT_VALUES));
     56:   }
     57:   PetscCall(VecAssemblyBegin(d));
     58:   PetscCall(VecAssemblyEnd(d));
     59:   PetscCall(MatDiagonalSet(A1,d,INSERT_VALUES));
     60:   for (i=Istart;i<Iend;i++) {
     61:     PetscCall(PetscRandomGetValueReal(myrand,&v));
     62:     PetscCall(VecSetValue(d,i,v,INSERT_VALUES));
     63:   }
     64:   PetscCall(VecAssemblyBegin(d));
     65:   PetscCall(VecAssemblyEnd(d));
     66:   PetscCall(MatDiagonalSet(A2,d,INSERT_VALUES));
     67:   PetscCall(VecDestroy(&d));
     68:   PetscCall(PetscRandomDestroy(&myrand));

     70:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     71:                         Create the eigensolver
     72:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     73:   PetscCall(EPSCreate(PETSC_COMM_WORLD,&eps));
     74:   PetscCall(EPSSetProblemType(eps,EPS_HEP));
     75:   PetscCall(EPSSetTolerances(eps,tol,PETSC_CURRENT));
     76:   PetscCall(EPSSetOperators(eps,A1,NULL));
     77:   PetscCall(EPSSetFromOptions(eps));

     79:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     80:                         Solve first eigenproblem
     81:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     82:   PetscCall(EPSSolve(eps));
     83:   PetscCall(PetscPrintf(PETSC_COMM_WORLD," - - - First matrix - - -\n"));
     84:   PetscCall(EPSErrorView(eps,EPS_ERROR_RELATIVE,NULL));

     86:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     87:                         Solve second eigenproblem
     88:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     89:   PetscCall(EPSSetOperators(eps,A2,NULL));
     90:   PetscCall(EPSSolve(eps));
     91:   PetscCall(PetscPrintf(PETSC_COMM_WORLD," - - - Second matrix - - -\n"));
     92:   PetscCall(EPSErrorView(eps,EPS_ERROR_RELATIVE,NULL));

     94:   PetscCall(EPSDestroy(&eps));
     95:   PetscCall(MatDestroy(&A1));
     96:   PetscCall(MatDestroy(&A2));
     97:   PetscCall(SlepcFinalize());
     98:   return 0;
     99: }

    101: /*TEST

    103:    testset:
    104:       args: -eps_nev 4
    105:       requires: !single
    106:       output_file: output/test3_1.out
    107:       test:
    108:          suffix: 1
    109:          args: -eps_type {{krylovschur subspace arnoldi lapack}}
    110:       test:
    111:          suffix: 1_lanczos
    112:          args: -eps_type lanczos -eps_lanczos_reorthog local
    113:       test:
    114:          suffix: 1_power
    115:          args: -eps_type power -eps_max_it 20000
    116:       test:
    117:          suffix: 1_jd
    118:          args: -eps_type jd -eps_jd_initial_size 7
    119:       test:
    120:          suffix: 1_gd
    121:          args: -eps_type gd -eps_gd_initial_size 7
    122:       test:
    123:          suffix: 1_gd2
    124:          args: -eps_type gd -eps_gd_double_expansion
    125:       test:
    126:          suffix: 1_arpack
    127:          args: -eps_type arpack
    128:          requires: arpack
    129:       test:
    130:          suffix: 1_primme
    131:          args: -eps_type primme -eps_conv_abs -eps_primme_blocksize 4
    132:          requires: primme
    133:       test:
    134:          suffix: 1_scalapack
    135:          args: -eps_type scalapack !__float128
    136:          requires: scalapack
    137:       test:
    138:          suffix: 1_elpa
    139:          args: -eps_type elpa
    140:          requires: elpa
    141:       test:
    142:          suffix: 1_elemental
    143:          args: -eps_type elemental
    144:          requires: elemental

    146:    testset:
    147:       args: -eps_nev 4 -eps_smallest_real -eps_max_it 500
    148:       output_file: output/test3_2.out
    149:       test:
    150:          suffix: 2_rqcg
    151:          args: -eps_type rqcg -eps_rqcg_reset 5 -eps_ncv 32
    152:          filter: sed -e "s/26644/26645/"
    153:       test:
    154:          suffix: 2_lobpcg
    155:          args: -eps_type lobpcg -eps_lobpcg_blocksize 5 -st_pc_type none
    156:       test:
    157:          suffix: 2_lanczos
    158:          args: -eps_type lanczos -eps_lanczos_reorthog local
    159:          requires: !single
    160:       test:
    161:          suffix: 2_lanczos_delayed
    162:          args: -eps_type lanczos -eps_lanczos_reorthog delayed -eps_tol 1e-8
    163:          requires: !single
    164:       test:
    165:          suffix: 2_blopex
    166:          args: -eps_type blopex -eps_conv_abs -st_shift -2
    167:          requires: blopex

    169: TEST*/