static char help[] = "Reads in a matrix in ASCII MATLAB format (I,J,A), read in vectors rhs and exact_solu in ASCII format.\n\ Writes them using the PETSc sparse format.\n\ Note: I and J start at 1, not 0, use -noshift if indices in file start with zero!\n\ Input parameters are:\n\ -Ain : input matrix in ascii format\n\ -rhs : input rhs in ascii format\n\ -solu : input true solution in ascii format\n\\n"; /* Example: ./ex78 -Ain Ain -rhs rhs -solu solu -noshift -mat_view with the datafiles in the following format: Ain (I and J start at 0): ------------------------ 3 3 6 0 0 1.0 0 1 2.0 1 0 3.0 1 1 4.0 1 2 5.0 2 2 6.0 rhs --- 0 3.0 1 12.0 2 6.0 solu ---- 0 1.0 0 1.0 0 1.0 */ #include int main(int argc,char **args) { Mat A = NULL; Vec b,u = NULL,u_tmp; char Ain[PETSC_MAX_PATH_LEN],rhs[PETSC_MAX_PATH_LEN],solu[PETSC_MAX_PATH_LEN]; int m,n = 0,nz,dummy; /* these are fscaned so kept as int */ PetscInt i,col,row,shift = 1,sizes[3],nsizes; PetscScalar val; PetscReal res_norm; FILE *Afile,*bfile,*ufile; PetscViewer view; PetscBool flg_A,flg_b,flg_u,flg; PetscMPIInt size; PetscCall(PetscInitialize(&argc,&args,(char*)0,help)); PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD,&size)); PetscCheck(size == 1,PETSC_COMM_WORLD,PETSC_ERR_WRONG_MPI_SIZE,"This is a uniprocessor example only!"); /* Read in matrix, rhs and exact solution from ascii files */ PetscCall(PetscOptionsGetString(NULL,NULL,"-Ain",Ain,sizeof(Ain),&flg_A)); PetscCall(PetscOptionsHasName(NULL,NULL,"-noshift",&flg)); if (flg) shift = 0; if (flg_A) { PetscCall(PetscPrintf(PETSC_COMM_SELF,"\n Read matrix in ascii format ...\n")); PetscCall(PetscFOpen(PETSC_COMM_SELF,Ain,"r",&Afile)); nsizes = 3; PetscCall(PetscOptionsGetIntArray(NULL,NULL,"-nosizesinfile",sizes,&nsizes,&flg)); if (flg) { PetscCheckFalse(nsizes != 3,PETSC_COMM_WORLD,PETSC_ERR_USER,"Must pass in three m,n,nz as arguments for -nosizesinfile"); m = sizes[0]; n = sizes[1]; nz = sizes[2]; } else { PetscCheckFalse(fscanf(Afile,"%d %d %d\n",&m,&n,&nz) != 3,PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Badly formatted input file"); } PetscCall(PetscPrintf(PETSC_COMM_SELF,"m: %d, n: %d, nz: %d \n", m,n,nz)); PetscCheckFalse(m != n,PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ, "Number of rows, cols must be same for this example"); PetscCall(MatCreate(PETSC_COMM_SELF,&A)); PetscCall(MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,m,n)); PetscCall(MatSetFromOptions(A)); PetscCall(MatSeqAIJSetPreallocation(A,nz/m,NULL)); PetscCall(MatSetOption(A,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE)); for (i=0; i