Actual source code: ex50.c
1: /*$Id: ex50.c,v 1.27 2001/08/07 03:03:07 balay Exp $*/
3: static char help[] = "Reads in a matrix and vector in ASCII format. Writes\n\
4: them using the PETSc sparse format. Input parameters are:\n\
5: -fin <filename> : input file\n\
6: -fout <filename> : output file\n\n";
8: #include petscmat.h
12: int main(int argc,char **args)
13: {
14: Mat A;
15: Vec b;
16: char filein[256],finname[256],fileout[256];
17: int n,ierr,col,row;
18: int rowin;
19: PetscTruth flg;
20: PetscScalar val,*array;
21: FILE* file;
22: PetscViewer view;
24: PetscInitialize(&argc,&args,(char *)0,help);
26: /* Read in matrix and RHS */
27: PetscOptionsGetString(PETSC_NULL,"-fin",filein,255,&flg);
28: if (!flg) SETERRQ(1,"Must indicate file for reading");
29: PetscOptionsGetString(PETSC_NULL,"-fout",fileout,255,&flg);
30: if (!flg) SETERRQ(1,"Must indicate file for writing");
32: PetscFixFilename(filein,finname);
33: if (!(file = fopen(finname,"r"))) {
34: SETERRQ(1,"cannot open input file\n");
35: }
36: fscanf(file,"%d\n",&n);
38: MatCreate(PETSC_COMM_WORLD,PETSC_DECIDE,PETSC_DECIDE,n,n,&A);
39: MatSetFromOptions(A);
40: VecCreate(PETSC_COMM_WORLD,&b);
41: VecSetSizes(b,PETSC_DECIDE,n);
42: VecSetFromOptions(b);
44: for (row=0; row<n; row++) {
45: fscanf(file,"row %d:",&rowin);
46: if (rowin != row) SETERRQ(1,"Bad file");
47: while (fscanf(file," %d %le",&col,&val)) {
48: MatSetValues(A,1,&row,1,&col,&val,INSERT_VALUES);
49: }
50: }
51: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
52: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
53: VecGetArray(b,&array);
54: for (row=0; row<n; row++) {
55: fscanf(file," ii= %d %le",&col,array+row);
56: }
57: VecRestoreArray(b,&array);
59: fclose(file);
61: PetscPrintf(PETSC_COMM_SELF,"Reading matrix complete.\n");
62: PetscViewerBinaryOpen(PETSC_COMM_WORLD,fileout,PETSC_FILE_CREATE,&view);
63: MatView(A,view);
64: VecView(b,view);
65: PetscViewerDestroy(view);
67: VecDestroy(b);
68: MatDestroy(A);
70: PetscFinalize();
71: return 0;
72: }