Actual source code: ex72.c

  1: /*$Id: ex72.c,v 1.17 2001/04/10 19:35:44 bsmith Exp $*/

  3: #if !defined(PETSC_USE_COMPLEX)

  5: static char help[] = "Reads in a Symmetric matrix in MatrixMarket format. Writesn
  6: it using the PETSc sparse format. It also adds a Vector set to random values to then
  7: output file. Input parameters are:n
  8:   -fin <filename> : input filen
  9:   -fout <filename> : output filenn";

 11:  #include petscmat.h

 13: int main(int argc,char **args)
 14: {
 15:   Mat         A;
 16:   Vec         b;
 17:   char        filein[128],fileout[128],buf[128];
 18:   int         i,m,n,nnz,ierr,size,col,row;
 19:   Scalar      val;
 20:   FILE*       file;
 21:   PetscViewer view;
 22:   PetscRandom r;

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

 26:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
 27:   if (size > 1) SETERRQ(1,"Uniprocessor Example onlyn");

 29:   /* Read in matrix and RHS */
 30:   PetscOptionsGetString(PETSC_NULL,"-fin",filein,127,PETSC_NULL);
 31:   PetscFOpen(PETSC_COMM_SELF,filein,"r",&file);

 33:   /* Ignore the first line */
 34:   /* while (getc(file) != 'n') ; */
 35:   fgets(buf,128,file);
 36:   printf("%s",buf);
 37:   fscanf(file,"%d %d %dn",&m,&n,&nnz);
 38:   printf ("m = %d, n = %d, nnz = %dn",m,n,nnz);

 40:   MatCreateSeqAIJ(PETSC_COMM_WORLD,m,n,20,0,&A);
 41:   VecCreateMPI(PETSC_COMM_WORLD,PETSC_DECIDE,n,&b);
 42:   PetscRandomCreate(PETSC_COMM_SELF,RANDOM_DEFAULT,&r);
 43:   VecSetRandom(r,b);

 45:   for (i=0; i<nnz; i++) {
 46:     fscanf(file,"%d %d %len",&row,&col,&val);
 47:     row = row-1; col = col-1 ;
 48:     MatSetValues(A,1,&row,1,&col,&val,INSERT_VALUES);
 49:     if (row != col) {
 50:       MatSetValues(A,1,&col,1,&row,&val,INSERT_VALUES);
 51:     }
 52:   }
 53:   fclose(file);

 55:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
 56:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);

 58:   PetscPrintf(PETSC_COMM_SELF,"Reading matrix completes.n");
 59:   PetscOptionsGetString(PETSC_NULL,"-fout",fileout,127,PETSC_NULL);
 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);
 67:   PetscRandomDestroy(r);

 69:   PetscFinalize();
 70:   return 0;
 71: }
 72: #else
 73: #include <stdio.h>
 74: int main(int argc,char **args)
 75: {
 76:   fprintf(stdout,"This example does not work for complex numbers.n");
 77:   return 0;
 78: }
 79: #endif