Actual source code: ex72.c
1: /*$Id: ex72.c,v 1.19 2001/09/11 16:32:50 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: PetscScalar 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: VecCreate(PETSC_COMM_WORLD,&b);
42: VecSetSizes(b,PETSC_DECIDE,n);
43: VecSetFromOptions(b);
44: PetscRandomCreate(PETSC_COMM_SELF,RANDOM_DEFAULT,&r);
45: VecSetRandom(r,b);
47: for (i=0; i<nnz; i++) {
48: fscanf(file,"%d %d %len",&row,&col,&val);
49: row = row-1; col = col-1 ;
50: MatSetValues(A,1,&row,1,&col,&val,INSERT_VALUES);
51: if (row != col) {
52: MatSetValues(A,1,&col,1,&row,&val,INSERT_VALUES);
53: }
54: }
55: fclose(file);
57: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
58: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
60: PetscPrintf(PETSC_COMM_SELF,"Reading matrix completes.n");
61: PetscOptionsGetString(PETSC_NULL,"-fout",fileout,127,PETSC_NULL);
62: PetscViewerBinaryOpen(PETSC_COMM_WORLD,fileout,PETSC_BINARY_CREATE,&view);
63: MatView(A,view);
64: VecView(b,view);
65: PetscViewerDestroy(view);
67: VecDestroy(b);
68: MatDestroy(A);
69: PetscRandomDestroy(r);
71: PetscFinalize();
72: return 0;
73: }
74: #else
75: #include <stdio.h>
76: int main(int argc,char **args)
77: {
78: fprintf(stdout,"This example does not work for complex numbers.n");
79: return 0;
80: }
81: #endif