Date: 2018-10-23T10:32:46+00:00

Actual source code: test2.c
slepc-3.10.1 2018-10-23
Report Typos and Errors

      1: /*
      2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
      4:    Copyright (c) 2002-2018, 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 NEP interface functions.\n\n";

     13: #include <slepcnep.h>

     15: int main(int argc,char **argv)
     16: {
     17:   Mat                  A[3],B;      /* problem matrices */
     18:   FN                   f[3],g;      /* problem functions */
     19:   NEP                  nep;         /* eigenproblem solver context */
     20:   DS                   ds;
     21:   RG                   rg;
     22:   PetscReal            tol;
     23:   PetscScalar          coeffs[2],target;
     24:   PetscInt             n=20,i,its,nev,ncv,mpd,Istart,Iend,nterm;
     25:   NEPWhich             which;
     26:   NEPConvergedReason   reason;
     27:   NEPType              type;
     28:   NEPRefine            refine;
     29:   NEPRefineScheme      rscheme;
     30:   NEPConv              conv;
     31:   NEPStop              stop;
     32:   NEPProblemType       ptype;
     33:   MatStructure         mstr;
     34:   PetscErrorCode       ierr;
     35:   PetscViewerAndFormat *vf;
     36:   const char           *str=NULL;

     38:   SlepcInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
     39:   PetscPrintf(PETSC_COMM_WORLD,"\nDiagonal Nonlinear Eigenproblem, n=%D\n\n",n);

     41:   /*
     42:      Matrices
     43:   */
     44:   MatCreate(PETSC_COMM_WORLD,&A[0]);
     45:   MatSetSizes(A[0],PETSC_DECIDE,PETSC_DECIDE,n,n);
     46:   MatSetFromOptions(A[0]);
     47:   MatSetUp(A[0]);
     48:   MatGetOwnershipRange(A[0],&Istart,&Iend);
     49:   for (i=Istart;i<Iend;i++) {
     50:     MatSetValue(A[0],i,i,i+1,INSERT_VALUES);
     51:   }
     52:   MatAssemblyBegin(A[0],MAT_FINAL_ASSEMBLY);
     53:   MatAssemblyEnd(A[0],MAT_FINAL_ASSEMBLY);

     55:   MatCreate(PETSC_COMM_WORLD,&A[1]);
     56:   MatSetSizes(A[1],PETSC_DECIDE,PETSC_DECIDE,n,n);
     57:   MatSetFromOptions(A[1]);
     58:   MatSetUp(A[1]);
     59:   MatGetOwnershipRange(A[1],&Istart,&Iend);
     60:   for (i=Istart;i<Iend;i++) {
     61:     MatSetValue(A[1],i,i,1.0,INSERT_VALUES);
     62:   }
     63:   MatAssemblyBegin(A[1],MAT_FINAL_ASSEMBLY);
     64:   MatAssemblyEnd(A[1],MAT_FINAL_ASSEMBLY);

     66:   MatCreate(PETSC_COMM_WORLD,&A[2]);
     67:   MatSetSizes(A[2],PETSC_DECIDE,PETSC_DECIDE,n,n);
     68:   MatSetFromOptions(A[2]);
     69:   MatSetUp(A[2]);
     70:   MatGetOwnershipRange(A[1],&Istart,&Iend);
     71:   for (i=Istart;i<Iend;i++) {
     72:     MatSetValue(A[2],i,i,n/(PetscReal)(i+1),INSERT_VALUES);
     73:   }
     74:   MatAssemblyBegin(A[2],MAT_FINAL_ASSEMBLY);
     75:   MatAssemblyEnd(A[2],MAT_FINAL_ASSEMBLY);

     77:   /*
     78:      Functions: f0=-lambda, f1=1.0, f2=sqrt(lambda)
     79:   */
     80:   FNCreate(PETSC_COMM_WORLD,&f[0]);
     81:   FNSetType(f[0],FNRATIONAL);
     82:   coeffs[0] = -1.0; coeffs[1] = 0.0;
     83:   FNRationalSetNumerator(f[0],2,coeffs);

     85:   FNCreate(PETSC_COMM_WORLD,&f[1]);
     86:   FNSetType(f[1],FNRATIONAL);
     87:   coeffs[0] = 1.0;
     88:   FNRationalSetNumerator(f[1],1,coeffs);

     90:   FNCreate(PETSC_COMM_WORLD,&f[2]);
     91:   FNSetType(f[2],FNSQRT);

     93:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     94:              Create eigensolver and test interface functions
     95:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     96:   NEPCreate(PETSC_COMM_WORLD,&nep);
     97:   NEPSetSplitOperator(nep,3,A,f,SAME_NONZERO_PATTERN);
     98:   NEPGetSplitOperatorInfo(nep,&nterm,&mstr);
     99:   switch (mstr) {
    100:     case DIFFERENT_NONZERO_PATTERN: str = "different"; break;
    101:     case SUBSET_NONZERO_PATTERN:    str = "subset"; break;
    102:     case SAME_NONZERO_PATTERN:      str = "same"; break;
    103:   }
    104:   PetscPrintf(PETSC_COMM_WORLD," Nonlinear function with %d terms, with %s nonzero pattern\n",(int)nterm,str);
    105:   NEPGetSplitOperatorTerm(nep,0,&B,&g);
    106:   MatView(B,NULL);
    107:   FNView(g,NULL);

    109:   NEPSetType(nep,NEPRII);
    110:   NEPGetType(nep,&type);
    111:   PetscPrintf(PETSC_COMM_WORLD," Type set to %s\n",type);

    113:   NEPGetProblemType(nep,&ptype);
    114:   PetscPrintf(PETSC_COMM_WORLD," Problem type before changing = %d",(int)ptype);
    115:   NEPSetProblemType(nep,NEP_RATIONAL);
    116:   NEPGetProblemType(nep,&ptype);
    117:   PetscPrintf(PETSC_COMM_WORLD," ... changed to %d.\n",(int)ptype);

    119:   NEPSetRefine(nep,NEP_REFINE_SIMPLE,1,1e-9,2,NEP_REFINE_SCHEME_EXPLICIT);
    120:   NEPGetRefine(nep,&refine,NULL,&tol,&its,&rscheme);
    121:   PetscPrintf(PETSC_COMM_WORLD," Refinement: %s, tol=%g, its=%D, scheme=%s\n",NEPRefineTypes[refine],(double)tol,its,NEPRefineSchemes[rscheme]);

    123:   NEPSetTarget(nep,1.1);
    124:   NEPGetTarget(nep,&target);
    125:   NEPSetWhichEigenpairs(nep,NEP_TARGET_MAGNITUDE);
    126:   NEPGetWhichEigenpairs(nep,&which);
    127:   PetscPrintf(PETSC_COMM_WORLD," Which = %d, target = %g\n",(int)which,(double)PetscRealPart(target));

    129:   NEPSetDimensions(nep,1,12,PETSC_DEFAULT);
    130:   NEPGetDimensions(nep,&nev,&ncv,&mpd);
    131:   PetscPrintf(PETSC_COMM_WORLD," Dimensions: nev=%D, ncv=%D, mpd=%D\n",nev,ncv,mpd);

    133:   NEPSetTolerances(nep,1.0e-6,200);
    134:   NEPGetTolerances(nep,&tol,&its);
    135:   PetscPrintf(PETSC_COMM_WORLD," Tolerance = %.6f, max_its = %D\n",(double)tol,its);

    137:   NEPSetConvergenceTest(nep,NEP_CONV_ABS);
    138:   NEPGetConvergenceTest(nep,&conv);
    139:   NEPSetStoppingTest(nep,NEP_STOP_BASIC);
    140:   NEPGetStoppingTest(nep,&stop);
    141:   PetscPrintf(PETSC_COMM_WORLD," Convergence test = %d, stopping test = %d\n",(int)conv,(int)stop);

    143:   PetscViewerAndFormatCreate(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_DEFAULT,&vf);
    144:   NEPMonitorSet(nep,(PetscErrorCode (*)(NEP,PetscInt,PetscInt,PetscScalar*,PetscScalar*,PetscReal*,PetscInt,void*))NEPMonitorFirst,vf,(PetscErrorCode (*)(void**))PetscViewerAndFormatDestroy);
    145:   NEPMonitorCancel(nep);

    147:   NEPGetDS(nep,&ds);
    148:   DSView(ds,NULL);
    149:   NEPSetFromOptions(nep);

    151:   NEPGetRG(nep,&rg);
    152:   RGView(rg,NULL);

    154:   NEPSolve(nep);
    155:   NEPGetConvergedReason(nep,&reason);
    156:   PetscPrintf(PETSC_COMM_WORLD," Finished - converged reason = %d\n",(int)reason);

    158:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    159:                     Display solution and clean up
    160:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
    161:   NEPErrorView(nep,NEP_ERROR_RELATIVE,NULL);
    162:   NEPDestroy(&nep);
    163:   MatDestroy(&A[0]);
    164:   MatDestroy(&A[1]);
    165:   MatDestroy(&A[2]);
    166:   FNDestroy(&f[0]);
    167:   FNDestroy(&f[1]);
    168:   FNDestroy(&f[2]);
    169:   SlepcFinalize();
    170:   return ierr;
    171: }

    173: /*TEST

    175:    test:
    176:       suffix: 1
    177:       requires: !single

    179: TEST*/