Date: 2022-08-09T09:05:59+00:00

Actual source code: ex26.c
slepc-3.17.2 2022-08-09
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[] = "Computes the action of the square root of the 2-D Laplacian.\n\n"
     12:   "The command line options are:\n"
     13:   "  -n <n>, where <n> = number of grid subdivisions in x dimension.\n"
     14:   "  -m <m>, where <m> = number of grid subdivisions in y dimension.\n\n"
     15:   "To draw the solution run with -mfn_view_solution draw -draw_pause -1\n\n";

     17: #include <slepcmfn.h>

     19: int main(int argc,char **argv)
     20: {
     21:   Mat            A;           /* problem matrix */
     22:   MFN            mfn;
     23:   FN             f;
     24:   PetscReal      norm,tol;
     25:   Vec            v,y,z;
     26:   PetscInt       N,n=10,m,Istart,Iend,i,j,II;
     27:   PetscBool      flag;

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

     31:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
     32:   PetscOptionsGetInt(NULL,NULL,"-m",&m,&flag);
     33:   if (!flag) m=n;
     34:   N = n*m;
     35:   PetscPrintf(PETSC_COMM_WORLD,"\nSquare root of Laplacian y=sqrt(A)*e_1, N=%" PetscInt_FMT " (%" PetscInt_FMT "x%" PetscInt_FMT " grid)\n\n",N,n,m);

     37:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     38:                  Compute the discrete 2-D Laplacian, A
     39:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     41:   MatCreate(PETSC_COMM_WORLD,&A);
     42:   MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,N,N);
     43:   MatSetFromOptions(A);
     44:   MatSetUp(A);

     46:   MatGetOwnershipRange(A,&Istart,&Iend);
     47:   for (II=Istart;II<Iend;II++) {
     48:     i = II/n; j = II-i*n;
     49:     if (i>0) MatSetValue(A,II,II-n,-1.0,INSERT_VALUES);
     50:     if (i<m-1) MatSetValue(A,II,II+n,-1.0,INSERT_VALUES);
     51:     if (j>0) MatSetValue(A,II,II-1,-1.0,INSERT_VALUES);
     52:     if (j<n-1) MatSetValue(A,II,II+1,-1.0,INSERT_VALUES);
     53:     MatSetValue(A,II,II,4.0,INSERT_VALUES);
     54:   }

     56:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
     57:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);

     59:   /* set symmetry flag so that solver can exploit it */
     60:   MatSetOption(A,MAT_HERMITIAN,PETSC_TRUE);

     62:   /* set v = e_1 */
     63:   MatCreateVecs(A,NULL,&v);
     64:   VecSetValue(v,0,1.0,INSERT_VALUES);
     65:   VecAssemblyBegin(v);
     66:   VecAssemblyEnd(v);
     67:   VecDuplicate(v,&y);
     68:   VecDuplicate(v,&z);

     70:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     71:              Create the solver, set the matrix and the function
     72:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     73:   MFNCreate(PETSC_COMM_WORLD,&mfn);
     74:   MFNSetOperator(mfn,A);
     75:   MFNGetFN(mfn,&f);
     76:   FNSetType(f,FNSQRT);
     77:   MFNSetErrorIfNotConverged(mfn,PETSC_TRUE);
     78:   MFNSetFromOptions(mfn);

     80:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     81:                       First solve: y=sqrt(A)*v
     82:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     84:   MFNSolve(mfn,v,y);
     85:   VecNorm(y,NORM_2,&norm);
     86:   PetscPrintf(PETSC_COMM_WORLD," Intermediate vector has norm %g\n",(double)norm);

     88:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     89:              Second solve: z=sqrt(A)*y and compare against A*v
     90:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     92:   MFNSolve(mfn,y,z);
     93:   MFNGetTolerances(mfn,&tol,NULL);

     95:   MatMult(A,v,y);   /* overwrite y */
     96:   VecAXPY(y,-1.0,z);
     97:   VecNorm(y,NORM_2,&norm);

     99:   if (norm<tol) PetscPrintf(PETSC_COMM_WORLD," Error norm is less than the requested tolerance\n\n");
    100:   else PetscPrintf(PETSC_COMM_WORLD," Error norm larger than tolerance: %3.1e\n\n",(double)norm);

    102:   /*
    103:      Free work space
    104:   */
    105:   MFNDestroy(&mfn);
    106:   MatDestroy(&A);
    107:   VecDestroy(&v);
    108:   VecDestroy(&y);
    109:   VecDestroy(&z);
    110:   SlepcFinalize();
    111:   return 0;
    112: }

    114: /*TEST

    116:    test:
    117:       suffix: 1
    118:       args: -mfn_tol 1e-4

    120: TEST*/