Actual source code: ex2.c
2: #ifdef PETSC_RCS_HEADER
3: static char vcid[] = "$Id: ex2.c,v 1.2 1999/04/06 21:27:41 bsmith Exp $";
4: #endif
6: static char help[] =
7: "Reads a PETSc matrix and vector from a file and saves in an ASCII file that\n\
8: can be read by the SPAI test program. Input parameters include\n\
9: -f0 <input_file> : file to load\n\n";
11: /*T
12: Routines: MatLoad(); VecLoad();
13: Routines: ViewerBinaryOpen();
14: Processors: 1
15: T*/
17: /*
18: Include "mat.h" so that we can use matrices. Note that this file
19: automatically includes:
20: petsc.h - base PETSc routines vec.h - vectors
21: sys.h - system routines is.h - index sets
22: viewer.h - viewers
23: */
24: #include "mat.h"
25: #include "src/contrib/spai/include/spai.h"
27: int main(int argc,char **args)
28: {
29: Mat A; /* matrix */
30: Vec b; /* RHS */
31: Viewer viewer; /* viewer */
32: char file[128]; /* input file name */
33: int ierr, flg;
34: PetscTruth set;
35: MatType mtype;
36: FILE *fd;
38: PetscInitialize(&argc,&args,(char *)0,help);
40: #if defined(USE_PETSC_COMPLEX)
41: SETERRQ(1,0,"This example does not work with complex numbers");
42: #else
44: /*
45: Determine files from which we read the linear system
46: (matrix and right-hand-side vector).
47: */
48: OptionsGetString(PETSC_NULL,"-f0",file,127,&flg); CHKERRA(ierr);
49: if (!flg) SETERRQ(1,0,"Must indicate binary file with the -f0 option");
52: /*
53: Open binary file. Note that we use BINARY_RDONLY to indicate
54: reading from this file.
55: */
56: ViewerBinaryOpen(PETSC_COMM_WORLD,file,BINARY_RDONLY,&viewer);CHKERRA(ierr);
58: /*
59: Determine matrix format to be used (specified at runtime).
60: See the manpage for MatLoad() for available formats.
61: */
62: MatGetTypeFromOptions(PETSC_COMM_WORLD,0,&mtype,&set);
64: /*
65: Load the matrix and vector; then destroy the viewer.
66: */
67: MatLoad(viewer,mtype,&A); CHKERRA(ierr);
68: VecLoad(viewer,&b); CHKERRA(ierr);
69: ViewerDestroy(viewer); CHKERRA(ierr);
71: fd = fopen("example_matrix","w");
72: MatDumpSPAI(A,fd);
73: fclose(fd);
74: fd = fopen("example_rhs","w");
75: VecDumpSPAI(b,fd);
76: fclose(fd);
78: MatDestroy(A); CHKERRA(ierr);
79: VecDestroy(b); CHKERRA(ierr);
81: PetscFinalize();
82: #endif
83: return 0;
84: }