LCOV - code coverage report
Current view: top level - mfn/tests - test5.c (source / functions) Hit Total Coverage
Test: SLEPc Lines: 54 54 100.0 %
Date: 2024-12-18 00:42:09 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 changing MFN type.\n\n";
      12             : 
      13             : #include <slepcmfn.h>
      14             : 
      15           1 : int main(int argc,char **argv)
      16             : {
      17           1 :   Mat            A;           /* problem matrix */
      18           1 :   MFN            mfn;
      19           1 :   FN             f;
      20           1 :   PetscReal      norm;
      21           1 :   PetscScalar    t=0.3;
      22           1 :   PetscInt       N,n=25,m,Istart,Iend,II,i,j;
      23           1 :   PetscBool      flag;
      24           1 :   Vec            v,y;
      25             : 
      26           1 :   PetscFunctionBeginUser;
      27           1 :   PetscCall(SlepcInitialize(&argc,&argv,NULL,help));
      28             : 
      29           1 :   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
      30           1 :   PetscCall(PetscOptionsGetInt(NULL,NULL,"-m",&m,&flag));
      31           1 :   if (!flag) m=n;
      32           1 :   N = n*m;
      33           1 :   PetscCall(PetscOptionsGetScalar(NULL,NULL,"-t",&t,NULL));
      34           1 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\nMatrix exponential y=exp(t*A)*e, of the 2-D Laplacian, N=%" PetscInt_FMT " (%" PetscInt_FMT "x%" PetscInt_FMT " grid)\n\n",N,n,m));
      35             : 
      36             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      37             :                          Build the 2-D Laplacian
      38             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      39             : 
      40           1 :   PetscCall(MatCreate(PETSC_COMM_WORLD,&A));
      41           1 :   PetscCall(MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,N,N));
      42           1 :   PetscCall(MatSetFromOptions(A));
      43             : 
      44           1 :   PetscCall(MatGetOwnershipRange(A,&Istart,&Iend));
      45         626 :   for (II=Istart;II<Iend;II++) {
      46         625 :     i = II/n; j = II-i*n;
      47         625 :     if (i>0) PetscCall(MatSetValue(A,II,II-n,-1.0,INSERT_VALUES));
      48         625 :     if (i<m-1) PetscCall(MatSetValue(A,II,II+n,-1.0,INSERT_VALUES));
      49         625 :     if (j>0) PetscCall(MatSetValue(A,II,II-1,-1.0,INSERT_VALUES));
      50         625 :     if (j<n-1) PetscCall(MatSetValue(A,II,II+1,-1.0,INSERT_VALUES));
      51         625 :     PetscCall(MatSetValue(A,II,II,4.0,INSERT_VALUES));
      52             :   }
      53             : 
      54           1 :   PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY));
      55           1 :   PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY));
      56             : 
      57             :   /* set v = ones(n,1) */
      58           1 :   PetscCall(MatCreateVecs(A,&v,&y));
      59           1 :   PetscCall(VecSet(v,1.0));
      60             : 
      61             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      62             :                 Create the solver and set various options
      63             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      64             : 
      65           1 :   PetscCall(FNCreate(PETSC_COMM_WORLD,&f));
      66           1 :   PetscCall(FNSetType(f,FNEXP));
      67             : 
      68           1 :   PetscCall(MFNCreate(PETSC_COMM_WORLD,&mfn));
      69           1 :   PetscCall(MFNSetOperator(mfn,A));
      70           1 :   PetscCall(MFNSetType(mfn,MFNEXPOKIT));
      71           1 :   PetscCall(MFNSetDimensions(mfn,24));
      72           1 :   PetscCall(MFNSetTolerances(mfn,1e-5,1000));
      73           1 :   PetscCall(MFNSetFN(mfn,f));
      74           1 :   PetscCall(MFNSetErrorIfNotConverged(mfn,PETSC_TRUE));
      75           1 :   PetscCall(MFNSetFromOptions(mfn));
      76           1 :   PetscCall(MFNView(mfn,NULL));
      77             : 
      78             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      79             :             Change MFN type and solve the problem, y=exp(t*A)*v
      80             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      81             : 
      82           1 :   PetscCall(MFNSetType(mfn,MFNKRYLOV));
      83           1 :   PetscCall(FNSetScale(f,t,1.0));
      84           1 :   PetscCall(MFNSolve(mfn,v,y));
      85           1 :   PetscCall(VecNorm(y,NORM_2,&norm));
      86           1 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Computed vector at time t=%.4g has norm %g\n\n",(double)PetscRealPart(t),(double)norm));
      87             : 
      88             :   /*
      89             :      Free work space
      90             :   */
      91           1 :   PetscCall(MFNDestroy(&mfn));
      92           1 :   PetscCall(FNDestroy(&f));
      93           1 :   PetscCall(MatDestroy(&A));
      94           1 :   PetscCall(VecDestroy(&v));
      95           1 :   PetscCall(VecDestroy(&y));
      96           1 :   PetscCall(SlepcFinalize());
      97             :   return 0;
      98             : }
      99             : 
     100             : /*TEST
     101             : 
     102             :    test:
     103             : 
     104             : TEST*/

Generated by: LCOV version 1.14