Actual source code: test4.c

slepc-3.17.0 2022-03-31
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[] = "Illustrates use of MFNSetBV().\n\n"
 12:   "The command line options are:\n"
 13:   "  -t <sval>, where <sval> = scalar value that multiplies the argument.\n"
 14:   "  -file <filename>, where <filename> = matrix file in PETSc binary form.\n\n";

 16: #include <slepcmfn.h>

 18: int main(int argc,char **argv)
 19: {
 20:   Mat                A;           /* problem matrix */
 21:   MFN                mfn;
 22:   FN                 f;
 23:   BV                 V;
 24:   PetscInt           k=8;
 25:   PetscReal          norm;
 26:   PetscScalar        t=2.0;
 27:   Vec                v,y;
 28:   PetscViewer        viewer;
 29:   PetscBool          flg;
 30:   char               filename[PETSC_MAX_PATH_LEN];
 31:   MFNConvergedReason reason;

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

 35:   PetscOptionsGetScalar(NULL,NULL,"-t",&t,NULL);
 36:   PetscOptionsGetInt(NULL,NULL,"-k",&k,NULL);
 37:   PetscPrintf(PETSC_COMM_WORLD,"\nMatrix exponential y=exp(t*A)*e, loaded from file\n\n");

 39:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 40:                 Load matrix A from binary file
 41:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 43:   PetscOptionsGetString(NULL,NULL,"-file",filename,sizeof(filename),&flg);

 46: #if defined(PETSC_USE_COMPLEX)
 47:   PetscPrintf(PETSC_COMM_WORLD," Reading COMPLEX matrix from a binary file...\n");
 48: #else
 49:   PetscPrintf(PETSC_COMM_WORLD," Reading REAL matrix from a binary file...\n");
 50: #endif
 51:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,FILE_MODE_READ,&viewer);
 52:   MatCreate(PETSC_COMM_WORLD,&A);
 53:   MatSetFromOptions(A);
 54:   MatLoad(A,viewer);
 55:   PetscViewerDestroy(&viewer);

 57:   /* set v = ones(n,1) */
 58:   MatCreateVecs(A,NULL,&y);
 59:   MatCreateVecs(A,NULL,&v);
 60:   VecSet(v,1.0);

 62:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 63:                         Create the BV object
 64:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 66:   BVCreate(PETSC_COMM_WORLD,&V);
 67:   PetscObjectSetName((PetscObject)V,"V");
 68:   BVSetSizesFromVec(V,v,k);
 69:   BVSetFromOptions(V);

 71:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 72:                 Create the solver and set various options
 73:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 75:   MFNCreate(PETSC_COMM_WORLD,&mfn);
 76:   MFNSetOperator(mfn,A);
 77:   MFNSetBV(mfn,V);
 78:   MFNGetFN(mfn,&f);
 79:   FNSetType(f,FNEXP);
 80:   FNSetScale(f,t,1.0);
 81:   MFNSetDimensions(mfn,k);
 82:   MFNSetFromOptions(mfn);

 84:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 85:                       Solve the problem, y=exp(t*A)*v
 86:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 88:   MFNSolve(mfn,v,y);
 89:   MFNGetConvergedReason(mfn,&reason);
 91:   VecNorm(y,NORM_2,&norm);
 92:   PetscPrintf(PETSC_COMM_WORLD," Computed vector at time t=%.4g has norm %g\n\n",(double)PetscRealPart(t),(double)norm);

 94:   /*
 95:      Free work space
 96:   */
 97:   BVDestroy(&V);
 98:   MFNDestroy(&mfn);
 99:   MatDestroy(&A);
100:   VecDestroy(&v);
101:   VecDestroy(&y);
102:   SlepcFinalize();
103:   return 0;
104: }

106: /*TEST

108:    test:
109:       suffix: 1
110:       args: -file ${SLEPC_DIR}/share/slepc/datafiles/matrices/bfw62b.petsc -k 12
111:       requires: double !complex !defined(PETSC_USE_64BIT_INDICES)

113:    test:
114:       suffix: 2
115:       args: -file ${DATAFILESPATH}/matrices/complex/qc324.petsc -k 12
116:       requires: double complex datafilespath !defined(PETSC_USE_64BIT_INDICES)

118: TEST*/