LCOV - code coverage report
Current view: top level - mfn/tests - test3.c (source / functions) Hit Total Coverage
Test: SLEPc Lines: 73 73 100.0 %
Date: 2024-11-21 00:34:55 Functions: 1 1 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
       3             :    SLEPc - Scalable Library for Eigenvalue Problem Computations
       4             :    Copyright (c) 2002-, Universitat Politecnica de Valencia, Spain
       5             : 
       6             :    This file is part of SLEPc.
       7             :    SLEPc is distributed under a 2-clause BSD license (see LICENSE).
       8             :    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
       9             : */
      10             : 
      11             : static char help[] = "Test MFN interface functions.\n\n"
      12             :   "The command line options are:\n"
      13             :   "  -n <n>, where <n> = number of grid subdivisions in x dimension.\n\n";
      14             : 
      15             : #include <slepcmfn.h>
      16             : 
      17           2 : int main(int argc,char **argv)
      18             : {
      19           2 :   Mat                  A,B;
      20           2 :   MFN                  mfn;
      21           2 :   FN                   f;
      22           2 :   MFNConvergedReason   reason;
      23           2 :   MFNType              type;
      24           2 :   PetscReal            norm,tol;
      25           2 :   Vec                  v,y;
      26           2 :   PetscInt             N,n=4,Istart,Iend,i,j,II,ncv,its,maxit;
      27           2 :   PetscBool            flg,testprefix=PETSC_FALSE;
      28           2 :   const char           *prefix;
      29           2 :   PetscViewerAndFormat *vf;
      30             : 
      31           2 :   PetscFunctionBeginUser;
      32           2 :   PetscCall(SlepcInitialize(&argc,&argv,NULL,help));
      33           2 :   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
      34           2 :   N = n*n;
      35           2 :   PetscCall(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,n));
      36           2 :   PetscCall(PetscOptionsGetBool(NULL,NULL,"-test_prefix",&testprefix,NULL));
      37             : 
      38             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      39             :                  Compute the discrete 2-D Laplacian, A
      40             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      41             : 
      42           2 :   PetscCall(MatCreate(PETSC_COMM_WORLD,&A));
      43           2 :   PetscCall(MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,N,N));
      44           2 :   PetscCall(MatSetFromOptions(A));
      45             : 
      46           2 :   PetscCall(MatGetOwnershipRange(A,&Istart,&Iend));
      47          34 :   for (II=Istart;II<Iend;II++) {
      48          32 :     i = II/n; j = II-i*n;
      49          32 :     if (i>0) PetscCall(MatSetValue(A,II,II-n,-1.0,INSERT_VALUES));
      50          32 :     if (i<n-1) PetscCall(MatSetValue(A,II,II+n,-1.0,INSERT_VALUES));
      51          32 :     if (j>0) PetscCall(MatSetValue(A,II,II-1,-1.0,INSERT_VALUES));
      52          32 :     if (j<n-1) PetscCall(MatSetValue(A,II,II+1,-1.0,INSERT_VALUES));
      53          32 :     PetscCall(MatSetValue(A,II,II,4.0,INSERT_VALUES));
      54             :   }
      55             : 
      56           2 :   PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY));
      57           2 :   PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY));
      58           2 :   PetscCall(MatSetOption(A,MAT_HERMITIAN,PETSC_TRUE));
      59             : 
      60           2 :   PetscCall(MatCreateVecs(A,&v,&y));
      61           2 :   PetscCall(VecSetValue(v,0,1.0,INSERT_VALUES));
      62           2 :   PetscCall(VecAssemblyBegin(v));
      63           2 :   PetscCall(VecAssemblyEnd(v));
      64             : 
      65             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      66             :              Create the solver, set the matrix and the function
      67             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      68           2 :   PetscCall(MFNCreate(PETSC_COMM_WORLD,&mfn));
      69           2 :   PetscCall(MFNSetOperator(mfn,A));
      70           2 :   PetscCall(MFNGetFN(mfn,&f));
      71           2 :   PetscCall(FNSetType(f,FNSQRT));
      72             : 
      73           2 :   PetscCall(MFNSetType(mfn,MFNKRYLOV));
      74           2 :   PetscCall(MFNGetType(mfn,&type));
      75           2 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Type set to %s\n",type));
      76             : 
      77             :   /* test prefix usage */
      78           2 :   if (testprefix) {
      79           1 :     PetscCall(MFNSetOptionsPrefix(mfn,"check_"));
      80           1 :     PetscCall(MFNAppendOptionsPrefix(mfn,"myprefix_"));
      81           1 :     PetscCall(MFNGetOptionsPrefix(mfn,&prefix));
      82           1 :     PetscCall(PetscPrintf(PETSC_COMM_WORLD," MFN prefix is currently: %s\n",prefix));
      83             :   }
      84             : 
      85             :   /* test some interface functions */
      86           2 :   PetscCall(MFNGetOperator(mfn,&B));
      87           2 :   PetscCall(MatView(B,PETSC_VIEWER_STDOUT_WORLD));
      88           2 :   PetscCall(MFNSetTolerances(mfn,1e-4,500));
      89           2 :   PetscCall(MFNSetDimensions(mfn,6));
      90           2 :   PetscCall(MFNSetErrorIfNotConverged(mfn,PETSC_TRUE));
      91             :   /* test monitors */
      92           2 :   PetscCall(PetscViewerAndFormatCreate(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_DEFAULT,&vf));
      93           2 :   PetscCall(MFNMonitorSet(mfn,(PetscErrorCode (*)(MFN,PetscInt,PetscReal,void*))MFNMonitorDefault,vf,(PetscCtxDestroyFn*)PetscViewerAndFormatDestroy));
      94             :   /* PetscCall(MFNMonitorCancel(mfn)); */
      95           2 :   PetscCall(MFNSetFromOptions(mfn));
      96             : 
      97             :   /* query properties and print them */
      98           2 :   PetscCall(MFNGetTolerances(mfn,&tol,&maxit));
      99           2 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Tolerance: %g, max iterations: %" PetscInt_FMT "\n",(double)tol,maxit));
     100           2 :   PetscCall(MFNGetDimensions(mfn,&ncv));
     101           2 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Subspace dimension: %" PetscInt_FMT "\n",ncv));
     102           2 :   PetscCall(MFNGetErrorIfNotConverged(mfn,&flg));
     103           2 :   if (flg) PetscCall(PetscPrintf(PETSC_COMM_WORLD," Erroring out if convergence fails\n"));
     104             : 
     105             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     106             :                            Solve  y=sqrt(A)*v
     107             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     108             : 
     109           2 :   PetscCall(MFNSolve(mfn,v,y));
     110           2 :   PetscCall(MFNGetConvergedReason(mfn,&reason));
     111           2 :   PetscCall(MFNGetIterationNumber(mfn,&its));
     112           2 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Finished - converged reason = %d\n",(int)reason));
     113             :   /* PetscCall(PetscPrintf(PETSC_COMM_WORLD," its = %" PetscInt_FMT "\n",its)); */
     114           2 :   PetscCall(VecNorm(y,NORM_2,&norm));
     115           2 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," sqrt(A)*v has norm %g\n",(double)norm));
     116             : 
     117             :   /*
     118             :      Free work space
     119             :   */
     120           2 :   PetscCall(MFNDestroy(&mfn));
     121           2 :   PetscCall(MatDestroy(&A));
     122           2 :   PetscCall(VecDestroy(&v));
     123           2 :   PetscCall(VecDestroy(&y));
     124           2 :   PetscCall(SlepcFinalize());
     125             :   return 0;
     126             : }
     127             : 
     128             : /*TEST
     129             : 
     130             :    testset:
     131             :       args: -mfn_monitor_cancel -mfn_converged_reason -mfn_view -log_exclude mfn,bv,fn
     132             :       output_file: output/test3_1.out
     133             :       test:
     134             :          suffix: 1
     135             :       test:
     136             :          suffix: 1_x
     137             :          args: -mfn_monitor draw::draw_lg -draw_virtual
     138             :          requires: x
     139             : 
     140             :    test:
     141             :       suffix: 2
     142             :       args: -test_prefix -check_myprefix_mfn_monitor
     143             :       filter: sed -e "s/estimate [0-9]\.[0-9]*e[+-]\([0-9]*\)/estimate (removed)/g" | sed -e "s/4.0[0-9]*e-10/4.03e-10/"
     144             : 
     145             : TEST*/

Generated by: LCOV version 1.14