Date: 2022-11-02T11:47:03+00:00

Actual source code: ex19.c
slepc-3.18.1 2022-11-02
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[] = "Standard symmetric eigenproblem for the 3-D Laplacian built with the DM interface.\n\n"
     12: "Use -seed <k> to modify the random initial vector.\n"
     13: "Use -da_grid_x <nx> etc. to change the problem size.\n\n";

     15: #include <slepceps.h>
     16: #include <petscdmda.h>
     17: #include <petsctime.h>

     19: PetscErrorCode GetExactEigenvalues(PetscInt M,PetscInt N,PetscInt P,PetscInt nconv,PetscReal *exact)
     20: {
     21:   PetscInt       n,i,j,k,l;
     22:   PetscReal      *evals,ax,ay,az,sx,sy,sz;

     25:   ax = PETSC_PI/2/(M+1);
     26:   ay = PETSC_PI/2/(N+1);
     27:   az = PETSC_PI/2/(P+1);
     28:   n = PetscCeilReal(PetscPowReal((PetscReal)nconv,0.33333)+1);
     29:   PetscMalloc1(n*n*n,&evals);
     30:   l = 0;
     31:   for (i=1;i<=n;i++) {
     32:     sx = PetscSinReal(ax*i);
     33:     for (j=1;j<=n;j++) {
     34:       sy = PetscSinReal(ay*j);
     35:       for (k=1;k<=n;k++) {
     36:         sz = PetscSinReal(az*k);
     37:         evals[l++] = 4.0*(sx*sx+sy*sy+sz*sz);
     38:       }
     39:     }
     40:   }
     41:   PetscSortReal(n*n*n,evals);
     42:   for (i=0;i<nconv;i++) exact[i] = evals[i];
     43:   PetscFree(evals);
     44:   return 0;
     45: }

     47: PetscErrorCode FillMatrix(DM da,Mat A)
     48: {
     49:   PetscInt       i,j,k,mx,my,mz,xm,ym,zm,xs,ys,zs,idx;
     50:   PetscScalar    v[7];
     51:   MatStencil     row,col[7];

     54:   DMDAGetInfo(da,0,&mx,&my,&mz,0,0,0,0,0,0,0,0,0);
     55:   DMDAGetCorners(da,&xs,&ys,&zs,&xm,&ym,&zm);

     57:   for (k=zs;k<zs+zm;k++) {
     58:     for (j=ys;j<ys+ym;j++) {
     59:       for (i=xs;i<xs+xm;i++) {
     60:         row.i=i; row.j=j; row.k=k;
     61:         col[0].i=row.i; col[0].j=row.j; col[0].k=row.k;
     62:         v[0]=6.0;
     63:         idx=1;
     64:         if (k>0) { v[idx]=-1.0; col[idx].i=i; col[idx].j=j; col[idx].k=k-1; idx++; }
     65:         if (j>0) { v[idx]=-1.0; col[idx].i=i; col[idx].j=j-1; col[idx].k=k; idx++; }
     66:         if (i>0) { v[idx]=-1.0; col[idx].i=i-1; col[idx].j=j; col[idx].k=k; idx++; }
     67:         if (i<mx-1) { v[idx]=-1.0; col[idx].i=i+1; col[idx].j=j; col[idx].k=k; idx++; }
     68:         if (j<my-1) { v[idx]=-1.0; col[idx].i=i; col[idx].j=j+1; col[idx].k=k; idx++; }
     69:         if (k<mz-1) { v[idx]=-1.0; col[idx].i=i; col[idx].j=j; col[idx].k=k+1; idx++; }
     70:         MatSetValuesStencil(A,1,&row,idx,col,v,INSERT_VALUES);
     71:       }
     72:     }
     73:   }
     74:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
     75:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
     76:   return 0;
     77: }

     79: int main(int argc,char **argv)
     80: {
     81:   Mat            A;               /* operator matrix */
     82:   EPS            eps;             /* eigenproblem solver context */
     83:   EPSType        type;
     84:   DM             da;
     85:   Vec            v0;
     86:   PetscReal      error,tol,re,im,*exact;
     87:   PetscScalar    kr,ki;
     88:   PetscInt       M,N,P,m,n,p,nev,maxit,i,its,nconv,seed;
     89:   PetscLogDouble t1,t2,t3;
     90:   PetscBool      flg,terse;
     91:   PetscRandom    rctx;

     94:   SlepcInitialize(&argc,&argv,(char*)0,help);

     96:   PetscPrintf(PETSC_COMM_WORLD,"\n3-D Laplacian Eigenproblem\n\n");

     98:   /* show detailed info unless -terse option is given by user */
     99:   PetscOptionsHasName(NULL,NULL,"-terse",&terse);

    101:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    102:      Compute the operator matrix that defines the eigensystem, Ax=kx
    103:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

    105:   PetscCall(DMDACreate3d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,DM_BOUNDARY_NONE,
    106:                       DM_BOUNDARY_NONE,DMDA_STENCIL_STAR,10,10,10,
    107:                       PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE,
    108:                       1,1,NULL,NULL,NULL,&da));
    109:   DMSetFromOptions(da);
    110:   DMSetUp(da);

    112:   /* print DM information */
    113:   DMDAGetInfo(da,NULL,&M,&N,&P,&m,&n,&p,NULL,NULL,NULL,NULL,NULL,NULL);
    114:   PetscPrintf(PETSC_COMM_WORLD," Grid partitioning: %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT "\n",m,n,p);

    116:   /* create and fill the matrix */
    117:   DMCreateMatrix(da,&A);
    118:   FillMatrix(da,A);

    120:   /* create random initial vector */
    121:   seed = 1;
    122:   PetscOptionsGetInt(NULL,NULL,"-seed",&seed,NULL);
    124:   MatCreateVecs(A,&v0,NULL);
    125:   PetscRandomCreate(PETSC_COMM_WORLD,&rctx);
    126:   PetscRandomSetFromOptions(rctx);
    127:   for (i=0;i<seed;i++) {   /* simulate different seeds in the random generator */
    128:     VecSetRandom(v0,rctx);
    129:   }

    131:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    132:                 Create the eigensolver and set various options
    133:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

    135:   /*
    136:      Create eigensolver context
    137:   */
    138:   EPSCreate(PETSC_COMM_WORLD,&eps);

    140:   /*
    141:      Set operators. In this case, it is a standard eigenvalue problem
    142:   */
    143:   EPSSetOperators(eps,A,NULL);
    144:   EPSSetProblemType(eps,EPS_HEP);

    146:   /*
    147:      Set specific solver options
    148:   */
    149:   EPSSetWhichEigenpairs(eps,EPS_SMALLEST_REAL);
    150:   EPSSetTolerances(eps,1e-8,PETSC_DEFAULT);
    151:   EPSSetInitialSpace(eps,1,&v0);

    153:   /*
    154:      Set solver parameters at runtime
    155:   */
    156:   EPSSetFromOptions(eps);

    158:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    159:                       Solve the eigensystem
    160:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

    162:   PetscTime(&t1);
    163:   EPSSetUp(eps);
    164:   PetscTime(&t2);
    165:   EPSSolve(eps);
    166:   PetscTime(&t3);
    167:   if (!terse) {
    168:     EPSGetIterationNumber(eps,&its);
    169:     PetscPrintf(PETSC_COMM_WORLD," Number of iterations of the method: %" PetscInt_FMT "\n",its);

    171:     /*
    172:        Optional: Get some information from the solver and display it
    173:     */
    174:     EPSGetType(eps,&type);
    175:     PetscPrintf(PETSC_COMM_WORLD," Solution method: %s\n\n",type);
    176:     EPSGetDimensions(eps,&nev,NULL,NULL);
    177:     PetscPrintf(PETSC_COMM_WORLD," Number of requested eigenvalues: %" PetscInt_FMT "\n",nev);
    178:     EPSGetTolerances(eps,&tol,&maxit);
    179:     PetscPrintf(PETSC_COMM_WORLD," Stopping condition: tol=%.4g, maxit=%" PetscInt_FMT "\n",(double)tol,maxit);
    180:   }

    182:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    183:                     Display solution and clean up
    184:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

    186:   if (terse) EPSErrorView(eps,EPS_ERROR_RELATIVE,NULL);
    187:   else {
    188:     /*
    189:        Get number of converged approximate eigenpairs
    190:     */
    191:     EPSGetConverged(eps,&nconv);
    192:     PetscPrintf(PETSC_COMM_WORLD," Number of converged approximate eigenpairs: %" PetscInt_FMT "\n\n",nconv);

    194:     if (nconv>0) {
    195:       PetscMalloc1(nconv,&exact);
    196:       GetExactEigenvalues(M,N,P,nconv,exact);
    197:       /*
    198:          Display eigenvalues and relative errors
    199:       */
    200:       PetscCall(PetscPrintf(PETSC_COMM_WORLD,
    201:            "           k          ||Ax-kx||/||kx||   Eigenvalue Error \n"
    202:            "   ----------------- ------------------ ------------------\n"));

    204:       for (i=0;i<nconv;i++) {
    205:         /*
    206:           Get converged eigenpairs: i-th eigenvalue is stored in kr (real part) and
    207:           ki (imaginary part)
    208:         */
    209:         EPSGetEigenpair(eps,i,&kr,&ki,NULL,NULL);
    210:         /*
    211:            Compute the relative error associated to each eigenpair
    212:         */
    213:         EPSComputeError(eps,i,EPS_ERROR_RELATIVE,&error);

    215: #if defined(PETSC_USE_COMPLEX)
    216:         re = PetscRealPart(kr);
    217:         im = PetscImaginaryPart(kr);
    218: #else
    219:         re = kr;
    220:         im = ki;
    221: #endif
    223:         PetscPrintf(PETSC_COMM_WORLD,"   %12g       %12g        %12g\n",(double)re,(double)error,(double)PetscAbsReal(re-exact[i]));
    224:       }
    225:       PetscFree(exact);
    226:       PetscPrintf(PETSC_COMM_WORLD,"\n");
    227:     }
    228:   }

    230:   /*
    231:      Show computing times
    232:   */
    233:   PetscOptionsHasName(NULL,NULL,"-showtimes",&flg);
    234:   if (flg) PetscPrintf(PETSC_COMM_WORLD," Elapsed time: %g (setup), %g (solve)\n",(double)(t2-t1),(double)(t3-t2));

    236:   /*
    237:      Free work space
    238:   */
    239:   EPSDestroy(&eps);
    240:   MatDestroy(&A);
    241:   VecDestroy(&v0);
    242:   PetscRandomDestroy(&rctx);
    243:   DMDestroy(&da);
    244:   SlepcFinalize();
    245:   return 0;
    246: }

    248: /*TEST

    250:    testset:
    251:       args: -eps_nev 8 -terse
    252:       requires: double
    253:       output_file: output/ex19_1.out
    254:       test:
    255:          suffix: 1_krylovschur
    256:          args: -eps_type krylovschur -eps_ncv 64
    257:       test:
    258:          suffix: 1_lobpcg
    259:          args: -eps_type lobpcg -eps_tol 1e-7
    260:       test:
    261:          suffix: 1_blopex
    262:          args: -eps_type blopex -eps_tol 1e-7 -eps_blopex_blocksize 4
    263:          requires: blopex

    265: TEST*/