Date: 2024-10-28T09:13:29+00:00

Actual source code: test24.c
slepc-3.22.1 2024-10-28
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[] = "Eigenproblem for the 1-D Laplacian with constraints. "
     12:   "Based on ex1.\n"
     13:   "The command line options are:\n"
     14:   "  -n <n>, where <n> = number of grid subdivisions = matrix dimension.\n\n";

     16: #include <slepceps.h>

     18: int main(int argc,char **argv)
     19: {
     20:   Mat            A;
     21:   EPS            eps;
     22:   EPSType        type;
     23:   Vec            *vi=NULL,*vc=NULL,t;
     24:   PetscInt       n=30,nev=4,i,j,Istart,Iend,nini=0,ncon=0,bs;
     25:   PetscReal      alpha,beta,restart;
     26:   PetscBool      flg,lock;

     28:   PetscFunctionBeginUser;
     29:   PetscCall(SlepcInitialize(&argc,&argv,NULL,help));
     30:   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
     31:   PetscCall(PetscOptionsGetInt(NULL,NULL,"-nini",&nini,NULL));
     32:   PetscCall(PetscOptionsGetInt(NULL,NULL,"-ncon",&ncon,NULL));
     33:   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\n1-D Laplacian Eigenproblem, n=%" PetscInt_FMT " nini=%" PetscInt_FMT " ncon=%" PetscInt_FMT "\n\n",n,nini,ncon));

     35:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     36:      Compute the operator matrix that defines the eigensystem, Ax=kx
     37:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     39:   PetscCall(MatCreate(PETSC_COMM_WORLD,&A));
     40:   PetscCall(MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,n,n));
     41:   PetscCall(MatSetFromOptions(A));

     43:   PetscCall(MatGetOwnershipRange(A,&Istart,&Iend));
     44:   for (i=Istart;i<Iend;i++) {
     45:     if (i>0) PetscCall(MatSetValue(A,i,i-1,-1.0,INSERT_VALUES));
     46:     if (i<n-1) PetscCall(MatSetValue(A,i,i+1,-1.0,INSERT_VALUES));
     47:     PetscCall(MatSetValue(A,i,i,2.0,INSERT_VALUES));
     48:   }
     49:   PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY));
     50:   PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY));

     52:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     53:                 Create the eigensolver and set various options
     54:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     55:   PetscCall(EPSCreate(PETSC_COMM_WORLD,&eps));
     56:   PetscCall(EPSSetOperators(eps,A,NULL));
     57:   PetscCall(EPSSetProblemType(eps,EPS_HEP));
     58:   PetscCall(EPSSetType(eps,EPSLOBPCG));
     59:   PetscCall(EPSSetWhichEigenpairs(eps,EPS_SMALLEST_REAL));
     60:   PetscCall(EPSSetConvergenceTest(eps,EPS_CONV_ABS));
     61:   PetscCall(EPSSetDimensions(eps,nev,PETSC_DETERMINE,PETSC_DETERMINE));
     62:   PetscCall(EPSLOBPCGSetBlockSize(eps,nev));
     63:   PetscCall(EPSLOBPCGSetRestart(eps,0.7));
     64:   PetscCall(EPSSetTolerances(eps,1e-8,1200));
     65:   PetscCall(EPSSetFromOptions(eps));

     67:   PetscCall(MatCreateVecs(A,&t,NULL));
     68:   if (nini) {
     69:     PetscCall(VecDuplicateVecs(t,nini,&vi));
     70:     for (i=0;i<nini;i++) PetscCall(VecSetRandom(vi[i],NULL));
     71:     PetscCall(EPSSetInitialSpace(eps,nini,vi));
     72:   }
     73:   if (ncon) {   /* constraints are exact eigenvectors of lowest eigenvalues */
     74:     alpha = PETSC_PI/(n+1);
     75:     beta  = PetscSqrtReal(2.0/(n+1));
     76:     PetscCall(VecDuplicateVecs(t,ncon,&vc));
     77:     for (i=0;i<ncon;i++) {
     78:       for (j=0;j<n;j++) PetscCall(VecSetValue(vc[i],j,PetscSinReal(alpha*(j+1)*(i+1))*beta,INSERT_VALUES));
     79:       PetscCall(VecAssemblyBegin(vc[i]));
     80:       PetscCall(VecAssemblyEnd(vc[i]));
     81:     }
     82:     PetscCall(EPSSetDeflationSpace(eps,ncon,vc));
     83:   }

     85:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     86:                       Solve the eigensystem
     87:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     89:   PetscCall(EPSSolve(eps));
     90:   PetscCall(EPSGetType(eps,&type));
     91:   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Solution method: %s\n",type));
     92:   PetscCall(PetscObjectTypeCompare((PetscObject)eps,EPSLOBPCG,&flg));
     93:   if (flg) {
     94:     PetscCall(EPSLOBPCGGetLocking(eps,&lock));
     95:     if (lock) PetscCall(PetscPrintf(PETSC_COMM_WORLD," Using soft locking\n"));
     96:     PetscCall(EPSLOBPCGGetRestart(eps,&restart));
     97:     PetscCall(PetscPrintf(PETSC_COMM_WORLD," LOBPCG Restart parameter=%.4g\n",(double)restart));
     98:     PetscCall(EPSLOBPCGGetBlockSize(eps,&bs));
     99:     PetscCall(PetscPrintf(PETSC_COMM_WORLD," LOBPCG Block size=%" PetscInt_FMT "\n",bs));
    100:   }

    102:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    103:                     Display solution and clean up
    104:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

    106:   PetscCall(EPSErrorView(eps,EPS_ERROR_RELATIVE,NULL));
    107:   PetscCall(EPSDestroy(&eps));
    108:   PetscCall(MatDestroy(&A));
    109:   PetscCall(VecDestroyVecs(nini,&vi));
    110:   PetscCall(VecDestroyVecs(ncon,&vc));
    111:   PetscCall(VecDestroy(&t));
    112:   PetscCall(SlepcFinalize());
    113:   return 0;
    114: }

    116: /*TEST

    118:    testset:
    119:       args: -ncon 2
    120:       output_file: output/test24_1.out
    121:       test:
    122:          suffix: 1
    123:          requires: !single
    124:       test:
    125:          suffix: 1_cuda
    126:          args: -mat_type aijcusparse
    127:          requires: cuda !single
    128:       test:
    129:          suffix: 1_hip
    130:          args: -mat_type aijhipsparse
    131:          requires: hip !single

    133: TEST*/