Date: 2023-01-26T12:18:08+00:00

Actual source code: test12.c
slepc-3.18.2 2023-01-26
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[] = "Test DSNEP.\n\n";

     13: #include <slepcds.h>

     15: int main(int argc,char **argv)
     16: {
     17:   DS             ds;
     18:   FN             f1,f2,f3,funs[3],qfun;
     19:   SlepcSC        sc;
     20:   PetscScalar    *Id,*A,*B,*wr,*wi,*X,*W,coeffs[2],auxr,alpha;
     21:   PetscReal      tol,tau=0.001,radius=10,h,a=20,xi,re,im,nrm,aux;
     22:   PetscInt       i,j,ii,jj,k,n=10,ld,nev,nfun;
     23:   PetscViewer    viewer;
     24:   PetscBool      verbose;
     25:   RG             rg;
     26:   DSMatType      mat[3]={DS_MAT_E0,DS_MAT_E1,DS_MAT_E2};
     27: #if !defined(PETSC_USE_COMPLEX)
     28:   PetscScalar    auxi;
     29: #endif

     32:   SlepcInitialize(&argc,&argv,(char*)0,help);
     33:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
     34:   PetscOptionsGetReal(NULL,NULL,"-tau",&tau,NULL);
     35:   PetscPrintf(PETSC_COMM_WORLD,"Solve a Dense System of type NEP - dimension %" PetscInt_FMT ", tau=%g.\n",n,(double)tau);
     36:   PetscOptionsHasName(NULL,NULL,"-verbose",&verbose);
     37:   PetscOptionsGetReal(NULL,NULL,"-radius",&radius,NULL);

     39:   /* Create DS object */
     40:   DSCreate(PETSC_COMM_WORLD,&ds);
     41:   DSSetType(ds,DSNEP);
     42:   tol  = 1000*n*PETSC_MACHINE_EPSILON;
     43:   DSNEPSetRefine(ds,tol,PETSC_DECIDE);
     44:   DSSetFromOptions(ds);

     46:   /* Set functions (prior to DSAllocate) */
     47:   FNCreate(PETSC_COMM_WORLD,&f1);
     48:   FNSetType(f1,FNRATIONAL);
     49:   coeffs[0] = -1.0; coeffs[1] = 0.0;
     50:   FNRationalSetNumerator(f1,2,coeffs);

     52:   FNCreate(PETSC_COMM_WORLD,&f2);
     53:   FNSetType(f2,FNRATIONAL);
     54:   coeffs[0] = 1.0;
     55:   FNRationalSetNumerator(f2,1,coeffs);

     57:   FNCreate(PETSC_COMM_WORLD,&f3);
     58:   FNSetType(f3,FNEXP);
     59:   FNSetScale(f3,-tau,1.0);

     61:   funs[0] = f1;
     62:   funs[1] = f2;
     63:   funs[2] = f3;
     64:   DSNEPSetFN(ds,3,funs);

     66:   /* Set dimensions */
     67:   ld = n+2;  /* test leading dimension larger than n */
     68:   DSAllocate(ds,ld);
     69:   DSSetDimensions(ds,n,0,0);

     71:   /* Set region (used only in method=1) */
     72:   RGCreate(PETSC_COMM_WORLD,&rg);
     73:   RGSetType(rg,RGELLIPSE);
     74:   RGEllipseSetParameters(rg,0.0,radius,1.0);
     75:   DSNEPSetRG(ds,rg);
     76:   RGDestroy(&rg);

     78:   /* Set up viewer */
     79:   PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&viewer);
     80:   PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_INFO_DETAIL);
     81:   DSView(ds,viewer);
     82:   PetscViewerPopFormat(viewer);
     83:   if (verbose) PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_MATLAB);

     85:   /* Show info about functions */
     86:   DSNEPGetNumFN(ds,&nfun);
     87:   for (i=0;i<nfun;i++) {
     88:     PetscPrintf(PETSC_COMM_WORLD,"Function %" PetscInt_FMT ":\n",i);
     89:     DSNEPGetFN(ds,i,&qfun);
     90:     FNView(qfun,NULL);
     91:   }

     93:   /* Fill matrices */
     94:   DSGetArray(ds,DS_MAT_E0,&Id);
     95:   for (i=0;i<n;i++) Id[i+i*ld]=1.0;
     96:   DSRestoreArray(ds,DS_MAT_E0,&Id);
     97:   h = PETSC_PI/(PetscReal)(n+1);
     98:   DSGetArray(ds,DS_MAT_E1,&A);
     99:   for (i=0;i<n;i++) A[i+i*ld]=-2.0/(h*h)+a;
    100:   for (i=1;i<n;i++) {
    101:     A[i+(i-1)*ld]=1.0/(h*h);
    102:     A[(i-1)+i*ld]=1.0/(h*h);
    103:   }
    104:   DSRestoreArray(ds,DS_MAT_E1,&A);
    105:   DSGetArray(ds,DS_MAT_E2,&B);
    106:   for (i=0;i<n;i++) {
    107:     xi = (i+1)*h;
    108:     B[i+i*ld] = -4.1+xi*(1.0-PetscExpReal(xi-PETSC_PI));
    109:   }
    110:   DSRestoreArray(ds,DS_MAT_E2,&B);

    112:   if (verbose) {
    113:     PetscPrintf(PETSC_COMM_WORLD,"Initial - - - - - - - - -\n");
    114:     DSView(ds,viewer);
    115:   }

    117:   /* Solve */
    118:   PetscCalloc2(n,&wr,n,&wi);
    119:   DSGetSlepcSC(ds,&sc);
    120:   sc->comparison    = SlepcCompareLargestMagnitude;
    121:   sc->comparisonctx = NULL;
    122:   sc->map           = NULL;
    123:   sc->mapobj        = NULL;
    124:   DSSolve(ds,wr,wi);
    125:   DSSort(ds,wr,wi,NULL,NULL,NULL);

    127:   if (verbose) {
    128:     PetscPrintf(PETSC_COMM_WORLD,"After solve - - - - - - - - -\n");
    129:     DSView(ds,viewer);
    130:   }
    131:   DSGetDimensions(ds,NULL,NULL,NULL,&nev);

    133:   /* Print computed eigenvalues */
    134:   PetscMalloc1(ld*ld,&W);
    135:   DSVectors(ds,DS_MAT_X,NULL,NULL);
    136:   DSGetArray(ds,DS_MAT_X,&X);
    137:   PetscPrintf(PETSC_COMM_WORLD,"Computed eigenvalues =\n");
    138:   for (i=0;i<nev;i++) {
    139: #if defined(PETSC_USE_COMPLEX)
    140:     re = PetscRealPart(wr[i]);
    141:     im = PetscImaginaryPart(wr[i]);
    142: #else
    143:     re = wr[i];
    144:     im = wi[i];
    145: #endif
    146:     /* Residual */
    147:     PetscArrayzero(W,ld*ld);
    148:     for (k=0;k<nfun;k++) {
    149:       FNEvaluateFunction(funs[k],wr[i],&alpha);
    150:       DSGetArray(ds,mat[k],&A);
    151:       for (jj=0;jj<n;jj++) for (ii=0;ii<n;ii++) W[jj*ld+ii] += alpha*A[jj*ld+ii];
    152:       DSRestoreArray(ds,mat[k],&A);
    153:     }
    154:     nrm = 0.0;
    155:     for (k=0;k<n;k++) {
    156:       auxr = 0.0;
    157: #if !defined(PETSC_USE_COMPLEX)
    158:       auxi = 0.0;
    159: #endif
    160:       for (j=0;j<n;j++) {
    161:         auxr += W[k+j*ld]*X[i*ld+j];
    162: #if !defined(PETSC_USE_COMPLEX)
    163:         if (PetscAbs(wi[j])!=0.0) auxi += W[k+j*ld]*X[(i+1)*ld+j];
    164: #endif
    165:       }
    166:       aux = SlepcAbsEigenvalue(auxr,auxi);
    167:       nrm += aux*aux;
    168:     }
    169:     nrm = PetscSqrtReal(nrm);
    170:     if (nrm/SlepcAbsEigenvalue(wr[i],wi[i])>tol) PetscPrintf(PETSC_COMM_WORLD,"Warning: the residual norm of the %" PetscInt_FMT "-th computed eigenpair %g\n",i,(double)nrm);
    171:     if (PetscAbs(im)<1e-10) PetscViewerASCIIPrintf(viewer,"  %.5f\n",(double)re);
    172:     else PetscViewerASCIIPrintf(viewer,"  %.5f%+.5fi\n",(double)re,(double)im);
    173:   }
    174:   PetscFree(W);
    175:   DSRestoreArray(ds,DS_MAT_X,&X);
    176:   DSRestoreArray(ds,DS_MAT_W,&W);
    177:   PetscFree2(wr,wi);
    178:   FNDestroy(&f1);
    179:   FNDestroy(&f2);
    180:   FNDestroy(&f3);
    181:   DSDestroy(&ds);
    182:   SlepcFinalize();
    183:   return 0;
    184: }

    186: /*TEST

    188:    testset:
    189:       test:
    190:          filter: grep -v "solving the problem"
    191:          suffix: 1
    192:       test:
    193:          suffix: 2
    194:          args: -ds_method 1 -radius 10 -ds_nep_refine_its 1
    195:          filter: grep -v "solving the problem" | sed -e "s/[+-]0\.0*i//g" | sed -e "s/37411/37410/" | sed -e "s/tolerance [0-9]\.[0-9]*e[+-]\([0-9]*\)/tolerance removed/" | sed -e "s/tolerance [0-9]\.\([0-9]*\)/tolerance removed/"
    196:          requires: complex

    198: TEST*/