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. Writesn
  4: them using the PETSc sparse format. Input parameters are:n
  5:   -fin <filename> : input filen
  6:   -fout <filename> : output filenn";

 8:  #include petscmat.h

 10: int main(int argc,char **args)
 11: {
 12:   Mat         A;
 13:   Vec         b;
 14:   char        filein[256],finname[256],fileout[256];
 15:   int         n,ierr,col,row;
 16:   int         rowin;
 17:   PetscTruth  flg;
 18:   PetscScalar val,*array;
 19:   FILE*       file;
 20:   PetscViewer view;

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

 24:   /* Read in matrix and RHS */
 25:   PetscOptionsGetString(PETSC_NULL,"-fin",filein,255,&flg);
 26:   if (!flg) SETERRQ(1,"Must indicate file for reading");
 27:   PetscOptionsGetString(PETSC_NULL,"-fout",fileout,255,&flg);
 28:   if (!flg) SETERRQ(1,"Must indicate file for writing");

 30:   PetscFixFilename(filein,finname);
 31:   if (!(file = fopen(finname,"r"))) {
 32:     SETERRQ(1,"cannot open input filen");
 33:   }
 34:   fscanf(file,"%dn",&n);

 36:   MatCreate(PETSC_COMM_WORLD,PETSC_DECIDE,PETSC_DECIDE,n,n,&A);
 37:   MatSetFromOptions(A);
 38:   VecCreate(PETSC_COMM_WORLD,&b);
 39:   VecSetSizes(b,PETSC_DECIDE,n);
 40:   VecSetFromOptions(b);

 42:   for (row=0; row<n; row++) {
 43:     fscanf(file,"row %d:",&rowin);
 44:     if (rowin != row) SETERRQ(1,"Bad file");
 45:     while (fscanf(file," %d %le",&col,&val)) {
 46:       MatSetValues(A,1,&row,1,&col,&val,INSERT_VALUES);
 47:     }
 48:   }
 49:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
 50:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
 51:   VecGetArray(b,&array);
 52:   for (row=0; row<n; row++) {
 53:     fscanf(file," ii= %d %le",&col,array+row);
 54:   }
 55:   VecRestoreArray(b,&array);

 57:   fclose(file);

 59:   PetscPrintf(PETSC_COMM_SELF,"Reading matrix complete.n");
 60:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,fileout,PETSC_BINARY_CREATE,&view);
 61:   MatView(A,view);
 62:   VecView(b,view);
 63:   PetscViewerDestroy(view);

 65:   VecDestroy(b);
 66:   MatDestroy(A);

 68:   PetscFinalize();
 69:   return 0;
 70: }