Actual source code: ex2.c

  2: static char help[] =
  3: "Reads a PETSc matrix and vector from a file and saves in an ASCII file that\n\
  4:   can be read by the SPAI test program.  Input parameters include\n\
  5:   -f0 <input_file> : file to load\n\n";

  7: /*T
  8:    Routines: MatLoad(); VecLoad();
  9:    Routines: ViewerBinaryOpen();
 10:    Processors: 1
 11: T*/

 13: /* 
 14:   Include "mat.h" so that we can use matrices.  Note that this file
 15:   automatically includes:
 16:      petsc.h  - base PETSc routines   vec.h - vectors
 17:      sys.h    - system routines       is.h  - index sets
 18:      viewer.h - viewers
 19: */
 20: #include "mat.h"
 21: #include "src/contrib/spai/include/spai.h"

 23: int main(int argc,char **args)
 24: {
 25:   Mat        A;                /* matrix */
 26:   Vec        b;                /* RHS */
 27:   Viewer     viewer;               /* viewer */
 28:   char       file[PETSC_MAX_PATH_LEN];        /* input file name */
 30:   int flg;
 31:   PetscTruth set;
 32:   MatType    mtype;
 33:   FILE       *fd;

 35:   PetscInitialize(&argc,&args,(char *)0,help);

 37: #if defined(USE_PETSC_COMPLEX)
 38:   SETERRQ(1,0,"This example does not work with complex numbers");
 39: #else

 41:   /* 
 42:      Determine files from which we read the linear system
 43:      (matrix and right-hand-side vector).
 44:   */
 45:   OptionsGetString(PETSC_NULL,"-f0",file,PETSC_MAX_PATH_LEN-1,&flg); CHKERRA(ierr);
 46:   if (!flg) SETERRQ(1,0,"Must indicate binary file with the -f0 option");


 49:   /* 
 50:        Open binary file.  Note that we use BINARY_RDONLY to indicate
 51:        reading from this file.
 52:   */
 53:   ViewerBinaryOpen(PETSC_COMM_WORLD,file,BINARY_RDONLY,&viewer);CHKERRA(ierr);

 55:   /* 
 56:        Determine matrix format to be used (specified at runtime).
 57:        See the manpage for MatLoad() for available formats.
 58:   */
 59:   MatGetTypeFromOptions(PETSC_COMM_WORLD,0,&mtype,&set);

 61:   /*
 62:        Load the matrix and vector; then destroy the viewer.
 63:   */
 64:   MatLoad(viewer,mtype,&A); CHKERRA(ierr);
 65:   VecLoad(viewer,PETSC_NULL,&b); CHKERRA(ierr);
 66:   ViewerDestroy(viewer); CHKERRA(ierr);

 68:   fd = fopen("example_matrix","w");
 69:   MatDumpSPAI(A,fd);
 70:   fclose(fd);
 71:   fd = fopen("example_rhs","w");
 72:   VecDumpSPAI(b,fd);
 73:   fclose(fd);

 75:   MatDestroy(A); CHKERRA(ierr);
 76:   VecDestroy(b); CHKERRA(ierr);

 78:   PetscFinalize();
 79: #endif
 80:   return 0;
 81: }