Date: 2021-02-01T14:42:00+00:00

Actual source code: test24.c
slepc-3.14.2 2021-02-01
Report Typos and Errors

      1: /*
      2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
      4:    Copyright (c) 2002-2020, 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;

     29:   SlepcInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
     30:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
     31:   PetscOptionsGetInt(NULL,NULL,"-nini",&nini,NULL);
     32:   PetscOptionsGetInt(NULL,NULL,"-ncon",&ncon,NULL);
     33:   PetscPrintf(PETSC_COMM_WORLD,"\n1-D Laplacian Eigenproblem, n=%D nini=%D ncon=%D\n\n",n,nini,ncon);

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

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

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

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

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

     90:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     91:                       Solve the eigensystem
     92:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

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

    107:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    108:                     Display solution and clean up
    109:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

    111:   EPSErrorView(eps,EPS_ERROR_RELATIVE,NULL);
    112:   EPSDestroy(&eps);
    113:   MatDestroy(&A);
    114:   VecDestroyVecs(nini,&vi);
    115:   VecDestroyVecs(ncon,&vc);
    116:   VecDestroy(&t);
    117:   SlepcFinalize();
    118:   return ierr;
    119: }

    121: /*TEST

    123:    testset:
    124:       args: -ncon 2
    125:       output_file: output/test24_1.out
    126:       test:
    127:          suffix: 1
    128:          requires: !single
    129:       test:
    130:          suffix: 2_cuda
    131:          args: -mat_type aijcusparse
    132:          requires: cuda !single

    134: TEST*/